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
--- 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);
     }
   });
 });
modifiedtests/src/eth/reFungible.test.tsdiffbeforeafterboth
363 });363 });
364 364
365 itEth('Can perform transferCross()', async ({helper}) => {365 itEth('Can perform transferCross()', async ({helper}) => {
366 const caller = await helper.eth.createAccountWithBalance(donor);366 const sender = await helper.eth.createAccountWithBalance(donor);
367 const receiver = await helper.eth.createAccountWithBalance(donor);367 const receiverEth = await helper.eth.createAccountWithBalance(donor);
368 const to = helper.ethCrossAccount.fromAddress(receiver);368 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
369 const toSubstrate = helper.ethCrossAccount.fromKeyringPair(minter);369 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);
370
370 const collection = await helper.rft.mintCollection(minter, {});371 const collection = await helper.rft.mintCollection(minter, {});
371 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);372 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
372 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);373 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);
373374
374 const {tokenId} = await collection.mintToken(minter, 1n, {Ethereum: caller});375 const token = await collection.mintToken(minter, 50n, {Ethereum: sender});
375376
376 {377 {
378 // Can transferCross to ethereum address:
377 const result = await contract.methods.transferCross(to, tokenId).send({from: caller});379 const result = await collectionEvm.methods.transferCross(receiverCrossEth, token.tokenId).send({from: sender});
378380 // Check events:
379 const event = result.events.Transfer;381 const event = result.events.Transfer;
380 expect(event.address).to.equal(collectionAddress);382 expect(event.address).to.equal(collectionAddress);
381 expect(event.returnValues.from).to.equal(caller);383 expect(event.returnValues.from).to.equal(sender);
382 expect(event.returnValues.to).to.equal(receiver);384 expect(event.returnValues.to).to.equal(receiverEth);
383 expect(event.returnValues.tokenId).to.equal(tokenId.toString());385 expect(event.returnValues.tokenId).to.equal(token.tokenId.toString());
386 // Sender's balance decreased:
387 const senderBalance = await collectionEvm.methods.balanceOf(sender).call();
388 expect(+senderBalance).to.equal(0);
389 expect(await token.getBalance({Ethereum: sender})).to.eq(0n);
390 // Receiver's balance increased:
391 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();
392 expect(+receiverBalance).to.equal(1);
393 expect(await token.getBalance({Ethereum: receiverEth})).to.eq(50n);
384 }394 }
385395
386 {396 {
387 const balance = await contract.methods.balanceOf(caller).call();397 // Can transferCross to substrate address:
388 expect(+balance).to.equal(0);
389 }
390
391 {398 const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, token.tokenId).send({from: receiverEth});
392 const balance = await contract.methods.balanceOf(receiver).call();399 // Check events:
393 expect(+balance).to.equal(1);
394 }
395
396 {
397 const substrateResult = await contract.methods.transferCross(toSubstrate, tokenId).send({from: receiver});
398
399
400 const event = substrateResult.events.Transfer;400 const event = substrateResult.events.Transfer;
401 expect(event.address).to.be.equal(collectionAddress);401 expect(event.address).to.be.equal(collectionAddress);
402 expect(event.returnValues.from).to.be.equal(receiver);402 expect(event.returnValues.from).to.be.equal(receiverEth);
403 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));403 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));
404 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);404 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);
405 }405 // Sender's balance decreased:
406
407 {
408 const balance = await contract.methods.balanceOf(receiver).call();406 const senderBalance = await collectionEvm.methods.balanceOf(receiverEth).call();
409 expect(+balance).to.equal(0);407 expect(+senderBalance).to.equal(0);
410 }408 expect(await token.getBalance({Ethereum: receiverEth})).to.eq(0n);
411409 // Receiver's balance increased:
412 {
413 const balance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});410 const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});
414 expect(balance).to.be.contain(tokenId);411 expect(receiverBalance).to.contain(token.tokenId);
415 }412 expect(await token.getBalance({Substrate: minter.address})).to.eq(50n);
413 }
416 });414 });
415
416 itEth('Cannot transferCross with invalid params', async ({helper}) => {
417 const sender = await helper.eth.createAccountWithBalance(donor);
418 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);
419
420 const collection = await helper.rft.mintCollection(minter, {});
421 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
422 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);
423
424 const {tokenId} = await collection.mintToken(minter, 50n, {Ethereum: sender});
425 // FIXME (transaction successful): Cannot transfer token if it does not exist:
426 await expect(collectionEvm.methods.transferCross(receiverCrossSub, tokenId + 1).send({from: sender})).to.be.rejected;
427 });
417428
418 itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {429 itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {
419 const caller = await helper.eth.createAccountWithBalance(donor);430 const caller = await helper.eth.createAccountWithBalance(donor);