-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: add factory mode and pglite persistence (#123)
- add `@electric-sql/pglite` as external dependency in rollup for cli - add pglite persistence in example - add factory mode example
- Loading branch information
Showing
5 changed files
with
83 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ dist | |
.turbo | ||
bun.lockb | ||
.vscode | ||
.apibara | ||
.apibara | ||
.persistence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "cli: add @electric-sql/pglite as external dependency", | ||
"packageName": "apibara", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { defineIndexer } from "@apibara/indexer"; | ||
import { useLogger } from "@apibara/indexer/plugins/logger"; | ||
import { StarknetStream } from "@apibara/starknet"; | ||
import type { ApibaraRuntimeConfig } from "apibara/types"; | ||
import { hash } from "starknet"; | ||
|
||
const PAIR_CREATED = hash.getSelectorFromName("PairCreated") as `0x${string}`; | ||
const SWAP = hash.getSelectorFromName("Swap") as `0x${string}`; | ||
const shortAddress = (addr?: string) => | ||
addr ? `${addr.slice(0, 6)}...${addr.slice(-4)}` : ""; | ||
|
||
export default function (runtimeConfig: ApibaraRuntimeConfig) { | ||
return defineIndexer(StarknetStream)({ | ||
streamUrl: "https://starknet.preview.apibara.org", | ||
finality: "accepted", | ||
startingCursor: { | ||
orderKey: 800_000n, | ||
}, | ||
filter: { | ||
header: "always", | ||
events: [ | ||
{ | ||
address: | ||
"0x00dad44c139a476c7a17fc8141e6db680e9abc9f56fe249a105094c44382c2fd", | ||
keys: [PAIR_CREATED], | ||
}, | ||
], | ||
}, | ||
async factory({ block: { events } }) { | ||
const logger = useLogger(); | ||
|
||
const poolEvents = (events ?? []).flatMap((event) => { | ||
const pairAddress = event.data?.[2]; | ||
|
||
logger.log( | ||
"Factory: PairAddress : ", | ||
`\x1b[35m${pairAddress}\x1b[0m`, | ||
); | ||
return { | ||
address: pairAddress, | ||
keys: [SWAP], | ||
}; | ||
}); | ||
return { | ||
filter: { | ||
events: poolEvents, | ||
}, | ||
}; | ||
}, | ||
async transform({ block, endCursor }) { | ||
const logger = useLogger(); | ||
const { events } = block; | ||
|
||
logger.log("Transforming... : ", endCursor?.orderKey); | ||
for (const event of events) { | ||
logger.log( | ||
"Event Address : ", | ||
shortAddress(event.address), | ||
"| Txn hash :", | ||
shortAddress(event.transactionHash), | ||
); | ||
} | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters