Skip to content

Commit 336c612

Browse files
committed
replace ethers with viem in vm examples/tests
1 parent 77bf2e9 commit 336c612

File tree

5 files changed

+139
-50
lines changed

5 files changed

+139
-50
lines changed

package-lock.json

Lines changed: 100 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/vm/examples/helpers/tx-builder.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
1-
import { AbiCoder, Interface } from 'ethers' // cspell:disable-line
2-
31
import type { LegacyTxData } from '@ethereumjs/tx'
42

5-
export const encodeFunction = (
6-
method: string,
7-
params?: {
8-
types: any[]
9-
values: unknown[]
10-
},
11-
): string => {
12-
const parameters = params?.types ?? []
13-
const methodWithParameters = `function ${method}(${parameters.join(',')})`
14-
const signatureHash = new Interface([methodWithParameters]).getFunction(method)?.selector
15-
const encodedArgs = new AbiCoder().encode(parameters, params?.values ?? [])
16-
17-
return signatureHash + encodedArgs.slice(2)
18-
}
19-
20-
export const encodeDeployment = (
21-
bytecode: string,
22-
params?: {
23-
types: any[]
24-
values: unknown[]
25-
},
26-
) => {
27-
const deploymentData = '0x' + bytecode
28-
if (params) {
29-
const argumentsEncoded = new AbiCoder().encode(params.types, params.values)
30-
return deploymentData + argumentsEncoded.slice(2)
31-
}
32-
return deploymentData
33-
}
34-
353
export const buildTransaction = (data: Partial<LegacyTxData>): LegacyTxData => {
364
const defaultData: Partial<LegacyTxData> = {
375
nonce: BigInt(0),

packages/vm/examples/run-solidity-contract.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
66
import { createLegacyTx } from '@ethereumjs/tx'
77
import { bytesToHex, createAddressFromPrivateKey, hexToBytes } from '@ethereumjs/util'
88
import { createVM, runTx } from '@ethereumjs/vm'
9-
import { AbiCoder, Interface } from 'ethers'
109
import solc from 'solc'
10+
import { decodeAbiParameters, encodeAbiParameters, encodeFunctionData } from 'viem'
1111

1212
import { getAccountNonce, insertAccount } from './helpers/account-utils.ts'
13-
import { buildTransaction, encodeDeployment, encodeFunction } from './helpers/tx-builder.ts'
13+
import { buildTransaction } from './helpers/tx-builder.ts'
1414

1515
import type { Address, PrefixedHexString } from '@ethereumjs/util'
1616
import type { VM } from '@ethereumjs/vm'
@@ -100,10 +100,8 @@ async function deployContract(
100100
// Contracts are deployed by sending their deployment bytecode to the address 0
101101
// The contract params should be abi-encoded and appended to the deployment bytecode.
102102

103-
const data = encodeDeployment(deploymentBytecode, {
104-
types: ['string'],
105-
values: [greeting],
106-
})
103+
const data =
104+
'0x' + deploymentBytecode + encodeAbiParameters([{ type: 'string' }], [greeting]).slice(2)
107105
const txData = {
108106
data,
109107
nonce: await getAccountNonce(vm, senderPrivateKey),
@@ -126,9 +124,18 @@ async function setGreeting(
126124
contractAddress: Address,
127125
greeting: string,
128126
) {
129-
const data = encodeFunction('setGreeting', {
130-
types: ['string'],
131-
values: [greeting],
127+
const data = encodeFunctionData({
128+
abi: [
129+
{
130+
type: 'function',
131+
name: 'setGreeting',
132+
inputs: [{ type: 'string' }],
133+
outputs: [],
134+
stateMutability: 'nonpayable',
135+
},
136+
],
137+
functionName: 'setGreeting',
138+
args: [greeting],
132139
})
133140

134141
const txData = {
@@ -147,8 +154,18 @@ async function setGreeting(
147154
}
148155

149156
async function getGreeting(vm: VM, contractAddress: Address, caller: Address) {
150-
const sigHash = new Interface(['function greet()']).getFunction('greet')!
151-
.selector as PrefixedHexString
157+
const sigHash = encodeFunctionData({
158+
abi: [
159+
{
160+
type: 'function',
161+
name: 'greet',
162+
inputs: [],
163+
outputs: [{ type: 'string' }],
164+
stateMutability: 'view',
165+
},
166+
],
167+
functionName: 'greet',
168+
})
152169

153170
const greetResult = await vm.evm.runCall({
154171
to: contractAddress,
@@ -162,7 +179,7 @@ async function getGreeting(vm: VM, contractAddress: Address, caller: Address) {
162179
throw greetResult.execResult.exceptionError
163180
}
164181

165-
const results = new AbiCoder().decode(['string'], greetResult.execResult.returnValue)
182+
const results = decodeAbiParameters([{ type: 'string' }], greetResult.execResult.returnValue)
166183

167184
return results[0]
168185
}

packages/vm/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@
7575
"devDependencies": {
7676
"@ethereumjs/blockchain": "^10.0.0-rc.1",
7777
"@ethereumjs/ethash": "^10.0.0-rc.1",
78+
"@ethereumjs/testdata": "1.0.0",
7879
"@paulmillr/trusted-setups": "^0.1.2",
7980
"@types/benchmark": "^2.1.5",
8081
"@types/core-js": "^2.5.8",
8182
"@types/minimist": "^1.2.5",
8283
"@types/node-dir": "^0.0.37",
8384
"benchmark": "^2.1.4",
84-
"ethers": "^6.13.5",
8585
"mcl-wasm": "^1.8.0",
8686
"micro-eth-signer": "^0.14.0",
8787
"minimist": "^1.2.8",
8888
"node-dir": "^0.1.17",
8989
"nyc": "^17.1.0",
9090
"solc": "^0.8.28",
9191
"tape": "^5.9.0",
92-
"yargs": "^17.7.2",
93-
"@ethereumjs/testdata": "1.0.0"
92+
"viem": "^2.27.2",
93+
"yargs": "^17.7.2"
9494
},
9595
"engines": {
9696
"node": ">=18"

packages/vm/test/api/customChain.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
createAddressFromString,
1010
hexToBytes,
1111
} from '@ethereumjs/util'
12-
import { Interface } from 'ethers'
12+
import { encodeFunctionData } from 'viem'
1313
import { assert, describe, it } from 'vitest'
1414

1515
import { createVM, runTx } from '../../src/index.ts'
@@ -96,7 +96,12 @@ describe('VM initialized with custom state', () => {
9696
common.setHardfork(Hardfork.London)
9797
const vm = await createVM({ blockchain, common })
9898
await vm.stateManager.generateCanonicalGenesis!(genesisState)
99-
const calldata = new Interface(['function retrieve()']).getFunction('retrieve')!.selector
99+
const calldata = encodeFunctionData({
100+
abi: [
101+
{ type: 'function', name: 'retrieve', inputs: [], outputs: [], stateMutability: 'view' },
102+
],
103+
functionName: 'retrieve',
104+
})
100105

101106
const callResult = await vm.evm.runCall({
102107
to: createAddressFromString(contractAddress),

0 commit comments

Comments
 (0)