difftreelog
test(evm) add contract helpers
in: master
3 files changed
tests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth1[2 {3 "inputs": [4 {5 "internalType": "address",6 "name": "contract",7 "type": "address"8 }9 ],10 "name": "allowlistEnabled",11 "outputs": [12 {13 "internalType": "bool",14 "name": "",15 "type": "bool"16 }17 ],18 "stateMutability": "view",19 "type": "function"20 },21 {22 "inputs": [23 {24 "internalType": "address",25 "name": "target",26 "type": "address"27 },28 {29 "internalType": "address",30 "name": "caller",31 "type": "address"32 }33 ],34 "name": "allowed",35 "outputs": [36 {37 "internalType": "bool",38 "name": "",39 "type": "bool"40 }41 ],42 "stateMutability": "view",43 "type": "function"44 },45 {46 "inputs": [47 {48 "internalType": "address",49 "name": "target",50 "type": "address"51 }52 ],53 "name": "contractOwner",54 "outputs": [55 {56 "internalType": "address",57 "name": "",58 "type": "address"59 }60 ],61 "stateMutability": "view",62 "type": "function"63 },64 {65 "inputs": [66 {67 "internalType": "address",68 "name": "target",69 "type": "address"70 }71 ],72 "name": "sponsoringEnabled",73 "outputs": [74 {75 "internalType": "bool",76 "name": "",77 "type": "bool"78 }79 ],80 "stateMutability": "view",81 "type": "function"82 },83 {84 "inputs": [85 {86 "internalType": "address",87 "name": "target",88 "type": "address"89 },90 {91 "internalType": "address",92 "name": "user",93 "type": "address"94 },95 {96 "internalType": "bool",97 "name": "isAllowed",98 "type": "bool"99 }100 ],101 "name": "toggleAllowed",102 "outputs": [],103 "stateMutability": "nonpayable",104 "type": "function"105 },106 {107 "inputs": [108 {109 "internalType": "address",110 "name": "target",111 "type": "address"112 },113 {114 "internalType": "bool",115 "name": "enabled",116 "type": "bool"117 }118 ],119 "name": "toggleAllowlist",120 "outputs": [],121 "stateMutability": "nonpayable",122 "type": "function"123 },124 {125 "inputs": [126 {127 "internalType": "address",128 "name": "target",129 "type": "address"130 },131 {132 "internalType": "bool",133 "name": "enabled",134 "type": "bool"135 }136 ],137 "name": "toggleSponsoring",138 "outputs": [],139 "stateMutability": "nonpayable",140 "type": "function"141 }142]tests/src/eth/util/helpers.d.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/eth/util/helpers.d.ts
@@ -0,0 +1 @@
+declare module 'solc';
\ No newline at end of file
tests/src/eth/util/helpers.tsdiffbeforeafterboth--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -3,6 +3,9 @@
// file 'LICENSE', which is part of this source code package.
//
+// eslint-disable-next-line @typescript-eslint/triple-slash-reference
+/// <reference path="helpers.d.ts" />
+
import { ApiPromise } from '@polkadot/api';
import { addressToEvm, evmToAddress } from '@polkadot/util-crypto';
import Web3 from 'web3';
@@ -10,6 +13,12 @@
import { IKeyringPair } from '@polkadot/types/types';
import { expect } from 'chai';
import { getGenericResult } from '../../util/helpers';
+import * as solc from 'solc';
+import config from '../../config';
+import privateKey from '../../substrate/privateKey';
+import contractHelpersAbi from './contractHelpersAbi.json';
+
+export const GAS_ARGS = { gas: 0x10000000, gasPrice: '0x01' };
let web3Connected = false;
export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {
@@ -28,6 +37,16 @@
}
}
+type Web3HttpMarker = {web3Http: true};
+
+export async function usingWeb3Http<T>(cb: (web3: Web3 & Web3HttpMarker) => Promise<T> | T): Promise<T> {
+ const provider = new Web3.providers.HttpProvider(config.frontierUrl);
+ const web3: Web3 & Web3HttpMarker = new Web3(provider) as any;
+ web3.web3Http = true;
+
+ return await cb(web3);
+}
+
export function collectionIdToAddress(address: number): string {
if (address >= 0xffffffff || address < 0) throw new Error('id overflow');
const buf = Buffer.from([0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,
@@ -126,4 +145,80 @@
const bytes = addressToEvm(eth);
const string = '0x' + Buffer.from(bytes).toString('hex');
return Web3.utils.toChecksumAddress(string);
+}
+
+export function compileContract(name: string, src: string) {
+ const out = JSON.parse(solc.compile(JSON.stringify({
+ language: 'Solidity',
+ sources: {
+ [`${name}.sol`]: {
+ content: `
+ // SPDX-License-Identifier: UNLICENSED
+ pragma solidity ^0.8.6;
+
+ ${src}
+ `,
+ },
+ },
+ settings: {
+ outputSelection: {
+ '*': {
+ '*': ['*'],
+ },
+ },
+ },
+ }))).contracts[`${name}.sol`][name];
+
+ return {
+ abi: out.abi,
+ object: '0x' + out.evm.bytecode.object,
+ };
+}
+
+export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {
+ const compiled = compileContract('Flipper', `
+ contract Flipper {
+ bool value = false;
+ function flip() public {
+ value = !value;
+ }
+ function getValue() public view returns (bool) {
+ return value;
+ }
+ }
+ `);
+ const Flipper = new web3.eth.Contract(compiled.abi, undefined, {
+ data: compiled.object,
+ from: deployer,
+ ...GAS_ARGS,
+ });
+ const flipper = await Flipper.deploy({ data: compiled.object }).send({from: deployer});
+
+ return flipper;
+}
+
+export async function deployCollector(web3: Web3 & Web3HttpMarker, deployer: string) {
+ const compiled = compileContract('Collector', `
+ contract Collector {
+ uint256 collected;
+ function giveMoney() public payable {
+ collected += msg.value;
+ }
+ function getCollected() public view returns (uint256) {
+ return collected;
+ }
+ }
+ `);
+ const Collector = new web3.eth.Contract(compiled.abi, undefined, {
+ data: compiled.object,
+ from: deployer,
+ ...GAS_ARGS,
+ });
+ const collector = await Collector.deploy({ data: compiled.object }).send({ from: deployer });
+
+ return collector;
+}
+
+export function contractHelpers(web3: Web3, caller: string) {
+ return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS});
}
\ No newline at end of file