dynamic chainid

remix_simulator
yann300 2 months ago
parent 36e20332b6
commit e2a09537a0
  1. 8
      libs/remix-simulator/src/methods/accounts.ts
  2. 4
      libs/remix-simulator/src/methods/miner.ts
  3. 32
      libs/remix-simulator/src/methods/net.ts
  4. 7
      libs/remix-simulator/src/provider.ts

@ -13,9 +13,11 @@ export class Web3Accounts {
accounts: Record<string, AccountType>
accountsKeys: Record<string, string>
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) {

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

@ -1,20 +1,24 @@
export function methods (): Record<string, unknown> {
export class Net {
vmContext
options
constructor (vmContext, options) {
this.vmContext = vmContext
this.options = options
}
methods () {
return {
net_version: net_version,
net_listening: net_listening,
net_peerCount: net_peerCount
net_version: this.net_version.bind(this),
net_listening: this.net_listening.bind(this),
net_peerCount: this.net_peerCount.bind(this)
}
}
}
export function net_version (payload, cb): void {
// should be configured networkId
cb(null, 1337)
}
net_version (payload, cb) { cb(null, 1337) }
export function net_listening (payload, cb): void {
cb(null, true)
}
net_listening (payload, cb) { cb(null, true)}
export function net_peerCount (payload, cb): void {
cb(null, 0)
net_peerCount (payload, cb) { cb(null, 0)}
}

@ -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<string, string>
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())

Loading…
Cancel
Save