git.delta.rocks / unique-network / refs/commits / bbafef43a4e8

difftreelog

source

tests/src/eth/fungible.test.ts21.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([20n, 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  });8182  itEth('Can perform mintBulk()', async ({helper}) => {83    const owner = await helper.eth.createAccountWithBalance(donor);84    const bulkSize = 3;85    const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());86    const collection = await helper.ft.mintCollection(alice);87    await collection.addAdmin(alice, {Ethereum: owner});8889    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);90    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9192    const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (93      [receivers[i], (i + 1) * 10]94    ))).send();95    const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);96    for (let i = 0; i < bulkSize; i++) {97      const event = events[i];98      expect(event.address).to.equal(collectionAddress);99      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');100      expect(event.returnValues.to).to.equal(receivers[i]);101      expect(event.returnValues.value).to.equal(String(10 * (i + 1)));102    }103  });104105  itEth('Can perform burn()', async ({helper}) => {106    const owner = await helper.eth.createAccountWithBalance(donor);107    const receiver = await helper.eth.createAccountWithBalance(donor);108    const collection = await helper.ft.mintCollection(alice);109    await collection.addAdmin(alice, {Ethereum: owner});110111    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);112    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);113    await contract.methods.mint(receiver, 100).send();114115    const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});116    117    const event = result.events.Transfer;118    expect(event.address).to.equal(collectionAddress);119    expect(event.returnValues.from).to.equal(receiver);120    expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');121    expect(event.returnValues.value).to.equal('49');122123    const balance = await contract.methods.balanceOf(receiver).call();124    expect(balance).to.equal('51');125  });126127  itEth('Can perform approve()', async ({helper}) => {128    const owner = await helper.eth.createAccountWithBalance(donor);129    const spender = helper.eth.createAccount();130    const collection = await helper.ft.mintCollection(alice);131    await collection.mint(alice, 200n, {Ethereum: owner});132133    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);134    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);135136    {137      const result = await contract.methods.approve(spender, 100).send({from: owner});138139      const event = result.events.Approval;140      expect(event.address).to.be.equal(collectionAddress);141      expect(event.returnValues.owner).to.be.equal(owner);142      expect(event.returnValues.spender).to.be.equal(spender);143      expect(event.returnValues.value).to.be.equal('100');144    }145146    {147      const allowance = await contract.methods.allowance(owner, spender).call();148      expect(+allowance).to.equal(100);149    }150  });151152  itEth('Can perform burnFromCross()', async ({helper}) => {153    const sender = await helper.eth.createAccountWithBalance(donor, 100n);154155    const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);156157    await collection.mint(owner, 200n, {Substrate: owner.address});158    await collection.approveTokens(owner, {Ethereum: sender}, 100n);159160    const address = helper.ethAddress.fromCollectionId(collection.collectionId);161    const contract = helper.ethNativeContract.collection(address, 'ft');162163    const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});164    165    const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);166    const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});167    const events = result.events;168169    expect(events).to.be.like({170      Transfer: {171        address: helper.ethAddress.fromCollectionId(collection.collectionId),172        event: 'Transfer',173        returnValues: {174          from: helper.address.substrateToEth(owner.address),175          to: '0x0000000000000000000000000000000000000000',176          value: '49',177        },178      },179      Approval: {180        address: helper.ethAddress.fromCollectionId(collection.collectionId),181        returnValues: {182          owner: helper.address.substrateToEth(owner.address),183          spender: sender,184          value: '51',185        },186        event: 'Approval',187      },188    });189190    const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});191    expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);192  });193194  itEth('Can perform transferFrom()', async ({helper}) => {195    const owner = await helper.eth.createAccountWithBalance(donor);196    const spender = await helper.eth.createAccountWithBalance(donor);197    const receiver = helper.eth.createAccount();198    const collection = await helper.ft.mintCollection(alice);199    await collection.mint(alice, 200n, {Ethereum: owner});200201    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);202    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);203204    await contract.methods.approve(spender, 100).send();205206    {207      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});208      209      let event = result.events.Transfer;210      expect(event.address).to.be.equal(collectionAddress);211      expect(event.returnValues.from).to.be.equal(owner);212      expect(event.returnValues.to).to.be.equal(receiver);213      expect(event.returnValues.value).to.be.equal('49');214215      event = result.events.Approval;216      expect(event.address).to.be.equal(collectionAddress);217      expect(event.returnValues.owner).to.be.equal(owner);218      expect(event.returnValues.spender).to.be.equal(spender);219      expect(event.returnValues.value).to.be.equal('51');220    }221222    {223      const balance = await contract.methods.balanceOf(receiver).call();224      expect(+balance).to.equal(49);225    }226227    {228      const balance = await contract.methods.balanceOf(owner).call();229      expect(+balance).to.equal(151);230    }231  });232233  itEth('Can perform transferCross()', async ({helper}) => {234    const owner = await helper.eth.createAccountWithBalance(donor);235    const receiver = await helper.eth.createAccountWithBalance(donor);236    const to = helper.ethCrossAccount.fromAddress(receiver);237    const collection = await helper.ft.mintCollection(alice);238    await collection.mint(alice, 200n, {Ethereum: owner});239240    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);241    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);242243    {244      const result = await contract.methods.transferCross(to, 50).send({from: owner});245      246      const event = result.events.Transfer;247      expect(event.address).to.be.equal(collectionAddress);248      expect(event.returnValues.from).to.be.equal(owner);249      expect(event.returnValues.to).to.be.equal(receiver);250      expect(event.returnValues.value).to.be.equal('50');251    }252253    {254      const balance = await contract.methods.balanceOf(owner).call();255      expect(+balance).to.equal(150);256    }257258    {259      const balance = await contract.methods.balanceOf(receiver).call();260      expect(+balance).to.equal(50);261    }262  });263  264  itEth('Can perform transfer()', async ({helper}) => {265    const owner = await helper.eth.createAccountWithBalance(donor);266    const receiver = await helper.eth.createAccountWithBalance(donor);267    const collection = await helper.ft.mintCollection(alice);268    await collection.mint(alice, 200n, {Ethereum: owner});269270    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);271    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);272273    {274      const result = await contract.methods.transfer(receiver, 50).send({from: owner});275      276      const event = result.events.Transfer;277      expect(event.address).to.be.equal(collectionAddress);278      expect(event.returnValues.from).to.be.equal(owner);279      expect(event.returnValues.to).to.be.equal(receiver);280      expect(event.returnValues.value).to.be.equal('50');281    }282283    {284      const balance = await contract.methods.balanceOf(owner).call();285      expect(+balance).to.equal(150);286    }287288    {289      const balance = await contract.methods.balanceOf(receiver).call();290      expect(+balance).to.equal(50);291    }292  });293294  itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {295    const sender = await helper.eth.createAccountWithBalance(donor, 100n);296297    const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);298299    const receiver = helper.eth.createAccount();300301    await collection.mint(owner, 200n, {Substrate: owner.address});302    await collection.approveTokens(owner, {Ethereum: sender}, 100n);303304    const address = helper.ethAddress.fromCollectionId(collection.collectionId);305    const contract = helper.ethNativeContract.collection(address, 'ft');306307    const from = helper.ethCrossAccount.fromKeyringPair(owner);308    const to = helper.ethCrossAccount.fromAddress(receiver);309310    const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});311    const toBalanceBefore = await collection.getBalance({Ethereum: receiver});312    313    const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});314315    expect(result.events).to.be.like({316      Transfer: {317        address,318        event: 'Transfer',319        returnValues: {320          from: helper.address.substrateToEth(owner.address),321          to: receiver,322          value: '51',323        },324      },325      Approval: {326        address,327        event: 'Approval',328        returnValues: {329          owner: helper.address.substrateToEth(owner.address),330          spender: sender,331          value: '49',332        },333      }});334335    const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});336    expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);337    const toBalanceAfter = await collection.getBalance({Ethereum: receiver});338    expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);339  });340});341342describe('Fungible: Fees', () => {343  let donor: IKeyringPair;344  let alice: IKeyringPair;345346  before(async function() {347    await usingEthPlaygrounds(async (helper, privateKey) => {348      donor = await privateKey({filename: __filename});349      [alice] = await helper.arrange.createAccounts([20n], donor);350    });351  });352  353  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {354    const owner = await helper.eth.createAccountWithBalance(donor);355    const spender = helper.eth.createAccount();356    const collection = await helper.ft.mintCollection(alice);357    await collection.mint(alice, 200n, {Ethereum: owner});358359    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);360    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);361362    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));363    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));364  });365366  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {367    const owner = await helper.eth.createAccountWithBalance(donor);368    const spender = await helper.eth.createAccountWithBalance(donor);369    const collection = await helper.ft.mintCollection(alice);370    await collection.mint(alice, 200n, {Ethereum: owner});371372    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);373    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);374375    await contract.methods.approve(spender, 100).send({from: owner});376377    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));378    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));379  });380381  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {382    const owner = await helper.eth.createAccountWithBalance(donor);383    const receiver = helper.eth.createAccount();384    const collection = await helper.ft.mintCollection(alice);385    await collection.mint(alice, 200n, {Ethereum: owner});386387    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);388    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);389390    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));391    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));392  });393});394395describe('Fungible: Substrate calls', () => {396  let donor: IKeyringPair;397  let alice: IKeyringPair;398  let owner: IKeyringPair;399400  before(async function() {401    await usingEthPlaygrounds(async (helper, privateKey) => {402      donor = await privateKey({filename: __filename});403      [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);404    });405  });406407  itEth('Events emitted for approve()', async ({helper}) => {408    const receiver = helper.eth.createAccount();409    const collection = await helper.ft.mintCollection(alice);410    await collection.mint(alice, 200n);411412    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);413    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');414415    const events: any = [];416    contract.events.allEvents((_: any, event: any) => {417      events.push(event);418    });419    420    await collection.approveTokens(alice, {Ethereum: receiver}, 100n);421    if (events.length == 0) await helper.wait.newBlocks(1);422    const event = events[0];423424    expect(event.event).to.be.equal('Approval');425    expect(event.address).to.be.equal(collectionAddress);426    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));427    expect(event.returnValues.spender).to.be.equal(receiver);428    expect(event.returnValues.value).to.be.equal('100');429  });430431  itEth('Events emitted for transferFrom()', async ({helper}) => {432    const [bob] = await helper.arrange.createAccounts([10n], donor);433    const receiver = helper.eth.createAccount();434    const collection = await helper.ft.mintCollection(alice);435    await collection.mint(alice, 200n);436    await collection.approveTokens(alice, {Substrate: bob.address}, 100n);437438    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);439    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');440441    const events: any = [];442    contract.events.allEvents((_: any, event: any) => {443      events.push(event);444    });445446    await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);447    if (events.length == 0) await helper.wait.newBlocks(1);448    let event = events[0];449450    expect(event.event).to.be.equal('Transfer');451    expect(event.address).to.be.equal(collectionAddress);452    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));453    expect(event.returnValues.to).to.be.equal(receiver);454    expect(event.returnValues.value).to.be.equal('51');455456    event = events[1];457    expect(event.event).to.be.equal('Approval');458    expect(event.address).to.be.equal(collectionAddress);459    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));460    expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));461    expect(event.returnValues.value).to.be.equal('49');462  });463464  itEth('Events emitted for transfer()', async ({helper}) => {465    const receiver = helper.eth.createAccount();466    const collection = await helper.ft.mintCollection(alice);467    await collection.mint(alice, 200n);468469    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);470    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');471472    const events: any = [];473    contract.events.allEvents((_: any, event: any) => {474      events.push(event);475    });476    477    await collection.transfer(alice, {Ethereum:receiver}, 51n);478    if (events.length == 0) await helper.wait.newBlocks(1);479    const event = events[0];480481    expect(event.event).to.be.equal('Transfer');482    expect(event.address).to.be.equal(collectionAddress);483    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));484    expect(event.returnValues.to).to.be.equal(receiver);485    expect(event.returnValues.value).to.be.equal('51');486  });487488  itEth('Events emitted for transferFromCross()', async ({helper, privateKey}) => {489    const sender = await helper.eth.createAccountWithBalance(donor, 100n);490491    const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);492493    const receiver = helper.eth.createAccount();494495    await collection.mint(owner, 200n, {Substrate: owner.address});496    await collection.approveTokens(owner, {Ethereum: sender}, 100n);497498    const address = helper.ethAddress.fromCollectionId(collection.collectionId);499    const contract = helper.ethNativeContract.collection(address, 'ft');500501    const from = helper.ethCrossAccount.fromKeyringPair(owner);502    const to = helper.ethCrossAccount.fromAddress(receiver);503    504    const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});505506    expect(result.events).to.be.like({507      Transfer: {508        address,509        event: 'Transfer',510        returnValues: {511          from: helper.address.substrateToEth(owner.address),512          to: receiver,513          value: '51',514        },515      },516      Approval: {517        address,518        event: 'Approval',519        returnValues: {520          owner: helper.address.substrateToEth(owner.address),521          spender: sender,522          value: '49',523        },524      }});525  });526});