@@ -13,6 +13,7 @@ import config from '../config'
1313import authFetch from '../../../src/rest/authFetch'
1414import { createClient , createMockAddress , expectInvalidAddress } from '../../utils'
1515import { AmbMessageHash , DataUnionWithdrawOptions , MemberStatus } from '../../../src/dataunion/DataUnion'
16+ import { EthereumAddress } from '../../../src'
1617
1718const log = debug ( 'StreamrClient::DataUnion::integration-test-withdraw' )
1819
@@ -29,13 +30,13 @@ const tokenSidechain = new Contract(config.clientOptions.tokenSidechainAddress,
2930let testWalletId = 1000000 // ensure fixed length as string
3031
3132async function testWithdraw (
32- getBalance : ( memberWallet : Wallet ) => Promise < BigNumber > ,
3333 withdraw : (
34- dataUnionAddress : string ,
34+ dataUnionAddress : EthereumAddress ,
3535 memberClient : StreamrClient ,
3636 memberWallet : Wallet ,
3737 adminClient : StreamrClient
3838 ) => Promise < ContractReceipt | AmbMessageHash | null > ,
39+ recipientAddress : EthereumAddress | null , // null means memberClient itself
3940 requiresMainnetETH : boolean ,
4041 options : DataUnionWithdrawOptions ,
4142) {
@@ -73,7 +74,7 @@ async function testWithdraw(
7374 auth : {
7475 privateKey : memberWallet . privateKey
7576 }
76- } as any )
77+ } )
7778
7879 // product is needed for join requests to analyze the DU version
7980 const createProductUrl = getEndpointUrl ( config . clientOptions . restUrl , 'products' )
@@ -137,14 +138,21 @@ async function testWithdraw(
137138 const stats = await memberClient . getDataUnion ( dataUnion . getAddress ( ) ) . getMemberStats ( memberWallet . address )
138139 log ( `Stats: ${ JSON . stringify ( stats ) } ` )
139140
141+ const getRecipientBalance = async ( ) => {
142+ const a = recipientAddress || await memberClient . getAddress ( )
143+ return options . sendToMainnet ? memberClient . getTokenBalance ( a ) : memberClient . getSidechainTokenBalance ( a )
144+ }
145+
146+ const balanceBefore = await getRecipientBalance ( )
147+ log ( `Balance before: ${ balanceBefore } . Withdrawing tokens...` )
148+
140149 // "bridge-sponsored mainnet withdraw" case
141150 if ( ! options . payForTransport && options . waitUntilTransportIsComplete ) {
151+ log ( `Adding ${ memberWallet . address } to bridge-sponsored withdraw whitelist` )
142152 bridgeWhitelist . push ( memberWallet . address )
143153 }
144154
145155 // test setup done, do the withdraw
146- const balanceBefore = await getBalance ( memberWallet )
147- log ( `Balance before: ${ balanceBefore } . Withdrawing tokens...` )
148156 let ret = await withdraw ( dataUnion . getAddress ( ) , memberClient , memberWallet , adminClient )
149157
150158 // "other-sponsored mainnet withdraw" case
@@ -158,10 +166,10 @@ async function testWithdraw(
158166 // we need to wait nevertheless, to be able to assert that balance in fact changed
159167 if ( ! options . waitUntilTransportIsComplete ) {
160168 log ( `Waiting until balance changes from ${ balanceBefore . toString ( ) } ` )
161- await until ( async ( ) => getBalance ( memberWallet ) . then ( ( b ) => ! b . eq ( balanceBefore ) ) )
169+ await until ( async ( ) => getRecipientBalance ( ) . then ( ( b ) => ! b . eq ( balanceBefore ) ) )
162170 }
163171
164- const balanceAfter = await getBalance ( memberWallet )
172+ const balanceAfter = await getRecipientBalance ( )
165173 const balanceIncrease = balanceAfter . sub ( balanceBefore )
166174
167175 expect ( stats ) . toMatchObject ( {
@@ -177,15 +185,15 @@ log('Starting the simulated bridge-sponsored signature transport process')
177185// event UserRequestForSignature(bytes32 indexed messageId, bytes encodedData)
178186const signatureRequestEventSignature = '0x520d2afde79cbd5db58755ac9480f81bc658e5c517fcae7365a3d832590b0183'
179187const sidechainAmbAddress = '0xaFA0dc5Ad21796C9106a36D68f69aAD69994BB64'
180- const bridgeWhitelist : string [ ] = [ ]
188+ const bridgeWhitelist : EthereumAddress [ ] = [ ]
181189providerSidechain . on ( {
182190 address : sidechainAmbAddress ,
183191 topics : [ signatureRequestEventSignature ]
184192} , async ( event ) => {
185193 log ( `Observed signature request for message id=${ event . topics [ 1 ] } ` ) // messageId is indexed so it's in topics...
186194 const message = defaultAbiCoder . decode ( [ 'bytes' ] , event . data ) [ 0 ] // ...only encodedData is in data
187195 const recipient = '0x' + message . slice ( 200 , 240 )
188- if ( bridgeWhitelist . find ( ( address ) => address . toLowerCase ( ) === recipient ) ) {
196+ if ( ! bridgeWhitelist . find ( ( address ) => address . toLowerCase ( ) === recipient ) ) {
189197 log ( `Recipient ${ recipient } not whitelisted, ignoring` )
190198 return
191199 }
@@ -196,8 +204,6 @@ providerSidechain.on({
196204} )
197205
198206describe ( 'DataUnion withdraw' , ( ) => {
199- const balanceClient = createClient ( )
200-
201207 afterAll ( ( ) => {
202208 providerMainnet . removeAllListeners ( )
203209 providerSidechain . removeAllListeners ( )
@@ -216,56 +222,39 @@ describe('DataUnion withdraw', () => {
216222
217223 const options = { sendToMainnet, payForTransport, waitUntilTransportIsComplete }
218224
219- async function getTokenBalance ( wallet : Wallet ) {
220- return sendToMainnet ? balanceClient . getTokenBalance ( wallet . address ) : balanceClient . getSidechainTokenBalance ( wallet . address )
221- }
222-
223225 describe ( 'by member' , ( ) => {
224226
225227 it ( 'to itself' , ( ) => {
226- const getBalance = async ( memberWallet : Wallet ) => getTokenBalance ( memberWallet )
227- const withdraw = async ( dataUnionAddress : string , memberClient : StreamrClient ) => (
228+ return testWithdraw ( async ( dataUnionAddress , memberClient ) => (
228229 memberClient . getDataUnion ( dataUnionAddress ) . withdrawAll ( options )
229- )
230- return testWithdraw ( getBalance , withdraw , true , options )
230+ ) , null , true , options )
231231 } , 3600000 )
232232
233233 it ( 'to any address' , ( ) => {
234234 const outsiderWallet = new Wallet ( `0x100000000000000000000000000000000000000012300000002${ Date . now ( ) } ` , providerSidechain )
235- const getBalance = async ( ) => getTokenBalance ( outsiderWallet )
236- const withdraw = ( dataUnionAddress : string , memberClient : StreamrClient ) => (
235+ return testWithdraw ( async ( dataUnionAddress , memberClient ) => (
237236 memberClient . getDataUnion ( dataUnionAddress ) . withdrawAllTo ( outsiderWallet . address , options )
238- )
239- return testWithdraw ( getBalance , withdraw , true , options )
237+ ) , outsiderWallet . address , true , options )
240238 } , 3600000 )
241239
242240 } )
243241
244242 describe ( 'by admin' , ( ) => {
245243
246244 it ( 'to member without signature' , async ( ) => {
247- const getBalance = async ( memberWallet : Wallet ) => getTokenBalance ( memberWallet )
248- const withdraw = ( dataUnionAddress : string , _ : StreamrClient , memberWallet : Wallet , adminClient : StreamrClient ) => (
245+ return testWithdraw ( async ( dataUnionAddress , memberClient , memberWallet , adminClient ) => (
249246 adminClient . getDataUnion ( dataUnionAddress ) . withdrawAllToMember ( memberWallet . address , options )
250- )
251- return testWithdraw ( getBalance , withdraw , false , options )
247+ ) , null , false , options )
252248 } , 3600000 )
253249
254250 it ( "to anyone with member's signature" , async ( ) => {
255251 const member2Wallet = new Wallet ( `0x100000000000000000000000000040000000000012300000007${ Date . now ( ) } ` , providerSidechain )
256- const getBalance = async ( ) => getTokenBalance ( member2Wallet )
257- const withdraw = async (
258- dataUnionAddress : string ,
259- memberClient : StreamrClient ,
260- memberWallet : Wallet ,
261- adminClient : StreamrClient
262- ) => {
252+ return testWithdraw ( async ( dataUnionAddress , memberClient , memberWallet , adminClient ) => {
263253 const signature = await memberClient . getDataUnion ( dataUnionAddress ) . signWithdrawAllTo ( member2Wallet . address )
264254 return adminClient
265255 . getDataUnion ( dataUnionAddress )
266256 . withdrawAllToSigned ( memberWallet . address , member2Wallet . address , signature , options )
267- }
268- return testWithdraw ( getBalance , withdraw , false , options )
257+ } , member2Wallet . address , false , options )
269258 } , 3600000 )
270259 } )
271260 } )
0 commit comments