git.delta.rocks / unique-network / refs/commits / 5e93b06be93a

difftreelog

source

tests/src/evmCoder.test.ts4.0 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 Web3 from 'web3';18import {createEthAccountWithBalance, createNonfungibleCollection, GAS_ARGS, itWeb3} from './eth/util/helpers';19import * as solc from 'solc';2021import chai from 'chai';22const expect = chai.expect;2324async function compileTestContract(collectionAddress: string, contractAddress: string) {25  const input = {26    language: 'Solidity',27    sources: {28      ['Test.sol']: {29        content: 30        `31        // SPDX-License-Identifier: MIT32        pragma solidity ^0.8.0;33        interface ITest {34          function ztestzzzzzzz() external returns (uint256 n);35        }36        contract Test {37          event Result(bool, uint256);38          function test1() public {39              try40                  ITest(${collectionAddress}).ztestzzzzzzz()41              returns (uint256 n) {42                  // enters43                  emit Result(true, n); // => [true, BigNumber { value: "43648854190028290368124427828690944273759144372138548774646036134290060795932" }]44              } catch {45                  emit Result(false, 0);46              }47          }48          function test2() public {49              try50                  ITest(${contractAddress}).ztestzzzzzzz()51              returns (uint256 n) {52                  emit Result(true, n);53              } catch {54                  // enters55                  emit Result(false, 0); // => [ false, BigNumber { value: "0" } ]56              }57          }58          function test3() public {59            ITest(${collectionAddress}).ztestzzzzzzz();60          }61        }62        `,63      },64    },65    settings: {66      outputSelection: {67        '*': {68          '*': ['*'],69        },70      },71    },72  };73  const json = JSON.parse(solc.compile(JSON.stringify(input)));74  const out = json.contracts['Test.sol']['Test'];7576  return  {77    abi: out.abi,78    object: '0x' + out.evm.bytecode.object,79  };80}8182async function deployTestContract(web3: Web3, owner: string, collectionAddress: string, contractAddress: string) {83  const compiled = await compileTestContract(collectionAddress, contractAddress);84  const fractionalizerContract = new web3.eth.Contract(compiled.abi, undefined, {85    data: compiled.object,86    from: owner,87    ...GAS_ARGS,88  });89  return await fractionalizerContract.deploy({data: compiled.object}).send({from: owner});90}9192describe('Evm Coder tests', () => {93  itWeb3('Call non-existing function', async ({api, web3, privateKeyWrapper}) => {94    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);95    const {collectionIdAddress} = await createNonfungibleCollection(api, web3, owner);96    const contract = await deployTestContract(web3, owner, collectionIdAddress, '0x1bfed5D614b886b9Ab2eA4CBAc22A96B7EC29c9c');97    const testContract = await deployTestContract(web3, owner, collectionIdAddress, contract.options.address);98    {99      const result = await testContract.methods.test1().send();100      expect(result.events.Result.returnValues).to.deep.equal({101        '0': false,102        '1': '0',103      });104    }105    {106      const result = await testContract.methods.test2().send();107      expect(result.events.Result.returnValues).to.deep.equal({108        '0': false,109        '1': '0',110      });111    }112    {113      await expect(testContract.methods.test3().call())114        .to.be.rejectedWith(/unrecognized selector: 0xd9f02b36$/g);115    }116  });117});