Last active
January 8, 2020 04:00
-
-
Save liamaharon/ed9ede5eb93367e731d5ec7b795995af to your computer and use it in GitHub Desktop.
wip internal tx decoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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