Skip to content

Commit

Permalink
Merge commit '03ec619002c15b54f292cbaa1e69470bfa63088c' into add_stat…
Browse files Browse the repository at this point in the history
…e_dump_and_load_to_api
  • Loading branch information
0xNonCents committed Jan 17, 2024
2 parents b06e358 + 03ec619 commit d3d66e2
Show file tree
Hide file tree
Showing 30 changed files with 643 additions and 300 deletions.
3 changes: 2 additions & 1 deletion extensions/viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@tevm/server": "workspace:^",
"@tevm/tsconfig": "workspace:^",
"@tevm/tsupconfig": "workspace:^",
"@tevm/state": "workspace:^",
"abitype": "^0.10.3",
"viem": "^2.0.0"
},
Expand All @@ -82,4 +83,4 @@
"publishConfig": {
"access": "public"
}
}
}
75 changes: 47 additions & 28 deletions extensions/viem/src/tevmViemExtension.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
bytesToHex,
decodeFunctionResult,
encodeFunctionData,
hexToBigInt,
Expand Down Expand Up @@ -272,28 +273,28 @@ export const tevmViemExtension = () => {
}

/**
* @type {import('@tevm/api').EthChainIdHandler}
* @type {import('@tevm/api').DumpStateHandler}
*/
const chainId = async () => {
const dumpState = async () => {
return /** @type {any} */ (
formatResult(
await request({
method: 'eth_chainId',
method: 'tevm_dump_state',
jsonrpc: '2.0',
params: [],
params: {},
}),
)
)
}

/**
* @type {import('@tevm/api').EthGasPriceHandler}
* @type {import('@tevm/api').EthChainIdHandler}
*/
const gasPrice = async () => {
const chainId = async () => {
return /** @type {any} */ (
formatResult(
await request({
method: 'eth_gasPrice',
method: 'eth_chainId',
jsonrpc: '2.0',
params: [],
}),
Expand All @@ -302,75 +303,93 @@ export const tevmViemExtension = () => {
}

/**
* @type {import('@tevm/api').EthGetBalanceHandler}
* @type {import('@tevm/api').LoadStateHandler}
*/
const getBalance = async (params) => {
const loadState = async (params) => {
/**
* @type {import('@tevm/state').ParameterizedTevmState}
*/
const encodedState = {}

for (const [k, v] of Object.entries(params.state)) {
const { nonce, balance, storageRoot, codeHash } = v
//turn all bigints to hex strings
const account = {
...v,
nonce: numberToHex(nonce),
balance: numberToHex(balance),
storageRoot: bytesToHex(storageRoot),
codeHash: bytesToHex(codeHash),
}

encodedState[k] = account
}

return /** @type {any} */ (
formatResult(
await request({
method: 'eth_getBalance',
method: 'tevm_load_state',
jsonrpc: '2.0',
params: [params.address, params.blockTag ?? 'pending'],
params: { state: encodedState },
}),
)
)
}

/**
* @type {import('@tevm/api').EthGetCodeHandler}
* @type {import('@tevm/api').EthGasPriceHandler}
*/
const getCode = async (params) => {
const gasPrice = async () => {
return /** @type {any} */ (
formatResult(
await request({
method: 'eth_getCode',
method: 'eth_gasPrice',
jsonrpc: '2.0',
params: [params.address, params.tag],
params: [],
}),
)
)
}

/**
* @type {import('@tevm/api').EthGetStorageAtHandler}
* @type {import('@tevm/api').EthGetBalanceHandler}
*/
const getStorageAt = async (params) => {
const getBalance = async (params) => {
return /** @type {any} */ (
formatResult(
await request({
method: 'eth_getStorageAt',
method: 'eth_getBalance',
jsonrpc: '2.0',
params: [params.address, params.position, params.tag],
params: [params.address, params.blockTag ?? 'pending'],
}),
)
)
}

/**
* @type {import('@tevm/api').LoadStateHandler}
* @type {import('@tevm/api').EthGetCodeHandler}
*/
const loadState = async (params) => {
const getCode = async (params) => {
return /** @type {any} */ (
formatResult(
await request({
method: 'tevm_load_state',
method: 'eth_getCode',
jsonrpc: '2.0',
params,
params: [params.address, params.tag],
}),
)
)
}

/**
* @type {import('@tevm/api').DumpStateHandler}
* @type {import('@tevm/api').EthGetStorageAtHandler}
*/
const dumpState = async () => {
const getStorageAt = async (params) => {
return /** @type {any} */ (
formatResult(
await request({
method: 'tevm_dump_state',
method: 'eth_getStorageAt',
jsonrpc: '2.0',
params: undefined,
params: [params.address, params.position, params.tag],
}),
)
)
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions tevm/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ export type {
ContractJsonRpcProcedure,
ScriptJsonRpcProcedure,
AccountJsonRpcProcedure,
LoadStateJsonRpcProcedure,
DumpStateJsonRpcProcedure,
EthCallJsonRpcProcedure,
EthSignJsonRpcProcedure,
EthMiningJsonRpcProcedure,
Expand Down Expand Up @@ -394,8 +396,6 @@ export type {
AnvilImpersonateAccountProcedure,
AnvilStopImpersonatingAccountProcedure,
DebugTraceTransactionProcedure,
LoadStateJsonRpcProcedure,
DumpStateJsonRpcProcedure,
TevmJsonRpcRequestHandler,
EthJsonRpcRequestHandler,
Tevm,
Expand Down
Loading

0 comments on commit d3d66e2

Please sign in to comment.