Skip to content

Commit

Permalink
RPC: add data field to RpcError / eth_call error (#3547)
Browse files Browse the repository at this point in the history
* RPC: add optional data field to RpcError

* RPC: throw error with data in eth_call

* update tests

* add tests for error code

---------

Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
ScottyPoi and holgerd77 authored Jul 26, 2024
1 parent 8441f9c commit bcb0a84
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/client/src/rpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type RpcError = {
code: number
message: string
trace?: string
data?: string
}

export function callWithStackTrace(handler: Function, debug: boolean) {
Expand All @@ -21,6 +22,7 @@ export function callWithStackTrace(handler: Function, debug: boolean) {
const e: RpcError = {
code: error.code ?? INTERNAL_ERROR,
message: error.message,
data: error.data,
}
if (debug === true) {
e['trace'] = error.stack
Expand Down
7 changes: 7 additions & 0 deletions packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,13 @@ export class Eth {
block,
}
const { execResult } = await vm.evm.runCall(runCallOpts)
if (execResult.exceptionError !== undefined) {
throw {
code: 3,
data: bytesToHex(execResult.returnValue),
message: execResult.exceptionError.error,
}
}
return bytesToHex(execResult.returnValue)
}

Expand Down
6 changes: 4 additions & 2 deletions packages/client/test/rpc/eth/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ describe(method, () => {
{ ...estimateTxData, gas: estimateTxData.gasLimit },
'latest',
])
assert.equal(res.error.code, 3, 'should return the correct error code')
assert.equal(
res.result,
res.error.data,
bytesToHex(execResult.returnValue),
'should return the correct return value',
)

res = await rpc.request(method, [{ ...estimateTxData }, 'latest'])
assert.equal(res.error.code, 3, 'should return the correct error code')
assert.equal(
res.result,
res.error.data,
bytesToHex(execResult.returnValue),
'should return the correct return value with no gas limit provided',
)
Expand Down

0 comments on commit bcb0a84

Please sign in to comment.