git.delta.rocks / unique-network / refs/commits / 49182bd0a7fd

difftreelog

source

tests/src/eth/fungible.test.ts24.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('Fungible: Information getting', () => {21  let donor: IKeyringPair;22  let alice: IKeyringPair;2324  before(async function() {25    await usingEthPlaygrounds(async (helper, privateKey) => {26      donor = await privateKey({filename: __filename});27      [alice] = await helper.arrange.createAccounts([20n], donor);28    });29  });3031  itEth('totalSupply', async ({helper}) => {32    const caller = await helper.eth.createAccountWithBalance(donor);33    const collection = await helper.ft.mintCollection(alice);34    await collection.mint(alice, 200n);3536    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);37    const totalSupply = await contract.methods.totalSupply().call();38    expect(totalSupply).to.equal('200');39  });4041  itEth('balanceOf', async ({helper}) => {42    const caller = await helper.eth.createAccountWithBalance(donor);43    const collection = await helper.ft.mintCollection(alice);44    await collection.mint(alice, 200n, {Ethereum: caller});4546    const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);47    const balance = await contract.methods.balanceOf(caller).call();48    expect(balance).to.equal('200');49  });50});5152describe('Fungible: Plain calls', () => {53  let donor: IKeyringPair;54  let alice: IKeyringPair;55  let owner: IKeyringPair;5657  before(async function() {58    await usingEthPlaygrounds(async (helper, privateKey) => {59      donor = await privateKey({filename: __filename});60      [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor);61    });62  });6364  itEth('Can perform mint()', async ({helper}) => {65    const owner = await helper.eth.createAccountWithBalance(donor);66    const receiver = helper.eth.createAccount();67    const collection = await helper.ft.mintCollection(alice);68    await collection.addAdmin(alice, {Ethereum: owner});6970    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);71    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7273    const result = await contract.methods.mint(receiver, 100).send();74    75    const event = result.events.Transfer;76    expect(event.address).to.equal(collectionAddress);77    expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');78    expect(event.returnValues.to).to.equal(receiver);79    expect(event.returnValues.value).to.equal('100');80  });81  82  83  itEth('Can perform mintCross()', async ({helper}) => {84    const receiverCross = helper.ethCrossAccount.fromKeyringPair(owner);85    const ethOwner = await helper.eth.createAccountWithBalance(donor);86    const collection = await helper.ft.mintCollection(alice);87    await collection.addAdmin(alice, {Ethereum: ethOwner});8889    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);90    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);9192    const result = await contract.methods.mintCross(receiverCross, 100).send();93    94    const event = result.events.Transfer;95    expect(event.address).to.equal(collectionAddress);96    expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');97    expect(event.returnValues.to).to.equal(helper.address.substrateToEth(owner.address));98    expect(event.returnValues.value).to.equal('100');99  });100101  itEth('Can perform mintBulk()', async ({helper}) => {102    const owner = await helper.eth.createAccountWithBalance(donor);103    const bulkSize = 3;104    const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());105    const collection = await helper.ft.mintCollection(alice);106    await collection.addAdmin(alice, {Ethereum: owner});107108    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);109    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);110111    const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (112      [receivers[i], (i + 1) * 10]113    ))).send();114    const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);115    for (let i = 0; i < bulkSize; i++) {116      const event = events[i];117      expect(event.address).to.equal(collectionAddress);118      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');119      expect(event.returnValues.to).to.equal(receivers[i]);120      expect(event.returnValues.value).to.equal(String(10 * (i + 1)));121    }122  });123124  // Soft-deprecated125  itEth('Can perform burn()', async ({helper}) => {126    const owner = await helper.eth.createAccountWithBalance(donor);127    const receiver = await helper.eth.createAccountWithBalance(donor);128    const collection = await helper.ft.mintCollection(alice);129    await collection.addAdmin(alice, {Ethereum: owner});130131    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);132    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);133    await contract.methods.mint(receiver, 100).send();134135    const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});136    137    const event = result.events.Transfer;138    expect(event.address).to.equal(collectionAddress);139    expect(event.returnValues.from).to.equal(receiver);140    expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');141    expect(event.returnValues.value).to.equal('49');142143    const balance = await contract.methods.balanceOf(receiver).call();144    expect(balance).to.equal('51');145  });146147  itEth('Can perform approve()', async ({helper}) => {148    const owner = await helper.eth.createAccountWithBalance(donor);149    const spender = helper.eth.createAccount();150    const collection = await helper.ft.mintCollection(alice);151    await collection.mint(alice, 200n, {Ethereum: owner});152153    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);154    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);155156    {157      const result = await contract.methods.approve(spender, 100).send({from: owner});158159      const event = result.events.Approval;160      expect(event.address).to.be.equal(collectionAddress);161      expect(event.returnValues.owner).to.be.equal(owner);162      expect(event.returnValues.spender).to.be.equal(spender);163      expect(event.returnValues.value).to.be.equal('100');164    }165166    {167      const allowance = await contract.methods.allowance(owner, spender).call();168      expect(+allowance).to.equal(100);169    }170  });171172  itEth('Can perform burnFromCross()', async ({helper}) => {173    const sender = await helper.eth.createAccountWithBalance(donor, 100n);174175    const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);176177    await collection.mint(owner, 200n, {Substrate: owner.address});178    await collection.approveTokens(owner, {Ethereum: sender}, 100n);179180    const address = helper.ethAddress.fromCollectionId(collection.collectionId);181    const contract = helper.ethNativeContract.collection(address, 'ft');182183    const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});184    185    const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);186    const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});187    const events = result.events;188189    expect(events).to.be.like({190      Transfer: {191        address: helper.ethAddress.fromCollectionId(collection.collectionId),192        event: 'Transfer',193        returnValues: {194          from: helper.address.substrateToEth(owner.address),195          to: '0x0000000000000000000000000000000000000000',196          value: '49',197        },198      },199      Approval: {200        address: helper.ethAddress.fromCollectionId(collection.collectionId),201        returnValues: {202          owner: helper.address.substrateToEth(owner.address),203          spender: sender,204          value: '51',205        },206        event: 'Approval',207      },208    });209210    const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});211    expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);212  });213214  itEth('Can perform transferFrom()', async ({helper}) => {215    const owner = await helper.eth.createAccountWithBalance(donor);216    const spender = await helper.eth.createAccountWithBalance(donor);217    const receiver = helper.eth.createAccount();218    const collection = await helper.ft.mintCollection(alice);219    await collection.mint(alice, 200n, {Ethereum: owner});220221    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);222    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);223224    await contract.methods.approve(spender, 100).send();225226    {227      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});228      229      let event = result.events.Transfer;230      expect(event.address).to.be.equal(collectionAddress);231      expect(event.returnValues.from).to.be.equal(owner);232      expect(event.returnValues.to).to.be.equal(receiver);233      expect(event.returnValues.value).to.be.equal('49');234235      event = result.events.Approval;236      expect(event.address).to.be.equal(collectionAddress);237      expect(event.returnValues.owner).to.be.equal(owner);238      expect(event.returnValues.spender).to.be.equal(spender);239      expect(event.returnValues.value).to.be.equal('51');240    }241242    {243      const balance = await contract.methods.balanceOf(receiver).call();244      expect(+balance).to.equal(49);245    }246247    {248      const balance = await contract.methods.balanceOf(owner).call();249      expect(+balance).to.equal(151);250    }251  });252253  itEth('Can perform transferCross()', async ({helper}) => {254    const sender = await helper.eth.createAccountWithBalance(donor);255    const receiverEth = await helper.eth.createAccountWithBalance(donor);256    const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);257    const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor);258    const collection = await helper.ft.mintCollection(alice);259    await collection.mint(alice, 200n, {Ethereum: sender});260261    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);262    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);263264    {265      // Can transferCross to ethereum address:266      const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});267      // Check events:268      const event = result.events.Transfer;269      expect(event.address).to.be.equal(collectionAddress);270      expect(event.returnValues.from).to.be.equal(sender);271      expect(event.returnValues.to).to.be.equal(receiverEth);272      expect(event.returnValues.value).to.be.equal('50');273      // Sender's balance decreased:274      const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();275      expect(+ownerBalance).to.equal(150);276      // Receiver's balance increased:277      const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();278      expect(+receiverBalance).to.equal(50);279    }280    281    {282      // Can transferCross to substrate address:283      const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});284      // Check events:285      const event = result.events.Transfer;286      expect(event.address).to.be.equal(collectionAddress);287      expect(event.returnValues.from).to.be.equal(sender);288      expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address));289      expect(event.returnValues.value).to.be.equal('50');290      // Sender's balance decreased:291      const senderBalance = await collection.getBalance({Ethereum: sender});292      expect(senderBalance).to.equal(100n);293      // Receiver's balance increased:294      const balance = await collection.getBalance({Substrate: donor.address});295      expect(balance).to.equal(50n);296    }297  });298299  ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {300    const sender = await helper.eth.createAccountWithBalance(donor);301    const receiverEth = await helper.eth.createAccountWithBalance(donor);302    const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);303    const BALANCE = 200n;304    const BALANCE_TO_TRANSFER = BALANCE + 100n;305306    const collection = await helper.ft.mintCollection(alice);307    await collection.mint(alice, BALANCE, {Ethereum: sender});308    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);309    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);310311    // 1. Cannot transfer more than have312    const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;313    await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;314    // 2. Zero transfer allowed (EIP-20):315    await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});316  }));317  318  319  itEth('Can perform transfer()', async ({helper}) => {320    const owner = await helper.eth.createAccountWithBalance(donor);321    const receiver = await helper.eth.createAccountWithBalance(donor);322    const collection = await helper.ft.mintCollection(alice);323    await collection.mint(alice, 200n, {Ethereum: owner});324325    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);326    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);327328    {329      const result = await contract.methods.transfer(receiver, 50).send({from: owner});330      331      const event = result.events.Transfer;332      expect(event.address).to.be.equal(collectionAddress);333      expect(event.returnValues.from).to.be.equal(owner);334      expect(event.returnValues.to).to.be.equal(receiver);335      expect(event.returnValues.value).to.be.equal('50');336    }337338    {339      const balance = await contract.methods.balanceOf(owner).call();340      expect(+balance).to.equal(150);341    }342343    {344      const balance = await contract.methods.balanceOf(receiver).call();345      expect(+balance).to.equal(50);346    }347  });348349  itEth('Can perform transferFromCross()', async ({helper}) => {350    const sender = await helper.eth.createAccountWithBalance(donor, 100n);351352    const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);353354    const receiver = helper.eth.createAccount();355356    await collection.mint(owner, 200n, {Substrate: owner.address});357    await collection.approveTokens(owner, {Ethereum: sender}, 100n);358359    const address = helper.ethAddress.fromCollectionId(collection.collectionId);360    const contract = helper.ethNativeContract.collection(address, 'ft');361362    const from = helper.ethCrossAccount.fromKeyringPair(owner);363    const to = helper.ethCrossAccount.fromAddress(receiver);364365    const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});366    const toBalanceBefore = await collection.getBalance({Ethereum: receiver});367    368    const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});369370    expect(result.events).to.be.like({371      Transfer: {372        address,373        event: 'Transfer',374        returnValues: {375          from: helper.address.substrateToEth(owner.address),376          to: receiver,377          value: '51',378        },379      },380      Approval: {381        address,382        event: 'Approval',383        returnValues: {384          owner: helper.address.substrateToEth(owner.address),385          spender: sender,386          value: '49',387        },388      }});389390    const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});391    expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);392    const toBalanceAfter = await collection.getBalance({Ethereum: receiver});393    expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);394  });395});396397describe('Fungible: Fees', () => {398  let donor: IKeyringPair;399  let alice: IKeyringPair;400401  before(async function() {402    await usingEthPlaygrounds(async (helper, privateKey) => {403      donor = await privateKey({filename: __filename});404      [alice] = await helper.arrange.createAccounts([20n], donor);405    });406  });407  408  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {409    const owner = await helper.eth.createAccountWithBalance(donor);410    const spender = helper.eth.createAccount();411    const collection = await helper.ft.mintCollection(alice);412    await collection.mint(alice, 200n, {Ethereum: owner});413414    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);415    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);416417    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));418    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));419  });420421  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {422    const owner = await helper.eth.createAccountWithBalance(donor);423    const spender = await helper.eth.createAccountWithBalance(donor);424    const collection = await helper.ft.mintCollection(alice);425    await collection.mint(alice, 200n, {Ethereum: owner});426427    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);428    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);429430    await contract.methods.approve(spender, 100).send({from: owner});431432    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));433    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));434  });435436  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {437    const owner = await helper.eth.createAccountWithBalance(donor);438    const receiver = helper.eth.createAccount();439    const collection = await helper.ft.mintCollection(alice);440    await collection.mint(alice, 200n, {Ethereum: owner});441442    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);443    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);444445    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));446    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));447  });448});449450describe('Fungible: Substrate calls', () => {451  let donor: IKeyringPair;452  let alice: IKeyringPair;453  let owner: IKeyringPair;454455  before(async function() {456    await usingEthPlaygrounds(async (helper, privateKey) => {457      donor = await privateKey({filename: __filename});458      [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);459    });460  });461462  itEth('Events emitted for approve()', async ({helper}) => {463    const receiver = helper.eth.createAccount();464    const collection = await helper.ft.mintCollection(alice);465    await collection.mint(alice, 200n);466467    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);468    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');469470    const events: any = [];471    contract.events.allEvents((_: any, event: any) => {472      events.push(event);473    });474    475    await collection.approveTokens(alice, {Ethereum: receiver}, 100n);476    if (events.length == 0) await helper.wait.newBlocks(1);477    const event = events[0];478479    expect(event.event).to.be.equal('Approval');480    expect(event.address).to.be.equal(collectionAddress);481    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));482    expect(event.returnValues.spender).to.be.equal(receiver);483    expect(event.returnValues.value).to.be.equal('100');484  });485486  itEth('Events emitted for transferFrom()', async ({helper}) => {487    const [bob] = await helper.arrange.createAccounts([10n], donor);488    const receiver = helper.eth.createAccount();489    const collection = await helper.ft.mintCollection(alice);490    await collection.mint(alice, 200n);491    await collection.approveTokens(alice, {Substrate: bob.address}, 100n);492493    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);494    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');495496    const events: any = [];497    contract.events.allEvents((_: any, event: any) => {498      events.push(event);499    });500501    await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);502    if (events.length == 0) await helper.wait.newBlocks(1);503    let event = events[0];504505    expect(event.event).to.be.equal('Transfer');506    expect(event.address).to.be.equal(collectionAddress);507    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));508    expect(event.returnValues.to).to.be.equal(receiver);509    expect(event.returnValues.value).to.be.equal('51');510511    event = events[1];512    expect(event.event).to.be.equal('Approval');513    expect(event.address).to.be.equal(collectionAddress);514    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));515    expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));516    expect(event.returnValues.value).to.be.equal('49');517  });518519  itEth('Events emitted for transfer()', async ({helper}) => {520    const receiver = helper.eth.createAccount();521    const collection = await helper.ft.mintCollection(alice);522    await collection.mint(alice, 200n);523524    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);525    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');526527    const events: any = [];528    contract.events.allEvents((_: any, event: any) => {529      events.push(event);530    });531    532    await collection.transfer(alice, {Ethereum:receiver}, 51n);533    if (events.length == 0) await helper.wait.newBlocks(1);534    const event = events[0];535536    expect(event.event).to.be.equal('Transfer');537    expect(event.address).to.be.equal(collectionAddress);538    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));539    expect(event.returnValues.to).to.be.equal(receiver);540    expect(event.returnValues.value).to.be.equal('51');541  });542543  itEth('Events emitted for transferFromCross()', async ({helper}) => {544    const sender = await helper.eth.createAccountWithBalance(donor, 100n);545546    const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);547548    const receiver = helper.eth.createAccount();549550    await collection.mint(owner, 200n, {Substrate: owner.address});551    await collection.approveTokens(owner, {Ethereum: sender}, 100n);552553    const address = helper.ethAddress.fromCollectionId(collection.collectionId);554    const contract = helper.ethNativeContract.collection(address, 'ft');555556    const from = helper.ethCrossAccount.fromKeyringPair(owner);557    const to = helper.ethCrossAccount.fromAddress(receiver);558    559    const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});560561    expect(result.events).to.be.like({562      Transfer: {563        address,564        event: 'Transfer',565        returnValues: {566          from: helper.address.substrateToEth(owner.address),567          to: receiver,568          value: '51',569        },570      },571      Approval: {572        address,573        event: 'Approval',574        returnValues: {575          owner: helper.address.substrateToEth(owner.address),576          spender: sender,577          value: '49',578        },579      }});580  });581});