git.delta.rocks / unique-network / refs/commits / 51bdbddb0c24

difftreelog

test(evm) add contract helpers

Yaroslav Bolyukin2021-07-27parent: #7233370.patch.diff
in: master

3 files changed

addedtests/src/eth/util/contractHelpersAbi.jsondiffbeforeafterboth
--- /dev/null
+++ b/tests/src/eth/util/contractHelpersAbi.json
@@ -0,0 +1,142 @@
+[
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "contract",
+                "type": "address"
+            }
+        ],
+        "name": "allowlistEnabled",
+        "outputs": [
+            {
+                "internalType": "bool",
+                "name": "",
+                "type": "bool"
+            }
+        ],
+        "stateMutability": "view",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "target",
+                "type": "address"
+            },
+            {
+                "internalType": "address",
+                "name": "caller",
+                "type": "address"
+            }
+        ],
+        "name": "allowed",
+        "outputs": [
+            {
+                "internalType": "bool",
+                "name": "",
+                "type": "bool"
+            }
+        ],
+        "stateMutability": "view",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "target",
+                "type": "address"
+            }
+        ],
+        "name": "contractOwner",
+        "outputs": [
+            {
+                "internalType": "address",
+                "name": "",
+                "type": "address"
+            }
+        ],
+        "stateMutability": "view",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "target",
+                "type": "address"
+            }
+        ],
+        "name": "sponsoringEnabled",
+        "outputs": [
+            {
+                "internalType": "bool",
+                "name": "",
+                "type": "bool"
+            }
+        ],
+        "stateMutability": "view",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "target",
+                "type": "address"
+            },
+            {
+                "internalType": "address",
+                "name": "user",
+                "type": "address"
+            },
+            {
+                "internalType": "bool",
+                "name": "isAllowed",
+                "type": "bool"
+            }
+        ],
+        "name": "toggleAllowed",
+        "outputs": [],
+        "stateMutability": "nonpayable",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "target",
+                "type": "address"
+            },
+            {
+                "internalType": "bool",
+                "name": "enabled",
+                "type": "bool"
+            }
+        ],
+        "name": "toggleAllowlist",
+        "outputs": [],
+        "stateMutability": "nonpayable",
+        "type": "function"
+    },
+    {
+        "inputs": [
+            {
+                "internalType": "address",
+                "name": "target",
+                "type": "address"
+            },
+            {
+                "internalType": "bool",
+                "name": "enabled",
+                "type": "bool"
+            }
+        ],
+        "name": "toggleSponsoring",
+        "outputs": [],
+        "stateMutability": "nonpayable",
+        "type": "function"
+    }
+]
\ No newline at end of file
addedtests/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
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
3// file 'LICENSE', which is part of this source code package.3// file 'LICENSE', which is part of this source code package.
4//4//
5
6// eslint-disable-next-line @typescript-eslint/triple-slash-reference
7/// <reference path="helpers.d.ts" />
58
6import { ApiPromise } from '@polkadot/api';9import { ApiPromise } from '@polkadot/api';
7import { addressToEvm, evmToAddress } from '@polkadot/util-crypto';10import { addressToEvm, evmToAddress } from '@polkadot/util-crypto';
10import { IKeyringPair } from '@polkadot/types/types';13import { IKeyringPair } from '@polkadot/types/types';
11import { expect } from 'chai';14import { expect } from 'chai';
12import { getGenericResult } from '../../util/helpers';15import { getGenericResult } from '../../util/helpers';
16import * as solc from 'solc';
17import config from '../../config';
18import privateKey from '../../substrate/privateKey';
19import contractHelpersAbi from './contractHelpersAbi.json';
20
21export const GAS_ARGS = { gas: 0x10000000, gasPrice: '0x01' };
1322
14let web3Connected = false;23let web3Connected = false;
15export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {24export async function usingWeb3<T>(cb: (web3: Web3) => Promise<T> | T): Promise<T> {
28 }37 }
29}38}
39
40type Web3HttpMarker = {web3Http: true};
41
42export async function usingWeb3Http<T>(cb: (web3: Web3 & Web3HttpMarker) => Promise<T> | T): Promise<T> {
43 const provider = new Web3.providers.HttpProvider(config.frontierUrl);
44 const web3: Web3 & Web3HttpMarker = new Web3(provider) as any;
45 web3.web3Http = true;
46
47 return await cb(web3);
48}
3049
31export function collectionIdToAddress(address: number): string {50export function collectionIdToAddress(address: number): string {
32 if (address >= 0xffffffff || address < 0) throw new Error('id overflow');51 if (address >= 0xffffffff || address < 0) throw new Error('id overflow');
128 return Web3.utils.toChecksumAddress(string);147 return Web3.utils.toChecksumAddress(string);
129}148}
149
150export function compileContract(name: string, src: string) {
151 const out = JSON.parse(solc.compile(JSON.stringify({
152 language: 'Solidity',
153 sources: {
154 [`${name}.sol`]: {
155 content: `
156 // SPDX-License-Identifier: UNLICENSED
157 pragma solidity ^0.8.6;
158
159 ${src}
160 `,
161 },
162 },
163 settings: {
164 outputSelection: {
165 '*': {
166 '*': ['*'],
167 },
168 },
169 },
170 }))).contracts[`${name}.sol`][name];
171
172 return {
173 abi: out.abi,
174 object: '0x' + out.evm.bytecode.object,
175 };
176}
177
178export async function deployFlipper(web3: Web3 & Web3HttpMarker, deployer: string) {
179 const compiled = compileContract('Flipper', `
180 contract Flipper {
181 bool value = false;
182 function flip() public {
183 value = !value;
184 }
185 function getValue() public view returns (bool) {
186 return value;
187 }
188 }
189 `);
190 const Flipper = new web3.eth.Contract(compiled.abi, undefined, {
191 data: compiled.object,
192 from: deployer,
193 ...GAS_ARGS,
194 });
195 const flipper = await Flipper.deploy({ data: compiled.object }).send({from: deployer});
196
197 return flipper;
198}
199
200export async function deployCollector(web3: Web3 & Web3HttpMarker, deployer: string) {
201 const compiled = compileContract('Collector', `
202 contract Collector {
203 uint256 collected;
204 function giveMoney() public payable {
205 collected += msg.value;
206 }
207 function getCollected() public view returns (uint256) {
208 return collected;
209 }
210 }
211 `);
212 const Collector = new web3.eth.Contract(compiled.abi, undefined, {
213 data: compiled.object,
214 from: deployer,
215 ...GAS_ARGS,
216 });
217 const collector = await Collector.deploy({ data: compiled.object }).send({ from: deployer });
218
219 return collector;
220}
221
222export function contractHelpers(web3: Web3, caller: string) {
223 return new web3.eth.Contract(contractHelpersAbi as any, '0x842899ECF380553E8a4de75bF534cdf6fBF64049', {from: caller, ...GAS_ARGS});
224}