Skip to content

Instantly share code, notes, and snippets.

@Namaskar-1F64F
Last active January 4, 2023 17:50
Show Gist options
  • Save Namaskar-1F64F/349d3761d6ef4c271f7c1722ab8d97ee to your computer and use it in GitHub Desktop.
Save Namaskar-1F64F/349d3761d6ef4c271f7c1722ab8d97ee to your computer and use it in GitHub Desktop.
TheGraph Subgraph Log Inspection using Transaction Receipt
/*
This function returns an address from an event log like a Transfer event.
param receipt a transaction receipt to search through
param topic the topic hash of the event
param index the topic index of the argument to return
*/
function getAddressFromEvent(
receipt: ethereum.TransactionReceipt | null,
topic: string,
index: i32
): Address | null {
/*
The receipt property on the event contains the raw transaction receipt. This has an array of logs, which are the events emitted by the transaction.
This should always be defined because:
receipt: true
is set in the subgraph.yaml file for this event. The following check proves to assemblyscript that the receipt is defined.
*/
if (receipt) {
/*
To find the ERC20 Transfer event, check the first topic of each log. Also known as topic0.
The topic is the first 32 bytes of the keccak256 hash of the event signature. In this case, the ERC20 Transfer event signature is:
Transfer(indexed address, indexed address,uint256).
*/
for (let i = 0; i < receipt.logs.length; i++) {
/*
Since the transaction will have multiple logs (one for each event emitted), we need to loop through the
logs and check the topic of each log.
*/
const currentLog = receipt.logs[i];
if (currentLog.topics[0].toHexString() == topic) {
/*
The ERC20 Transfer event has 2 indexed parameters, so the topics array will have 3 elements.
0: The topic0, which is the ERC20 Transfer event signature hash.
1: The first indexed parameter, which is the "from" address.
2: The second indexed parameter, which is the "to" address.
Additionally, the log's data field contains the non-indexed parameters. In this case, the amount.
*/
return Address.fromBytes(
Address.fromHexString(
currentLog.topics[index].toHexString().slice(26)
)
);
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment