From 35094561612ee98d42933d99f4c47bce4c4c7aea Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Fri, 02 Dec 2022 10:33:46 +0000 Subject: [PATCH] Add transferCross tests --- --- a/tests/src/eth/fungible.test.ts +++ b/tests/src/eth/fungible.test.ts @@ -232,56 +232,49 @@ }); itEth('Can perform transferCross()', async ({helper}) => { - const owner = await helper.eth.createAccountWithBalance(donor); - const receiver = await helper.eth.createAccountWithBalance(donor); - const to = helper.ethCrossAccount.fromAddress(receiver); - const toSubstrate = helper.ethCrossAccount.fromKeyringPair(donor); + const sender = await helper.eth.createAccountWithBalance(donor); + const receiverEth = await helper.eth.createAccountWithBalance(donor); + const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth); + const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor); const collection = await helper.ft.mintCollection(alice); - await collection.mint(alice, 200n, {Ethereum: owner}); + await collection.mint(alice, 200n, {Ethereum: sender}); const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); - const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner); + const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender); { - const result = await contract.methods.transferCross(to, 50).send({from: owner}); - + // Can transferCross to ethereum address: + const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender}); + // Check events: const event = result.events.Transfer; expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal(owner); - expect(event.returnValues.to).to.be.equal(receiver); + expect(event.returnValues.from).to.be.equal(sender); + expect(event.returnValues.to).to.be.equal(receiverEth); expect(event.returnValues.value).to.be.equal('50'); - } - - { - const balance = await contract.methods.balanceOf(owner).call(); - expect(+balance).to.equal(150); - } - - { - const balance = await contract.methods.balanceOf(receiver).call(); - expect(+balance).to.equal(50); + // Sender's balance decreased: + const ownerBalance = await collectionEvm.methods.balanceOf(sender).call(); + expect(+ownerBalance).to.equal(150); + // Receiver's balance increased: + const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call(); + expect(+receiverBalance).to.equal(50); } { - const result = await contract.methods.transferCross(toSubstrate, 50).send({from: owner}); - + // Can transferCross to substrate address: + const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender}); + // Check events: const event = result.events.Transfer; expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal(owner); + expect(event.returnValues.from).to.be.equal(sender); expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address)); expect(event.returnValues.value).to.be.equal('50'); - } - - { - const balance = await collection.getBalance({Ethereum: owner}); - expect(balance).to.equal(100n); - } - - { + // Sender's balance decreased: + const senderBalance = await collection.getBalance({Ethereum: sender}); + expect(senderBalance).to.equal(100n); + // Receiver's balance increased: const balance = await collection.getBalance({Substrate: donor.address}); expect(balance).to.equal(50n); } - }); itEth('Can perform transfer()', async ({helper}) => { --- a/tests/src/eth/nonFungible.test.ts +++ b/tests/src/eth/nonFungible.test.ts @@ -252,8 +252,9 @@ itEth('Can perform burnFromCross()', async ({helper}) => { const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'}); const ownerSub = bob; - const ownerCross = helper.ethCrossAccount.fromKeyringPair(ownerSub); + const ownerCrossSub = helper.ethCrossAccount.fromKeyringPair(ownerSub); const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n); + const ownerCrossEth = helper.ethCrossAccount.fromAddress(ownerEth); const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n); const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth); @@ -269,20 +270,23 @@ await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth}); // can burnFromCross: - const result1 = await collectionEvm.methods.burnFromCross(ownerCross, token1.tokenId).send({from: burnerEth}); - // FIXME Error No Permission?: - const result2 = await collectionEvm.methods.burnFromCross(ownerCross, token2.tokenId).send({from: burnerEth}); + const result1 = await collectionEvm.methods.burnFromCross(ownerCrossSub, token1.tokenId).send({from: burnerEth}); + const result2 = await collectionEvm.methods.burnFromCross(ownerCrossEth, token2.tokenId).send({from: burnerEth}); const events1 = result1.events.Transfer; const events2 = result2.events.Transfer; - [[events1, token1], [events2, token2]].map(burnEvents => { - expect(burnEvents[0]).to.be.like({ + // Check events for burnFromCross (substrate and ethereum): + [ + [events1, token1, helper.address.substrateToEth(ownerSub.address)], + [events2, token2, ownerEth], + ].map(burnData => { + expect(burnData[0]).to.be.like({ address: collectionAddress, event: 'Transfer', returnValues: { - from: helper.address.substrateToEth(ownerSub.address), + from: burnData[2], to: '0x0000000000000000000000000000000000000000', - tokenId: burnEvents[1].tokenId.toString(), + tokenId: burnData[1].tokenId.toString(), }, }); }); @@ -341,6 +345,7 @@ itEth('Can reaffirm approved address', async ({helper}) => { const owner = await helper.eth.createAccountWithBalance(donor, 100n); + const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner); const [receiver1, receiver2] = await helper.arrange.createAccounts([100n, 100n], donor); const receiver1Cross = helper.ethCrossAccount.fromKeyringPair(receiver1); const receiver2Cross = helper.ethCrossAccount.fromKeyringPair(receiver2); @@ -358,12 +363,9 @@ // receiver2 can transferFrom: await helper.nft.transferTokenFrom(receiver2, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver2.address}); - // can set approved address to zero address: + // can set approved address to self address to remove approval: await collectionEvm.methods.approveCross(receiver1Cross, token2.tokenId).send({from: owner}); - - // FIXME how to remove approval?: - await collectionEvm.methods.approveCross({eth: '0x0000000000000000000000000000000000000000', sub: '0'}, token2.tokenId).call({from: owner}); - await collectionEvm.methods.approve('0x0000000000000000000000000000000000000000', token2.tokenId).call({from: owner}); + await collectionEvm.methods.approveCross(ownerCrossEth, token2.tokenId).send({from: owner}); // receiver1 cannot transfer token anymore: await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token2.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected; @@ -469,54 +471,50 @@ itEth('Can perform transferCross()', async ({helper}) => { const collection = await helper.nft.mintCollection(minter, {}); const owner = await helper.eth.createAccountWithBalance(donor); - const receiver = await helper.eth.createAccountWithBalance(donor); - const to = helper.ethCrossAccount.fromAddress(receiver); - const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter); + const receiverEth = await helper.eth.createAccountWithBalance(donor); + const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth); + const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter); const {tokenId} = await collection.mintToken(minter, {Ethereum: owner}); const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); - const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner); + const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner); { - const result = await contract.methods.transferCross(to, tokenId).send({from: owner}); - + // Can transferCross to ethereum address: + const result = await collectionEvm.methods.transferCross(receiverCrossEth, tokenId).send({from: owner}); + // Check events: const event = result.events.Transfer; expect(event.address).to.be.equal(collectionAddress); expect(event.returnValues.from).to.be.equal(owner); - expect(event.returnValues.to).to.be.equal(receiver); + expect(event.returnValues.to).to.be.equal(receiverEth); expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`); - } - - { - const balance = await contract.methods.balanceOf(owner).call(); - expect(+balance).to.equal(0); + + // owner has balance = 0: + const ownerBalance = await collectionEvm.methods.balanceOf(owner).call(); + expect(+ownerBalance).to.equal(0); + // receiver owns token: + const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call(); + expect(+receiverBalance).to.equal(1); + expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()}); } - - { - const balance = await contract.methods.balanceOf(receiver).call(); - expect(+balance).to.equal(1); - } { - const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver}); - - + // Can transferCross to substrate address: + const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, tokenId).send({from: receiverEth}); + // Check events: const event = substrateResult.events.Transfer; expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal(receiver); + expect(event.returnValues.from).to.be.equal(receiverEth); expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address)); expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`); - } - - { - const balance = await contract.methods.balanceOf(receiver).call(); - expect(+balance).to.equal(0); - } - - { - const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address}); - expect(balance).to.be.contain(tokenId); + + // owner has balance = 0: + const ownerBalance = await collectionEvm.methods.balanceOf(receiverEth).call(); + expect(+ownerBalance).to.equal(0); + // receiver owns token: + const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address}); + expect(receiverBalance).to.contain(tokenId); } }); }); --- a/tests/src/eth/reFungible.test.ts +++ b/tests/src/eth/reFungible.test.ts @@ -363,56 +363,67 @@ }); itEth('Can perform transferCross()', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const receiver = await helper.eth.createAccountWithBalance(donor); - const to = helper.ethCrossAccount.fromAddress(receiver); - const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter); + const sender = await helper.eth.createAccountWithBalance(donor); + const receiverEth = await helper.eth.createAccountWithBalance(donor); + const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth); + const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter); + const collection = await helper.rft.mintCollection(minter, {}); const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); - const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller); + const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender); - const {tokenId} = await collection.mintToken(minter, 1n, {Ethereum: caller}); + const token = await collection.mintToken(minter, 50n, {Ethereum: sender}); { - const result = await contract.methods.transferCross(to, tokenId).send({from: caller}); - + // Can transferCross to ethereum address: + const result = await collectionEvm.methods.transferCross(receiverCrossEth, token.tokenId).send({from: sender}); + // Check events: const event = result.events.Transfer; expect(event.address).to.equal(collectionAddress); - expect(event.returnValues.from).to.equal(caller); - expect(event.returnValues.to).to.equal(receiver); - expect(event.returnValues.tokenId).to.equal(tokenId.toString()); - } - - { - const balance = await contract.methods.balanceOf(caller).call(); - expect(+balance).to.equal(0); - } - - { - const balance = await contract.methods.balanceOf(receiver).call(); - expect(+balance).to.equal(1); + expect(event.returnValues.from).to.equal(sender); + expect(event.returnValues.to).to.equal(receiverEth); + expect(event.returnValues.tokenId).to.equal(token.tokenId.toString()); + // Sender's balance decreased: + const senderBalance = await collectionEvm.methods.balanceOf(sender).call(); + expect(+senderBalance).to.equal(0); + expect(await token.getBalance({Ethereum: sender})).to.eq(0n); + // Receiver's balance increased: + const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call(); + expect(+receiverBalance).to.equal(1); + expect(await token.getBalance({Ethereum: receiverEth})).to.eq(50n); } { - const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver}); - - + // Can transferCross to substrate address: + const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, token.tokenId).send({from: receiverEth}); + // Check events: const event = substrateResult.events.Transfer; expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal(receiver); + expect(event.returnValues.from).to.be.equal(receiverEth); expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address)); - expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`); + expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`); + // Sender's balance decreased: + const senderBalance = await collectionEvm.methods.balanceOf(receiverEth).call(); + expect(+senderBalance).to.equal(0); + expect(await token.getBalance({Ethereum: receiverEth})).to.eq(0n); + // Receiver's balance increased: + const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address}); + expect(receiverBalance).to.contain(token.tokenId); + expect(await token.getBalance({Substrate: minter.address})).to.eq(50n); } + }); - { - const balance = await contract.methods.balanceOf(receiver).call(); - expect(+balance).to.equal(0); - } + itEth('Cannot transferCross with invalid params', async ({helper}) => { + const sender = await helper.eth.createAccountWithBalance(donor); + const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter); - { - const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address}); - expect(balance).to.be.contain(tokenId); - } + const collection = await helper.rft.mintCollection(minter, {}); + const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); + const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender); + + const {tokenId} = await collection.mintToken(minter, 50n, {Ethereum: sender}); + // FIXME (transaction successful): Cannot transfer token if it does not exist: + await expect(collectionEvm.methods.transferCross(receiverCrossSub, tokenId + 1).send({from: sender})).to.be.rejected; }); itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => { -- gitstuff