refactor types in vm.ts

pull/3577/head
yann300 2 years ago committed by Aniket
parent 321613f231
commit 5e17c54bf9
  1. 20
      apps/remix-ide/src/blockchain/blockchain.tsx
  2. 14
      apps/remix-ide/src/blockchain/providers/vm.ts
  3. 4
      libs/remix-lib/src/execution/txRunnerWeb3.ts
  4. 2
      libs/remix-simulator/src/index.ts
  5. 17
      libs/remix-simulator/src/provider.ts

@ -28,6 +28,12 @@ const profile = {
version: packageJson.version 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 // see TxRunner.ts in remix-lib
export type Transaction = { export type Transaction = {
from: string, from: string,
@ -45,7 +51,7 @@ export class Blockchain extends Plugin {
events: EventEmitter events: EventEmitter
executionContext: ExecutionContext executionContext: ExecutionContext
config: Config config: Config
txRunner: any txRunner: any // TxRunner
networkcallid: number networkcallid: number
networkStatus: { networkStatus: {
network: { network: {
@ -54,8 +60,8 @@ export class Blockchain extends Plugin {
} }
error?: string error?: string
} }
providers: any providers: { [key: string]: VMProvider | InjectedProvider | NodeProvider }
transactionContextAPI: any transactionContextAPI: TransactionContextAPI
// NOTE: the config object will need to be refactored out in remix-lib // NOTE: the config object will need to be refactored out in remix-lib
constructor (config: Config) { constructor (config: Config) {
@ -478,14 +484,14 @@ export class Blockchain extends Plugin {
} }
web3VM () { web3VM () {
return this.providers.vm.web3 return (this.providers.vm as VMProvider).web3
} }
web3 () { web3 () {
// @todo(https://github.com/ethereum/remix-project/issues/431) // @todo(https://github.com/ethereum/remix-project/issues/431)
const isVM = this.executionContext.isVM() const isVM = this.executionContext.isVM()
if (isVM) { if (isVM) {
return this.providers.vm.web3 return (this.providers.vm as VMProvider).web3
} }
return this.executionContext.web3() return this.executionContext.web3()
} }
@ -541,7 +547,7 @@ export class Blockchain extends Plugin {
} }
// NOTE: the config is only needed because exectuionContext.init does // NOTE: the config is only needed because exectuionContext.init does
async resetAndInit (config, transactionContextAPI) { async resetAndInit (config: Config, transactionContextAPI: TransactionContextAPI) {
this.transactionContextAPI = transactionContextAPI this.transactionContextAPI = transactionContextAPI
this.executionContext.init(config) this.executionContext.init(config)
this.executionContext.stopListenOnLastBlock() this.executionContext.stopListenOnLastBlock()
@ -603,7 +609,7 @@ export class Blockchain extends Plugin {
if (!this.executionContext.isVM()) { if (!this.executionContext.isVM()) {
throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') 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) { newAccount (_password, passwordPromptCb, cb) {

@ -1,17 +1,19 @@
import Web3 from 'web3' import Web3 from 'web3'
import { privateToAddress, hashPersonalMessage } from '@ethereumjs/util' import { privateToAddress, hashPersonalMessage } from '@ethereumjs/util'
import BN from 'bn.js' 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' import { ExecutionContext } from '../execution-context'
export class VMProvider { export class VMProvider {
executionContext: ExecutionContext executionContext: ExecutionContext
web3: Web3 web3: Web3
worker: Worker worker: Worker
provider: any provider: {
newAccountCallback: any sendAsync: (query: JSONRPCRequestPayload, callback: JSONRPCResponseCallback) => void
accounts: any }
constructor (executionContext) { newAccountCallback: {[stamp: number]: (error: Error, address: string) => void}
constructor (executionContext: ExecutionContext) {
this.executionContext = executionContext this.executionContext = executionContext
this.worker = null this.worker = null
this.provider = null this.provider = null
@ -29,7 +31,6 @@ export class VMProvider {
async resetEnvironment () { async resetEnvironment () {
if (this.worker) this.worker.terminate() if (this.worker) this.worker.terminate()
this.accounts = {}
this.worker = new Worker(new URL('./worker-vm', import.meta.url)) this.worker = new Worker(new URL('./worker-vm', import.meta.url))
const provider = this.executionContext.getProviderObject() const provider = this.executionContext.getProviderObject()
@ -52,7 +53,6 @@ export class VMProvider {
} }
this.web3 = new Web3(this.provider) this.web3 = new Web3(this.provider)
extend(this.web3) extend(this.web3)
this.accounts = {}
this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3) this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3)
resolve({}) resolve({})
} else { } else {

@ -90,7 +90,7 @@ export class TxRunnerWeb3 {
} }
runInNode (from, to, data, value, gasLimit, useCall, timestamp, confirmCb, gasEstimationForceSend, promptCb, callback) { 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 (!from) return callback('the value of "from" is not defined. Please make sure an account is selected.')
if (useCall) { if (useCall) {
tx['gas'] = gasLimit tx['gas'] = gasLimit
@ -107,7 +107,7 @@ export class TxRunnerWeb3 {
console.log(errNetWork) console.log(errNetWork)
return return
} }
const txCopy = { ...tx } const txCopy = { ...tx, type: undefined, maxFeePerGas: undefined, gasPrice: undefined }
if (network && network.lastBlock) { if (network && network.lastBlock) {
if (network.lastBlock.baseFeePerGas) { if (network.lastBlock.baseFeePerGas) {
// the sending stack (web3.js / metamask need to have the type defined) // the sending stack (web3.js / metamask need to have the type defined)

@ -1 +1 @@
export { Provider, extend } from './provider' export { Provider, extend, JSONRPCRequestPayload, JSONRPCResponsePayload, JSONRPCResponseCallback } from './provider'

@ -12,6 +12,21 @@ import { Debug } from './methods/debug'
import { generateBlock } from './genesis' import { generateBlock } from './genesis'
import { VMContext } from './vm-context' 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 { export class Provider {
options: Record<string, string | number> options: Record<string, string | number>
vmContext 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 // log.info('payload method is ', payload.method) // commented because, this floods the IDE console
if (!this.initialized) { if (!this.initialized) {
this.pendingRequests.push({ payload, callback }) this.pendingRequests.push({ payload, callback })

Loading…
Cancel
Save