Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wip internal tx decoding
function deriveGas(stack) {
const gasHexString = stack[stack.length - 1]
return parseInt(gasHexString, 16)
}
function deriveAddress(stack) {
const addressRaw = stack[stack.length - 2]
return `0x${addressRaw.slice(24)}`
}
function deriveInput(op, stack, memoryArr) {
let inputStart
let inputLen
// 1 byte is two chars
if (op === 'DELEGATECALL') {
inputStart = parseInt(stack[stack.length - 3], 16) * 2
inputLen = parseInt(stack[stack.length - 4], 16) * 2
} else {
inputStart = parseInt(stack[stack.length - 4], 16) * 2
inputLen = parseInt(stack[stack.length - 5], 16) * 2
}
const memory = memoryArr.join('')
return `0x${memory.slice(inputStart, inputStart + inputLen)}`
}
function deriveValue(op, stack, lastValue) {
if (op === 'DELEGATECALL') return lastValue
return parseInt(stack[stack.length - 3], 16)
}
function decodeRawTrace({ op, stack, memory }, lastValue) {
if (op !== 'CALL' && op !== 'DELEGATECALL' && op !== 'CALLCODE') {
throw Error(`op code not supported [${op}]`)
}
const gas = deriveGas(stack)
const to = deriveAddress(stack)
const input = deriveInput(op, stack, memory)
const value = deriveValue(op, stack, lastValue)
return { gas, to, input, value, type: op }
}
function decodeInternalTransactions(traces, firstValue) {
let lastValue = firstValue
const internalTransactions = traces.map((trace) => {
const internalTransaction = decodeRawTrace(trace, lastValue)
if (trace.op !== 'DELEGATECALL') {
lastValue = internalTransaction.value
}
return internalTransaction
})
return internalTransactions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment