Skip to content

Instantly share code, notes, and snippets.

@justinpan0
Last active August 27, 2020 06:15
Show Gist options
  • Save justinpan0/5d76e2bf0cc0e1444c3cdbff294a2b76 to your computer and use it in GitHub Desktop.
Save justinpan0/5d76e2bf0cc0e1444c3cdbff294a2b76 to your computer and use it in GitHub Desktop.
查询Conflux合约的"Error encountered during contract execution, or out of gas"失败原因
const request = require('request');
const { Conflux } = require("js-conflux-sdk");
const cfx = new Conflux({
url: "http://mainnet-jsonrpc.conflux-chain.org:12537"
});
function hex_to_ascii(str1) {
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
}
async function reason(code) {
let reason = hex_to_ascii(code.substr(138))
console.log('Reason:\t', reason ? reason : "Possibly Out of Gas")
}
async function main() {
const tx = await cfx.getTransactionByHash(process.argv[2])
let options = {
url: "http://mainnet-jsonrpc.conflux-chain.org:12537",
method: "post",
headers:
{
"content-type": "application/json"
},
body: JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "cfx_call",
"params": [{
"from": tx.from,
"to": tx.to,
"data": tx.data,
"nonce": "0x" + tx.nonce.toString(16),
"value": "0x" + tx.value.toString(16),
"gas": "0x" + tx.gas.toString(16)
}]
})
};
request(options, (error, response, body) => {
if (error) {
console.error('An error has occurred: ', error);
} else {
let res = JSON.parse(body)
if (res.error) {
console.log('Code:\t', res.error.code);
console.log('Msg:\t', res.error.message);
reason(res.error.data.substring(1, res.error.data.length - 1))
} else {
console.log('Transaction succeeded...');
console.log('Result: ', res.result);
}
}
});
}
main()
{
"name": "conflux-reason",
"version": "1.0.0",
"description": "",
"main": "conflux-reason.js",
"author": "zmpan",
"license": "MIT",
"dependencies": {
"js-conflux-sdk": "^1.0.0-alpha.2",
"request": "^2.88.2"
}
}
@justinpan0
Copy link
Author

  1. Set up node modules
npm install
  1. Execute the script with the reverted transaction's hash
node conflux-reason.js <tx hash>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment