1234567891011121314151617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itEth, usingEthPlaygrounds} from './util/index.js';19import {UniqueHelper} from '@unique/playgrounds/src/unique.js';2021describe('NativeFungible: ERC20 calls', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (_, privateKey) => {26 donor = await privateKey({url: import.meta.url});27 });28 });2930 itEth('approve()', async ({helper}) => {31 const owner = await helper.eth.createAccountWithBalance(donor);32 const spender = helper.eth.createAccount();33 const collectionAddress = helper.ethAddress.fromCollectionId(0);34 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);3536 await expect(contract.methods.approve(spender, 100).call({from: owner})).to.be.rejectedWith('approve not supported');37 });3839 itEth('balanceOf()', async ({helper}) => {40 const owner = await helper.eth.createAccountWithBalance(donor, 123n);41 const collectionAddress = helper.ethAddress.fromCollectionId(0);42 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);4344 const balance = await contract.methods.balanceOf(owner).call({from: owner});45 expect(balance).to.be.eq('123000000000000000000');46 });4748 itEth('decimals()', async ({helper}) => {49 const owner = await helper.eth.createAccountWithBalance(donor);50 const collectionAddress = helper.ethAddress.fromCollectionId(0);51 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);5253 const realDecimals = (await helper.chain.getChainProperties().tokenDecimals)[0].toString();54 const decimals = await contract.methods.decimals().call({from: owner});55 expect(decimals).to.be.eq(realDecimals);56 });5758 itEth('name()', async ({helper}) => {59 const owner = await helper.eth.createAccountWithBalance(donor);60 const collectionAddress = helper.ethAddress.fromCollectionId(0);61 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);6263 const realName = await UniqueHelper.detectNetwork(helper.getApi());64 const name = await contract.methods.name().call({from: owner});65 expect(name).to.be.eq(realName);66 });6768 itEth('symbol()', async ({helper}) => {69 const owner = await helper.eth.createAccountWithBalance(donor);70 const collectionAddress = helper.ethAddress.fromCollectionId(0);71 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7273 const realName = (await helper.chain.getChainProperties().tokenSymbol)[0];74 const name = await contract.methods.symbol().call({from: owner});75 expect(name).to.be.eq(realName);76 });7778 itEth('totalSupply()', async ({helper}) => {79 const owner = await helper.eth.createAccountWithBalance(donor);80 const collectionAddress = helper.ethAddress.fromCollectionId(0);81 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);8283 const totalSupplyEth = BigInt(await contract.methods.totalSupply().call({from: owner}));84 const totalSupplySub = await helper.balance.getTotalIssuance();85 expect(totalSupplyEth).to.be.eq(totalSupplySub);86 });8788 itEth('transfer()', async ({helper}) => {89 const owner = await helper.eth.createAccountWithBalance(donor);90 const receiver = await helper.eth.createAccountWithBalance(donor);91 const collectionAddress = helper.ethAddress.fromCollectionId(0);92 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9394 const balanceOwnerBefore = await helper.balance.getEthereum(owner);95 const balanceReceiverBefore = await helper.balance.getEthereum(receiver);9697 await contract.methods.transfer(receiver, 50).send({from: owner});9899 const balanceOwnerAfter = await helper.balance.getEthereum(owner);100 const balanceReceiverAfter = await helper.balance.getEthereum(receiver);101102 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;103 expect(balanceReceiverBefore + 50n).to.be.equal(balanceReceiverAfter);104 });105106 itEth('transferFrom()', async ({helper}) => {107 const owner = await helper.eth.createAccountWithBalance(donor);108 const receiver = await helper.eth.createAccountWithBalance(donor);109 const collectionAddress = helper.ethAddress.fromCollectionId(0);110 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);111112 const balanceOwnerBefore = await helper.balance.getEthereum(owner);113 const balanceReceiverBefore = await helper.balance.getEthereum(receiver);114115 await contract.methods.transferFrom(owner, receiver, 50).send({from: owner});116117 const balanceOwnerAfter = await helper.balance.getEthereum(owner);118 const balanceReceiverAfter = await helper.balance.getEthereum(receiver);119120 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;121 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;122123 await expect(contract.methods.transferFrom(receiver, receiver, 50).call({from: owner})).to.be.rejectedWith('ApprovedValueTooLow');124 });125});126127describe('NativeFungible: ERC20UniqueExtensions calls', () => {128 let donor: IKeyringPair;129130 before(async function() {131 await usingEthPlaygrounds(async (_, privateKey) => {132 donor = await privateKey({url: import.meta.url});133 });134 });135136 itEth('transferCross()', async ({helper}) => {137 const owner = await helper.eth.createAccountWithBalance(donor);138 const receiver = await helper.ethCrossAccount.createAccountWithBalance(donor);139 const collectionAddress = helper.ethAddress.fromCollectionId(0);140 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);141142 const balanceOwnerBefore = await helper.balance.getEthereum(owner);143 const balanceReceiverBefore = await helper.balance.getEthereum(receiver.eth);144145 await contract.methods.transferCross(receiver, 50).send({from: owner});146147 const balanceOwnerAfter = await helper.balance.getEthereum(owner);148 const balanceReceiverAfter = await helper.balance.getEthereum(receiver.eth);149150 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;151 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;152 });153154 itEth('transferFromCross()', async ({helper}) => {155 const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);156 const receiver = await helper.ethCrossAccount.createAccountWithBalance(donor);157 const collectionAddress = helper.ethAddress.fromCollectionId(0);158 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner.eth);159160 const balanceOwnerBefore = await helper.balance.getEthereum(owner.eth);161 const balanceReceiverBefore = await helper.balance.getEthereum(receiver.eth);162163 await contract.methods.transferFromCross(owner, receiver, 50).send({from: owner.eth});164165 const balanceOwnerAfter = await helper.balance.getEthereum(owner.eth);166 const balanceReceiverAfter = await helper.balance.getEthereum(receiver.eth);167168 expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;169 expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;170171 await expect(contract.methods.transferFromCross(receiver, receiver, 50).call({from: owner.eth})).to.be.rejectedWith('no permission');172 });173});