Skip to content

Commit

Permalink
dynamic chainid
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Jan 6, 2025
1 parent 3be453f commit 2c0ae5a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
8 changes: 6 additions & 2 deletions libs/remix-simulator/src/methods/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -97,6 +99,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)
Expand All @@ -119,7 +123,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) {
Expand Down
4 changes: 2 additions & 2 deletions libs/remix-simulator/src/methods/miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
34 changes: 19 additions & 15 deletions libs/remix-simulator/src/methods/net.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
export function methods (): Record<string, unknown> {
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)}
}

7 changes: 4 additions & 3 deletions libs/remix-simulator/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand All @@ -55,15 +56,15 @@ 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 = {}
this.methods = merge(this.methods, this.Accounts.methods())
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())
Expand Down

0 comments on commit 2c0ae5a

Please sign in to comment.