From ebdb67ffae1d9d522582e8c3ec9b5a978bb698a1 Mon Sep 17 00:00:00 2001 From: str-mv Date: Wed, 11 Aug 2021 08:05:05 +0000 Subject: [PATCH] CORE-159. Transfer through substrate and EVM --- --- /dev/null +++ b/tests/src/eth/crossTransfer.test.ts @@ -0,0 +1,89 @@ +// +// This file is subject to the terms and conditions defined in +// file 'LICENSE', which is part of this source code package. +// + +import privateKey from '../substrate/privateKey'; +import { createCollectionExpectSuccess, + createFungibleItemExpectSuccess, + transferExpectSuccess, + transferFromExpectSuccess, + CrossAccountId, + createItemExpectSuccess, + normalizeAccountId } from '../util/helpers'; +import { collectionIdToAddress, + createEthAccountWithBalance, + GAS_ARGS, itWeb3 } from './util/helpers'; +import fungibleAbi from './fungibleAbi.json'; +import nonFungibleAbi from './nonFungibleAbi.json'; + +describe('Token transfer between substrate address and EVM address. Fungible', () => { + itWeb3('The private key X create a substrate address. Alice sends a token to the corresponding EVM address, and X can send it to Bob in the substrate', async () => { + const collection = await createCollectionExpectSuccess({ + name: 'token name', + mode: { type: 'Fungible', decimalPoints: 0 }, + }); + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + const charlie = privateKey('//Charlie'); + const ethAccountVariant: CrossAccountId = normalizeAccountId(charlie.address); + await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, { substrate: alice.address }); + await transferExpectSuccess(collection, 0, alice, ethAccountVariant , 200, 'Fungible'); + await transferExpectSuccess(collection, 0, charlie, bob , 200, 'Fungible'); + }); + + itWeb3('The private key X create a EVM address. Alice sends a token to the substrate address corresponding to this EVM address, and X can send it to Bob in the EVM', async ({ api, web3 }) => { + const collection = await createCollectionExpectSuccess({ + name: 'token name', + mode: { type: 'Fungible', decimalPoints: 0 }, + }); + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + const bobProxy = await createEthAccountWithBalance(api, web3); + const aliceProxy = await createEthAccountWithBalance(api, web3); + + await createFungibleItemExpectSuccess(alice, collection, { Value: 200n }, alice.address); + await transferExpectSuccess(collection, 0, alice, { ethereum: aliceProxy } , 200, 'Fungible'); + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(fungibleAbi as any, address, {from: aliceProxy, ...GAS_ARGS}); + + await contract.methods.transfer(bobProxy, 50).send({ from: aliceProxy }); + await transferFromExpectSuccess(collection, 0, alice, {ethereum: bobProxy}, bob, 50, 'Fungible'); + await transferExpectSuccess(collection, 0, bob, alice, 50, 'Fungible'); + }); +}); + +describe.only('Token transfer between substrate address and EVM address. NFT', () => { + itWeb3.skip('The private key X create a substrate address. Alice sends a token to the corresponding EVM address, and X can send it to Bob in the substrate', async () => { + const collection = await createCollectionExpectSuccess({ + name: 'token name', + mode: { type: 'NFT' }, + }); + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + const charlie = privateKey('//Charlie'); + const ethAccountVariant: CrossAccountId = normalizeAccountId(charlie.address); + const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address }); + await transferExpectSuccess(collection, tokenId, alice, ethAccountVariant, 1, 'NFT'); + await transferExpectSuccess(collection, tokenId, charlie, bob, 1, 'NFT'); + }); + + itWeb3('The private key X create a EVM address. Alice sends a token to the substrate address corresponding to this EVM address, and X can send it to Bob in the EVM', async ({ api, web3 }) => { + const collection = await createCollectionExpectSuccess({ + name: 'token name', + mode: { type: 'NFT' }, + }); + const alice = privateKey('//Alice'); + const bob = privateKey('//Bob'); + const charlie = privateKey('//Charlie'); + const bobProxy = await createEthAccountWithBalance(api, web3); + const aliceProxy = await createEthAccountWithBalance(api, web3); + const tokenId = await createItemExpectSuccess(alice, collection, 'NFT', { substrate: alice.address }); + await transferExpectSuccess(collection, tokenId, alice, { ethereum: aliceProxy } , 1, 'NFT'); + const address = collectionIdToAddress(collection); + const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: aliceProxy, ...GAS_ARGS}); + await contract.methods.transfer(bobProxy, 1).send({ from: aliceProxy }); + await transferFromExpectSuccess(collection, tokenId, alice, {ethereum: bobProxy}, bob, 1, 'NFT'); + await transferExpectSuccess(collection, tokenId, bob, charlie, 1, 'NFT'); + }); +}); \ No newline at end of file -- gitstuff