1234567891011121314151617import {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});