git.delta.rocks / unique-network / refs/commits / 3a01a5229f7a

difftreelog

source

tests/src/eth/fungible.test.ts14.9 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/playgrounds';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 = privateKey('//Alice');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;5556  before(async function() {57    await usingEthPlaygrounds(async (helper, privateKey) => {58      donor = privateKey('//Alice');59      [alice] = await helper.arrange.createAccounts([20n], donor);60    });61  });6263  itEth('Can perform mint()', async ({helper}) => {64    const owner = await helper.eth.createAccountWithBalance(donor);65    const receiver = helper.eth.createAccount();66    const collection = await helper.ft.mintCollection(alice);67    await collection.addAdmin(alice, {Ethereum: owner});6869    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);70    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7172    const result = await contract.methods.mint(receiver, 100).send();73    74    const event = result.events.Transfer;75    expect(event.address).to.equal(collectionAddress);76    expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');77    expect(event.returnValues.to).to.equal(receiver);78    expect(event.returnValues.value).to.equal('100');79  });8081  itEth('Can perform mintBulk()', async ({helper}) => {82    const owner = await helper.eth.createAccountWithBalance(donor);83    const bulkSize = 3;84    const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());85    const collection = await helper.ft.mintCollection(alice);86    await collection.addAdmin(alice, {Ethereum: owner});8788    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);89    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9091    const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (92      [receivers[i], (i + 1) * 10]93    ))).send();94    const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);95    for (let i = 0; i < bulkSize; i++) {96      const event = events[i];97      expect(event.address).to.equal(collectionAddress);98      expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');99      expect(event.returnValues.to).to.equal(receivers[i]);100      expect(event.returnValues.value).to.equal(String(10 * (i + 1)));101    }102  });103104  itEth('Can perform burn()', async ({helper}) => {105    const owner = await helper.eth.createAccountWithBalance(donor);106    const receiver = await helper.eth.createAccountWithBalance(donor);107    const collection = await helper.ft.mintCollection(alice);108    await collection.addAdmin(alice, {Ethereum: owner});109110    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);111    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);112    await contract.methods.mint(receiver, 100).send();113114    const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});115    116    const event = result.events.Transfer;117    expect(event.address).to.equal(collectionAddress);118    expect(event.returnValues.from).to.equal(receiver);119    expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');120    expect(event.returnValues.value).to.equal('49');121122    const balance = await contract.methods.balanceOf(receiver).call();123    expect(balance).to.equal('51');124  });125126  itEth('Can perform approve()', async ({helper}) => {127    const owner = await helper.eth.createAccountWithBalance(donor);128    const spender = helper.eth.createAccount();129    const collection = await helper.ft.mintCollection(alice);130    await collection.mint(alice, 200n, {Ethereum: owner});131132    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);133    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);134135    {136      const result = await contract.methods.approve(spender, 100).send({from: owner});137138      const event = result.events.Approval;139      expect(event.address).to.be.equal(collectionAddress);140      expect(event.returnValues.owner).to.be.equal(owner);141      expect(event.returnValues.spender).to.be.equal(spender);142      expect(event.returnValues.value).to.be.equal('100');143    }144145    {146      const allowance = await contract.methods.allowance(owner, spender).call();147      expect(+allowance).to.equal(100);148    }149  });150151  itEth('Can perform transferFrom()', async ({helper}) => {152    const owner = await helper.eth.createAccountWithBalance(donor);153    const spender = await helper.eth.createAccountWithBalance(donor);154    const receiver = helper.eth.createAccount();155    const collection = await helper.ft.mintCollection(alice);156    await collection.mint(alice, 200n, {Ethereum: owner});157158    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);159    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);160161    await contract.methods.approve(spender, 100).send();162163    {164      const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});165      166      let event = result.events.Transfer;167      expect(event.address).to.be.equal(collectionAddress);168      expect(event.returnValues.from).to.be.equal(owner);169      expect(event.returnValues.to).to.be.equal(receiver);170      expect(event.returnValues.value).to.be.equal('49');171172      event = result.events.Approval;173      expect(event.address).to.be.equal(collectionAddress);174      expect(event.returnValues.owner).to.be.equal(owner);175      expect(event.returnValues.spender).to.be.equal(spender);176      expect(event.returnValues.value).to.be.equal('51');177    }178179    {180      const balance = await contract.methods.balanceOf(receiver).call();181      expect(+balance).to.equal(49);182    }183184    {185      const balance = await contract.methods.balanceOf(owner).call();186      expect(+balance).to.equal(151);187    }188  });189190  itEth('Can perform transfer()', async ({helper}) => {191    const owner = await helper.eth.createAccountWithBalance(donor);192    const receiver = await helper.eth.createAccountWithBalance(donor);193    const collection = await helper.ft.mintCollection(alice);194    await collection.mint(alice, 200n, {Ethereum: owner});195196    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);197    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);198199    {200      const result = await contract.methods.transfer(receiver, 50).send({from: owner});201      202      const event = result.events.Transfer;203      expect(event.address).to.be.equal(collectionAddress);204      expect(event.returnValues.from).to.be.equal(owner);205      expect(event.returnValues.to).to.be.equal(receiver);206      expect(event.returnValues.value).to.be.equal('50');207    }208209    {210      const balance = await contract.methods.balanceOf(owner).call();211      expect(+balance).to.equal(150);212    }213214    {215      const balance = await contract.methods.balanceOf(receiver).call();216      expect(+balance).to.equal(50);217    }218  });219});220221describe('Fungible: Fees', () => {222  let donor: IKeyringPair;223  let alice: IKeyringPair;224225  before(async function() {226    await usingEthPlaygrounds(async (helper, privateKey) => {227      donor = privateKey('//Alice');228      [alice] = await helper.arrange.createAccounts([20n], donor);229    });230  });231  232  itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {233    const owner = await helper.eth.createAccountWithBalance(donor);234    const spender = helper.eth.createAccount();235    const collection = await helper.ft.mintCollection(alice);236    await collection.mint(alice, 200n, {Ethereum: owner});237238    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);239    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);240241    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));242    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));243  });244245  itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {246    const owner = await helper.eth.createAccountWithBalance(donor);247    const spender = await helper.eth.createAccountWithBalance(donor);248    const collection = await helper.ft.mintCollection(alice);249    await collection.mint(alice, 200n, {Ethereum: owner});250251    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);252    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);253254    await contract.methods.approve(spender, 100).send({from: owner});255256    const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));257    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));258  });259260  itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {261    const owner = await helper.eth.createAccountWithBalance(donor);262    const receiver = helper.eth.createAccount();263    const collection = await helper.ft.mintCollection(alice);264    await collection.mint(alice, 200n, {Ethereum: owner});265266    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);267    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);268269    const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));270    expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));271  });272});273274describe('Fungible: Substrate calls', () => {275  let donor: IKeyringPair;276  let alice: IKeyringPair;277278  before(async function() {279    await usingEthPlaygrounds(async (helper, privateKey) => {280      donor = privateKey('//Alice');281      [alice] = await helper.arrange.createAccounts([20n], donor);282    });283  });284285  itEth('Events emitted for approve()', async ({helper}) => {286    const receiver = helper.eth.createAccount();287    const collection = await helper.ft.mintCollection(alice);288    await collection.mint(alice, 200n);289290    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);291    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');292293    const events: any = [];294    contract.events.allEvents((_: any, event: any) => {295      events.push(event);296    });297    await collection.approveTokens(alice, {Ethereum: receiver}, 100n);298299    const event = events[0];300    expect(event.event).to.be.equal('Approval');301    expect(event.address).to.be.equal(collectionAddress);302    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));303    expect(event.returnValues.spender).to.be.equal(receiver);304    expect(event.returnValues.value).to.be.equal('100');305  });306307  itEth('Events emitted for transferFrom()', async ({helper}) => {308    const [bob] = await helper.arrange.createAccounts([10n], donor);309    const receiver = helper.eth.createAccount();310    const collection = await helper.ft.mintCollection(alice);311    await collection.mint(alice, 200n);312    await collection.approveTokens(alice, {Substrate: bob.address}, 100n);313314    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);315    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');316317    const events: any = [];318    contract.events.allEvents((_: any, event: any) => {319      events.push(event);320    });321    await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);322323    let event = events[0];324    expect(event.event).to.be.equal('Transfer');325    expect(event.address).to.be.equal(collectionAddress);326    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));327    expect(event.returnValues.to).to.be.equal(receiver);328    expect(event.returnValues.value).to.be.equal('51');329330    event = events[1];331    expect(event.event).to.be.equal('Approval');332    expect(event.address).to.be.equal(collectionAddress);333    expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));334    expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));335    expect(event.returnValues.value).to.be.equal('49');336  });337338  itEth('Events emitted for transfer()', async ({helper}) => {339    const receiver = helper.eth.createAccount();340    const collection = await helper.ft.mintCollection(alice);341    await collection.mint(alice, 200n);342343    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);344    const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');345346    const events: any = [];347    contract.events.allEvents((_: any, event: any) => {348      events.push(event);349    });350    await collection.transfer(alice, {Ethereum:receiver}, 51n);351352    const event = events[0];353    expect(event.event).to.be.equal('Transfer');354    expect(event.address).to.be.equal(collectionAddress);355    expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));356    expect(event.returnValues.to).to.be.equal(receiver);357    expect(event.returnValues.value).to.be.equal('51');358  });359});