From 09a417ba56012c333626a7d6f4da3ed97c1b794c Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Thu, 01 Dec 2022 09:51:29 +0000 Subject: [PATCH] Add approveCross and trasferFromCross checks --- --- a/tests/src/eth/allowlist.test.ts +++ b/tests/src/eth/allowlist.test.ts @@ -91,7 +91,7 @@ expect(await collectionEvm.methods.allowlistedCross(crossUser).call({from: owner})).to.be.false; }); - itEth.only('Collection allowlist can be added and removed by [cross] address', async ({helper}) => { + itEth('Collection allowlist can be added and removed by [cross] address', async ({helper}) => { const owner = (await helper.eth.createAccountWithBalance(donor)).toLowerCase(); const [userSub] = await helper.arrange.createAccounts([10n], donor); const userEth = await helper.eth.createAccountWithBalance(donor); @@ -100,6 +100,7 @@ const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner); const userCrossSub = helper.ethCrossAccount.fromKeyringPair(userSub); const userCrossEth = helper.ethCrossAccount.fromAddress(userEth); + const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner); // Can addToCollectionAllowListCross: expect(await helper.collection.allowed(collectionId, {Substrate: userSub.address})).to.be.false; @@ -114,6 +115,7 @@ await collectionEvm.methods.mint(userEth).send(); await collectionEvm.methods.setCollectionAccess(1).send(); await collectionEvm.methods.transfer(owner, 1).send({from: userEth}); // FIXME: why not? + // await collectionEvm.methods.transferCross(ownerCrossEth, 1).send({from: userEth}); // FIXME: why not? expect(await helper.nft.getTokenOwner(collectionId, 1)).to.eq({Ethereum: owner}); // can removeFromCollectionAllowListCross --- a/tests/src/eth/nonFungible.test.ts +++ b/tests/src/eth/nonFungible.test.ts @@ -17,7 +17,6 @@ import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util'; import {IKeyringPair} from '@polkadot/types/types'; import {Contract} from 'web3-eth-contract'; -import exp from 'constants'; describe('NFT: Information getting', () => { @@ -280,30 +279,51 @@ }); itEth('Can perform approveCross()', async ({helper}) => { + // arrange: create accounts + const owner = await helper.eth.createAccountWithBalance(donor, 100n); + const ownerCross = helper.ethCrossAccount.fromAddress(owner); + const receiverSub = charlie; + const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub); + const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n); + const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth); + + // arrange: create collection and tokens: const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'}); + const token1 = await collection.mintToken(minter, {Ethereum: owner}); + const token2 = await collection.mintToken(minter, {Ethereum: owner}); - const owner = await helper.eth.createAccountWithBalance(donor, 100n); - const receiver = charlie; + const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft'); - const token = await collection.mintToken(minter, {Ethereum: owner}); + // Can approveCross substrate and ethereum address: + const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner}); + const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner}); + const eventSub = resultSub.events.Approval; + const eventEth = resultEth.events.Approval; + expect(eventSub).to.be.like({ + address: helper.ethAddress.fromCollectionId(collection.collectionId), + event: 'Approval', + returnValues: { + owner, + approved: helper.address.substrateToEth(receiverSub.address), + tokenId: token1.tokenId.toString(), + }, + }); + expect(eventEth).to.be.like({ + address: helper.ethAddress.fromCollectionId(collection.collectionId), + event: 'Approval', + returnValues: { + owner, + approved: receiverEth, + tokenId: token2.tokenId.toString(), + }, + }); - const address = helper.ethAddress.fromCollectionId(collection.collectionId); - const contract = helper.ethNativeContract.collection(address, 'nft'); - - { - const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver); - const result = await contract.methods.approveCross(recieverCross, token.tokenId).send({from: owner}); - const event = result.events.Approval; - expect(event).to.be.like({ - address: helper.ethAddress.fromCollectionId(collection.collectionId), - event: 'Approval', - returnValues: { - owner, - approved: helper.address.substrateToEth(receiver.address), - tokenId: token.tokenId.toString(), - }, - }); - } + // Substrate address can transferFrom approved tokens: + await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address}); + expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address}); + // Ethereum address can transferFromCross approved tokens: + await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth}); + expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()}); }); itEth('Can perform transferFrom()', async ({helper}) => { @@ -501,7 +521,7 @@ expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal()))); }); - itEth('Can perform transferFromCross()', async ({helper, privateKey}) => { + itEth('Can perform transferFromCross()', async ({helper}) => { const collectionMinter = alice; const owner = bob; const receiver = charlie; -- gitstuff