Accessing event logs
Be sure to have read the ABI parsing section first !
Accessing all the logs of a transaction can be done in multiple ways.
In the below examples, let's say you defined some abi:
const { Transfer } = abi<TransferAbi>`event Transfer(address indexed from, address indexed to, uint256 value);`;
Accesing all raw logs
If you'd like to have access to raw logs, non decoded, you can use the tx.logs()
functions, like so:
onTx(tx => {
const logs = tx.logs();
console.log(tranfers.map(x => x.topics));
})
... or you can optionally give it an HexString
representing the topic0 to only get events having the given topic:
onTx(tx => {
const transfers = tx.logs(Transfer.topic0);
console.log(tranfers.map(x => x.topics));
})
All returned logs will be in their raw form, with a topics: HexString[]
, an address: HexString
and a data: HexString
property.
Accessing all decoded logs
You can ask Bloomr to decode logs for you by giving your decoder as .logs()
argument, which will yield an array of all matching logs, in their decoded form, with an additional emittedBy: HexString
property.
onTx(tx => {
for (const t of tx.logs(Transfer)) {
emit({
tokenTransfered: t.emittedBy,
from: t.from,
to: t.to,
value: t.value,
});
//... or you could just `emit(t)`, but the above is more declarative.
}
})
Accessing only logs emitted by a given internal transaction
If you want to access the logs that has been emitted by a specific internal transaction (and not by the whole transaction), you can use tx.localLogs()
instead of tx.logs()
, which works the same.
By default, .localLogs()
will also retreive logs emitted by sub-calls performed by the current context. Not only logs emitted by the current contract.
To only retreive logs emitted in the current context (and not sub-calls, pass true
as second argument)
See the traversing transaction tree to get local logs recursively manually.