This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
124 lines (105 loc) · 3.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable no-console */
const Web3 = require('web3')
const kovanPOA = 'https://kovan.poa.network'
const kovanInfura = 'https://kovan.infura.io/v3/c7463beadf2144e68646ff049917b716'
const mainnetInfura = 'https://mainnet.infura.io/v3/c7463beadf2144e68646ff049917b716'
const rinkebyInfura = 'https://rinkeby.infura.io/v3/c7463beadf2144e68646ff049917b716'
const mainnetTrust = 'http://ethereum-rpc.trustwalletapp.com'
const mainnetMEW = 'https://api.mycryptoapi.com/eth'
const localhost = 'http://localhost:8545'
const web3 = new Web3(mainnetTrust)
if (process.env.PARITY) {
web3.eth.extend({
property: 'parity',
methods: [
{
name: 'traceReplayTransaction',
call: 'trace_replayTransaction',
params: 2,
},
],
})
}
async function getResult({ hash, to, input, from, value, gas, gasPrice, blockNumber }) {
// Parity call to get result
if (process.env.PARITY) {
return (await web3.eth.parity.traceReplayTransaction(hash, ['trace'])).output
}
// Geth call to get result
return web3.eth.call(
{
to,
data: input,
from,
value,
gas,
gasPrice,
},
blockNumber
)
}
async function main() {
if (process.argv.length < 3) {
console.error('Missing transaction hash argument which must be a 32 byte hex string with a 0x prefix which is 64 characters in total.')
process.exit(1)
}
const txHash = process.argv[2]
if (!txHash.match(/^0x([A-Fa-f0-9]{64})$/)) {
console.error('Invalid transaction hash argument. Must be a 32 byte hex string with a 0x prefix which is 64 characters in total.')
process.exit(2)
}
const receipt = await web3.eth.getTransactionReceipt(txHash)
if (!receipt) {
console.error('Could not get transaction receipt. Are you sure it was mined?')
process.exit(3)
}
if (receipt.status) {
console.error('Transaction did not fail. Can only read the revert reason from failed transactions')
process.exit(3)
}
const transaction = await web3.eth.getTransaction(txHash)
if (receipt.gasUsed === transaction.gas) {
console.error('Transaction failed as it ran out of gas.')
process.exit(4)
}
let rawMessageData
try {
const result = await getResult(transaction)
console.log('RAW result:', result)
// Trim the 0x prefix
rawMessageData = result.slice(2)
} catch (e) {
console.log('RAW error message:', e.message)
if (e.message.startsWith('Node error: ')) {
// Trim "Node error: "
const errorObjectStr = e.message.slice(12)
// Parse the error object
const errorObject = JSON.parse(errorObjectStr)
if (!errorObject.data) {
throw Error('Failed to parse data field error object:' + errorObjectStr)
}
if (errorObject.data.startsWith('Reverted 0x')) {
// Trim "Reverted 0x" from the data field
rawMessageData = errorObject.data.slice(11)
}
else if (errorObject.data.startsWith('0x')) {
// Trim "0x" from the data field
rawMessageData = errorObject.data.slice(2)
}
else {
throw Error('Failed to parse data field error object:' + errorObjectStr)
}
}
else {
throw Error('Failed to parse error message from Ethereum call: ' + e.message)
}
}
// Get the length of the revert reason
const strLen = parseInt(rawMessageData.slice(8 + 64, 8 + 128), 16)
// Using the length and known offset, extract and convert the revert reason
const reasonCodeHex = rawMessageData.slice(8 + 128, 8 + 128 + (strLen * 2))
// Convert reason from hex to string
const reason = web3.utils.hexToAscii('0x' + reasonCodeHex)
console.log('\nDecoded reason: "' + reason + '"')
}
main()