1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itEth, usingEthPlaygrounds} from './util';19import {UniqueHelper} from '../util/playgrounds/unique';2021describe('NativeFungible: ERC20 calls', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 donor = await privateKey({url: import.meta.url});27 28 });29 });3031 itEth('approve()', async ({helper}) => {32 const owner = await helper.eth.createAccountWithBalance(donor);33 const spender = helper.eth.createAccount();34 const collectionAddress = helper.ethAddress.fromCollectionId(0);35 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);3637 await expect(contract.methods.approve(spender, 100).call({from: owner})).to.be.rejectedWith('Approve not supported');38 });3940 itEth('balanceOf()', async ({helper}) => {41 const owner = await helper.eth.createAccountWithBalance(donor, 123n);42 const collectionAddress = helper.ethAddress.fromCollectionId(0);43 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);4445 const balance = await contract.methods.balanceOf(owner).call({from: owner});46 expect(balance).to.be.eq('123000000000000000000');47 });4849 itEth('decimals()', async ({helper}) => {50 const owner = await helper.eth.createAccountWithBalance(donor);51 const collectionAddress = helper.ethAddress.fromCollectionId(0);52 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);5354 const realDecimals = (await helper.chain.getChainProperties().tokenDecimals)[0].toString();55 const decimals = await contract.methods.decimals().call({from: owner});56 expect(decimals).to.be.eq(realDecimals);57 });5859 itEth('name()', async ({helper}) => {60 const owner = await helper.eth.createAccountWithBalance(donor);61 const collectionAddress = helper.ethAddress.fromCollectionId(0);62 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);6364 const realName = await UniqueHelper.detectNetwork(helper.getApi());65 const name = await contract.methods.name().call({from: owner});66 expect(name).to.be.eq(realName);67 });6869 itEth('symbol()', async ({helper}) => {70 const owner = await helper.eth.createAccountWithBalance(donor);71 const collectionAddress = helper.ethAddress.fromCollectionId(0);72 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7374 const realName = (await helper.chain.getChainProperties().tokenSymbol)[0];75 const name = await contract.methods.symbol().call({from: owner});76 expect(name).to.be.eq(realName);77 });7879 itEth('totalSupply()', async ({helper}) => {80 const owner = await helper.eth.createAccountWithBalance(donor);81 const collectionAddress = helper.ethAddress.fromCollectionId(0);82 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);8384 const totalSupplyEth = BigInt(await contract.methods.totalSupply().call({from: owner}));85 const totalSupplySub = await helper.balance.getTotalIssuance();86 expect(totalSupplyEth).to.be.eq(totalSupplySub);87 });8889 itEth('transfer()', async ({helper}) => {90 const owner = await helper.eth.createAccountWithBalance(donor);91 const receiver = await helper.eth.createAccountWithBalance(donor);92 const collectionAddress = helper.ethAddress.fromCollectionId(0);93 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9495 const balanceOwnerBefore = await helper.balance.getEthereum(owner);96 const balanceReceiverBefore = await helper.balance.getEthereum(receiver);9798 await contract.methods.transfer(receiver, 50).send({from: owner});99100 const balanceOwnerAfter = await helper.balance.getEthereum(owner);101 const balanceReceiverAfter = await helper.balance.getEthereum(receiver);102103 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;104 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;105 });106107 itEth('transferFrom()', async ({helper}) => {108 const owner = await helper.eth.createAccountWithBalance(donor);109 const receiver = await helper.eth.createAccountWithBalance(donor);110 const collectionAddress = helper.ethAddress.fromCollectionId(0);111 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);112113 const balanceOwnerBefore = await helper.balance.getEthereum(owner);114 const balanceReceiverBefore = await helper.balance.getEthereum(receiver);115116 await contract.methods.transferFrom(owner, receiver, 50).send({from: owner});117118 const balanceOwnerAfter = await helper.balance.getEthereum(owner);119 const balanceReceiverAfter = await helper.balance.getEthereum(receiver);120121 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;122 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;123124 await expect(contract.methods.transferFrom(receiver, receiver, 50).call({from: owner})).to.be.rejectedWith('ApprovedValueTooLow');125 });126});127128describe('NativeFungible: ERC20UniqueExtensions calls', () => {129 let donor: IKeyringPair;130131 before(async function() {132 await usingEthPlaygrounds(async (helper, privateKey) => {133 donor = await privateKey({url: import.meta.url});134 135 });136 });137138 itEth('transferCross()', async ({helper}) => {139 const owner = await helper.eth.createAccountWithBalance(donor);140 const receiver = await helper.ethCrossAccount.createAccountWithBalance(donor);141 const collectionAddress = helper.ethAddress.fromCollectionId(0);142 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);143144 const balanceOwnerBefore = await helper.balance.getEthereum(owner);145 const balanceReceiverBefore = await helper.balance.getEthereum(receiver.eth);146147 await contract.methods.transferCross(receiver, 50).send({from: owner});148149 const balanceOwnerAfter = await helper.balance.getEthereum(owner);150 const balanceReceiverAfter = await helper.balance.getEthereum(receiver.eth);151152 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;153 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;154 });155156 itEth('transferFromCross()', async ({helper}) => {157 const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);158 const receiver = await helper.ethCrossAccount.createAccountWithBalance(donor);159 const collectionAddress = helper.ethAddress.fromCollectionId(0);160 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner.eth);161162 const balanceOwnerBefore = await helper.balance.getEthereum(owner.eth);163 const balanceReceiverBefore = await helper.balance.getEthereum(receiver.eth);164165 await contract.methods.transferFromCross(owner, receiver, 50).send({from: owner.eth});166167 const balanceOwnerAfter = await helper.balance.getEthereum(owner.eth);168 const balanceReceiverAfter = await helper.balance.getEthereum(receiver.eth);169170 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;171 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;172173 await expect(contract.methods.transferFromCross(receiver, receiver, 50).call({from: owner.eth})).to.be.rejectedWith('no permission');174 });175});