Skip to content

Commit

Permalink
VM: add Optimism L2 block run example
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Jun 1, 2023
1 parent f37bde3 commit 48c61b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/statemanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { Account, Address } from '@ethereumjs/util'
import { EthersStateManager } from '@ethereumjs/statemanager'
import { ethers } from 'ethers'

const provider = new ethers.providers.JsonRpcProvider('https://path.to.my.provider.com')
const provider = new ethers.JsonRpcProvider('https://path.to.my.provider.com')
const stateManager = new EthersStateManager({ provider, blockTag: 500000n })
const vitalikDotEth = Address.fromString('0xd8da6bf26964af9d7eed9e03e53415d37aa96045')
const account = await stateManager.getAccount(vitalikDotEth)
Expand Down
31 changes: 31 additions & 0 deletions packages/vm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ await blockBuilder.addTransaction(tx)
const block = await blockBuilder.build()
```

### Running an Optimism L2 Block (RPC) [UNRELEASED]

Our library stack has now all building blocks in place for ad-hoc block execution via RPC on top of the correct state, using the [EthersStateManager](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/statemanager#ethersstatemanager).

The following example shows how to easily run a block from the Optimism network, where enhanced support has been introduced along the VM v7 breaking release series (Summer 2023):

```typescript
import { Block } from '@ethereumjs/block'
import { Chain, Common } from '@ethereumjs/common'
import { EthersStateManager } from '@ethereumjs/statemanager'
import { VM } from '@ethereumjs/vm'
import { ethers } from 'ethers'

const providerURL = 'https://goerli.optimism.io'
const blockNumber = 6826186n

const common = new Common({ chain: Chain.OptimismGoerli })
const block = await Block.fromJsonRpcProvider(providerURL, 6826186n, {
common,
skipTxTypes: [126], // Skip Optimism system tx type
})

const provider = new ethers.JsonRpcProvider(providerURL)
const stateManager = new EthersStateManager({ provider, blockTag: blockNumber - 1n }) // Set state to block before execution block

const vm = await VM.create({ common, stateManager })

const res = await vm.runBlock({ block, skipBlockValidation: true })
console.log(res)
```

## Example

This projects contain the following examples:
Expand Down

0 comments on commit 48c61b2

Please sign in to comment.