Skip to content

Commit 3b1acd8

Browse files
committed
no web3 in remix-tests
1 parent 8338059 commit 3b1acd8

File tree

10 files changed

+96
-95
lines changed

10 files changed

+96
-95
lines changed

libs/remix-simulator/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { Provider, extend, JSONRPCRequestPayload, JSONRPCResponsePayload, JSONRPCResponseCallback } from './provider'
1+
export { Provider, extendProvider, JSONRPCRequestPayload, JSONRPCResponsePayload, JSONRPCResponseCallback } from './provider'
22
export { Server } from './server'

libs/remix-simulator/src/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class Provider {
157157
}
158158
}
159159

160-
export function extend (provider) { // Provider should be ethers.js provider
160+
export function extendProvider (provider) { // Provider should be ethers.js provider
161161

162162
provider.remix.getExecutionResultFromSimulator = async (transactionHash) => {
163163
return await this.send('eth_getExecutionResultFromSimulator', [transactionHash])

libs/remix-tests/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"string-similarity": "^4.0.4",
6363
"time-stamp": "^2.2.0",
6464
"tslib": "^2.3.0",
65-
"web3": "^4.1.1",
6665
"winston": "^3.0.0"
6766
},
6867
"devDependencies": {

libs/remix-tests/src/deployer.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import async from 'async'
22
import { execution } from '@remix-project/remix-lib'
3-
import { Web3, FMT_BYTES, FMT_NUMBER } from 'web3'
43
import { compilationInterface } from './types'
4+
import { BrowserProvider, ContractFactory, ethers } from 'ethers'
55

66
/**
77
* @dev Deploy all contracts from compilation result
88
* @param compileResult compilation result
9-
* @param web3 web3 object
9+
* @param provider BrowserProvider object
1010
* @param withDoubleGas If true, try deployment with gas double of estimation (used for Out-of-gas error only)
1111
* @param callback Callback
1212
*/
1313

14-
export function deployAll (compileResult: compilationInterface, web3: Web3, testsAccounts, withDoubleGas: boolean, deployCb, callback) {
14+
export function deployAll (compileResult: compilationInterface, provider: BrowserProvider, testsAccounts, withDoubleGas: boolean, deployCb, callback) {
1515
const compiledObject = {}
1616
const contracts = {}
1717
const accounts: string[] = testsAccounts
@@ -58,7 +58,7 @@ export function deployAll (compileResult: compilationInterface, web3: Web3, test
5858
},
5959
function deployContracts (contractsToDeploy: string[], next) {
6060
const deployRunner = (deployObject, contractObject, contractName, filename, callback) => {
61-
deployObject.estimateGas(undefined, { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }).then((gasValue) => {
61+
deployObject.estimateGas(undefined).then((gasValue) => {
6262
const gasBase = Math.ceil(gasValue * 1.2)
6363
const gas = withDoubleGas ? gasBase * 2 : gasBase
6464
deployObject.send({
@@ -89,19 +89,25 @@ export function deployAll (compileResult: compilationInterface, web3: Web3, test
8989
const contract = compiledObject[contractName]
9090
const encodeDataFinalCallback = (error, contractDeployData) => {
9191
if (error) return nextEach(error)
92-
const contractObject = new web3.eth.Contract(contract.abi)
93-
const deployObject = contractObject.deploy({ arguments: [], data: '0x' + contractDeployData.dataHex })
94-
deployRunner(deployObject, contractObject, contractName, contract.filename, (error) => { nextEach(error) })
92+
provider.getSigner().then((signer) => {
93+
const contractObject: ContractFactory = new ethers.ContractFactory(contract.abi, '0x' + contractDeployData.dataHex, signer)
94+
contractObject.deploy().then((deployObject) => {
95+
deployRunner(deployObject, contractObject, contractName, contract.filename, (error) => { nextEach(error) })
96+
})
97+
})
9598
}
9699

97100
const encodeDataStepCallback = (msg) => { console.dir(msg) }
98101

99102
const encodeDataDeployLibraryCallback = (libData, callback) => {
100103
const abi = compiledObject[libData.data.contractName].abi
101104
const code = compiledObject[libData.data.contractName].code
102-
const libraryObject = new web3.eth.Contract(abi)
103-
const deployObject = libraryObject.deploy({ arguments: [], data: '0x' + code })
104-
deployRunner(deployObject, libraryObject, libData.data.contractName, contract.filename, callback)
105+
provider.getSigner().then((signer) => {
106+
const libraryObject = new ethers.ContractFactory(abi, '0x' + code, signer)
107+
contract.deploy().then((deployObject) => {
108+
deployRunner(deployObject, libraryObject, libData.data.contractName, contract.filename, callback)
109+
})
110+
})
105111
}
106112

107113
const funAbi = null // no need to set the abi for encoding the constructor

libs/remix-tests/src/run.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Command } from 'commander';
2-
import { Web3 } from 'web3'
2+
import { ethers, BrowserProvider } from "ethers"
33
import path from 'path'
44
import axios, { AxiosResponse } from 'axios'
55
import { runTestFiles } from './runTestFiles'
66
import fs from './fileSystem'
7-
import { Provider, extend } from '@remix-project/remix-simulator'
7+
import { Provider, extendProvider } from '@remix-project/remix-simulator'
88
import { CompilerConfiguration } from './types'
99
import Log from './logger'
1010
import colors from 'colors'
@@ -127,11 +127,11 @@ commander
127127
nodeUrl: options.nodeUrl || null,
128128
blockNumber: options.blockNumber || null
129129
}
130-
const provider: any = new Provider(providerConfig)
131-
await provider.init()
132-
const web3 = new Web3(provider)
133-
extend(web3)
134-
runTestFiles(path.resolve(file_path), isDirectory, web3, compilerConfig, (error, totalPassing, totalFailing) => {
130+
const simulatorProvider: any = new Provider(providerConfig)
131+
await simulatorProvider.init()
132+
const provider: BrowserProvider = new ethers.BrowserProvider(simulatorProvider as any)
133+
extendProvider(provider)
134+
runTestFiles(path.resolve(file_path), isDirectory, provider, compilerConfig, (error, totalPassing, totalFailing) => {
135135
if (error) process.exit(1)
136136
if (totalFailing > 0 && options.killProcess) process.exit(1)
137137
})

libs/remix-tests/src/runTestFiles.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import fs from './fileSystem'
33
import { runTest } from './testRunner'
44
import { TestResultInterface, ResultsInterface, CompilerConfiguration, compilationInterface, ASTInterface, Options, AstNode } from './types'
55
import colors from 'colors'
6-
import { Web3 } from 'web3'
76
import { format } from 'util'
87
import { compileFileOrFiles } from './compiler'
98
import { deployAll } from './deployer'
9+
import { BrowserProvider } from 'ethers'
1010

1111
/**
1212
* @dev run test contract files (used for CLI)
1313
* @param filepath Path of file
1414
* @param isDirectory True, if path is a directory
15-
* @param web3 Web3
15+
* @param provider BrowserProvider
1616
* @param finalCallback optional callback to run finally
1717
* @param opts Options
1818
*/
1919

2020
// eslint-disable-next-line @typescript-eslint/no-empty-function
21-
export function runTestFiles (filepath: string, isDirectory: boolean, web3: Web3, compilerConfig: CompilerConfiguration, finalCallback: any = () => {}, opts?: Options) {
21+
export function runTestFiles (filepath: string, isDirectory: boolean, provider: BrowserProvider, compilerConfig: CompilerConfiguration, finalCallback: any = () => {}, opts?: Options) {
2222
opts = opts || {}
2323
compilerConfig = compilerConfig || {} as CompilerConfiguration
2424
const sourceASTs: any = {}
@@ -62,7 +62,7 @@ export function runTestFiles (filepath: string, isDirectory: boolean, web3: Web3
6262
async.waterfall([
6363
function getAccountList (next) {
6464
if (accounts) return next(null)
65-
web3.eth.getAccounts()
65+
provider.send("eth_requestAccounts", [])
6666
.then(_accounts => {
6767
accounts = _accounts
6868
next(null)
@@ -77,13 +77,13 @@ export function runTestFiles (filepath: string, isDirectory: boolean, web3: Web3
7777
for (const filename in asts) {
7878
if (filename.endsWith('_test.sol')) { sourceASTs[filename] = asts[filename].ast }
7979
}
80-
deployAll(compilationResult, web3, accounts, false, null, (err, contracts) => {
80+
deployAll(compilationResult, provider, accounts, false, null, (err, contracts) => {
8181
if (err) {
8282
// If contract deployment fails because of 'Out of Gas' error, try again with double gas
8383
// This is temporary, should be removed when remix-tests will have a dedicated UI to
8484
// accept deployment params from UI
8585
if (err.error.includes('The contract code couldn\'t be stored, please check your gas limit')) {
86-
deployAll(compilationResult, web3, accounts, true, null, (error, contracts) => {
86+
deployAll(compilationResult, provider, accounts, true, null, (error, contracts) => {
8787
if (error) next([{ message: 'contract deployment failed after trying twice: ' + (error.innerError || error.error), severity: 'error' }]) // IDE expects errors in array
8888
else next(null, compilationResult, contracts)
8989
})
@@ -150,7 +150,7 @@ export function runTestFiles (filepath: string, isDirectory: boolean, web3: Web3
150150
async.eachOfLimit(contractsToTest, 1, (contractName: string, index, cb) => {
151151
try {
152152
const fileAST: AstNode = sourceASTs[contracts[contractName]['filename']]
153-
runTest(contractName, contracts[contractName], contractsToTestDetails[index], fileAST, { accounts, web3 }, _testCallback, (err, result) => {
153+
runTest(contractName, contracts[contractName], contractsToTestDetails[index], fileAST, { accounts, provider }, _testCallback, (err, result) => {
154154
if (err) {
155155
console.log(err)
156156
return cb(err)

libs/remix-tests/src/runTestSources.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import async, { ErrorCallback } from 'async'
22
import { compileContractSources, writeTestAccountsContract } from './compiler'
33
import { deployAll } from './deployer'
44
import { runTest } from './testRunner'
5-
import { Web3 } from 'web3'
65
import { EventEmitter } from 'events'
7-
import { Provider, extend } from '@remix-project/remix-simulator'
6+
import { extendProvider } from '@remix-project/remix-simulator'
87
import {
98
FinalResult, SrcIfc, compilationInterface, ASTInterface, Options,
109
TestResultInterface, AstNode, CompilerConfiguration
@@ -15,23 +14,23 @@ export class UnitTestRunner {
1514
event
1615
accountsLibCode
1716
testsAccounts: string[] | null
18-
web3
17+
provider
1918
compiler
2019
compilerConfig
2120

2221
constructor () {
2322
this.event = new EventEmitter()
2423
}
2524

26-
async init (web3 = null, accounts = null) {
27-
this.web3 = await this.createWeb3Provider(web3)
28-
this.testsAccounts = accounts || (this.web3 && await this.web3.eth.getAccounts()) || []
25+
async init (provider = null, accounts = null) {
26+
this.provider = await this.createWeb3Provider(provider)
27+
this.testsAccounts = accounts || (this.provider && await this.provider.send("eth_requestAccounts", []) ) || []
2928
this.accountsLibCode = writeTestAccountsContract(this.testsAccounts)
3029
}
3130

32-
async createWeb3Provider (optWeb3) {
33-
const web3 = optWeb3
34-
if (web3) extend(web3)
31+
async createWeb3Provider (provider) {
32+
const web3 = provider
33+
if (web3) extendProvider(provider)
3534
return web3
3635
}
3736

@@ -48,7 +47,7 @@ export class UnitTestRunner {
4847
async runTestSources (contractSources: SrcIfc, newCompilerConfig: CompilerConfiguration, testCallback, resultCallback, deployCb:any, finalCallback: any, importFileCb, opts: Options) {
4948
opts = opts || {}
5049
const sourceASTs: any = {}
51-
if (opts.web3 || opts.accounts) this.init(opts.web3, opts.accounts)
50+
if (opts.provider || opts.accounts) this.init(opts.provider, opts.accounts)
5251
async.waterfall([
5352
(next) => {
5453
compileContractSources(contractSources, newCompilerConfig, importFileCb, this, { accounts: this.testsAccounts, testFilePath: opts.testFilePath, event: this.event }, next)
@@ -57,13 +56,13 @@ export class UnitTestRunner {
5756
for (const filename in asts) {
5857
if (filename.endsWith('_test.sol')) { sourceASTs[filename] = asts[filename].ast }
5958
}
60-
deployAll(compilationResult, this.web3, this.testsAccounts, false, deployCb, (err, contracts) => {
59+
deployAll(compilationResult, this.provider, this.testsAccounts, false, deployCb, (err, contracts) => {
6160
if (err) {
6261
// If contract deployment fails because of 'Out of Gas' error, try again with double gas
6362
// This is temporary, should be removed when remix-tests will have a dedicated UI to
6463
// accept deployment params from UI
6564
if (err.error.includes('The contract code couldn\'t be stored, please check your gas limit')) {
66-
deployAll(compilationResult, this.web3, this.testsAccounts, true, deployCb, (error, contracts) => {
65+
deployAll(compilationResult, this.provider, this.testsAccounts, true, deployCb, (error, contracts) => {
6766
if (error) next([{ message: 'contract deployment failed after trying twice: ' + (error.innerError || error.error), severity: 'error' }]) // IDE expects errors in array
6867
else next(null, compilationResult, contracts)
6968
})
@@ -109,7 +108,7 @@ export class UnitTestRunner {
109108

110109
async.eachOfLimit(contractsToTest, 1, (contractName: string, index: string | number, cb: ErrorCallback) => {
111110
const fileAST: AstNode = sourceASTs[contracts[contractName]['filename']]
112-
runTest(contractName, contracts[contractName], contractsToTestDetails[index], fileAST, { accounts: this.testsAccounts, web3: this.web3 }, _testCallback, (err, result) => {
111+
runTest(contractName, contracts[contractName], contractsToTestDetails[index], fileAST, { accounts: this.testsAccounts, provider: this.provider }, _testCallback, (err, result) => {
113112
if (err) {
114113
return cb(err)
115114
}

libs/remix-tests/src/testRunner.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import async from 'async'
22
import * as changeCase from 'change-case'
3-
import { Web3 } from 'web3'
3+
import {keccak256, AbiCoder } from 'ethers'
44
import assertionEvents from './assertionEvents'
55
import {
66
RunListInterface, TestCbInterface, TestResultInterface, ResultCbInterface,
@@ -218,8 +218,7 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
218218
const isJSONInterfaceAvailable = testObject && testObject.options && testObject.options.jsonInterface
219219
if (!isJSONInterfaceAvailable) { return resultsCallback(new Error('Contract interface not available'), { passingNum, failureNum, timePassed }) }
220220
const runList: RunListInterface[] = createRunList(testObject.options.jsonInterface, fileAST, testName)
221-
const web3 = opts.web3 || new Web3()
222-
web3.eth.handleRevert = true // enables returning error reason on revert
221+
const provider = opts.provider
223222
const accts: TestResultInterface = {
224223
type: 'accountList',
225224
value: opts.accounts
@@ -250,12 +249,12 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
250249
if (func.constant) {
251250
sendParams = {}
252251
const tagTimestamp = 'remix_tests_tag' + Date.now()
253-
if (web3.remix && web3.remix.registerCallId) web3.remix.registerCallId(tagTimestamp)
252+
if (provider.remix && provider.remix.registerCallId) provider.remix.registerCallId(tagTimestamp)
254253
method.call(sendParams).then(async (result) => {
255254
const time = (Date.now() - startTime) / 1000.0
256255
let tagTxHash
257-
if (web3.remix && web3.remix.getHashFromTagBySimulator) tagTxHash = await web3.remix.getHashFromTagBySimulator(tagTimestamp)
258-
if (web3.remix && web3.remix.getHHLogsForTx) hhLogs = await web3.remix.getHHLogsForTx(tagTxHash)
256+
if (provider.remix && provider.remix.getHashFromTagBySimulator) tagTxHash = await provider.remix.getHashFromTagBySimulator(tagTimestamp)
257+
if (provider.remix && provider.remix.getHHLogsForTx) hhLogs = await provider.remix.getHHLogsForTx(tagTxHash)
259258
debugTxHash = tagTxHash
260259
if (result) {
261260
const resp: TestResultInterface = {
@@ -264,7 +263,7 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
264263
filename: testObject.filename,
265264
time: time,
266265
context: testName,
267-
web3,
266+
provider,
268267
debugTxHash
269268
}
270269
if (hhLogs && hhLogs.length) resp.hhLogs = hhLogs
@@ -279,7 +278,7 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
279278
time: time,
280279
errMsg: 'function returned false',
281280
context: testName,
282-
web3,
281+
provider,
283282
debugTxHash
284283
}
285284
if (hhLogs && hhLogs.length) resp.hhLogs = hhLogs
@@ -302,17 +301,17 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
302301
method.send(sendParams).on('receipt', async (receipt) => {
303302
try {
304303
debugTxHash = receipt.transactionHash
305-
if (web3.remix && web3.remix.getHHLogsForTx) hhLogs = await web3.remix.getHHLogsForTx(receipt.transactionHash)
304+
if (provider.remix && provider.remix.getHHLogsForTx) hhLogs = await provider.remix.getHHLogsForTx(receipt.transactionHash)
306305
const time: number = (Date.now() - startTime) / 1000.0
307-
const assertionEventHashes = assertionEvents.map(e => Web3.utils.sha3(e.name + '(' + e.params.join() + ')'))
306+
const assertionEventHashes = assertionEvents.map(e => keccak256(e.name + '(' + e.params.join() + ')'))
308307
let testPassed = false
309308
for (const i in receipt.logs) {
310309
let events = receipt.logs[i]
311310
if (!Array.isArray(events)) events = [events]
312311
for (const event of events) {
313312
const eIndex = assertionEventHashes.indexOf(event.topics[0]) // event name topic will always be at index 0
314313
if (eIndex >= 0) {
315-
const testEvent = web3.eth.abi.decodeParameters(assertionEvents[eIndex].params, event.data)
314+
const testEvent = AbiCoder.defaultAbiCoder().decode(assertionEvents[eIndex].params, event.data)
316315
if (!testEvent[0]) {
317316
const assertMethod = testEvent[2]
318317
if (assertMethod === 'ok') { // for 'Assert.ok' method
@@ -331,7 +330,7 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
331330
returned: testEvent[3],
332331
expected: testEvent[4],
333332
location,
334-
web3,
333+
provider,
335334
debugTxHash
336335
}
337336
if (hhLogs && hhLogs.length) resp.hhLogs = hhLogs
@@ -352,7 +351,7 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
352351
filename: testObject.filename,
353352
time: time,
354353
context: testName,
355-
web3,
354+
provider,
356355
debugTxHash
357356
}
358357
if (hhLogs && hhLogs.length) resp.hhLogs = hhLogs
@@ -391,13 +390,13 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
391390
time: time,
392391
errMsg,
393392
context: testName,
394-
web3
393+
provider
395394
}
396395
if (err.receipt) txHash = err.receipt.transactionHash
397396
else if (err.message.includes('Transaction has been reverted by the EVM')) {
398397
txHash = JSON.parse(err.message.replace('Transaction has been reverted by the EVM:', '')).transactionHash
399398
}
400-
if (web3.remix && web3.remix.getHHLogsForTx && txHash) hhLogs = await web3.remix.getHHLogsForTx(txHash)
399+
if (provider.remix && provider.remix.getHHLogsForTx && txHash) hhLogs = await provider.remix.getHHLogsForTx(txHash)
401400
if (hhLogs && hhLogs.length) resp.hhLogs = hhLogs
402401
resp.debugTxHash = txHash
403402
testCallback(undefined, resp)

libs/remix-tests/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface TestResultInterface {
3737
expected?: string | number
3838
location?: string
3939
hhLogs?: []
40-
web3?: any
40+
provider?: any
4141
debugTxHash?: string
4242
}
4343
export interface TestCbInterface {
@@ -50,7 +50,7 @@ export interface ResultCbInterface {
5050
export interface Options {
5151
accounts?: string[] | null,
5252
testFilePath?: string
53-
web3?: any
53+
provider?: any
5454
}
5555

5656
export interface CompilerConfiguration {

0 commit comments

Comments
 (0)