git.delta.rocks / unique-network / refs/commits / 2dd0a6af978d

difftreelog

source

tests/src/eth/nativeFungible.test.ts7.7 KiBsourcehistory
1// Copyright 2019-2023 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 {IKeyringPair} from '@polkadot/types/types';18import {expect, itEth, usingEthPlaygrounds} from './util';1920describe('NativeFungible: ERC20 calls', () => {21  let donor: IKeyringPair;2223  before(async function() {24    await usingEthPlaygrounds(async (helper, privateKey) => {25      donor = await privateKey({url: import.meta.url});26      // [alice] = await helper.arrange.createAccounts([30n], donor);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 decimals = await contract.methods.decimals().call({from: owner});54    expect(decimals).to.be.eq('18');55  });5657  itEth('name()', async ({helper}) => {58    const owner = await helper.eth.createAccountWithBalance(donor);59    const collectionAddress = helper.ethAddress.fromCollectionId(0);60    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);6162    const name = await contract.methods.name().call({from: owner});63    expect(name).to.be.eq('opal');64  });6566  itEth('symbol()', async ({helper}) => {67    const owner = await helper.eth.createAccountWithBalance(donor);68    const collectionAddress = helper.ethAddress.fromCollectionId(0);69    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7071    const name = await contract.methods.symbol().call({from: owner});72    expect(name).to.be.eq('OPL');73  });7475  itEth('totalSupply()', async ({helper}) => {76    const owner = await helper.eth.createAccountWithBalance(donor);77    const collectionAddress = helper.ethAddress.fromCollectionId(0);78    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7980    const totalSupplyEth = BigInt(await contract.methods.totalSupply().call({from: owner}));81    const totalSupplySub = await helper.balance.getTotalIssuance();82    expect(totalSupplyEth).to.be.eq(totalSupplySub);83  });8485  itEth('transfer()', async ({helper}) => {86    const owner = await helper.eth.createAccountWithBalance(donor);87    const receiver = await helper.eth.createAccountWithBalance(donor);88    const collectionAddress = helper.ethAddress.fromCollectionId(0);89    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);9091    const balanceOwnerBefore = await helper.balance.getEthereum(owner);92    const balanceReceiverBefore = await helper.balance.getEthereum(receiver);9394    await contract.methods.transfer(receiver, 50).send({from: owner});9596    const balanceOwnerAfter = await helper.balance.getEthereum(owner);97    const balanceReceiverAfter = await helper.balance.getEthereum(receiver);9899    expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;100    expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;101  });102103  itEth('transferFrom()', async ({helper}) => {104    const owner = await helper.eth.createAccountWithBalance(donor);105    const receiver = await helper.eth.createAccountWithBalance(donor);106    const collectionAddress = helper.ethAddress.fromCollectionId(0);107    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);108109    const balanceOwnerBefore = await helper.balance.getEthereum(owner);110    const balanceReceiverBefore = await helper.balance.getEthereum(receiver);111112    await contract.methods.transferFrom(owner, receiver, 50).send({from: owner});113114    const balanceOwnerAfter = await helper.balance.getEthereum(owner);115    const balanceReceiverAfter = await helper.balance.getEthereum(receiver);116117    expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;118    expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;119120    await expect(contract.methods.transferFrom(receiver, receiver, 50).call({from: owner})).to.be.rejectedWith('ApprovedValueTooLow');121  });122});123124describe('NativeFungible: ERC20UniqueExtensions calls', () => {125  let donor: IKeyringPair;126127  before(async function() {128    await usingEthPlaygrounds(async (helper, privateKey) => {129      donor = await privateKey({url: import.meta.url});130      // [alice] = await helper.arrange.createAccounts([30n], donor);131    });132  });133134  itEth('transferCross()', async ({helper}) => {135    const owner = await helper.eth.createAccountWithBalance(donor);136    const receiver = await helper.ethCrossAccount.createAccountWithBalance(donor);137    const collectionAddress = helper.ethAddress.fromCollectionId(0);138    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);139140    const balanceOwnerBefore = await helper.balance.getEthereum(owner);141    const balanceReceiverBefore = await helper.balance.getEthereum(receiver.eth);142143    await contract.methods.transferCross(receiver, 50).send({from: owner});144145    const balanceOwnerAfter = await helper.balance.getEthereum(owner);146    const balanceReceiverAfter = await helper.balance.getEthereum(receiver.eth);147148    expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;149    expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;150  });151152  itEth('transferFromCross()', async ({helper}) => {153    const owner = await helper.ethCrossAccount.createAccountWithBalance(donor);154    const receiver = await helper.ethCrossAccount.createAccountWithBalance(donor);155    const collectionAddress = helper.ethAddress.fromCollectionId(0);156    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner.eth);157158    const balanceOwnerBefore = await helper.balance.getEthereum(owner.eth);159    const balanceReceiverBefore = await helper.balance.getEthereum(receiver.eth);160161    await contract.methods.transferFromCross(owner, receiver, 50).send({from: owner.eth});162163    const balanceOwnerAfter = await helper.balance.getEthereum(owner.eth);164    const balanceReceiverAfter = await helper.balance.getEthereum(receiver.eth);165166    expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;167    expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;168169    await expect(contract.methods.transferFromCross(receiver, receiver, 50).call({from: owner.eth})).to.be.rejectedWith('no permission');170  });171});