This is a follow up from How to identify token transfers looking at a transaction?.
I have transaction that results in a transfer of tokens.
From PancakeSwap: Router v2
To 0xd423fde466315d45fb0da457df28d5eeb92be580
For 0.005 ($2.97) Wrapped BNB (WBNB)
From 0xd423fde466315d45fb0da457df28d5eeb92be580
To 0xb54eb0cc6457e9d74e20672d8961fad3cde4511c
For 0.154823285118196657 ($2.94) Bogged Finan... (BOG)
I can access transaction logs using getTransactionReceipt
, e.g.
{
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
blockNumber: 7460735,
contractAddress: null,
cumulativeGasUsed: '39606975',
from: '0xb54eb0cc6457e9d74e20672d8961fad3cde4511c',
gasUsed: '292724',
logs: [
{
address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
topics: [
'0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c',
'0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e'
],
data: '0x0000000000000000000000000000000000000000000000000011c37937e08000',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1007,
removed: false,
id: 'log_9d934f9a'
},
{
address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e',
'0x000000000000000000000000d423fde466315d45fb0da457df28d5eeb92be580'
],
data: '0x0000000000000000000000000000000000000000000000000011c37937e08000',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1008,
removed: false,
id: 'log_f759d019'
},
{
address: '0xD7B729ef857Aa773f47D37088A1181bB3fbF0099',
topics: [
'0x63be20cc905d5ba0cf65db405c4999f99f2575e521875f13a8009d6eed7a09d9'
],
data: '0x000000000000000000000000d423fde466315d45fb0da457df28d5eeb92be580000000000000000000000000000000000000000000000000000aa739b8dd5525',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1009,
removed: false,
id: 'log_e07c5a40'
},
{
address: '0xD7B729ef857Aa773f47D37088A1181bB3fbF0099',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x000000000000000000000000d423fde466315d45fb0da457df28d5eeb92be580',
'0x000000000000000000000000b54eb0cc6457e9d74e20672d8961fad3cde4511c'
],
data: '0x00000000000000000000000000000000000000000000000002260af5206ddbb1',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1010,
removed: false,
id: 'log_4051d23e'
},
{
address: '0xD423fDe466315D45fB0da457dF28D5EEB92be580',
topics: [
'0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1'
],
data: '0x0000000000000000000000000000000000000000000000257558ffc7c1bb7c9d0000000000000000000000000000000000000000000004a14e4339975756b2ea',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1011,
removed: false,
id: 'log_382fd81b'
},
{
address: '0xD423fDe466315D45fB0da457dF28D5EEB92be580',
topics: [
'0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822',
'0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e',
'0x000000000000000000000000b54eb0cc6457e9d74e20672d8961fad3cde4511c'
],
data: '0x0000000000000000000000000000000000000000000000000011c37937e08000000000000000000000000000000000000000000000000000000aa739b8dd552600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000230b22ed94b30d7',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1012,
removed: false,
id: 'log_abf4a762'
}
],
logsBloom: '0x002002000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000020400000000000000000000000000401000000000008000080000002000000000000000000004000080000000100000000000000002000000000000000000000000000000000000100000000000000000000000000040000000000000002400010000000800000040000000000000000c0000000000000000020000000000000000000400000000000000000000000002000000000000000000020080000000000000001000000000000080000080000000000000020000000000000000000000000000400000000000000000',
status: true,
to: '0x10ed43c718714eb63d5aa57b78b54704e256024e',
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265
}
As is, events are not readable; I need to decode them, and as far as I understand, I should be using web3js web3.eth.abi.decodeLog(inputs, hexString, topics)
. However, I am not clear on how do I get the inputs
value.
As far as I understand, it needs to be ABI of the contract that produced the event, e.g. In case of event:
{
address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
topics: [
'0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c',
'0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e'
],
data: '0x0000000000000000000000000000000000000000000000000011c37937e08000',
blockNumber: 7460735,
transactionHash: '0xcf95271d63836f17c0c3a7beda7df2ea7029ce9161dc6808da86f5f9ce740830',
transactionIndex: 265,
blockHash: '0x7a3e03a2eaaf39ba85e5513205545d2509c24e8a58f03bb77d82f136f7028697',
logIndex: 1007,
removed: false,
id: 'log_9d934f9a'
}
I need to get ABI of contract 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c.
I have tried:
web3.eth.abi.decodeLog(
abi,
'0x0000000000000000000000000000000000000000000000000011c37937e08000',
[
'0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c',
'0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e',
],
);
However, this produced error:
Error: invalid type (argument="type", value="_factory", code=INVALID_ARGUMENT, version=abi/5.0.7)
It seems like I am providing wrong value.
How do I know what ABI do I use to decode this log?
Best Answer
This isn't a complete answer, but here is what I have so far.
If you look at the contract
0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c
code, it looks like it implements BEP20 interface. I don't know how to determine this programmatically, but that seems to be the case.I looked up BEP20 ABI and used it to construct
web3.eth.Contract
.I then extracted ABI tokens that describe events:
If you explore the value of
knownEventTokens
, tokens now have asignature
property:As far as I can tell, as long as the signature matches the first topic in a log event, then the
inputs
can be used to decode log event values, e.g.Which gives:
This is good enough for my use case. However, I do not understand how: