Created
August 6, 2022 11:01
-
-
Save adamazad/7d27915bd9847f7b0ea1aae324f69bb3 to your computer and use it in GitHub Desktop.
Swapr subgraph: parse transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Address, ethereum, log } from '@graphprotocol/graph-ts' | |
import { Mint, Transfer } from '../types/templates/Pair/Pair' | |
/** | |
* The method ID for: | |
* `Transfer(address indexed from, address indexed to, uint256 value)` | |
*/ | |
export let TRANSFER_METHOD_ID = '0xddf252ad' | |
/** | |
* The method ID for: | |
* `Mint(address indexed sender, uint256 amount0, uint256 amount1)` | |
*/ | |
export let MINT_METHOD_ID = '0x4c209b5f' | |
/** | |
* @todo add other networks | |
*/ | |
export function getSwaprFeeReceiver(): Address { | |
return Address.fromString('0x65f29020d07A6CFa3B0bF63d749934d5A6E6ea18') | |
} | |
/** | |
* Parses the events emitted from the transtaction and returns the | |
* @param originalEvent The original event emitted from the transaction | |
*/ | |
export function parseTransactionEvents(originalEvent: ethereum.Event): ethereum.Event[] { | |
let transactionReceipt = originalEvent.receipt as ethereum.TransactionReceipt | |
// Check | |
if (!transactionReceipt) { | |
return [] | |
} | |
/** | |
* Return value | |
*/ | |
let transactionEvents: ethereum.Event[] = [] | |
// Find the TokenTransfer event from the receipt | |
let otherEventsFromPairContract: ethereum.Log[] = [] | |
let finalLogOutput = '' | |
for (let i = 0; i < transactionReceipt.logs.length; i++) { | |
let receiptLog = transactionReceipt.logs[i] | |
// Mint event | |
if (receiptLog.topics[0].toString().startsWith(MINT_METHOD_ID)) { | |
// Parse the event | |
let sender = new ethereum.EventParam( | |
'sender', | |
new ethereum.Value(ethereum.ValueKind.ADDRESS, receiptLog.topics[1].toU64()) | |
) | |
// let amount0 = receiptLog.topics[2].toString() | |
// let amount1 = receiptLog.topics[3].toString() | |
transactionEvents.push( | |
new Mint( | |
receiptLog.address, | |
receiptLog.logIndex, | |
receiptLog.transactionIndex, | |
'Mint', | |
originalEvent.block, | |
originalEvent.transaction, | |
[sender], // parameters | |
originalEvent.receipt | |
) | |
) | |
} | |
// ERC20 Transfers | |
if (receiptLog.topics[0].toString().startsWith(TRANSFER_METHOD_ID)) { | |
let from = new ethereum.EventParam( | |
'from', | |
new ethereum.Value(ethereum.ValueKind.ADDRESS, receiptLog.topics[1].toU64()) | |
) | |
let to = new ethereum.EventParam( | |
'to', | |
new ethereum.Value(ethereum.ValueKind.ADDRESS, receiptLog.topics[2].toU64()) | |
) | |
let value = new ethereum.EventParam('value', new ethereum.Value(ethereum.ValueKind.UINT, receiptLog.data.toU64())) | |
transactionEvents.push( | |
new Transfer( | |
receiptLog.address, | |
receiptLog.logIndex, | |
receiptLog.transactionIndex, | |
'Transfer', | |
originalEvent.block, | |
originalEvent.transaction, | |
[from, to, value], // parameters | |
originalEvent.receipt | |
) | |
) | |
otherEventsFromPairContract.push(receiptLog) | |
finalLogOutput.concat( | |
'\r\n' + | |
'receiptLog.topics: ' + | |
receiptLog.data.toHexString() + | |
', receiptLog.data: ' + | |
receiptLog.data.toHexString() | |
) | |
} | |
} | |
log.info(finalLogOutput, []) | |
return transactionEvents | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment