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

Postgres cache #538

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 34 additions & 21 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ const PENDING_ROLE_TYPE = "text";
export async function migrate<T>(db: Kysely<T>, schemaName: string) {
const ref = (name: string) => sql.table(`${schemaName}.${name}`);

const schema = db.withSchema(schemaName).schema;
const cacheSchema = db.withSchema("cache").schema;
const chainDataSchema = db.withSchema(schemaName).schema;

await schema
await db.schema.createSchema("cache").ifNotExists().execute();

await cacheSchema
.createTable("blocks")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("number", BIGINT_TYPE)
.addColumn("timestamp", "timestamptz")
.ifNotExists()
.execute();

await chainDataSchema
.createType("project_type")
.asEnum(["canonical", "linked"])
.execute();

await schema
await chainDataSchema
.createTable("projects")
.addColumn("id", "text")
.addColumn("name", "text")
Expand All @@ -37,7 +48,7 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addPrimaryKeyConstraint("projects_pkey", ["id", "chainId"])
.execute();

await schema
await chainDataSchema
.createTable("pending_project_roles")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("chainId", CHAIN_ID_TYPE)
Expand All @@ -46,12 +57,12 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addColumn("createdAtBlock", BIGINT_TYPE)
.execute();

await schema
await chainDataSchema
.createType("project_role_name")
.asEnum(["owner", "member"])
.execute();

await schema
await chainDataSchema
.createTable("project_roles")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("projectId", "text")
Expand All @@ -72,7 +83,7 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
)
.execute();

await schema
await chainDataSchema
.createTable("rounds")
.addColumn("id", "text")
.addColumn("chainId", CHAIN_ID_TYPE)
Expand Down Expand Up @@ -130,19 +141,19 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addPrimaryKeyConstraint("rounds_pkey", ["id", "chainId"])
.execute();

await schema
await chainDataSchema
.createIndex("idx_rounds_manager_role")
.on("rounds")
.columns(["managerRole"])
.execute();

await schema
await chainDataSchema
.createIndex("idx_rounds_admin_role")
.on("rounds")
.columns(["adminRole"])
.execute();

await schema
await chainDataSchema
.createTable("pending_round_roles")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("chainId", CHAIN_ID_TYPE)
Expand All @@ -151,12 +162,12 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addColumn("createdAtBlock", BIGINT_TYPE)
.execute();

await schema
await chainDataSchema
.createType("round_role_name")
.asEnum(["admin", "manager"])
.execute();

await schema
await chainDataSchema
.createTable("round_roles")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("roundId", "text")
Expand All @@ -177,12 +188,12 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
)
.execute();

await schema
await chainDataSchema
.createType("application_status")
.asEnum(["PENDING", "APPROVED", "REJECTED", "CANCELLED", "IN_REVIEW"])
.execute();

await schema
await chainDataSchema
.createTable("applications")
.addColumn("id", "text")
.addColumn("chainId", CHAIN_ID_TYPE)
Expand Down Expand Up @@ -218,7 +229,7 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {

.execute();

await schema
await chainDataSchema
.createTable("applications_payouts")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("chainId", CHAIN_ID_TYPE)
Expand All @@ -238,7 +249,7 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
)
.execute();

await schema
await chainDataSchema
.createTable("donations")

.addColumn("id", "text")
Expand All @@ -252,6 +263,8 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addColumn("blockNumber", BIGINT_TYPE)
.addColumn("tokenAddress", ADDRESS_TYPE)

.addColumn("timestamp", "timestamptz")

