git.delta.rocks / unique-network / refs/commits / 909792523ad1

difftreelog

source

tests/src/evmCoder.test.ts3.8 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, createRefungibleCollection, 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        }59        `,60      },61    },62    settings: {63      outputSelection: {64        '*': {65          '*': ['*'],66        },67      },68    },69  };70  const json = JSON.parse(solc.compile(JSON.stringify(input)));71  const out = json.contracts['Test.sol']['Test'];7273  return  {74    abi: out.abi,75    object: '0x' + out.evm.bytecode.object,76  };77}7879async function deployTestContract(web3: Web3, owner: string, collectionAddress: string, contractAddress: string) {80  const compiled = await compileTestContract(collectionAddress, contractAddress);81  const fractionalizerContract = new web3.eth.Contract(compiled.abi, undefined, {82    data: compiled.object,83    from: owner,84    ...GAS_ARGS,85  });86  return await fractionalizerContract.deploy({data: compiled.object}).send({from: owner});87}8889describe('Evm Coder tests', () => {90  itWeb3('Call non-existing function', async ({api, web3, privateKeyWrapper}) => {91    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);92    const {collectionIdAddress} = await createRefungibleCollection(api, web3, owner);93    const contract = await deployTestContract(web3, owner, collectionIdAddress, '0x1bfed5D614b886b9Ab2eA4CBAc22A96B7EC29c9c');94    const testContract = await deployTestContract(web3, owner, collectionIdAddress, contract.options.address);95    {96      const result = await testContract.methods.test1().send();97      expect(result.events.Result.returnValues).to.deep.equal({98        '0': false,99        '1': '0',100      });101    }102    {103      const result = await testContract.methods.test2().send();104      expect(result.events.Result.returnValues).to.deep.equal({105        '0': false,106        '1': '0',107      });108    }109  });110});