From 5e17c54bf99468126f88b1967979b415940ee3d9 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 29 Mar 2023 09:36:06 +0200 Subject: [PATCH] refactor types in vm.ts --- apps/remix-ide/src/blockchain/blockchain.tsx | 20 ++++++++++++------- apps/remix-ide/src/blockchain/providers/vm.ts | 14 ++++++------- libs/remix-lib/src/execution/txRunnerWeb3.ts | 4 ++-- libs/remix-simulator/src/index.ts | 2 +- libs/remix-simulator/src/provider.ts | 17 +++++++++++++++- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/apps/remix-ide/src/blockchain/blockchain.tsx b/apps/remix-ide/src/blockchain/blockchain.tsx index be5d1b2817..0aec55082c 100644 --- a/apps/remix-ide/src/blockchain/blockchain.tsx +++ b/apps/remix-ide/src/blockchain/blockchain.tsx @@ -28,6 +28,12 @@ const profile = { version: packageJson.version } +export type TransactionContextAPI = { + getAddress: (cb: (error: Error, result: string) => void) => void, + getValue: (cb: (error: Error, result: string) => void) => void, + getGasLimit: (cb: (error: Error, result: string) => void) => void +} + // see TxRunner.ts in remix-lib export type Transaction = { from: string, @@ -45,7 +51,7 @@ export class Blockchain extends Plugin { events: EventEmitter executionContext: ExecutionContext config: Config - txRunner: any + txRunner: any // TxRunner networkcallid: number networkStatus: { network: { @@ -54,8 +60,8 @@ export class Blockchain extends Plugin { } error?: string } - providers: any - transactionContextAPI: any + providers: { [key: string]: VMProvider | InjectedProvider | NodeProvider } + transactionContextAPI: TransactionContextAPI // NOTE: the config object will need to be refactored out in remix-lib constructor (config: Config) { @@ -478,14 +484,14 @@ export class Blockchain extends Plugin { } web3VM () { - return this.providers.vm.web3 + return (this.providers.vm as VMProvider).web3 } web3 () { // @todo(https://github.com/ethereum/remix-project/issues/431) const isVM = this.executionContext.isVM() if (isVM) { - return this.providers.vm.web3 + return (this.providers.vm as VMProvider).web3 } return this.executionContext.web3() } @@ -541,7 +547,7 @@ export class Blockchain extends Plugin { } // NOTE: the config is only needed because exectuionContext.init does - async resetAndInit (config, transactionContextAPI) { + async resetAndInit (config: Config, transactionContextAPI: TransactionContextAPI) { this.transactionContextAPI = transactionContextAPI this.executionContext.init(config) this.executionContext.stopListenOnLastBlock() @@ -603,7 +609,7 @@ export class Blockchain extends Plugin { if (!this.executionContext.isVM()) { throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') } - return this.providers.vm.createVMAccount(newAccount) + return (this.providers.vm as VMProvider).createVMAccount(newAccount) } newAccount (_password, passwordPromptCb, cb) { diff --git a/apps/remix-ide/src/blockchain/providers/vm.ts b/apps/remix-ide/src/blockchain/providers/vm.ts index d23a72f205..958eb9f6d4 100644 --- a/apps/remix-ide/src/blockchain/providers/vm.ts +++ b/apps/remix-ide/src/blockchain/providers/vm.ts @@ -1,17 +1,19 @@ import Web3 from 'web3' import { privateToAddress, hashPersonalMessage } from '@ethereumjs/util' import BN from 'bn.js' -import { extend } from '@remix-project/remix-simulator' +import { extend, JSONRPCRequestPayload, JSONRPCResponseCallback } from '@remix-project/remix-simulator' import { ExecutionContext } from '../execution-context' export class VMProvider { executionContext: ExecutionContext web3: Web3 worker: Worker - provider: any - newAccountCallback: any - accounts: any - constructor (executionContext) { + provider: { + sendAsync: (query: JSONRPCRequestPayload, callback: JSONRPCResponseCallback) => void + } + newAccountCallback: {[stamp: number]: (error: Error, address: string) => void} + + constructor (executionContext: ExecutionContext) { this.executionContext = executionContext this.worker = null this.provider = null @@ -29,7 +31,6 @@ export class VMProvider { async resetEnvironment () { if (this.worker) this.worker.terminate() - this.accounts = {} this.worker = new Worker(new URL('./worker-vm', import.meta.url)) const provider = this.executionContext.getProviderObject() @@ -52,7 +53,6 @@ export class VMProvider { } this.web3 = new Web3(this.provider) extend(this.web3) - this.accounts = {} this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3) resolve({}) } else { diff --git a/libs/remix-lib/src/execution/txRunnerWeb3.ts b/libs/remix-lib/src/execution/txRunnerWeb3.ts index 1d09db22a9..f9e625b2d2 100644 --- a/libs/remix-lib/src/execution/txRunnerWeb3.ts +++ b/libs/remix-lib/src/execution/txRunnerWeb3.ts @@ -90,7 +90,7 @@ export class TxRunnerWeb3 { } runInNode (from, to, data, value, gasLimit, useCall, timestamp, confirmCb, gasEstimationForceSend, promptCb, callback) { - const tx = { from: from, to: to, data: data, value: value, type: null, maxFeePerGas: null, gasPrice: null } + const tx = { from: from, to: to, data: data, value: value } if (!from) return callback('the value of "from" is not defined. Please make sure an account is selected.') if (useCall) { tx['gas'] = gasLimit @@ -107,7 +107,7 @@ export class TxRunnerWeb3 { console.log(errNetWork) return } - const txCopy = { ...tx } + const txCopy = { ...tx, type: undefined, maxFeePerGas: undefined, gasPrice: undefined } if (network && network.lastBlock) { if (network.lastBlock.baseFeePerGas) { // the sending stack (web3.js / metamask need to have the type defined) diff --git a/libs/remix-simulator/src/index.ts b/libs/remix-simulator/src/index.ts index 19ca3ab802..e49f4baf86 100644 --- a/libs/remix-simulator/src/index.ts +++ b/libs/remix-simulator/src/index.ts @@ -1 +1 @@ -export { Provider, extend } from './provider' +export { Provider, extend, JSONRPCRequestPayload, JSONRPCResponsePayload, JSONRPCResponseCallback } from './provider' diff --git a/libs/remix-simulator/src/provider.ts b/libs/remix-simulator/src/provider.ts index 03b2840943..ebeca2e803 100644 --- a/libs/remix-simulator/src/provider.ts +++ b/libs/remix-simulator/src/provider.ts @@ -12,6 +12,21 @@ import { Debug } from './methods/debug' import { generateBlock } from './genesis' import { VMContext } from './vm-context' +export interface JSONRPCRequestPayload { + params: any[]; + method: string; + id: number; + jsonrpc: string; +} + +export interface JSONRPCResponsePayload { + result: any; + id: number; + jsonrpc: string; +} + +export type JSONRPCResponseCallback = (err: Error, result?: JSONRPCResponsePayload) => void + export class Provider { options: Record vmContext @@ -56,7 +71,7 @@ export class Provider { } } - sendAsync (payload, callback) { + sendAsync (payload: JSONRPCRequestPayload, callback: (err: Error, result?: JSONRPCResponsePayload) => void) { // log.info('payload method is ', payload.method) // commented because, this floods the IDE console if (!this.initialized) { this.pendingRequests.push({ payload, callback })