remix-simulator: use web3-* instead of web3

pull/3262/head^2
yann300 2 years ago committed by Aniket
parent ae2bb4a41d
commit 0f2161fc21
  1. 6
      apps/remix-ide/src/blockchain/execution-context.js
  2. 41
      libs/remix-simulator/src/VmProxy.ts
  3. 10
      libs/remix-simulator/src/methods/accounts.ts
  4. 6
      libs/remix-simulator/src/methods/blocks.ts
  5. 4
      libs/remix-simulator/src/methods/misc.ts
  6. 12
      libs/remix-simulator/src/methods/transactions.ts
  7. 5
      libs/remix-simulator/src/provider.ts
  8. 11
      libs/remix-simulator/src/vm-context.ts
  9. 1
      libs/remix-ui/run-tab/src/lib/types/execution-context.d.ts

@ -133,11 +133,7 @@ export class ExecutionContext {
internalWeb3 () {
return web3
}
blankWeb3 () {
return new Web3()
}
setContext (context, endPointUrl, confirmCb, infoCb) {
this.executionContext = context
this.executionContextChange(context, endPointUrl, confirmCb, infoCb, null)

@ -4,13 +4,13 @@ import { helpers } from '@remix-project/remix-lib'
const { normalizeHexAddress } = helpers.ui
import { ConsoleLogs } from '@remix-project/remix-lib'
import { toChecksumAddress, BN, keccak, bufferToHex, Address, toBuffer } from 'ethereumjs-util'
import Web3 from 'web3'
import utils from 'web3-utils'
import { ethers } from 'ethers'
import { VMContext } from './vm-context'
import type { InterpreterStep } from '@ethereumjs/evm/dist/interpreter'
export class VmProxy {
vmContext: VMContext
web3: Web3
vm
vmTraces
txs
@ -43,7 +43,6 @@ export class VmProxy {
constructor (vmContext: VMContext) {
this.vmContext = vmContext
this.web3 = new Web3()
this.vm = null
this.vmTraces = {}
this.txs = {}
@ -71,16 +70,16 @@ export class VmProxy {
this.lastProcessedStorageTxHash = {}
this.sha3Preimages = {}
// util
this.sha3 = (...args) => this.web3.utils.sha3.apply(this, args)
this.toHex = (...args) => this.web3.utils.toHex.apply(this, args)
this.toAscii = (...args) => this.web3.utils.toAscii.apply(this, args)
this.fromAscii = (...args) => this.web3.utils.fromAscii.apply(this, args)
this.fromDecimal = (...args) => this.web3.utils.fromDecimal.apply(this, args)
this.fromWei = (...args) => this.web3.utils.fromWei.apply(this, args)
this.toWei = (...args) => this.web3.utils.toWei.apply(this, args)
this.toBigNumber = (...args) => this.web3.utils.toBN.apply(this, args)
this.isAddress = (...args) => this.web3.utils.isAddress.apply(this, args)
this.utils = Web3.utils || []
this.sha3 = (...args) => utils.sha3.apply(this, args)
this.toHex = (...args) => utils.toHex.apply(this, args)
this.toAscii = (...args) => utils.toAscii.apply(this, args)
this.fromAscii = (...args) => utils.fromAscii.apply(this, args)
this.fromDecimal = (...args) => utils.fromDecimal.apply(this, args)
this.fromWei = (...args) => utils.fromWei.apply(this, args)
this.toWei = (...args) => utils.toWei.apply(this, args)
this.toBigNumber = (...args) => utils.toBN.apply(this, args)
this.isAddress = (...args) => utils.isAddress.apply(this, args)
this.utils = utils
this.txsMapBlock = {}
this.blocks = {}
}
@ -88,17 +87,14 @@ export class VmProxy {
setVM (vm) {
if (this.vm === vm) return
this.vm = vm
this.vm.on('step', async (data, next) => {
this.vm.evm.events.on('step', async (data: InterpreterStep) => {
await this.pushTrace(data)
next()
})
this.vm.on('afterTx', async (data, next) => {
this.vm.events.on('afterTx', async (data: any) => {
await this.txProcessed(data)
next()
})
this.vm.on('beforeTx', async (data, next) => {
this.vm.events.on('beforeTx', async (data: any) => {
await this.txWillProcess(data)
next()
})
}
@ -200,7 +196,7 @@ export class VmProxy {
this.previousDepth = 0
}
async pushTrace (data) {
async pushTrace (data: InterpreterStep) {
const depth = data.depth + 1 // geth starts the depth from 1
if (!this.processingHash) {
console.log('no tx processing')
@ -218,13 +214,12 @@ export class VmProxy {
const step = {
stack: hexListFromBNs(data.stack),
memory: formatMemory(data.memory),
storage: data.storage,
storage: {},
op: data.opcode.name,
pc: data.pc,
gasCost: data.opcode.fee.toString(),
gas: data.gasLeft.toString(),
depth: depth,
error: data.error === false ? undefined : data.error
depth: depth
}
this.vmTraces[this.processingHash].structLogs.push(step)
// Track hardhat console.log call

@ -1,18 +1,18 @@
import { BN, privateToAddress, toChecksumAddress, isValidPrivate, Address } from 'ethereumjs-util'
import Web3 from 'web3'
const Web3EthAccounts = require('web3-eth-accounts');
import * as crypto from 'crypto'
export class Accounts {
web3
export class Web3Accounts {
accounts: Record<string, unknown>
accountsKeys: Record<string, unknown>
web3Accounts: any
vmContext
constructor (vmContext) {
this.web3 = new Web3()
this.vmContext = vmContext
// TODO: make it random and/or use remix-libs
this.web3Accounts = new Web3EthAccounts()
this.accounts = {}
this.accountsKeys = {}
}
@ -98,7 +98,7 @@ export class Accounts {
if (!privateKey) {
return cb(new Error('unknown account'))
}
const account = this.web3.eth.accounts.privateKeyToAccount(privateKey)
const account = this.web3Accounts.privateKeyToAccount(privateKey as string)
const data = account.sign(message)

@ -1,4 +1,4 @@
import Web3 from 'web3'
import { toHex } from 'web3-utils'
import { VMContext } from '../vm-context'
import { bigIntToHex } from '@ethereumjs/util'
@ -51,7 +51,7 @@ export class Blocks {
blockHash: '0x' + block.hash().toString('hex'),
blockNumber: bigIntToHex(block.header.number),
from: receipt.from,
gas: Web3.utils.toHex(receipt.gas),
gas: toHex(receipt.gas),
chainId: '0xd05',
gasPrice: '0x4a817c800', // 20000000000
hash: receipt.transactionHash,
@ -105,7 +105,7 @@ export class Blocks {
blockHash: '0x' + block.hash().toString('hex'),
blockNumber: bigIntToHex(block.header.number),
from: receipt.from,
gas: Web3.utils.toHex(receipt.gas),
gas: toHex(receipt.gas),
chainId: '0xd05',
gasPrice: '0x4a817c800', // 20000000000
hash: receipt.transactionHash,

@ -1,4 +1,4 @@
import Web3 from 'web3'
import { sha3 } from 'web3-utils'
const version = require('../../package.json').version
export function methods () {
@ -39,7 +39,7 @@ export function eth_hashrate (payload, cb) {
export function web3_sha3 (payload, cb) {
const str: string = payload.params[0]
cb(null, Web3.utils.sha3(str))
cb(null, sha3(str))
}
export function eth_getCompilers (payload, cb) {

@ -1,4 +1,4 @@
import Web3 from 'web3'
import { toHex, toDecimal } from 'web3-utils'
import { toChecksumAddress, BN, Address } from 'ethereumjs-util'
import { processTx } from './txProcess'
import { execution } from '@remix-project/remix-lib'
@ -224,7 +224,7 @@ export class Transactions {
blockHash: '0x' + txBlock.hash().toString('hex'),
blockNumber: '0x' + txBlock.header.number.toString('hex'),
from: receipt.from,
gas: Web3.utils.toHex(receipt.gas),
gas: toHex(receipt.gas),
chainId: '0xd05',
// 'gasPrice': '2000000000000', // 0x123
gasPrice: '0x4a817c800', // 20000000000
@ -259,7 +259,7 @@ export class Transactions {
const txIndex = payload.params[1]
const txBlock = this.vmContext.blocks[payload.params[0]]
const txHash = '0x' + txBlock.transactions[Web3.utils.toDecimal(txIndex)].hash().toString('hex')
const txHash = '0x' + txBlock.transactions[toDecimal(txIndex)].hash().toString('hex')
this.vmContext.web3().eth.getTransactionReceipt(txHash, (error, receipt) => {
if (error) {
@ -273,7 +273,7 @@ export class Transactions {
blockHash: '0x' + txBlock.hash().toString('hex'),
blockNumber: '0x' + txBlock.header.number.toString('hex'),
from: receipt.from,
gas: Web3.utils.toHex(receipt.gas),
gas: toHex(receipt.gas),
chainId: '0xd05',
// 'gasPrice': '2000000000000', // 0x123
gasPrice: '0x4a817c800', // 20000000000
@ -304,7 +304,7 @@ export class Transactions {
const txIndex = payload.params[1]
const txBlock = this.vmContext.blocks[payload.params[0]]
const txHash = '0x' + txBlock.transactions[Web3.utils.toDecimal(txIndex)].hash().toString('hex')
const txHash = '0x' + txBlock.transactions[toDecimal(txIndex)].hash().toString('hex')
this.vmContext.web3().eth.getTransactionReceipt(txHash, (error, receipt) => {
if (error) {
@ -318,7 +318,7 @@ export class Transactions {
blockHash: '0x' + txBlock.hash().toString('hex'),
blockNumber: '0x' + txBlock.header.number.toString('hex'),
from: receipt.from,
gas: Web3.utils.toHex(receipt.gas),
gas: toHex(receipt.gas),
// 'gasPrice': '2000000000000', // 0x123
chainId: '0xd05',
gasPrice: '0x4a817c800', // 20000000000

@ -3,7 +3,7 @@ import { Blocks } from './methods/blocks'
import { info } from './utils/logs'
import merge from 'merge'
import { Accounts } from './methods/accounts'
import { Web3Accounts } from './methods/accounts'
import { Filters } from './methods/filters'
import { methods as miscMethods } from './methods/misc'
import { methods as netMethods } from './methods/net'
@ -25,7 +25,7 @@ export class Provider {
this.connected = true
this.vmContext = new VMContext(options['fork'])
this.Accounts = new Accounts(this.vmContext)
this.Accounts = new Web3Accounts(this.vmContext)
this.Transactions = new Transactions(this.vmContext)
this.methods = {}
@ -39,6 +39,7 @@ export class Provider {
}
async init () {
await this.vmContext.init()
await generateBlock(this.vmContext)
await this.Accounts.resetAccounts()
this.Transactions.init(this.Accounts.accounts)

@ -1,6 +1,5 @@
/* global ethereum */
'use strict'
import Web3 from 'web3'
import { rlp, keccak, bufferToHex } from 'ethereumjs-util'
import { execution } from '@remix-project/remix-lib'
const { LogsManager } = execution
@ -109,7 +108,7 @@ export class VMContext {
this.blockGasLimitDefault = 4300000
this.blockGasLimit = this.blockGasLimitDefault
this.currentFork = fork || 'london'
this.createVm(this.currentFork).then((vm) => this.currentVm = vm )
this.blocks = {}
this.latestBlockNumber = "0x0"
this.blockByTxHash = {}
@ -118,6 +117,10 @@ export class VMContext {
this.logsManager = new LogsManager()
}
async init () {
this.currentVm = await this.createVm(this.currentFork)
}
async createVm (hardfork) {
const stateManager = new StateManagerCommonStorageDump()
const common = new Common({ chain: 'mainnet', hardfork })
@ -143,10 +146,6 @@ export class VMContext {
return this.currentVm.web3vm
}
blankWeb3 () {
return new Web3()
}
vm () {
return this.currentVm.vm
}

@ -24,7 +24,6 @@ export class ExecutionContext {
removeProvider(name: any): void;
addProvider(network: any): void;
internalWeb3(): any;
blankWeb3(): Web3;
setContext(context: any, endPointUrl: any, confirmCb: any, infoCb: any): void;
executionContextChange(value: any, endPointUrl: any, confirmCb: any, infoCb: any, cb: any): Promise<any>;
currentblockGasLimit(): number;

Loading…
Cancel
Save