Skip to content

Commit

Permalink
Adds calldata decoding task
Browse files Browse the repository at this point in the history
  • Loading branch information
raiseerco committed Sep 24, 2024
1 parent b68d511 commit 0eef392
Show file tree
Hide file tree
Showing 2 changed files with 406 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/ethernaut-util/src/tasks/calldata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { ethers } = require('ethers')
const types = require('ethernaut-common/src/validation/types')
const output = require('ethernaut-common/src/ui/output')
const EthernautCliError = require('ethernaut-common/src/error/error')

require('../scopes/util')
.task('calldata', 'Decodes calldata of a tx')
.addPositionalParam(
'transactionId',
'The transaction to lookup',
undefined,
types.string,
)
.addPositionalParam(
'abi',
'The ABI of the contract to decode the calldata',
undefined,
types.array,
)
.setAction(async ({ transactionId, abi }, hre) => {
try {
const tx = await hre.ethers.provider.getTransaction(transactionId)

if (!tx) {
throw new EthernautCliError('ethernaut-network', 'No tx found.', false)
}

const calldata = tx.data
const iface = new ethers.utils.Interface(abi)
const decodedData = iface.parseTransaction({ data: calldata })

const callData = {
functionName: decodedData.name,
args: decodedData.args,
}

return output.resultBox(callData)
} catch (err) {
return output.errorBox(err)
}
})
Loading

0 comments on commit 0eef392

Please sign in to comment.