import { solanaInstructionDecoder, solanaPortalStream } from '@subsquid/pipes/solana'
import { chunkForInsert, drizzleTarget } from '@subsquid/pipes/targets/drizzle/node-postgres'
import { drizzle } from 'drizzle-orm/node-postgres'
import { bigint, integer, pgTable, primaryKey, varchar } from 'drizzle-orm/pg-core'
import * as orcaWhirlpool from './abi/orca_whirlpool/index.js'
const swapsTable = pgTable('swaps', {
slot: bigint({ mode: 'bigint' }).notNull(),
transactionIndex: integer().notNull(),
instructionAddress: varchar().notNull(),
programId: varchar().notNull(),
}, (t) => [primaryKey({ columns: [t.slot, t.transactionIndex, t.instructionAddress] })])
await solanaPortalStream({
id: 'orca-swaps-drizzle',
portal: 'https://portal.sqd.dev/datasets/solana-mainnet',
outputs: solanaInstructionDecoder({
range: { from: '340000000' },
programId: orcaWhirlpool.programId,
instructions: { swap: orcaWhirlpool.instructions.swap },
}),
}).pipeTo(
drizzleTarget({
db: drizzle('postgresql://postgres:postgres@localhost:5432/postgres'),
tables: [swapsTable],
onData: async ({ tx, data }) => {
for (const batch of chunkForInsert(data.swap)) {
await tx.insert(swapsTable).values(
batch.map((d) => ({
slot: BigInt(d.block.number),
transactionIndex: d.rawInstruction.transactionIndex,
instructionAddress: d.rawInstruction.instructionAddress.join('.'),
programId: d.programId,
})),
)
}
},
}),
)