git.delta.rocks / unique-network / refs/commits / 0dc93c7a2ef6

difftreelog

source

tests/src/eth/base.test.ts5.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 {18  collectionIdFromAddress, 19  collectionIdToAddress, 20  contractHelpers, 21  createEthAccount, 22  createEthAccountWithBalance, 23  deployFlipper, 24  ethBalanceViaSub, 25  GAS_ARGS, 26  itWeb3, 27  recordEthFee, 28  usingWeb3,29} from './util/helpers';30import {expect} from 'chai';31import {createCollectionExpectSuccess, createItemExpectSuccess, getCreatedCollectionCount, getDetailedCollectionInfo, UNIQUE} from '../util/helpers';32import nonFungibleAbi from './nonFungibleAbi.json';33import privateKey from '../substrate/privateKey';34import {Contract} from 'web3-eth-contract';35import Web3 from 'web3';3637describe('Contract calls', () => {38  itWeb3('Call of simple contract fee is less than 0.2 UNQ', async ({web3, api}) => {39    const deployer = await createEthAccountWithBalance(api, web3);40    const flipper = await deployFlipper(web3, deployer);4142    const cost = await recordEthFee(api, deployer, () => flipper.methods.flip().send({from: deployer}));43    expect(cost < BigInt(0.2 * Number(UNIQUE))).to.be.true;44  });4546  itWeb3('Balance transfer fee is less than 0.2 UNQ', async ({web3, api}) => {47    const userA = await createEthAccountWithBalance(api, web3);48    const userB = createEthAccount(web3);4950    const cost = await recordEthFee(api, userA, () => web3.eth.sendTransaction({from: userA, to: userB, value: '1000000', ...GAS_ARGS}));51    const balanceB = await ethBalanceViaSub(api, userB);52    expect(cost - balanceB < BigInt(0.2 * Number(UNIQUE))).to.be.true;53  });5455  itWeb3('NFT transfer is close to 0.15 UNQ', async ({web3, api}) => {56    const caller = await createEthAccountWithBalance(api, web3);57    const receiver = createEthAccount(web3);5859    const alice = privateKey('//Alice');60    const collection = await createCollectionExpectSuccess({61      mode: {type: 'NFT'},62    });63    const itemId = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: caller});6465    const address = collectionIdToAddress(collection);66    const contract = new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS});6768    const cost = await recordEthFee(api, caller, () => contract.methods.transfer(receiver, itemId).send(caller));6970    const fee = Number(cost) / Number(UNIQUE);71    const expectedFee = 0.15;72    const tolerance = 0.00002;7374    expect(Math.abs(fee - expectedFee)).to.be.lessThan(tolerance);75  });76});7778describe('ERC165 tests', async () => {79  // https://eips.ethereum.org/EIPS/eip-1658081  let collection: number;82  let minter: string;8384  function contract(web3: Web3): Contract {85    return new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection), {from: minter, ...GAS_ARGS});86  }8788  before(async () => {89    await usingWeb3 (async (web3) => {90      collection = await createCollectionExpectSuccess();91      minter = createEthAccount(web3);92    });93  });9495  itWeb3('interfaceID == 0xffffffff always false', async ({web3}) => {96    expect(await contract(web3).methods.supportsInterface('0xffffffff').call()).to.be.false;97  });9899  itWeb3('ERC721 support', async ({web3}) => {100    expect(await contract(web3).methods.supportsInterface('0x58800161').call()).to.be.true;101  });102103  itWeb3('ERC721Metadata support', async ({web3}) => {104    expect(await contract(web3).methods.supportsInterface('0x5b5e139f').call()).to.be.true;105  });106107  itWeb3('ERC721Mintable support', async ({web3}) => {108    expect(await contract(web3).methods.supportsInterface('0x68ccfe89').call()).to.be.true;109  });110111  itWeb3('ERC721Enumerable support', async ({web3}) => {112    expect(await contract(web3).methods.supportsInterface('0x780e9d63').call()).to.be.true;113  });114115  itWeb3('ERC721UniqueExtensions support', async ({web3}) => {116    expect(await contract(web3).methods.supportsInterface('0xd74d154f').call()).to.be.true;117  });118119  itWeb3('ERC721Burnable support', async ({web3}) => {120    expect(await contract(web3).methods.supportsInterface('0x42966c68').call()).to.be.true;121  });122123  itWeb3('ERC165 support', async ({web3}) => {124    expect(await contract(web3).methods.supportsInterface('0x01ffc9a7').call()).to.be.true;125  });126});127128describe('Create collection from EVM', () => {129  itWeb3('Create collection', async ({api, web3}) => {130    const owner = await createEthAccountWithBalance(api, web3);131    const helpers = contractHelpers(web3, owner);132    const collectionName = 'CollectionEVM';133    const description = 'Some description';134    const tokenPrefix = 'token prefix';135136    const collectionCountBefore = await getCreatedCollectionCount(api);137    const result = await helpers.methods138      .create721Collection(collectionName, description, tokenPrefix)139      .send();140    const collectionCountAfter = await getCreatedCollectionCount(api);141142    const collectionId = collectionIdFromAddress(result.events[0].raw.topics[2]);143    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);144    expect(collectionId).to.be.eq(collectionCountAfter);145    146    const collection = (await getDetailedCollectionInfo(api, collectionId))!;147    expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);148    expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);149    expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);150  });151});