git.delta.rocks / unique-network / refs/commits / 35094561612e

difftreelog

Add transferCross tests

Max Andreev2022-12-02parent: #8c823a6.patch.diff
in: master

3 files changed

modifiedtests/src/eth/fungible.test.tsdiffbeforeafterboth
--- 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}) => {
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
252 itEth('Can perform burnFromCross()', async ({helper}) => {252 itEth('Can perform burnFromCross()', async ({helper}) => {
253 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});253 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});
254 const ownerSub = bob;254 const ownerSub = bob;
255 const ownerCross = helper.ethCrossAccount.fromKeyringPair(ownerSub);255 const ownerCrossSub = helper.ethCrossAccount.fromKeyringPair(ownerSub);
256 const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n);256 const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n);
257 const ownerCrossEth = helper.ethCrossAccount.fromAddress(ownerEth);
257258
258 const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n);259 const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n);
259 const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth);260 const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth);
269 await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth});270 await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth});
270271
271 // can burnFromCross:272 // can burnFromCross:
272 const result1 = await collectionEvm.methods.burnFromCross(ownerCross, token1.tokenId).send({from: burnerEth});273 const result1 = await collectionEvm.methods.burnFromCross(ownerCrossSub, token1.tokenId).send({from: burnerEth});
273 // FIXME Error No Permission?:
274 const result2 = await collectionEvm.methods.burnFromCross(ownerCross, token2.tokenId).send({from: burnerEth});274 const result2 = await collectionEvm.methods.burnFromCross(ownerCrossEth, token2.tokenId).send({from: burnerEth});
275 const events1 = result1.events.Transfer;275 const events1 = result1.events.Transfer;
276 const events2 = result2.events.Transfer;276 const events2 = result2.events.Transfer;
277277
278 // Check events for burnFromCross (substrate and ethereum):
278 [[events1, token1], [events2, token2]].map(burnEvents => {279 [
280 [events1, token1, helper.address.substrateToEth(ownerSub.address)],
281 [events2, token2, ownerEth],
282 ].map(burnData => {
279 expect(burnEvents[0]).to.be.like({283 expect(burnData[0]).to.be.like({
280 address: collectionAddress,284 address: collectionAddress,
281 event: 'Transfer',285 event: 'Transfer',
282 returnValues: {286 returnValues: {
283 from: helper.address.substrateToEth(ownerSub.address),287 from: burnData[2],
284 to: '0x0000000000000000000000000000000000000000',288 to: '0x0000000000000000000000000000000000000000',
285 tokenId: burnEvents[1].tokenId.toString(),289 tokenId: burnData[1].tokenId.toString(),
286 },290 },
287 });291 });
288 });292 });
341345
342 itEth('Can reaffirm approved address', async ({helper}) => {346 itEth('Can reaffirm approved address', async ({helper}) => {
343 const owner = await helper.eth.createAccountWithBalance(donor, 100n);347 const owner = await helper.eth.createAccountWithBalance(donor, 100n);
348 const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);
344 const [receiver1, receiver2] = await helper.arrange.createAccounts([100n, 100n], donor);349 const [receiver1, receiver2] = await helper.arrange.createAccounts([100n, 100n], donor);
345 const receiver1Cross = helper.ethCrossAccount.fromKeyringPair(receiver1);350 const receiver1Cross = helper.ethCrossAccount.fromKeyringPair(receiver1);
346 const receiver2Cross = helper.ethCrossAccount.fromKeyringPair(receiver2);351 const receiver2Cross = helper.ethCrossAccount.fromKeyringPair(receiver2);
358 // receiver2 can transferFrom:363 // receiver2 can transferFrom:
359 await helper.nft.transferTokenFrom(receiver2, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver2.address});364 await helper.nft.transferTokenFrom(receiver2, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver2.address});
360365
361 // can set approved address to zero address:366 // can set approved address to self address to remove approval:
362 await collectionEvm.methods.approveCross(receiver1Cross, token2.tokenId).send({from: owner});367 await collectionEvm.methods.approveCross(receiver1Cross, token2.tokenId).send({from: owner});
363
364 // FIXME how to remove approval?:
365 await collectionEvm.methods.approveCross({eth: '0x0000000000000000000000000000000000000000', sub: '0'}, token2.tokenId).call({from: owner});368 await collectionEvm.methods.approveCross(ownerCrossEth, token2.tokenId).send({from: owner});
366 await collectionEvm.methods.approve('0x0000000000000000000000000000000000000000', token2.tokenId).call({from: owner});
367369
368 // receiver1 cannot transfer token anymore:370 // receiver1 cannot transfer token anymore:
369 await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token2.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;371 await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token2.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;
469 itEth('Can perform transferCross()', async ({helper}) => {471 itEth('Can perform transferCross()', async ({helper}) => {
470 const collection = await helper.nft.mintCollection(minter, {});472 const collection = await helper.nft.mintCollection(minter, {});
471 const owner = await helper.eth.createAccountWithBalance(donor);473 const owner = await helper.eth.createAccountWithBalance(donor);
472 const receiver = await helper.eth.createAccountWithBalance(donor);474 const receiverEth = await helper.eth.createAccountWithBalance(donor);
473 const to = helper.ethCrossAccount.fromAddress(receiver);475 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
474 const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);476 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);
475 477
476 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});478 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});
477479
478 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);480 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
479 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);481 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
480482
481 {483 {
482 const result = await contract.methods.transferCross(to, tokenId).send({from: owner});484 // Can transferCross to ethereum address:
483485 const result = await collectionEvm.methods.transferCross(receiverCrossEth, tokenId).send({from: owner});
486 // Check events:
484 const event = result.events.Transfer;487 const event = result.events.Transfer;
485 expect(event.address).to.be.equal(collectionAddress);488 expect(event.address).to.be.equal(collectionAddress);
486 expect(event.returnValues.from).to.be.equal(owner);489 expect(event.returnValues.from).to.be.equal(owner);
487 expect(event.returnValues.to).to.be.equal(receiver);490 expect(event.returnValues.to).to.be.equal(receiverEth);
488 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);491 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);
489 }492
490493 // owner has balance = 0:
491 {
492 const balance = await contract.methods.balanceOf(owner).call();494 const ownerBalance = await collectionEvm.methods.balanceOf(owner).call();
493 expect(+balance).to.equal(0);495 expect(+ownerBalance).to.equal(0);
494 }496 // receiver owns token:
495
496 {
497 const balance = await contract.methods.balanceOf(receiver).call();497 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();
498 expect(+balance).to.equal(1);498 expect(+receiverBalance).to.equal(1);
499 }
500
501 {
502 const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});499 expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});
503 500 }
504501
502 {
503 // Can transferCross to substrate address:
504 const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, tokenId).send({from: receiverEth});
505 // Check events:
505 const event = substrateResult.events.Transfer;506 const event = substrateResult.events.Transfer;
506 expect(event.address).to.be.equal(collectionAddress);507 expect(event.address).to.be.equal(collectionAddress);
507 expect(event.returnValues.from).to.be.equal(receiver);508 expect(event.returnValues.from).to.be.equal(receiverEth);
508 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));509 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));
509 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);510 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);
510 }511
511512 // owner has balance = 0:
512 {
513 const balance = await contract.methods.balanceOf(receiver).call();513 const ownerBalance = await collectionEvm.methods.balanceOf(receiverEth).call();
514 expect(+balance).to.equal(0);514 expect(+ownerBalance).to.equal(0);
515 }515 // receiver owns token:
516
517 {
518 const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});516 const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});
519 expect(balance).to.be.contain(tokenId);517 expect(receiverBalance).to.contain(tokenId);
520 }518 }
521 });519 });
522});520});
523521
modifiedtests/src/eth/reFungible.test.tsdiffbeforeafterboth
--- 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}) => {