From 3aa93ddeb12d8c6ce88b3bde43bfb34b50e998f8 Mon Sep 17 00:00:00 2001 From: Shahul Hameed <10547529+shahthepro@users.noreply.github.com> Date: Wed, 6 Sep 2023 18:14:45 +0400 Subject: [PATCH] Differentiate Sent/Received/Swap events --- src/main.ts | 22 ++++++++++++++-------- src/processor.ts | 18 +++++------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main.ts b/src/main.ts index 6c082000..a2f9747c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,6 +7,8 @@ import * as oeth from './abi/oeth'; import { APY, Address, History, Rebase } from './model'; import { Context, OETH_ADDRESS, processor } from './processor'; +const addressZero = "0x0000000000000000000000000000000000000000" + interface RawTransfer { id: string; value: bigint; @@ -27,14 +29,16 @@ interface RawRebase { txHash: string; } +type RawLog = RawTransfer | RawRebase; + /** * Aggregate Transfer and Rebase events from the logs * * @param {Context} ctx subsquid context - * @returns {(RawTransfer|RawRebase)[]} array of Transfer and Rebase events + * @returns {(RawLog)[]} array of Transfer and Rebase events */ -function getRawLogs(ctx: Context): (RawTransfer | RawRebase)[] { - let logs: (RawTransfer | RawRebase)[] = []; +function getRawLogs(ctx: Context): (RawLog)[] { + let logs: (RawLog)[] = []; for (let block of ctx.blocks) { for (let log of block.logs) { if ( @@ -78,10 +82,10 @@ function getRawLogs(ctx: Context): (RawTransfer | RawRebase)[] { /** * Verify if the log is a Transfer event * - * @param {(RawTransfer|RawRebase)} log + * @param {(RawLog)} log * @returns {boolean} true if the log is a Transfer event */ -function isRawTransfer(log: RawTransfer | RawRebase): log is RawTransfer { +function isRawTransfer(log: RawLog): log is RawTransfer { return ( (log as RawTransfer).value !== undefined && (log as RawTransfer).from !== undefined && @@ -92,10 +96,10 @@ function isRawTransfer(log: RawTransfer | RawRebase): log is RawTransfer { /** * Verify if the log is a Rebase event * - * @param {(RawTransfer|RawRebase)} log + * @param {(RawLog)} log * @returns {boolean} true if the log is a Rebase event */ -function isRawRebase(log: RawTransfer | RawRebase): log is RawRebase { +function isRawRebase(log: RawLog): log is RawRebase { return ( (log as RawRebase).totalSupply !== undefined && (log as RawRebase).rebasingCredits !== undefined && @@ -276,6 +280,8 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => { addressSub.lastUpdated = t.timestamp; addressAdd.lastUpdated = t.timestamp; + const isSwap = [t.from, t.to].includes(addressZero); + // update the address balance await Promise.all( [addressSub, addressAdd].map(async (address) => { @@ -291,7 +297,7 @@ processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => { timestamp: t.timestamp, blockNumber: t.blockNumber, txHash: t.txHash, - type: 'Swap', + type: isSwap ? 'Swap' : (addressSub === address ? 'Sent' : 'Received'), }), ); address.credits = BigInt(credits[0]); // token credits diff --git a/src/processor.ts b/src/processor.ts index 77a2a85b..d7c7ecc6 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -12,7 +12,7 @@ import { Store } from '@subsquid/typeorm-store'; import * as oeth from './abi/oeth'; export const OETH_ADDRESS = - '0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3'.toLocaleLowerCase(); + '0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3'.toLowerCase(); export const processor = new EvmBatchProcessor() .setDataSource({ @@ -32,10 +32,13 @@ export const processor = new EvmBatchProcessor() .setFields({ transaction: { from: true, - value: true, hash: true, + gasUsed: true, + gas: true, + value: true, }, log: { + transactionHash: true, topics: true, data: true, }, @@ -43,17 +46,6 @@ export const processor = new EvmBatchProcessor() .setBlockRange({ from: 16933090, // https://etherscan.io/tx/0x3b4ece4f5fef04bf7ceaec4f6c6edf700540d7597589f8da0e3a8c94264a3b50 }) - .setFields({ - log: { - transactionHash: true, - }, - transaction: { - hash: true, - gasUsed: true, - gas: true, - value: true, - }, - }) .addLog({ address: [OETH_ADDRESS], topic0: [