git.delta.rocks / unique-network / refs/commits / 09b9e64d0480

difftreelog

source

tests/src/evmCoder.test.ts4.2 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 {IKeyringPair} from '@polkadot/types/types';18import Web3 from 'web3';19import {itEth, expect, usingEthPlaygrounds} from './eth/util/playgrounds';20import * as solc from 'solc';2122async function compileTestContract(collectionAddress: string, contractAddress: string) {23  const input = {24    language: 'Solidity',25    sources: {26      ['Test.sol']: {27        content: 28        `29        // SPDX-License-Identifier: MIT30        pragma solidity ^0.8.0;31        interface ITest {32          function ztestzzzzzzz() external returns (uint256 n);33        }34        contract Test {35          event Result(bool, uint256);36          function test1() public {37              try38                  ITest(${collectionAddress}).ztestzzzzzzz()39              returns (uint256 n) {40                  // enters41                  emit Result(true, n); // => [true, BigNumber { value: "43648854190028290368124427828690944273759144372138548774646036134290060795932" }]42              } catch {43                  emit Result(false, 0);44              }45          }46          function test2() public {47              try48                  ITest(${contractAddress}).ztestzzzzzzz()49              returns (uint256 n) {50                  emit Result(true, n);51              } catch {52                  // enters53                  emit Result(false, 0); // => [ false, BigNumber { value: "0" } ]54              }55          }56          function test3() public {57            ITest(${collectionAddress}).ztestzzzzzzz();58          }59        }60        `,61      },62    },63    settings: {64      outputSelection: {65        '*': {66          '*': ['*'],67        },68      },69    },70  };71  const json = JSON.parse(solc.compile(JSON.stringify(input)));72  const out = json.contracts['Test.sol']['Test'];7374  return  {75    abi: out.abi,76    object: '0x' + out.evm.bytecode.object,77  };78}7980async function deployTestContract(web3: Web3, owner: string, collectionAddress: string, contractAddress: string, gas: number) {81  const compiled = await compileTestContract(collectionAddress, contractAddress);82  const fractionalizerContract = new web3.eth.Contract(compiled.abi, undefined, {83    data: compiled.object,84    from: owner,85    gas,86  });87  return await fractionalizerContract.deploy({data: compiled.object}).send({from: owner});88}8990describe('Evm Coder tests', () => {91  let donor: IKeyringPair;9293  before(async function() {94    await usingEthPlaygrounds(async (_helper, privateKey) => {95      donor = privateKey('//Alice');96    });97  });98  99  itEth('Call non-existing function', async ({helper}) => {100    const owner = await helper.eth.createAccountWithBalance(donor);101    const collection = await helper.eth.createNonfungibleCollection(owner, 'EVMCODER', '', 'TEST');102    const contract = await deployTestContract(helper.getWeb3(), owner, collection.collectionAddress, '0x1bfed5D614b886b9Ab2eA4CBAc22A96B7EC29c9c', helper.eth.DEFAULT_GAS);103    const testContract = await deployTestContract(helper.getWeb3(), owner, collection.collectionAddress, contract.options.address, helper.eth.DEFAULT_GAS);104    {105      const result = await testContract.methods.test1().send();106      expect(result.events.Result.returnValues).to.deep.equal({107        '0': false,108        '1': '0',109      });110    }111    {112      const result = await testContract.methods.test2().send();113      expect(result.events.Result.returnValues).to.deep.equal({114        '0': false,115        '1': '0',116      });117    }118    {119      await expect(testContract.methods.test3().call())120        .to.be.rejectedWith(/unrecognized selector: 0xd9f02b36$/g);121    }122  });123});