@@ -2,7 +2,7 @@ import MPEAbi from 'singularitynet-platform-contracts/abi/MultiPartyEscrow';
2
2
import MPENetworks from 'singularitynet-platform-contracts/networks/MultiPartyEscrow' ;
3
3
import { BigNumber } from 'bignumber.js' ;
4
4
import { map } from 'lodash' ;
5
-
5
+ import Web3 from 'web3' ;
6
6
import PaymentChannel from './PaymentChannel' ;
7
7
import logger from './utils/logger' ;
8
8
import { toBNString } from './utils/bignumber_helper' ;
@@ -12,9 +12,10 @@ class MPEContract {
12
12
* @param {Web3 } web3
13
13
* @param {number } networkId
14
14
*/
15
- constructor ( web3 , networkId ) {
15
+ constructor ( web3 , networkId , rpcEndpoint ) {
16
16
this . _web3 = web3 ;
17
17
this . _networkId = networkId ;
18
+ this . rpcEndpoint = rpcEndpoint ;
18
19
this . _contract = new this . _web3 . eth . Contract ( MPEAbi , MPENetworks [ networkId ] . address ) ;
19
20
}
20
21
@@ -207,19 +208,26 @@ class MPEContract {
207
208
*/
208
209
async getPastOpenChannels ( account , service , startingBlockNumber ) {
209
210
const fromBlock = startingBlockNumber || await this . _deploymentBlockNumber ( ) ;
211
+ let contract = this . _contract ;
212
+ if ( this . rpcEndpoint ) {
213
+ const _web3 = new Web3 ( this . rpcEndpoint ) ;
214
+ contract = new _web3 . eth . Contract ( MPEAbi , MPENetworks [ this . _networkId ] . address ) ;
215
+ }
210
216
logger . debug ( `Fetching all payment channel open events starting at block: ${ fromBlock } ` , { tags : [ 'MPE' ] } ) ;
211
217
212
218
const address = await account . getAddress ( ) ;
219
+ const decodedData = Buffer . from ( service . group . group_id , 'base64' ) . toString ( 'hex' ) ;
220
+ const groupId = `0x${ decodedData } ` ;
213
221
const options = {
214
222
filter : {
215
223
sender : address ,
216
224
recipient : service . group . payment_address ,
217
- groupId : service . group . group_id_in_bytes ,
225
+ groupId,
218
226
} ,
219
227
fromBlock,
220
228
toBlock : 'latest' ,
221
229
} ;
222
- const channelsOpened = await this . contract . getPastEvents ( 'ChannelOpen' , options ) ;
230
+ const channelsOpened = await contract . getPastEvents ( 'ChannelOpen' , options ) ;
223
231
return map ( channelsOpened , ( channelOpenEvent ) => {
224
232
const { channelId } = channelOpenEvent . returnValues ;
225
233
return new PaymentChannel ( channelId , this . _web3 , account , service , this ) ;
@@ -228,7 +236,8 @@ class MPEContract {
228
236
229
237
async _fundEscrowAccount ( account , amountInCogs ) {
230
238
const address = await account . getAddress ( ) ;
231
- const currentEscrowBalance = await this . balance ( address ) ;
239
+ let currentEscrowBalance = await this . balance ( address ) ;
240
+ currentEscrowBalance = toBNString ( currentEscrowBalance ) ;
232
241
if ( amountInCogs > currentEscrowBalance ) {
233
242
await account . depositToEscrowAccount ( amountInCogs - currentEscrowBalance ) ;
234
243
}
0 commit comments