Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/cspell-ts.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
}
],
"words": [
"viem",
"unerasable",
"bytelist",
"bytestring",
Expand Down
101 changes: 100 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 0 additions & 32 deletions packages/vm/examples/helpers/tx-builder.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import { AbiCoder, Interface } from 'ethers' // cspell:disable-line

import type { LegacyTxData } from '@ethereumjs/tx'

export const encodeFunction = (
method: string,
params?: {
types: any[]
values: unknown[]
},
): string => {
const parameters = params?.types ?? []
const methodWithParameters = `function ${method}(${parameters.join(',')})`
const signatureHash = new Interface([methodWithParameters]).getFunction(method)?.selector
const encodedArgs = new AbiCoder().encode(parameters, params?.values ?? [])

return signatureHash + encodedArgs.slice(2)
}

export const encodeDeployment = (
bytecode: string,
params?: {
types: any[]
values: unknown[]
},
) => {
const deploymentData = '0x' + bytecode
if (params) {
const argumentsEncoded = new AbiCoder().encode(params.types, params.values)
return deploymentData + argumentsEncoded.slice(2)
}
return deploymentData
}

export const buildTransaction = (data: Partial<LegacyTxData>): LegacyTxData => {
const defaultData: Partial<LegacyTxData> = {
nonce: BigInt(0),
Expand Down
41 changes: 29 additions & 12 deletions packages/vm/examples/run-solidity-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import { createLegacyTx } from '@ethereumjs/tx'
import { bytesToHex, createAddressFromPrivateKey, hexToBytes } from '@ethereumjs/util'
import { createVM, runTx } from '@ethereumjs/vm'
import { AbiCoder, Interface } from 'ethers'
import solc from 'solc'
import { decodeAbiParameters, encodeAbiParameters, encodeFunctionData } from 'viem'

Check warning on line 10 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L10 was not covered by tests

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

Check warning on line 13 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L13 was not covered by tests

import type { Address, PrefixedHexString } from '@ethereumjs/util'
import type { VM } from '@ethereumjs/vm'
Expand Down Expand Up @@ -100,10 +100,8 @@
// Contracts are deployed by sending their deployment bytecode to the address 0
// The contract params should be abi-encoded and appended to the deployment bytecode.

const data = encodeDeployment(deploymentBytecode, {
types: ['string'],
values: [greeting],
})
const data =
'0x' + deploymentBytecode + encodeAbiParameters([{ type: 'string' }], [greeting]).slice(2)

Check warning on line 104 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/run-solidity-contract.ts#L103-L104

Added lines #L103 - L104 were not covered by tests
const txData = {
data,
nonce: await getAccountNonce(vm, senderPrivateKey),
Expand All @@ -126,9 +124,18 @@
contractAddress: Address,
greeting: string,
) {
const data = encodeFunction('setGreeting', {
types: ['string'],
values: [greeting],
const data = encodeFunctionData({
abi: [
{
type: 'function',
name: 'setGreeting',
inputs: [{ type: 'string' }],
outputs: [],
stateMutability: 'nonpayable',
},
],
functionName: 'setGreeting',
args: [greeting],

Check warning on line 138 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/run-solidity-contract.ts#L127-L138

Added lines #L127 - L138 were not covered by tests
})

const txData = {
Expand All @@ -147,8 +154,18 @@
}

async function getGreeting(vm: VM, contractAddress: Address, caller: Address) {
const sigHash = new Interface(['function greet()']).getFunction('greet')!
.selector as PrefixedHexString
const sigHash = encodeFunctionData({
abi: [
{
type: 'function',
name: 'greet',
inputs: [],
outputs: [{ type: 'string' }],
stateMutability: 'view',
},
],
functionName: 'greet',
})

Check warning on line 168 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/run-solidity-contract.ts#L157-L168

Added lines #L157 - L168 were not covered by tests

const greetResult = await vm.evm.runCall({
to: contractAddress,
Expand All @@ -162,7 +179,7 @@
throw greetResult.execResult.exceptionError
}

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

Check warning on line 182 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L182 was not covered by tests

return results[0]
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@
"devDependencies": {
"@ethereumjs/blockchain": "^10.0.0-rc.1",
"@ethereumjs/ethash": "^10.0.0-rc.1",
"@ethereumjs/testdata": "1.0.0",
"@paulmillr/trusted-setups": "^0.1.2",
"@types/benchmark": "^2.1.5",
"@types/core-js": "^2.5.8",
"@types/minimist": "^1.2.5",
"@types/node-dir": "^0.0.37",
"benchmark": "^2.1.4",
"ethers": "^6.13.5",
"mcl-wasm": "^1.8.0",
"micro-eth-signer": "^0.14.0",
"minimist": "^1.2.8",
"node-dir": "^0.1.17",
"nyc": "^17.1.0",
"solc": "^0.8.28",
"tape": "^5.9.0",
"yargs": "^17.7.2",
"@ethereumjs/testdata": "1.0.0"
"viem": "^2.27.2",
"yargs": "^17.7.2"
},
"engines": {
"node": ">=18"
Expand Down
9 changes: 7 additions & 2 deletions packages/vm/test/api/customChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createAddressFromString,
hexToBytes,
} from '@ethereumjs/util'
import { Interface } from 'ethers'
import { encodeFunctionData } from 'viem'
import { assert, describe, it } from 'vitest'

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

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