@@ -4,7 +4,7 @@ import Debug from 'debug'
44
55import { counterId , uuid , CacheAsyncFn } from './utils'
66import { validateOptions } from './stream/utils'
7- import Config , { StreamrClientOptions , StreamrClientConfig } from './Config'
7+ import Config , { StreamrClientOptions } from './Config'
88import StreamrEthereum from './Ethereum'
99import Session from './Session'
1010import Connection , { ConnectionError } from './Connection'
@@ -14,8 +14,10 @@ import { getUserId } from './user'
1414import { Todo , MaybeAsync } from './types'
1515import { StreamEndpoints } from './rest/StreamEndpoints'
1616import { LoginEndpoints } from './rest/LoginEndpoints'
17- import { DataUnionEndpoints } from './rest/DataUnionEndpoints'
1817import { DataUnion , DataUnionDeployOptions } from './dataunion/DataUnion'
18+ import { BigNumber } from '@ethersproject/bignumber'
19+ import { getAddress } from '@ethersproject/address'
20+ import { Contract } from '@ethersproject/contracts'
1921
2022// TODO get metadata type from streamr-protocol-js project (it doesn't export the type definitions yet)
2123export type OnMessageCallback = MaybeAsync < ( message : any , metadata : any ) => void >
@@ -32,8 +34,8 @@ export { StreamrClientOptions }
3234
3335class StreamrConnection extends Connection {
3436 // TODO define args type when we convert Connection class to TypeScript
35- constructor ( ... args : any ) {
36- super ( ... args )
37+ constructor ( options : Todo , client : StreamrClient ) {
38+ super ( options , client )
3739 this . on ( 'message' , this . onConnectionMessage )
3840 }
3941
@@ -137,13 +139,13 @@ function Plugin(targetInstance: any, srcInstance: any) {
137139}
138140
139141// these are mixed in via Plugin function above
140- interface StreamrClient extends StreamEndpoints , LoginEndpoints , DataUnionEndpoints { }
142+ interface StreamrClient extends StreamEndpoints , LoginEndpoints { }
141143
142144// eslint-disable-next-line no-redeclare
143145class StreamrClient extends EventEmitter {
144146 id : string
145147 debug : Debug . Debugger
146- options : StreamrClientConfig
148+ options : StreamrClientOptions
147149 session : Session
148150 connection : StreamrConnection
149151 publisher : Todo
@@ -152,18 +154,13 @@ class StreamrClient extends EventEmitter {
152154 ethereum : StreamrEthereum
153155 streamEndpoints : StreamEndpoints
154156 loginEndpoints : LoginEndpoints
155- dataUnionEndpoints : DataUnionEndpoints
156157
157158 constructor ( options : Partial < StreamrClientOptions > = { } , connection ?: StreamrConnection ) {
158159 super ( )
159160 this . id = counterId ( `${ this . constructor . name } :${ uid } ` )
160161 this . debug = Debug ( this . id )
161162
162- this . options = Config ( {
163- id : this . id ,
164- debug : this . debug ,
165- ...options ,
166- } )
163+ this . options = Config ( options )
167164
168165 this . debug ( 'new StreamrClient %s: %o' , this . id , {
169166 version : process . env . version ,
@@ -182,7 +179,7 @@ class StreamrClient extends EventEmitter {
182179 this . on ( 'error' , this . _onError ) // attach before creating sub-components incase they fire error events
183180
184181 this . session = new Session ( this , this . options . auth )
185- this . connection = connection || new StreamrConnection ( this . options )
182+ this . connection = connection || new StreamrConnection ( this . options , this )
186183
187184 this . connection
188185 . on ( 'connected' , this . onConnectionConnected )
@@ -195,7 +192,6 @@ class StreamrClient extends EventEmitter {
195192
196193 this . streamEndpoints = Plugin ( this , new StreamEndpoints ( this ) )
197194 this . loginEndpoints = Plugin ( this , new LoginEndpoints ( this ) )
198- this . dataUnionEndpoints = Plugin ( this , new DataUnionEndpoints ( this ) )
199195 this . cached = new StreamrCached ( this )
200196 }
201197
@@ -380,18 +376,42 @@ class StreamrClient extends EventEmitter {
380376 return this . getAddress ( )
381377 }
382378
379+ /**
380+ * Get token balance in "wei" (10^-18 parts) for given address
381+ */
382+ async getTokenBalance ( address : string ) : Promise < BigNumber > {
383+ const { tokenAddress } = this . options
384+ if ( ! tokenAddress ) {
385+ throw new Error ( 'StreamrClient has no tokenAddress configuration.' )
386+ }
387+ const addr = getAddress ( address )
388+ const provider = this . ethereum . getMainnetProvider ( )
389+
390+ const token = new Contract ( tokenAddress , [ {
391+ name : 'balanceOf' ,
392+ inputs : [ { type : 'address' } ] ,
393+ outputs : [ { type : 'uint256' } ] ,
394+ constant : true ,
395+ payable : false ,
396+ stateMutability : 'view' ,
397+ type : 'function'
398+ } ] , provider )
399+ return token . balanceOf ( addr )
400+ }
401+
383402 getDataUnion ( contractAddress : string ) {
384- return new DataUnion ( contractAddress , undefined , this . dataUnionEndpoints )
403+ return DataUnion . _fromContractAddress ( contractAddress , this ) // eslint-disable-line no-underscore-dangle
385404 }
386405
387406 async deployDataUnion ( options ?: DataUnionDeployOptions ) {
388- const contract = await this . dataUnionEndpoints . deployDataUnionContract ( options )
389- return new DataUnion ( contract . address , contract . sidechain . address , this . dataUnionEndpoints )
407+ return DataUnion . _deploy ( options , this ) // eslint-disable-line no-underscore-dangle
390408 }
391409
392410 _getDataUnionFromName ( { dataUnionName, deployerAddress } : { dataUnionName : string , deployerAddress : string } ) {
393- const contractAddress = this . dataUnionEndpoints . calculateDataUnionMainnetAddress ( dataUnionName , deployerAddress )
394- return this . getDataUnion ( contractAddress )
411+ return DataUnion . _fromName ( { // eslint-disable-line no-underscore-dangle
412+ dataUnionName,
413+ deployerAddress
414+ } , this )
395415 }
396416
397417 static generateEthereumAccount ( ) {
0 commit comments