Skip to content

Commit

Permalink
codegen for SwapContract
Browse files Browse the repository at this point in the history
  • Loading branch information
WooSungD committed Oct 16, 2023
1 parent 2eaf772 commit 3e13308
Show file tree
Hide file tree
Showing 9 changed files with 1,083 additions and 277 deletions.
692 changes: 692 additions & 0 deletions abi/SwapContract.json

Large diffs are not rendered by default.

450 changes: 260 additions & 190 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
},
"dependencies": {
"@ethersproject/abi": "^5.7.0",
"@subsquid/archive-registry": "^3.1.0",
"@subsquid/archive-registry": "^3.3.0",
"@subsquid/big-decimal": "^1.0.0",
"@subsquid/evm-processor": "^1.7.0",
"@subsquid/graphql-server": "4.2.0",
"@subsquid/typeorm-migration": "1.2.0",
"@subsquid/typeorm-store": "1.2.0",
"@subsquid/evm-processor": "^1.8.2",
"@subsquid/graphql-server": "4.3.0",
"@subsquid/typeorm-migration": "1.2.1",
"@subsquid/typeorm-store": "1.2.2",
"dotenv": "^16.3.1",
"ethers": "^6.6.0",
"pg": "^8.11.0",
"typeorm": "^0.3.17"
},
"devDependencies": {
"@subsquid/evm-typegen": "3.2.0",
"@subsquid/typeorm-codegen": "1.2.0",
"@subsquid/evm-typegen": "3.2.2",
"@subsquid/typeorm-codegen": "1.3.1",
"@typechain/ethers-v6": "^0.4.0",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.60.0",
Expand Down
17 changes: 12 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
type Gravatar @entity {
type Swap @entity {
id: ID!
owner: Bytes!
displayName: String!
imageUrl: String!
}
sender: Bytes! # address
recipient: Bytes! # address
amount0: BigInt! # int256
amount1: BigInt! # int256
sqrtPriceX96: BigInt! # uint160
liquidity: BigInt! # uint128
tick: Int! # int24
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
}
87 changes: 52 additions & 35 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
import { TypeormDatabase } from '@subsquid/typeorm-store'
import { decodeHex } from '@subsquid/evm-processor'
import { events } from './abi/Gravity'
import { ethers } from 'ethers'
import { Gravatar } from './model'
import { processor, GRAVATAR_CONTRACT } from './processor'
// import { TypeormDatabase } from "@subsquid/typeorm-store";
// import { decodeHex } from "@subsquid/evm-processor";
// import { events } from "./abi/SwapContract";
// import { ethers } from "ethers";
// import { Gravatar, Swap } from "./model";
// import { processor, SWAP_CONTRACT } from "./processor";

processor.run(new TypeormDatabase({supportHotBlocks: true}), async (ctx) => {
const gravatars: Map<string, Gravatar> = new Map()
for (const c of ctx.blocks) {
for (const e of c.logs) {
if (!(e.address === GRAVATAR_CONTRACT &&
(e.topics[0] === events.NewGravatar.topic ||
e.topics[0] === events.UpdatedGravatar.topic))) continue
const { id, owner, displayName, imageUrl } = extractData(e)
let idString = '0x' + id.toString(16)
gravatars.set(idString, new Gravatar({
id: idString,
owner: decodeHex(owner),
displayName,
imageUrl
}))
}
}
await ctx.store.upsert([...gravatars.values()])
})
// processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
// const gravatars: Map<string, Gravatar> = new Map();
// const swaps: Map<string, Swap> = new Map();
// for (const c of ctx.blocks) {
// for (const e of c.logs) {
// if (!(e.address === SWAP_CONTRACT && e.topics[0] === events.Swap.topic))
// continue;
// // extract information from the event
// const { recipient, sender, amount0, amount1 } = extractData(e);
// let idString = "0x" + id.toString(16);
// // create the swap entity
// gravatars.set(
// idString,
// new Gravatar({
// id: idString,
// owner: decodeHex(owner),
// displayName,
// imageUrl,
// })
// );
// swaps.set(
// idString,
// new Swap({
// id: idString,
// blockNumber: c.number,
// blockTimestamp: c.timestamp,
// owner: decodeHex(owner),
// displayName,
// imageUrl,
// });
// );
// }
// }
// await ctx.store.upsert([...swaps.values()]);
// });


function extractData(evmLog: any): { id: bigint, owner: string, displayName: string, imageUrl: string} {
if (evmLog.topics[0] === events.NewGravatar.topic) {
return events.NewGravatar.decode(evmLog)
}
if (evmLog.topics[0] === events.UpdatedGravatar.topic) {
return events.UpdatedGravatar.decode(evmLog)
}
throw new Error('Unsupported topic')
}
// function extractData(evmLog: any): {
// id: bigint;
// owner: string;
// displayName: string;
// imageUrl: string;
// } {
// if (evmLog.topics[0] === events.Swap.topic) {
// return events.Swap.decode(evmLog);
// }
// throw new Error("Unsupported topic");
// }
20 changes: 0 additions & 20 deletions src/model/generated/gravatar.model.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/model/generated/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./gravatar.model"
export * from "./swap.model"
42 changes: 42 additions & 0 deletions src/model/generated/swap.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_} from "typeorm"
import * as marshal from "./marshal"

@Entity_()
export class Swap {
constructor(props?: Partial<Swap>) {
Object.assign(this, props)
}

@PrimaryColumn_()
id!: string

@Column_("bytea", {nullable: false})
sender!: Uint8Array

@Column_("bytea", {nullable: false})
recipient!: Uint8Array

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
amount0!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
amount1!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
sqrtPriceX96!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
liquidity!: bigint

@Column_("int4", {nullable: false})
tick!: number

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
blockNumber!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
blockTimestamp!: bigint

@Column_("bytea", {nullable: false})
transactionHash!: Uint8Array
}
36 changes: 17 additions & 19 deletions src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { EvmBatchProcessor} from '@subsquid/evm-processor'
import { events } from './abi/Gravity'
import { lookupArchive } from '@subsquid/archive-registry'
// import { EvmBatchProcessor } from "@subsquid/evm-processor";
// import { events } from "./abi/SwapContract";
// import { lookupArchive } from "@subsquid/archive-registry";

export const GRAVATAR_CONTRACT = '0x2E645469f354BB4F5c8a05B3b30A929361cf77eC'.toLowerCase()

export const processor = new EvmBatchProcessor()
.setDataSource({
archive: lookupArchive('eth-mainnet'),
chain: 'https://rpc.ankr.com/eth'
})
.setBlockRange({ from: 6175243 })
.setFinalityConfirmation(75)
.addLog({
address: [ GRAVATAR_CONTRACT ],
topic0: [
events.NewGravatar.topic,
events.UpdatedGravatar.topic,
],
})
// export const SWAP_CONTRACT =
// "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".toLowerCase();

// export const processor = new EvmBatchProcessor()
// .setDataSource({
// archive: lookupArchive("eth-mainnet"),
// chain:
// "https://ethereum-mainnet.core.chainstack.com/4f684a0bcb02b58f9a2fd403d913783b",
// })
// .setBlockRange({ from: 12_376_729 })
// .setFinalityConfirmation(75)
// .addLog({
// address: [SWAP_CONTRACT],
// topic0: [events.Swap.topic],
// });

0 comments on commit 3e13308

Please sign in to comment.