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
}
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) {

@ -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 {

@ -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)

@ -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 { 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<string, string | number>
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 })

Loading…
Cancel
Save