|
| 1 | +import { BigNumber, Contract, providers, Wallet } from 'ethers' |
| 2 | +import { parseEther } from 'ethers/lib/utils' |
| 3 | +import debug from 'debug' |
| 4 | +import * as Token from '../../../contracts/TestToken.json' |
| 5 | +import config from '../config' |
| 6 | +import { getEndpointUrl, until } from '../../../src/utils' |
| 7 | +import { MemberStatus } from '../../../src/dataunion/DataUnion' |
| 8 | +import { StreamrClient } from '../../../src/StreamrClient' |
| 9 | +import { EthereumAddress } from '../../../src/types' |
| 10 | +import authFetch from '../../../src/rest/authFetch' |
| 11 | + |
| 12 | +const log = debug('StreamrClient::DataUnion::integration-test-transfer') |
| 13 | + |
| 14 | +const providerSidechain = new providers.JsonRpcProvider(config.clientOptions.sidechain) |
| 15 | +const providerMainnet = new providers.JsonRpcProvider(config.clientOptions.mainnet) |
| 16 | + |
| 17 | +const tokenAdminWallet = new Wallet(config.tokenAdminPrivateKey, providerMainnet) |
| 18 | +const tokenMainnet = new Contract(config.clientOptions.tokenAddress, Token.abi, tokenAdminWallet) |
| 19 | + |
| 20 | +const adminWalletSidechain = new Wallet(config.clientOptions.auth.privateKey, providerSidechain) |
| 21 | +const tokenSidechain = new Contract(config.clientOptions.tokenSidechainAddress, Token.abi, adminWalletSidechain) |
| 22 | + |
| 23 | +const sendTokensToSidechain = async (receiverAddress: EthereumAddress, amount: BigNumber) => { |
| 24 | + const relayTokensAbi = [ |
| 25 | + { |
| 26 | + inputs: [ |
| 27 | + { |
| 28 | + internalType: 'address', |
| 29 | + name: 'token', |
| 30 | + type: 'address' |
| 31 | + }, |
| 32 | + { |
| 33 | + internalType: 'address', |
| 34 | + name: '_receiver', |
| 35 | + type: 'address' |
| 36 | + }, |
| 37 | + { |
| 38 | + internalType: 'uint256', |
| 39 | + name: '_value', |
| 40 | + type: 'uint256' |
| 41 | + }, |
| 42 | + { |
| 43 | + internalType: 'bytes', |
| 44 | + name: '_data', |
| 45 | + type: 'bytes' |
| 46 | + } |
| 47 | + ], |
| 48 | + name: 'relayTokensAndCall', |
| 49 | + outputs: [], |
| 50 | + stateMutability: 'nonpayable', |
| 51 | + type: 'function' |
| 52 | + } |
| 53 | + ] |
| 54 | + const tokenMediator = new Contract(config.tokenMediator, relayTokensAbi, tokenAdminWallet) |
| 55 | + const tx1 = await tokenMainnet.approve(tokenMediator.address, amount) |
| 56 | + await tx1.wait() |
| 57 | + log('Approved') |
| 58 | + const tx2 = await tokenMediator.relayTokensAndCall(tokenMainnet.address, receiverAddress, amount, '0x1234') // dummy 0x1234 |
| 59 | + await tx2.wait() |
| 60 | + log('Relayed tokens') |
| 61 | + await until(async () => !(await tokenSidechain.balanceOf(receiverAddress)).eq('0'), 300000, 3000) |
| 62 | + log('Sidechain balance changed') |
| 63 | +} |
| 64 | + |
| 65 | +describe('DataUnion transfer within contract', () => { |
| 66 | + let adminClient: StreamrClient |
| 67 | + |
| 68 | + beforeAll(async () => { |
| 69 | + log(`Connecting to Ethereum networks, config = ${JSON.stringify(config)}`) |
| 70 | + const network = await providerMainnet.getNetwork() |
| 71 | + log('Connected to "mainnet" network: ', JSON.stringify(network)) |
| 72 | + const network2 = await providerSidechain.getNetwork() |
| 73 | + log('Connected to sidechain network: ', JSON.stringify(network2)) |
| 74 | + log(`Minting 100 tokens to ${tokenAdminWallet.address}`) |
| 75 | + const tx1 = await tokenMainnet.mint(tokenAdminWallet.address, parseEther('100')) |
| 76 | + await tx1.wait() |
| 77 | + |
| 78 | + await sendTokensToSidechain(adminWalletSidechain.address, parseEther('10')) |
| 79 | + |
| 80 | + adminClient = new StreamrClient(config.clientOptions as any) |
| 81 | + }, 150000) |
| 82 | + |
| 83 | + afterAll(() => { |
| 84 | + providerMainnet.removeAllListeners() |
| 85 | + providerSidechain.removeAllListeners() |
| 86 | + }) |
| 87 | + |
| 88 | + it('transfer token to member', async () => { |
| 89 | + const dataUnion = await adminClient.deployDataUnion() |
| 90 | + const secret = await dataUnion.createSecret('test secret') |
| 91 | + // eslint-disable-next-line no-underscore-dangle |
| 92 | + const contract = await dataUnion._getContract() |
| 93 | + log(`DU owner: ${await dataUnion.getAdminAddress()}`) |
| 94 | + log(`Sending tx from ${await adminClient.getAddress()}`) |
| 95 | + |
| 96 | + // product is needed for join requests to analyze the DU version |
| 97 | + const createProductUrl = getEndpointUrl(config.clientOptions.restUrl, 'products') |
| 98 | + await authFetch(createProductUrl, adminClient.session, { |
| 99 | + method: 'POST', |
| 100 | + body: JSON.stringify({ |
| 101 | + beneficiaryAddress: dataUnion.getAddress(), |
| 102 | + type: 'DATAUNION', |
| 103 | + dataUnionVersion: 2 |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + const memberWallet = new Wallet(`0x100000000000000000000000000000000000000012300000001${Date.now()}`, providerSidechain) |
| 108 | + const memberClient = new StreamrClient({ |
| 109 | + ...config.clientOptions, |
| 110 | + auth: { |
| 111 | + privateKey: memberWallet.privateKey |
| 112 | + } |
| 113 | + } as any) |
| 114 | + const res = await memberClient.getDataUnion(dataUnion.getAddress()).join(secret) |
| 115 | + log(`Member joined data union: ${JSON.stringify(res)}`) |
| 116 | + log(`DU member count: ${await contract.sidechain.activeMemberCount()}`) |
| 117 | + |
| 118 | + const stats = await memberClient.getDataUnion(dataUnion.getAddress()).getMemberStats(memberWallet.address) |
| 119 | + log(`Stats: ${JSON.stringify(stats)}`) |
| 120 | + |
| 121 | + const approve = await tokenSidechain.approve(dataUnion.getSidechainAddress(), parseEther('1')) |
| 122 | + await approve.wait() |
| 123 | + log(`Approve DU ${dataUnion.getSidechainAddress()} to access 1 token from ${adminWalletSidechain.address}`) |
| 124 | + |
| 125 | + const tx = await dataUnion.transferToMemberInContract(memberWallet.address, parseEther('1')) |
| 126 | + await tx.wait() |
| 127 | + log(`Transfer 1 token with transferWithinContract to ${memberWallet.address}`) |
| 128 | + |
| 129 | + const newStats = await memberClient.getDataUnion(dataUnion.getAddress()).getMemberStats(memberWallet.address) |
| 130 | + log(`Stats: ${JSON.stringify(newStats)}`) |
| 131 | + |
| 132 | + expect(stats).toMatchObject({ |
| 133 | + status: MemberStatus.ACTIVE, |
| 134 | + earningsBeforeLastJoin: BigNumber.from(0), |
| 135 | + totalEarnings: BigNumber.from(0), |
| 136 | + withdrawableEarnings: BigNumber.from(0) |
| 137 | + }) |
| 138 | + expect(newStats).toMatchObject({ |
| 139 | + status: MemberStatus.ACTIVE, |
| 140 | + earningsBeforeLastJoin: parseEther('1'), |
| 141 | + totalEarnings: parseEther('1'), |
| 142 | + withdrawableEarnings: parseEther('1') |
| 143 | + }) |
| 144 | + }, 150000) |
| 145 | +}) |
0 commit comments