diff --git a/libs/remix-simulator/src/methods/accounts.ts b/libs/remix-simulator/src/methods/accounts.ts index 6c99565455..316ccf2b26 100644 --- a/libs/remix-simulator/src/methods/accounts.ts +++ b/libs/remix-simulator/src/methods/accounts.ts @@ -13,9 +13,11 @@ export class Web3Accounts { accounts: Record accountsKeys: Record vmContext + options - constructor (vmContext) { + constructor (vmContext, options) { this.vmContext = vmContext + this.options = options // TODO: make it random and/or use remix-libs this.accounts = {} @@ -92,6 +94,8 @@ export class Web3Accounts { eth_getBalance (payload, cb) { const address = payload.params[0] this.vmContext.vm().stateManager.getAccount(Address.fromString(address)).then((account) => { + if (!account) return cb(null, toBigInt(0).toString(10)) + if (!account.balance) return cb(null, toBigInt(0).toString(10)) cb(null, toBigInt(account.balance).toString(10)) }).catch((error) => { cb(error) @@ -114,7 +118,7 @@ export class Web3Accounts { } eth_chainId (_payload, cb) { - return cb(null, '0x539') // 0x539 is hex of 1337 + return cb(null, this.options.chainId || '0x539') // 0x539 is hex of 1337 } eth_signTypedData_v4 (payload, cb) { diff --git a/libs/remix-simulator/src/methods/miner.ts b/libs/remix-simulator/src/methods/miner.ts index 55f8767111..2d8994c2bf 100644 --- a/libs/remix-simulator/src/methods/miner.ts +++ b/libs/remix-simulator/src/methods/miner.ts @@ -12,7 +12,7 @@ export class Miner { } } - miner_start (payload, cb) {} + miner_start (payload, cb) { cb() } - miner_stop (payload, cb) {} + miner_stop (payload, cb) { cb()} } diff --git a/libs/remix-simulator/src/methods/net.ts b/libs/remix-simulator/src/methods/net.ts index 1a064719f4..3c9c7a612c 100644 --- a/libs/remix-simulator/src/methods/net.ts +++ b/libs/remix-simulator/src/methods/net.ts @@ -1,20 +1,24 @@ -export function methods (): Record { - return { - net_version: net_version, - net_listening: net_listening, - net_peerCount: net_peerCount +export class Net { + vmContext + options + + constructor (vmContext, options) { + this.vmContext = vmContext + this.options = options } -} -export function net_version (payload, cb): void { - // should be configured networkId - cb(null, 1337) -} + methods () { + return { + net_version: this.net_version.bind(this), + net_listening: this.net_listening.bind(this), + net_peerCount: this.net_peerCount.bind(this) + } + } -export function net_listening (payload, cb): void { - cb(null, true) -} + net_version (payload, cb) { cb(null, 1337) } + + net_listening (payload, cb) { cb(null, true)} -export function net_peerCount (payload, cb): void { - cb(null, 0) + net_peerCount (payload, cb) { cb(null, 0)} } + diff --git a/libs/remix-simulator/src/provider.ts b/libs/remix-simulator/src/provider.ts index af9233263a..c28257018e 100644 --- a/libs/remix-simulator/src/provider.ts +++ b/libs/remix-simulator/src/provider.ts @@ -6,7 +6,7 @@ import merge from 'merge' import { Web3Accounts } from './methods/accounts' import { Filters } from './methods/filters' import { methods as miscMethods } from './methods/misc' -import { methods as netMethods } from './methods/net' +import { Net } from './methods/net' import { Transactions } from './methods/transactions' import { Miner } from './methods/miner' import { Debug } from './methods/debug' @@ -31,6 +31,7 @@ export type JSONRPCResponseCallback = (err: Error, result?: JSONRPCResponsePaylo export type State = Record export type ProviderOptions = { + chainId?: number fork?: string, nodeUrl?: string, blockNumber?: number | 'latest', @@ -55,7 +56,7 @@ export class Provider { this.connected = true this.vmContext = new VMContext(options['fork'], options['nodeUrl'], options['blockNumber'], options['stateDb'], options['blocks']) - this.Accounts = new Web3Accounts(this.vmContext) + this.Accounts = new Web3Accounts(this.vmContext, options) this.Transactions = new Transactions(this.vmContext) this.methods = {} @@ -63,7 +64,7 @@ export class Provider { this.methods = merge(this.methods, (new Blocks(this.vmContext, options)).methods()) this.methods = merge(this.methods, miscMethods()) this.methods = merge(this.methods, (new Filters(this.vmContext)).methods()) - this.methods = merge(this.methods, netMethods()) + this.methods = merge(this.methods, (new Net(this.vmContext, options)).methods()) this.methods = merge(this.methods, this.Transactions.methods()) this.methods = merge(this.methods, (new Debug(this.vmContext)).methods()) this.methods = merge(this.methods, (new Miner(this.vmContext)).methods())