Skip to content

Commit

Permalink
feedback updates
Browse files Browse the repository at this point in the history
  • Loading branch information
themacexpert committed Nov 16, 2023
1 parent deb91f4 commit 038b95a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 14 deletions.
108 changes: 108 additions & 0 deletions .snippets/code/tutorials/subsquid/main-with-logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {In} from 'typeorm'
import {assertNotNull} from '@subsquid/evm-processor'
import {TypeormDatabase} from '@subsquid/typeorm-store'
import * as erc20 from './abi/erc20'
import {Account, Transfer} from './model'
import {Block, CONTRACT_ADDRESS, Log, Transaction, ProcessorContext, processor} from './processor'

processor.run(new TypeormDatabase({supportHotBlocks: true}), async (ctx) => {
ctx.log.info('Processor started');
let transfers: TransferEvent[] = []

ctx.log.info(`Processing ${ctx.blocks.length} blocks`);
for (let block of ctx.blocks) {
ctx.log.debug(`Processing block number ${block.header.height}`);
for (let log of block.logs) {
ctx.log.debug(`Processing log with address ${log.address}`);
if (log.address === CONTRACT_ADDRESS && log.topics[0] === erc20.events.Transfer.topic) {
ctx.log.info(`Transfer event found in block ${block.header.height}`);
transfers.push(getTransfer(ctx, log))
}
}
}

ctx.log.info(`Found ${transfers.length} transfers, processing...`);
await processTransfers(ctx, transfers)
ctx.log.info('Processor finished');
})

interface TransferEvent {
id: string
block: Block
transaction: Transaction
from: string
to: string
amount: bigint
}

function getTransfer(ctx: any, log: Log): TransferEvent {
let event = erc20.events.Transfer.decode(log)

let from = event.from.toLowerCase()
let to = event.to.toLowerCase()
let amount = event.value

let transaction = assertNotNull(log.transaction, `Missing transaction`)

ctx.log.debug(`Decoded transfer event: from ${from} to ${to} amount ${amount.toString()}`);
return {
id: log.id,
block: log.block,
transaction,
from,
to,
amount,
}
}

async function processTransfers(ctx: any, transfersData: TransferEvent[]) {
ctx.log.info('Starting to process transfer data');
let accountIds = new Set<string>()
for (let t of transfersData) {
accountIds.add(t.from)
accountIds.add(t.to)
}

ctx.log.debug(`Fetching accounts for ${accountIds.size} addresses`);
let accounts = await ctx.store
.findBy(Account, {id: In([...accountIds])})
.then((q: any[]) => new Map(q.map((i: any) => [i.id, i])))
ctx.log.info(`Accounts fetched, processing ${transfersData.length} transfers`);

let transfers: Transfer[] = []

for (let t of transfersData) {
let {id, block, transaction, amount} = t

let from = getAccount(accounts, t.from)
let to = getAccount(accounts, t.to)

transfers.push(
new Transfer({
id,
blockNumber: block.height,
timestamp: new Date(block.timestamp),
txHash: transaction.hash,
from,
to,
amount,
})
)
}

ctx.log.debug(`Upserting ${accounts.size} accounts`);
await ctx.store.upsert(Array.from(accounts.values()))
ctx.log.debug(`Inserting ${transfers.length} transfers`);
await ctx.store.insert(transfers)
ctx.log.info('Transfer data processing completed');
}

function getAccount(m: Map<string, Account>, id: string): Account {
let acc = m.get(id)
if (acc == null) {
acc = new Account()
acc.id = id
m.set(id, acc)
}
return acc
}
14 changes: 0 additions & 14 deletions .snippets/code/tutorials/subsquid/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@ import {Account, Transfer} from './model'
import {Block, CONTRACT_ADDRESS, Log, Transaction, ProcessorContext, processor} from './processor'

processor.run(new TypeormDatabase({supportHotBlocks: true}), async (ctx) => {
ctx.log.info('Processor started');
let transfers: TransferEvent[] = []

ctx.log.info(`Processing ${ctx.blocks.length} blocks`);
for (let block of ctx.blocks) {
ctx.log.debug(`Processing block number ${block.header.height}`);
for (let log of block.logs) {
ctx.log.debug(`Processing log with address ${log.address}`);
if (log.address === CONTRACT_ADDRESS && log.topics[0] === erc20.events.Transfer.topic) {
ctx.log.info(`Transfer event found in block ${block.header.height}`);
transfers.push(getTransfer(ctx, log))
}
}
}

ctx.log.info(`Found ${transfers.length} transfers, processing...`);
await processTransfers(ctx, transfers)
ctx.log.info('Processor finished');
})

interface TransferEvent {
Expand All @@ -44,7 +37,6 @@ function getTransfer(ctx: any, log: Log): TransferEvent {

let transaction = assertNotNull(log.transaction, `Missing transaction`)

ctx.log.debug(`Decoded transfer event: from ${from} to ${to} amount ${amount.toString()}`);
return {
id: log.id,
block: log.block,
Expand All @@ -56,18 +48,15 @@ function getTransfer(ctx: any, log: Log): TransferEvent {
}

async function processTransfers(ctx: any, transfersData: TransferEvent[]) {
ctx.log.info('Starting to process transfer data');
let accountIds = new Set<string>()
for (let t of transfersData) {
accountIds.add(t.from)
accountIds.add(t.to)
}

ctx.log.debug(`Fetching accounts for ${accountIds.size} addresses`);
let accounts = await ctx.store
.findBy(Account, {id: In([...accountIds])})
.then((q: any[]) => new Map(q.map((i: any) => [i.id, i])))
ctx.log.info(`Accounts fetched, processing ${transfersData.length} transfers`);

let transfers: Transfer[] = []

Expand All @@ -90,11 +79,8 @@ async function processTransfers(ctx: any, transfersData: TransferEvent[]) {
)
}

ctx.log.debug(`Upserting ${accounts.size} accounts`);
await ctx.store.upsert(Array.from(accounts.values()))
ctx.log.debug(`Inserting ${transfers.length} transfers`);
await ctx.store.insert(transfers)
ctx.log.info('Transfer data processing completed');
}

function getAccount(m: Map<string, Account>, id: string): Account {
Expand Down
10 changes: 10 additions & 0 deletions builders/tutorials/subsquid.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,16 @@ It may seem tricky at first to debug errors when building your Squid, but fortun
SQD_DEBUG=*
```

You can also add logging statements directly to your `main.ts` file to indicate specific parameters like block height and more. For example, see this version of `main.ts` which has been enhanced with detailed logging:

??? code "main-with-logging.ts"

```ts
--8<-- 'code/tutorials/subsquid/main-with-logging.ts'
```



See the [Subsquid guide to logging](https://docs.subsquid.io/basics/logging/){target=_blank} for more information on debug mode.


Expand Down

0 comments on commit 038b95a

Please sign in to comment.