Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track wallet txs in OP and Base #23

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@
"deps": ["build", "migration:apply"],
"cmd": ["node", "--require=dotenv/config", "lib/chains/backfill.js"]
},
"process:wallets": {
"process:eth:wallets": {
"description": "Load .env and start the Wallets squid processor",
"deps": ["build", "migration:apply"],
"cmd": ["node", "--require=dotenv/config", "lib/chains/wallets.js"]
"cmd": ["node", "--require=dotenv/config", "lib/chains/wallets/eth.js"]
},
"process:base:wallets": {
"description": "Load .env and start the Base Wallets squid processor",
"deps": ["build", "migration:apply"],
"cmd": ["node", "--require=dotenv/config", "lib/chains/wallets/base.js"]
},
"process:optimism:wallets": {
"description": "Load .env and start the Optimism Wallets squid processor",
"deps": ["build", "migration:apply"],
"cmd": ["node", "--require=dotenv/config", "lib/chains/wallets/optimism.js"]
},
"process:prod:optimism": {
"description": "Start the Optimism squid processor",
Expand All @@ -80,10 +90,20 @@
"cmd": ["node", "lib/chains/backfill.js"],
"hidden": true
},
"process:prod:wallets": {
"description": "Start the wallets squid processor",
"cmd": ["node", "lib/chains/wallets.js"],
"hidden": true
"process:prod:eth:wallets": {
"description": "Start the wallets squid processor",
"cmd": ["node", "lib/chains/wallets/eth.js"],
"hidden": true
},
"process:prod:base:wallets": {
"description": "Start the wallets squid processor",
"cmd": ["node", "lib/chains/wallets/base.js"],
"hidden": true
},
"process:prod:optimism:wallets": {
"description": "Start the wallets squid processor",
"cmd": ["node", "lib/chains/wallets/optimism.js"],
"hidden": true
},
"serve": {
"description": "Start the GraphQL API server",
Expand Down
60 changes: 0 additions & 60 deletions src/chains/wallets.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/chains/wallets/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TypeormDatabase } from '@subsquid/typeorm-store'
import { IbcProcessor } from '../../utils/ibc-processor'
import { MAX_BATCH_CALL_SIZE, VERSION } from "../constants";
import { handler } from "../../handlers/wallets";

const ADDRESSES: string[] = [
process.env.FEE_VAULT_BASE!,
process.env.RELAYER_BASE!,
]

let processor = IbcProcessor()
.setRpcEndpoint({
url: process.env.BASE_RPC!,
rateLimit: Number(process.env.RPC_RATE_LIMIT!),
maxBatchCallSize: MAX_BATCH_CALL_SIZE,
})
.setBlockRange({
from: Number(process.env.DISPATCHER_ADDRESS_BASE_START_BLOCK!),
})
.addTransaction({
from: ADDRESSES,
})
.addTransaction({
to: ADDRESSES,
})

if (process.env.BASE_TXS_GATEWAY) {
processor = processor.setGateway(process.env.BASE_TXS_GATEWAY)
}

processor.run(new TypeormDatabase({
supportHotBlocks: true,
isolationLevel: "REPEATABLE READ",
stateSchema: `base_txs_processor_${VERSION}`
}),
async (ctx) => {
await handler(ctx)
})
35 changes: 35 additions & 0 deletions src/chains/wallets/eth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TypeormDatabase } from '@subsquid/typeorm-store'
import { IbcProcessor } from '../../utils/ibc-processor'
import { MAX_BATCH_CALL_SIZE, VERSION } from "../constants";
import { handler } from "../../handlers/wallets";

const ADDRESSES: string[] = [
process.env.BATCHER_ADDRESS!,
process.env.PROPOSER_ADDRESS!,
]

let processor = IbcProcessor()
.setRpcEndpoint({
url: process.env.TXS_RPC!,
rateLimit: Number(process.env.RPC_RATE_LIMIT!),
maxBatchCallSize: MAX_BATCH_CALL_SIZE,
})
.addTransaction({
from: ADDRESSES,
})
.addTransaction({
to: ADDRESSES,
})

if (process.env.TXS_GATEWAY) {
processor = processor.setGateway(process.env.TXS_GATEWAY)
}

processor.run(new TypeormDatabase({
supportHotBlocks: true,
isolationLevel: "REPEATABLE READ",
stateSchema: `txs_processor_${VERSION}`
}),
async (ctx) => {
await handler(ctx)
})
38 changes: 38 additions & 0 deletions src/chains/wallets/optimism.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TypeormDatabase } from '@subsquid/typeorm-store'
import { IbcProcessor } from '../../utils/ibc-processor'
import { MAX_BATCH_CALL_SIZE, VERSION } from "../constants";
import { handler } from "../../handlers/wallets";