.addColumn("amount", BIGINT_TYPE)
.addColumn("amountInUSD", "real")
.addColumn("amountInRoundMatchToken", BIGINT_TYPE)
Expand All @@ -260,25 +273,25 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {

.execute();

await schema
await chainDataSchema
.createIndex("idx_donations_donor_chain")
.on("donations")
.columns(["donorAddress"])
.execute();

await schema
await chainDataSchema
.createIndex("idx_donations_chain_round")
.on("donations")
.columns(["chainId", "roundId"])
.execute();

await schema
await chainDataSchema
.createIndex("idx_donations_chain_round_app")
.on("donations")
.columns(["chainId", "roundId", "applicationId"])
.execute();

await schema
await chainDataSchema
.createTable("prices")
.addColumn("id", "serial", (cb) => cb.primaryKey())
.addColumn("chainId", CHAIN_ID_TYPE)
Expand All @@ -294,7 +307,7 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.expression(sql`chain_id, token_address, block_number DESC`)
.execute();

await schema
await chainDataSchema
.createTable("legacy_projects")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("v1ProjectId", "text")
Expand Down
1 change: 1 addition & 0 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export type DonationTable = {
amount: bigint;
amountInUsd: number;
amountInRoundMatchToken: bigint;
timestamp: Date;
};

export type NewDonation = Insertable<DonationTable>;
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createIndexer,
createSqliteCache,
createPostgresCache,
createPostgresSubscriptionStore,
createHttpRpcClient,
SubscriptionStore,
Expand Down Expand Up @@ -120,9 +120,10 @@ async function main(): Promise<void> {
});

// the chainsauce cache is used to cache events and contract reads
const chainsauceCache = config.cacheDir
? createSqliteCache(path.join(config.cacheDir, "chainsauceCache.db"))
: null;
const chainsauceCache = createPostgresCache({
connectionPool: databaseConnectionPool,
schemaName: "chainsauce_cache",
});

const priceProvider = createPriceProvider({
db,
Expand Down
1 change: 1 addition & 0 deletions src/indexer/allo/v1/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ export async function handleEvent(
amount: event.params.amount,
amountInUsd: amountInUsd,
amountInRoundMatchToken,
timestamp: conversionToUSD.timestamp,
};

return [
Expand Down
19 changes: 10 additions & 9 deletions src/indexer/allo/v2/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,15 @@ export async function handleEvent(
[`${event.blockNumber}-${event.logIndex}`]
);

const amountInUsd = (
await convertToUSD(
priceProvider,
chainId,
token,
event.params.amount,
event.blockNumber
)
).amount;
const conversionToUSD = await convertToUSD(
priceProvider,
chainId,
token,
event.params.amount,
event.blockNumber
);

const amountInUsd = conversionToUSD.amount;

let amountInRoundMatchToken: bigint | null = null;
try {
Expand Down Expand Up @@ -1058,6 +1058,7 @@ export async function handleEvent(
amount: amount,
amountInUsd,
amountInRoundMatchToken,
timestamp: conversionToUSD.timestamp,
};

return [
Expand Down
6 changes: 4 additions & 2 deletions src/prices/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function convertFromUSD(
token: Address,
amountInUSD: number,
blockNumber: bigint | "latest"
): Promise<{ amount: bigint; price: number }> {
): Promise<{ amount: bigint; price: number; timestamp: Date }> {
const closestPrice = await priceProvider.getUSDConversionRate(
chainId,
token,
Expand All @@ -39,6 +39,7 @@ export async function convertFromUSD(
tokenPriceDecimals: 8,
tokenDecimals: closestPrice.tokenDecimals,
}),
timestamp: closestPrice.timestamp,
price: 1 / closestPrice.priceInUsd, // price is the token price in USD, we return the inverse
};
}
Expand All @@ -49,7 +50,7 @@ export async function convertToUSD(
token: Address,
amount: bigint,
blockNumber: bigint | "latest"
): Promise<{ amount: number; price: number }> {
): Promise<{ amount: number; price: number; timestamp: Date }> {
const closestPrice = await priceProvider.getUSDConversionRate(
chainId,
token,
Expand All @@ -63,6 +64,7 @@ export async function convertToUSD(
tokenPrice: closestPrice.priceInUsd,
tokenPriceDecimals: 8,
}),
timestamp: closestPrice.timestamp,
price: closestPrice.priceInUsd,
};
}
Expand Down
Loading