11import { BigNumber , Contract , providers , Wallet } from 'ethers'
2- import { formatEther , parseEther } from 'ethers/lib/utils'
2+ import { formatEther , parseEther , defaultAbiCoder } from 'ethers/lib/utils'
3+ import { ContractReceipt } from '@ethersproject/contracts'
4+ import { keccak256 } from '@ethersproject/keccak256'
5+
36import debug from 'debug'
47
58import { getEndpointUrl , until } from '../../../src/utils'
@@ -10,7 +13,6 @@ import config from '../config'
1013import authFetch from '../../../src/rest/authFetch'
1114import { createClient , createMockAddress , expectInvalidAddress } from '../../utils'
1215import { AmbMessageHash , DataUnionWithdrawOptions , MemberStatus } from '../../../src/dataunion/DataUnion'
13- import { ContractReceipt } from '@ethersproject/contracts'
1416
1517const log = debug ( 'StreamrClient::DataUnion::integration-test-withdraw' )
1618
@@ -24,7 +26,7 @@ const tokenMainnet = new Contract(config.clientOptions.tokenAddress, Token.abi,
2426
2527const tokenSidechain = new Contract ( config . clientOptions . tokenSidechainAddress , Token . abi , adminWalletSidechain )
2628
27- const testWithdraw = async (
29+ async function testWithdraw (
2830 getBalance : ( memberWallet : Wallet ) => Promise < BigNumber > ,
2931 withdraw : (
3032 dataUnionAddress : string ,
@@ -34,7 +36,7 @@ const testWithdraw = async (
3436 ) => Promise < ContractReceipt | AmbMessageHash | null > ,
3537 requiresMainnetETH : boolean ,
3638 options : DataUnionWithdrawOptions ,
37- ) => {
39+ ) {
3840 log ( `Connecting to Ethereum networks, config = ${ JSON . stringify ( config ) } ` )
3941 const network = await providerMainnet . getNetwork ( )
4042 log ( 'Connected to "mainnet" network: ' , JSON . stringify ( network ) )
@@ -45,7 +47,7 @@ const testWithdraw = async (
4547 const tx1 = await tokenMainnet . mint ( adminWalletMainnet . address , parseEther ( '100' ) )
4648 await tx1 . wait ( )
4749
48- const adminClient = new StreamrClient ( config . clientOptions as any )
50+ const adminClient = new StreamrClient ( config . clientOptions )
4951
5052 const dataUnion = await adminClient . deployDataUnion ( )
5153 const secret = await dataUnion . createSecret ( 'test secret' )
@@ -168,20 +170,45 @@ describe('DataUnion withdraw', () => {
168170 providerSidechain . removeAllListeners ( )
169171 } )
170172
171- // TODO: add tests for just getting the hash and doing the transportMessage manually
172173 describe . each ( [
173174 [ false , true , true ] , // sidechain withdraw
174175 [ true , true , true ] , // self-service mainnet withdraw
175176 [ true , true , false ] , // self-service mainnet withdraw without checking the recipient account
176177 [ true , false , true ] , // bridge-sponsored mainnet withdraw
177178 [ true , false , false ] , // other-sponsored mainnet withdraw
178179 ] ) ( 'Withdrawing with sendToMainnet=%p, payForTransport=%p, wait=%p' , ( sendToMainnet , payForTransport , waitUntilTransportIsComplete ) => {
180+
181+ // for test debugging: select only one case by uncommenting below, and comment out the above .each block
182+ // const [sendToMainnet, payForTransport, waitUntilTransportIsComplete] = [true, false, true] // bridge-sponsored mainnet withdraw
183+
179184 const options = { sendToMainnet, payForTransport, waitUntilTransportIsComplete }
180185
181- const getTokenBalance = async ( wallet : Wallet ) => {
186+ async function getTokenBalance ( wallet : Wallet ) {
182187 return sendToMainnet ? balanceClient . getTokenBalance ( wallet . address ) : balanceClient . getSidechainTokenBalance ( wallet . address )
183188 }
184189
190+ // emulate the bridge-sponsored withdrawals
191+ beforeAll ( ( ) => {
192+ if ( ! payForTransport && waitUntilTransportIsComplete ) {
193+ const sidechainAmbAddress = '0xaFA0dc5Ad21796C9106a36D68f69aAD69994BB64'
194+ const signatureRequestEventSignature = '0x520d2afde79cbd5db58755ac9480f81bc658e5c517fcae7365a3d832590b0183'
195+ providerSidechain . on ( {
196+ address : sidechainAmbAddress ,
197+ topics : [ signatureRequestEventSignature ]
198+ } , async ( e ) => {
199+ const message = defaultAbiCoder . decode ( [ 'bytes' ] , e . data ) [ 0 ]
200+ const hash = keccak256 ( message )
201+ const adminClient = new StreamrClient ( config . clientOptions )
202+ await adminClient . getDataUnion ( '0x0000000000000000000000000000000000000000' ) . transportMessage ( hash )
203+ } )
204+ }
205+ } )
206+ afterAll ( ( ) => {
207+ if ( ! payForTransport && waitUntilTransportIsComplete ) {
208+ providerSidechain . removeAllListeners ( )
209+ }
210+ } )
211+
185212 describe ( 'by member' , ( ) => {
186213
187214 it ( 'to itself' , ( ) => {
0 commit comments