const fetch = require("node-fetch");
const url = "YOUR_CHAINSTACK_ENDPOINT"
tracer = {
retVal: [],
afterSload: false,
callStack: [],
byte2Hex: function(byte) {
if (byte < 0x10)
return "0" + byte.toString(16);
return byte.toString(16);
},
array2Hex: function(arr) {
let retVal = "";
for (let i = 0; i < arr.length; i++)
retVal += this.byte2Hex(arr[i]);
return retVal;
},
getAddr: function(log) {
return this.array2Hex(log.contract.getAddress());
},
step: function(log, db) {
let opcode = log.op.toNumber();
// SLOAD
if (opcode == 0x54) {
this.retVal.push(log.getPC() + ": SLOAD " +
this.getAddr(log) + ":" +
log.stack.peek(0).toString(16));
this.afterSload = true;
}
// SLOAD Result
if (this.afterSload) {
this.retVal.push(" Result: " +
log.stack.peek(0).toString(16));
this.afterSload = false;
}
// SSTORE
if (opcode == 0x55)
this.retVal.push(log.getPC() + ": SSTORE " +
this.getAddr(log) + ":" +
log.stack.peek(0).toString(16) + " <- " +
log.stack.peek(1).toString(16));
// End of step
},
fault: function(log, db) { this.retVal.push("FAULT: " + JSON.stringify(log)) },
result: function(ctx, db) { return this.retVal }
};
// Flatten tracer's code
function getTracerString(tracer) {
result = "{"
for (property in tracer) {
if (typeof tracer[property] == "function")
result = result + property.toString() + ": " + tracer[property]
else
result = result + property.toString() + ": " + JSON.stringify(tracer[property])
result += ","
}
result += "}"
return result.replace(/"/g, "'")
}
async function main() {
const response = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"method": "debug_traceTransaction",
"params": ["0x2f1c5c2b44f771e942a8506148e256f94f1a464babc938ae0690c6e34cd79190", {
"tracer": getTracerString(tracer)
}],
"id": 1,
"jsonrpc": "2.0"
}),
});
result = await response.json()
console.log(JSON.stringify(result));
console.log("***end***")
}
main()