Skip to content

Commit b7f5875

Browse files
committed
fix initial ut of remix-tests
1 parent eb1111f commit b7f5875

File tree

5 files changed

+471
-472
lines changed

5 files changed

+471
-472
lines changed

libs/remix-simulator/src/provider.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,29 @@ export class Provider {
159159

160160
export function extendProvider (provider) { // Provider should be ethers.js provider
161161

162+
if (!provider.remix) provider.remix = {}
163+
162164
provider.remix.getExecutionResultFromSimulator = async (transactionHash) => {
163-
return await this.send('eth_getExecutionResultFromSimulator', [transactionHash])
165+
return await provider.send('eth_getExecutionResultFromSimulator', [transactionHash])
164166
}
165167

166168
provider.remix.getHHLogsForTx = async (transactionHash) => {
167-
return await this.send('eth_getHHLogsForTx',[transactionHash])
169+
return await provider.send('eth_getHHLogsForTx',[transactionHash])
168170
}
169171

170172
provider.remix.getHashFromTagBySimulator = async (timestamp) => {
171-
return await this.send('eth_getHashFromTagBySimulator', [timestamp])
173+
return await provider.send('eth_getHashFromTagBySimulator', [timestamp])
172174
}
173175

174176
provider.remix.registerCallId = async (id) => {
175-
return await this.send('eth_registerCallId',[id])
177+
return await provider.send('eth_registerCallId',[id])
176178
}
177179

178180
provider.remix.getStateDb = async () => {
179-
return await this.send('eth_getStateDb', [])
181+
return await provider.send('eth_getStateDb', [])
180182
}
181183

182184
provider.remix.getBlocksData = async () => {
183-
return await this.send('eth_getBlocksData',[])
185+
return await provider.send('eth_getBlocksData',[])
184186
}
185187
}

libs/remix-tests/src/deployer.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import async from 'async'
22
import { execution } from '@remix-project/remix-lib'
33
import { compilationInterface } from './types'
4-
import { BrowserProvider, ContractFactory, ethers } from 'ethers'
4+
import { BaseContract, BrowserProvider, Contract, ContractFactory, ethers, TransactionReceipt, TransactionResponse } from 'ethers'
55

66
/**
77
* @dev Deploy all contracts from compilation result
@@ -57,27 +57,26 @@ export function deployAll (compileResult: compilationInterface, provider: Browse
5757
next(null, contractsToDeploy)
5858
},
5959
function deployContracts (contractsToDeploy: string[], next) {
60-
const deployRunner = (deployObject, contractObject, contractName, filename, callback) => {
61-
deployObject.estimateGas(undefined).then((gasValue) => {
62-
const gasBase = Math.ceil(gasValue * 1.2)
63-
const gas = withDoubleGas ? gasBase * 2 : gasBase
64-
deployObject.send({
65-
from: accounts[0],
66-
gas: gas
67-
}).on('receipt', async function (receipt) {
68-
contractObject.options.address = receipt.contractAddress
69-
contractObject.options.from = accounts[0]
70-
contractObject.options.gas = 5000 * 1000
71-
compiledObject[contractName].deployedAddress = receipt.contractAddress
60+
const deployRunner = (deployObject, {abi, signer}, contractName, filename, callback) => {
61+
deployObject.getDeployTransaction().then((tx: TransactionResponse) => {
62+
provider.estimateGas(tx).then((gasValue) => {
63+
const gasBase = Math.ceil(Number(gasValue) * 1.2)
64+
const gas = withDoubleGas ? gasBase * 2 : gasBase
65+
deployObject.deploy({
66+
from: accounts[0],
67+
gasLimit: gas
68+
}).then(async function (deployContractObj: BaseContract) {
69+
const deployTx = deployContractObj.deploymentTransaction()
70+
const receipt: TransactionReceipt = await provider.getTransactionReceipt(deployTx.hash)
71+
const contractObject: Contract = new ethers.Contract(receipt.contractAddress, abi, signer)
72+
compiledObject[contractName].deployedAddress = receipt.contractAddress
7273

73-
contracts[contractName] = contractObject
74-
contracts[contractName].filename = filename
74+
contracts[contractName] = contractObject
75+
contracts[contractName].filename = filename
7576

76-
if (deployCb) await deployCb(filename, receipt.contractAddress)
77-
callback(null, { receipt: { contractAddress: receipt.contractAddress } }) // TODO this will only work with JavaScriptV VM
78-
}).on('error', function (err) {
79-
console.error(err)
80-
callback(err)
77+
if (deployCb) await deployCb(filename, receipt.contractAddress)
78+
callback(null, { receipt: { contractAddress: receipt.contractAddress } }) // TODO this will only work with JavaScriptV VM
79+
})
8180
})
8281
}).catch((err) => {
8382
console.error(err)
@@ -90,10 +89,8 @@ export function deployAll (compileResult: compilationInterface, provider: Browse
9089
const encodeDataFinalCallback = (error, contractDeployData) => {
9190
if (error) return nextEach(error)
9291
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-
})
92+
const deployObject: ContractFactory = new ethers.ContractFactory(contract.abi, '0x' + contractDeployData.dataHex, signer)
93+
deployRunner(deployObject, {abi: contract.abi, signer}, contractName, contract.filename, (error) => { nextEach(error) })
9794
})
9895
}
9996

@@ -103,10 +100,8 @@ export function deployAll (compileResult: compilationInterface, provider: Browse
103100
const abi = compiledObject[libData.data.contractName].abi
104101
const code = compiledObject[libData.data.contractName].code
105102
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-
})
103+
const deployObject: ContractFactory = new ethers.ContractFactory(abi, '0x' + code, signer)
104+
deployRunner(deployObject, {abi, signer}, libData.data.contractName, contract.filename, callback)
110105
})
111106
}
112107

libs/remix-tests/src/testRunner.ts

Lines changed: 14 additions & 12 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 {keccak256, AbiCoder } from 'ethers'
3+
import {keccak256, AbiCoder, ContractTransactionResponse, toUtf8Bytes } from 'ethers'
44
import assertionEvents from './assertionEvents'
55
import {
66
RunListInterface, TestCbInterface, TestResultInterface, ResultCbInterface,
@@ -215,9 +215,9 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
215215
let failureNum = 0
216216
let timePassed = 0
217217
const failedTransactions = {}
218-
const isJSONInterfaceAvailable = testObject && testObject.options && testObject.options.jsonInterface
218+
const isJSONInterfaceAvailable = testObject && testObject.interface && testObject.interface.fragments
219219
if (!isJSONInterfaceAvailable) { return resultsCallback(new Error('Contract interface not available'), { passingNum, failureNum, timePassed }) }
220-
const runList: RunListInterface[] = createRunList(testObject.options.jsonInterface, fileAST, testName)
220+
const runList: RunListInterface[] = createRunList(testObject.interface.fragments, fileAST, testName)
221221
const provider = opts.provider
222222
const accts: TestResultInterface = {
223223
type: 'accountList',
@@ -243,14 +243,14 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
243243
let sendParams: Record<string, any> | null = null
244244
if (sender) sendParams = { from: sender }
245245
if (func.inputs && func.inputs.length > 0) { return resultsCallback(new Error(`Method '${func.name}' can not have parameters inside a test contract`), { passingNum, failureNum, timePassed }) }
246-
const method = testObject.methods[func.name].apply(testObject.methods[func.name], [])
246+
247247
const startTime = Date.now()
248248
let debugTxHash:string
249249
if (func.constant) {
250250
sendParams = {}
251251
const tagTimestamp = 'remix_tests_tag' + Date.now()
252252
if (provider.remix && provider.remix.registerCallId) provider.remix.registerCallId(tagTimestamp)
253-
method.call(sendParams).then(async (result) => {
253+
testObject[func.name](sendParams).then(async (result) => {
254254
const time = (Date.now() - startTime) / 1000.0
255255
let tagTxHash
256256
if (provider.remix && provider.remix.getHashFromTagBySimulator) tagTxHash = await provider.remix.getHashFromTagBySimulator(tagTimestamp)
@@ -298,20 +298,22 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
298298
}
299299
if (!sendParams) sendParams = {}
300300
sendParams.gas = 10000000 * 8
301-
method.send(sendParams).on('receipt', async (receipt) => {
301+
testObject[func.name](sendParams).then(async (txResponse: ContractTransactionResponse) => {
302302
try {
303-
debugTxHash = receipt.transactionHash
304-
if (provider.remix && provider.remix.getHHLogsForTx) hhLogs = await provider.remix.getHHLogsForTx(receipt.transactionHash)
303+
const receipt = await provider.getTransactionReceipt(txResponse.hash)
304+
debugTxHash = receipt.hash
305+
if (provider.remix && provider.remix.getHHLogsForTx) hhLogs = await provider.remix.getHHLogsForTx(receipt.hash)
305306
const time: number = (Date.now() - startTime) / 1000.0
306-
const assertionEventHashes = assertionEvents.map(e => keccak256(e.name + '(' + e.params.join() + ')'))
307+
const assertionEventHashes = assertionEvents.map(e => keccak256(toUtf8Bytes(e.name + '(' + e.params.join() + ')')))
307308
let testPassed = false
308309
for (const i in receipt.logs) {
309310
let events = receipt.logs[i]
310311
if (!Array.isArray(events)) events = [events]
311312
for (const event of events) {
312313
const eIndex = assertionEventHashes.indexOf(event.topics[0]) // event name topic will always be at index 0
313314
if (eIndex >= 0) {
314-
const testEvent = AbiCoder.defaultAbiCoder().decode(assertionEvents[eIndex].params, event.data)
315+
const testEventArray = AbiCoder.defaultAbiCoder().decode(assertionEvents[eIndex].params, event.data)
316+
const testEvent = [...testEventArray] // Make it mutable
315317
if (!testEvent[0]) {
316318
const assertMethod = testEvent[2]
317319
if (assertMethod === 'ok') { // for 'Assert.ok' method
@@ -376,9 +378,9 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
376378
console.error(err)
377379
return next(err)
378380
}
379-
}).on('error', async (err) => {
381+
}).catch(async (err) => {
380382
const time: number = (Date.now() - startTime) / 1000.0
381-
if (failedTransactions[err.receipt.transactionHash]) return // we are already aware of this transaction failing.
383+
if (!err.receipt || failedTransactions[err.receipt.transactionHash]) return // we are already aware of this transaction failing.
382384
failedTransactions[err.receipt.transactionHash] = time
383385
let errMsg = err.message
384386
let txHash

0 commit comments

Comments
 (0)