const ADDRESSES: string[] = [
process.env.FEE_VAULT_OPTIMISM!,
process.env.RELAYER_OPTIMISM!,
]

let processor = IbcProcessor()
.setRpcEndpoint({
url: process.env.OPTIMISM_RPC!,
rateLimit: Number(process.env.RPC_RATE_LIMIT!),
maxBatchCallSize: MAX_BATCH_CALL_SIZE,
})
.setBlockRange({
from: Number(process.env.DISPATCHER_ADDRESS_OPTIMISM_START_BLOCK!),
})
.addTransaction({
from: ADDRESSES,
})
.addTransaction({
to: ADDRESSES,
})

if (process.env.OPTIMISM_TXS_GATEWAY) {
processor = processor.setGateway(process.env.OPTIMISM_TXS_GATEWAY)
}
Comment on lines +11 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance error handling for processor setup.

Consider adding error handling for the processor setup, especially for RPC endpoint and block range configurations. This can prevent potential issues if the environment variables are missing or incorrect.

try {
  processor = IbcProcessor()
    .setRpcEndpoint({
      url: process.env.OPTIMISM_RPC!,
      rateLimit: Number(process.env.RPC_RATE_LIMIT!),
      maxBatchCallSize: MAX_BATCH_CALL_SIZE,
    })
    .setBlockRange({
      from: Number(process.env.DISPATCHER_ADDRESS_OPTIMISM_START_BLOCK!),
    })
    .addTransaction({
      from: ADDRESSES,
    })
    .addTransaction({
      to: ADDRESSES,
    });

  if (process.env.OPTIMISM_TXS_GATEWAY) {
    processor = processor.setGateway(process.env.OPTIMISM_TXS_GATEWAY);
  }
} catch (error) {
  console.error("Error setting up processor:", error);
  process.exit(1);
}


processor.run(new TypeormDatabase({
supportHotBlocks: true,
isolationLevel: "REPEATABLE READ",
stateSchema: `optimism_txs_processor_${VERSION}`
}),
async (ctx) => {
await handler(ctx)
})
31 changes: 31 additions & 0 deletions src/handlers/wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Context } from "../utils/types";
import { Transaction } from "../model";

export async function handler(ctx: Context) {
let chainId = Number(await ctx._chain.client.call("eth_chainId"))

let txs: Transaction[] = []
for (let block of ctx.blocks) {
for (let tx of block.transactions) {
txs.push(new Transaction({
id: tx.hash,
transactionHash: tx.hash,
blockNumber: BigInt(block.header.height),
blockTimestamp: BigInt(block.header.timestamp),
chainId: chainId,
from: tx.from,
to: tx.to,
value: BigInt(tx.value),
gas: BigInt(tx.gas),
gasPrice: BigInt(tx.gasPrice),
maxFeePerGas: tx.maxFeePerGas ? BigInt(tx.maxFeePerGas) : null,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas ? BigInt(tx.maxPriorityFeePerGas) : null,
gasUsed: BigInt(tx.gasUsed),
cumulativeGasUsed: BigInt(tx.cumulativeGasUsed),
transactionType: tx.type,
}))
Comment on lines +10 to +26
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize transaction processing.

Consider using a map or reduce function to process transactions more efficiently, especially if the number of transactions is large.

const txs = ctx.blocks.flatMap(block =>
  block.transactions.map(tx => new Transaction({
    id: tx.hash,
    transactionHash: tx.hash,
    blockNumber: BigInt(block.header.height),
    blockTimestamp: BigInt(block.header.timestamp),
    chainId: chainId,
    from: tx.from,
    to: tx.to,
    value: BigInt(tx.value),
    gas: BigInt(tx.gas),
    gasPrice: BigInt(tx.gasPrice),
    maxFeePerGas: tx.maxFeePerGas ? BigInt(tx.maxFeePerGas) : null,
    maxPriorityFeePerGas: tx.maxPriorityFeePerGas ? BigInt(tx.maxPriorityFeePerGas) : null,
    gasUsed: BigInt(tx.gasUsed),
    cumulativeGasUsed: BigInt(tx.cumulativeGasUsed),
    transactionType: tx.type,
  }))
);

}
}

await ctx.store.upsert(txs)
}
1 change: 1 addition & 0 deletions src/utils/ibc-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function IbcProcessor() {
from: true,
type: true,
value: true,
status: true,
}
})
}
Loading