Skip to content

Commit

Permalink
fix: update @ethereumjs/* pkgs, update context
Browse files Browse the repository at this point in the history
  • Loading branch information
hangleang committed Jul 31, 2024
1 parent 23aa697 commit 2af2edc
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 360 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ public
@/
tailwind.config.js
.eslintrc.js

# monorepo
ethereumjs-monorepo
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/*.mdx
pnpm-lock.yaml
ethereumjs-monorepo
16 changes: 16 additions & 0 deletions build_monorepo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
set -e

# start building packages
echo "Installing dependencies"
cd ethereumjs-monorepo
npm i

echo "Add @ethereumjs/* packages"
cd ..
pnpm add ethereumjs-monorepo/packages/block
pnpm add ethereumjs-monorepo/packages/common
pnpm add ethereumjs-monorepo/packages/evm
pnpm add ethereumjs-monorepo/packages/tx
pnpm add ethereumjs-monorepo/packages/util
pnpm add ethereumjs-monorepo/packages/vm
4 changes: 2 additions & 2 deletions components/Editor/SolidityAdvanceModeTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
} from 'react'

import { EvmError } from '@ethereumjs/evm/src/exceptions'
import { Address } from '@ethereumjs/util'
import { Address, createAddressFromString } from '@ethereumjs/util'
import abi from 'ethereumjs-abi'
import { BN, bufferToHex } from 'ethereumjs-util'
import Select, { OnChangeValue } from 'react-select'
Expand Down Expand Up @@ -206,7 +206,7 @@ const SolidityAdvanceModeTab: FC<Props> = ({
const transaction = await transactionData(
data,
getCallValue(),
Address.fromString(deployedContractAddress),
createAddressFromString(deployedContractAddress),
)
if (!transaction) {
return
Expand Down
50 changes: 23 additions & 27 deletions context/ethereumContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ import { Buffer } from 'buffer'

import React, { createContext, useEffect, useState, useRef } from 'react'

import { Block } from '@ethereumjs/block'
import { createBlock } from '@ethereumjs/block'
import { Common, Chain, HardforkTransitionConfig } from '@ethereumjs/common'
import {
EVM,
EvmError,
getActivePrecompiles,
InterpreterStep,
createEVM,
} from '@ethereumjs/evm'
import { RunState } from '@ethereumjs/evm/dist/cjs/interpreter'
import { Opcode, OpcodeList } from '@ethereumjs/evm/src/opcodes'
import { TypedTransaction, TxData, TransactionFactory } from '@ethereumjs/tx'
import { Address, Account, bytesToHex } from '@ethereumjs/util'
import { VM } from '@ethereumjs/vm'
import { TypedTransaction, TxData, createTxFromTxData } from '@ethereumjs/tx'
import {
Address,
bytesToHex,
createAccount,
createAddressFromPrivateKey,
createContractAddress,
} from '@ethereumjs/util'
import { VM, runTx } from '@ethereumjs/vm'
import OpcodesMeta from 'opcodes.json'
import PrecompiledMeta from 'precompiled.json'
import {
Expand Down Expand Up @@ -45,8 +52,8 @@ const privateKey = Buffer.from(
'hex',
)
const accountBalance = 18 // 1eth
const accountAddress = Address.fromPrivateKey(privateKey)
const contractAddress = Address.generate(accountAddress, 1n)
const accountAddress = createAddressFromPrivateKey(privateKey)
const contractAddress = createContractAddress(accountAddress, 1n)
const gasLimit = 0xffffffffffffn
const postMergeHardforkNames: Array<string> = ['merge', 'shanghai', 'cancun']
export const prevrandaoDocName = '44_merge'
Expand Down Expand Up @@ -185,7 +192,7 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {

vm = await VM.create({ common })

const evm = await EVM.create({
const evm = await createEVM({
common,
})
currentOpcodes = evm.getActiveOpcodes()
Expand Down Expand Up @@ -255,11 +262,11 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {
value: value,
gasLimit,
gasPrice: 10,
data: '0x' + data,
data: `0x${data}` as `0x${string}`,
nonce: account?.nonce,
}

return TransactionFactory.fromTxData(txData).sign(privateKey)
return createTxFromTxData(txData).sign(privateKey)
}

/**
Expand Down Expand Up @@ -315,10 +322,7 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {
value: bigint,
data: string,
) => {
vm.stateManager.putContractCode(
contractAddress,
Buffer.from(byteCode, 'hex'),
)
vm.stateManager.putCode(contractAddress, Buffer.from(byteCode, 'hex'))
transientStorageMemory.clear()
startTransaction(await transactionData(data, value, contractAddress))
}
Expand All @@ -334,8 +338,7 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {
setVmError(undefined)

// starting execution via deployed contract's transaction
return vm
.runTx({ tx: tx as TypedTransaction, block: _getBlock() })
return runTx(vm, { tx: tx as TypedTransaction, block: _getBlock() })
.then(({ execResult, totalGasSpent, createdAddress }) => {
_loadRunState({
totalGasSpent,
Expand Down Expand Up @@ -624,9 +627,8 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {
if (evm instanceof EVM) {
// Storage handler
const proxyStateManager = traceStorageMethodCalls(evm.stateManager)
evm.stateManager.putContractStorage = proxyStateManager.putContractStorage
evm.stateManager.clearContractStorage =
proxyStateManager.clearContractStorage
evm.stateManager.putStorage = proxyStateManager.putContractStorage
evm.stateManager.clearStorage = proxyStateManager.clearContractStorage
// Transient storage handler
const transientStorageMethodProxy = traceTransientStorageMethodCalls(
evm.transientStorage,
Expand All @@ -648,14 +650,8 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {
nonce: 0,
balance: 0,
}
vm.stateManager.putAccount(
accountAddress,
Account.fromAccountData(accountData),
)
vm.stateManager.putAccount(
contractAddress,
Account.fromAccountData(contractData),
)
vm.stateManager.putAccount(accountAddress, createAccount(accountData))
vm.stateManager.putAccount(contractAddress, createAccount(contractData))
}

const _loadRunState = ({
Expand Down Expand Up @@ -696,7 +692,7 @@ export const EthereumProvider: React.FC<{}> = ({ children }) => {
return undefined
}

return Block.fromBlockData(
return createBlock(
{
header: {
baseFeePerGas: 10,
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"build:solcjs-worker": "npx -y browserify -t babelify ./babel/solcWorker2.js > ./public/solcWorker.js",
"dev": "pnpm run build:solcjs-worker && next dev",
"format": "prettier --write --cache .",
"preinstall": "git submodule update --init",
"postinstall": "./build_monorepo.sh",
"lint": "eslint --ext js,jsx,ts,tsx .",
"lint:package": "sort-package-json",
"prepare": "husky",
Expand All @@ -18,12 +20,12 @@
"@babel/core": "^7.0.0",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@ethereumjs/block": "^5.2.0",
"@ethereumjs/common": "^4.3.0",
"@ethereumjs/evm": "^3.0.0",
"@ethereumjs/tx": "^5.2.1",
"@ethereumjs/util": "^9.0.2",
"@ethereumjs/vm": "^8.0.0",
"@ethereumjs/block": "link:ethereumjs-monorepo/packages/block",
"@ethereumjs/common": "link:ethereumjs-monorepo/packages/common",
"@ethereumjs/evm": "link:ethereumjs-monorepo/packages/evm",
"@ethereumjs/tx": "link:ethereumjs-monorepo/packages/tx",
"@ethereumjs/util": "link:ethereumjs-monorepo/packages/util",
"@ethereumjs/vm": "link:ethereumjs-monorepo/packages/vm",
"@kunigi/string-compression": "1.0.2",
"@mdx-js/loader": "^2.3.0",
"@mdx-js/react": "^2.3.0",
Expand All @@ -37,6 +39,7 @@
"classnames": "^2.3.1",
"clsx": "^2.0.0",
"copy-to-clipboard": "^3.3.1",
"crypto-browserify": "^3.12.0",
"ethereumjs-abi": "^0.6.8",
"ethereumjs-util": "^7.1.5",
"events": "^3.3.0",
Expand Down
Loading

0 comments on commit 2af2edc

Please sign in to comment.