Skip to content

Instantly share code, notes, and snippets.

@peekpi
Last active November 18, 2020 03:26
Show Gist options
  • Save peekpi/e6477d8260ffa1f555349452acd6d63f to your computer and use it in GitHub Desktop.
Save peekpi/e6477d8260ffa1f555349452acd6d63f to your computer and use it in GitHub Desktop.
trace inner transaction
  • complex.sol: the contract used for testing
  • trace.js: the js script to trace internal transactions
  • response_raw.json: raw response in json
  • response_format.txt: formated response

run node trace.js to get the inner transactions. The txhash and node url is hardcoded into the script.

curl example:

curl --request POST 'http://40.70.223.9:9500' --header 'Content-Type: application/json' --data-raw '{
    "jsonrpc": "2.0",
    "method": "debug_traceTransaction",
    "params": ["0x23ade9fdc308d41b39b81a841b208d1330a23f6bb2dcf34396a805f97f1de884", {"tracer": "callTracer"}],
    "id": 1
}'
pragma solidity 0.6.6;
contract SubContract {
function _revert(bool cond, string memory error) public pure {
require(cond, error);
}
function destruct() public {}
function deposit() payable public {}
function transfer(address payable to) payable public {
to.transfer(msg.value);
}
}
contract Entry {
SubContract public subc;
constructor() public {
subc = create();
}
function create() public returns(SubContract) {
return (new SubContract());
}
function create2() public returns(SubContract) {
subc = new SubContract{salt: blockhash(block.number-1)}();
return subc;
}
function testTransfer() payable public {
SubContract test = SubContract(create());
test.transfer{value:msg.value/2}(msg.sender);
msg.sender.transfer(msg.value/2);
SubContract test1 = SubContract(create());
test1._revert(msg.value&1 == 0, "only even");
}
}
CALL from:0x7c41e0668b551f4f902cfaec05b5bdca68b124ce to:0x19fda431a029f315821de5937ac1344811131699 value:0x3, input:0xd591221f only even
CREATE from:0x19fda431a029f315821de5937ac1344811131699 to:0x6ad1ae33f69c19064d55f38c53e4e83e2806a3e9 value:0x0, input:0x608060405234801561001057600080fd5b506102ae806100206000396000f3fe60806040526004361061003f5760003560e01c80631a695230146100445780632b68b9c614610088578063911374c81461009f578063d0e30db014610173575b600080fd5b6100866004803603602081101561005a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017d565b005b34801561009457600080fd5b5061009d6101c7565b005b3480156100ab57600080fd5b50610171600480360360408110156100c257600080fd5b81019080803515159060200190929190803590602001906401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101c9565b005b61017b610276565b005b8073ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101c3573d6000803e3d6000fd5b5050565b565b818190610271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050565b56fea2646970667358221220d1a646d4cea180383d92719fdb5eb82307a45bf3aae54de7ce18548b067a142e64736f6c63430006060033
CALL from:0x19fda431a029f315821de5937ac1344811131699 to:0x6ad1ae33f69c19064d55f38c53e4e83e2806a3e9 value:0x1, input:0x1a6952300000000000000000000000007c41e0668b551f4f902cfaec05b5bdca68b124ce
CALL from:0x6ad1ae33f69c19064d55f38c53e4e83e2806a3e9 to:0x7c41e0668b551f4f902cfaec05b5bdca68b124ce value:0x1, input:0x
CALL from:0x19fda431a029f315821de5937ac1344811131699 to:0x7c41e0668b551f4f902cfaec05b5bdca68b124ce value:0x1, input:0x
CREATE from:0x19fda431a029f315821de5937ac1344811131699 to:0xf3e12939a9746d38eca22208396f56f3f4fae099 value:0x0, input:0x608060405234801561001057600080fd5b506102ae806100206000396000f3fe60806040526004361061003f5760003560e01c80631a695230146100445780632b68b9c614610088578063911374c81461009f578063d0e30db014610173575b600080fd5b6100866004803603602081101561005a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017d565b005b34801561009457600080fd5b5061009d6101c7565b005b3480156100ab57600080fd5b50610171600480360360408110156100c257600080fd5b81019080803515159060200190929190803590602001906401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101c9565b005b61017b610276565b005b8073ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101c3573d6000803e3d6000fd5b5050565b565b818190610271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050565b56fea2646970667358221220d1a646d4cea180383d92719fdb5eb82307a45bf3aae54de7ce18548b067a142e64736f6c63430006060033
STATICCALL from:0x19fda431a029f315821de5937ac1344811131699 to:0xf3e12939a9746d38eca22208396f56f3f4fae099 value:undefined, input:0x911374c80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000096f6e6c79206576656e0000000000000000000000000000000000000000000000
{
"type": "CALL",
"from": "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce",
"to": "0x19fda431a029f315821de5937ac1344811131699",
"value": "0x3",
"gas": "0x79bfb8",
"gasUsed": "0x5941f",
"input": "0xd591221f",
"output": "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000096f6e6c79206576656e0000000000000000000000000000000000000000000000",
"error": "execution reverted",
"time": "23.070565ms",
"calls": [
{
"type": "CREATE",
"from": "0x19fda431a029f315821de5937ac1344811131699",
"to": "0x6ad1ae33f69c19064d55f38c53e4e83e2806a3e9",
"value": "0x0",
"gas": "0x775c56",
"gasUsed": "0x218a7",
"input": "0x608060405234801561001057600080fd5b506102ae806100206000396000f3fe60806040526004361061003f5760003560e01c80631a695230146100445780632b68b9c614610088578063911374c81461009f578063d0e30db014610173575b600080fd5b6100866004803603602081101561005a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017d565b005b34801561009457600080fd5b5061009d6101c7565b005b3480156100ab57600080fd5b50610171600480360360408110156100c257600080fd5b81019080803515159060200190929190803590602001906401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101c9565b005b61017b610276565b005b8073ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101c3573d6000803e3d6000fd5b5050565b565b818190610271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050565b56fea2646970667358221220d1a646d4cea180383d92719fdb5eb82307a45bf3aae54de7ce18548b067a142e64736f6c63430006060033",
"output": "0x60806040526004361061003f5760003560e01c80631a695230146100445780632b68b9c614610088578063911374c81461009f578063d0e30db014610173575b600080fd5b6100866004803603602081101561005a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017d565b005b34801561009457600080fd5b5061009d6101c7565b005b3480156100ab57600080fd5b50610171600480360360408110156100c257600080fd5b81019080803515159060200190929190803590602001906401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101c9565b005b61017b610276565b005b8073ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101c3573d6000803e3d6000fd5b5050565b565b818190610271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050565b56fea2646970667358221220d1a646d4cea180383d92719fdb5eb82307a45bf3aae54de7ce18548b067a142e64736f6c63430006060033"
},
{
"type": "CALL",
"from": "0x19fda431a029f315821de5937ac1344811131699",
"to": "0x6ad1ae33f69c19064d55f38c53e4e83e2806a3e9",
"value": "0x1",
"gas": "0x752c3a",
"gasUsed": "0x1e0b",
"input": "0x1a6952300000000000000000000000007c41e0668b551f4f902cfaec05b5bdca68b124ce",
"output": "0x",
"calls": [
{
"type": "CALL",
"from": "0x6ad1ae33f69c19064d55f38c53e4e83e2806a3e9",
"to": "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce",
"value": "0x1",
"input": "0x",
"output": "0x"
}
]
},
{
"type": "CALL",
"from": "0x19fda431a029f315821de5937ac1344811131699",
"to": "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce",
"value": "0x1",
"input": "0x",
"output": "0x"
},
{
"type": "CREATE",
"from": "0x19fda431a029f315821de5937ac1344811131699",
"to": "0xf3e12939a9746d38eca22208396f56f3f4fae099",
"value": "0x0",
"gas": "0x7475c3",
"gasUsed": "0x218a7",
"input": "0x608060405234801561001057600080fd5b506102ae806100206000396000f3fe60806040526004361061003f5760003560e01c80631a695230146100445780632b68b9c614610088578063911374c81461009f578063d0e30db014610173575b600080fd5b6100866004803603602081101561005a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017d565b005b34801561009457600080fd5b5061009d6101c7565b005b3480156100ab57600080fd5b50610171600480360360408110156100c257600080fd5b81019080803515159060200190929190803590602001906401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101c9565b005b61017b610276565b005b8073ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101c3573d6000803e3d6000fd5b5050565b565b818190610271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050565b56fea2646970667358221220d1a646d4cea180383d92719fdb5eb82307a45bf3aae54de7ce18548b067a142e64736f6c63430006060033",
"output": "0x60806040526004361061003f5760003560e01c80631a695230146100445780632b68b9c614610088578063911374c81461009f578063d0e30db014610173575b600080fd5b6100866004803603602081101561005a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061017d565b005b34801561009457600080fd5b5061009d6101c7565b005b3480156100ab57600080fd5b50610171600480360360408110156100c257600080fd5b81019080803515159060200190929190803590602001906401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101c9565b005b61017b610276565b005b8073ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501580156101c3573d6000803e3d6000fd5b5050565b565b818190610271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050565b56fea2646970667358221220d1a646d4cea180383d92719fdb5eb82307a45bf3aae54de7ce18548b067a142e64736f6c63430006060033"
},
{
"type": "STATICCALL",
"from": "0x19fda431a029f315821de5937ac1344811131699",
"to": "0xf3e12939a9746d38eca22208396f56f3f4fae099",
"gas": "0x725f1b",
"gasUsed": "0x40f",
"input": "0x911374c80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000096f6e6c79206576656e0000000000000000000000000000000000000000000000",
"error": "execution reverted"
}
]
}
const { AbiCoder } = require('@harmony-js/contract');
const fetch = require('node-fetch');
const url='http://40.70.223.9:9500'
const txhash='0x23ade9fdc308d41b39b81a841b208d1330a23f6bb2dcf34396a805f97f1de884';
async function traceTxs(txhash){
const body = `{
"jsonrpc":"2.0",
"id":0,
"method":"debug_traceTransaction",
"params":["${txhash}", {"tracer": "callTracer"}]
}`;
const msg = await fetch(url, {
"headers": {"content-type": "application/json",},
body,
"method": "POST",
});
return msg.json();
}
function revertMsg(output) {
const coder = AbiCoder();
if(output == undefined) return '';
if(!output.startsWith('0x08c379a0')) return output;
output=`0x${output.slice(10)}`
return coder.decodeParameter("string", output);
}
function showCall(call, depth){
const revert = call.error == 'execution reverted' ? revertMsg(call.output) : '';
const fmtmsg=`${' '.repeat(depth*4)} ${call.type} from:${call.from} to:${call.to} value:${call.value}, input:${call.input} ${revert}`;
console.log(fmtmsg);
if(call.calls)
call.calls.forEach(call => showCall(call, depth+1));
}
async function main(){
const resp = await traceTxs(txhash);
let depth = 0;
let call = resp.result;
//console.log(call);
showCall(call, 0)
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment