Skip to content

Commit

Permalink
index match amount and funded amount (#471)
Browse files Browse the repository at this point in the history
* index match amount and funded amount in indexer

* handle 0xe native token

* fix test

* update test

---------

Co-authored-by: Aditya Anand M C <[email protected]>
  • Loading branch information
boudra and thelostone-mc authored Mar 6, 2024
1 parent ef4caf4 commit 5368db6
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 10 deletions.
15 changes: 14 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ToBlock } from "chainsauce";
import { z } from "zod";
import path from "node:path";
import abis from "./indexer/abis/index.js";
import { Hex } from "./types.js";
import { Address, Hex } from "./types.js";
import os from "node:os";

type ChainId = number;
Expand Down Expand Up @@ -1306,6 +1306,19 @@ export const getChainConfigById = (chainId: ChainId): Chain => {
return chain;
};

export const getTokenForChain = (
chainId: ChainId,
tokenAddress: Address
): Token | null => {
const chain = getChainConfigById(chainId);

const token = chain.tokens.find(
(t) => t.address.toLowerCase() === tokenAddress.toLowerCase()
);

return token ?? null;
};

export type Config = {
buildTag: string | null;
storageDir: string;
Expand Down
9 changes: 8 additions & 1 deletion src/database/changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type DataChange =
| {
type: "UpdateRound";
chainId: ChainId;
roundId: Address | string;
roundId: string;
round: PartialRound;
}
| {
Expand All @@ -67,6 +67,13 @@ export type DataChange =
strategyAddress: Address;
round: PartialRound;
}
| {
type: "IncrementRoundFundedAmount";
chainId: ChainId;
roundId: string;
fundedAmount: bigint;
fundedAmountInUsd: number;
}
| {
type: "IncrementRoundDonationStats";
chainId: ChainId;
Expand Down
19 changes: 18 additions & 1 deletion src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ export class Database {
break;
}

case "IncrementRoundFundedAmount": {
await this.#db
.updateTable("rounds")
.set((eb) => ({
fundedAmount: eb("fundedAmount", "+", change.fundedAmount),
fundedAmountInUsd: eb(
"fundedAmountInUsd",
"+",
change.fundedAmountInUsd
),
}))
.where("chainId", "=", change.chainId)
.where("id", "=", change.roundId)
.execute();
break;
}

case "UpdateRoundByStrategyAddress": {
await this.#db
.updateTable("rounds")
Expand Down Expand Up @@ -472,7 +489,7 @@ export class Database {
return project ?? null;
}

async getRoundById(chainId: ChainId, roundId: Address) {
async getRoundById(chainId: ChainId, roundId: string) {
const round = await this.#db
.selectFrom("rounds")
.where("chainId", "=", chainId)
Expand Down
3 changes: 3 additions & 0 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addColumn("matchTokenAddress", ADDRESS_TYPE)
.addColumn("matchAmountInUSD", "real")

.addColumn("fundedAmount", BIGINT_TYPE, (col) => col.defaultTo("0"))
.addColumn("fundedAmountInUSD", "real", (col) => col.defaultTo(0.0))

.addColumn("applicationMetadataCid", "text")
.addColumn("applicationMetadata", "jsonb")
.addColumn("roundMetadataCid", "text")
Expand Down
2 changes: 2 additions & 0 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type RoundTable = {
matchAmount: bigint;
matchTokenAddress: Address;
matchAmountInUsd: number;
fundedAmount: bigint;
fundedAmountInUsd: number;
applicationMetadataCid: string;
applicationMetadata: unknown | null;
roundMetadataCid: string;
Expand Down
11 changes: 11 additions & 0 deletions src/indexer/allo/roundMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod";

export const RoundMetadataSchema = z
.object({
name: z.string(),
roundType: z.union([z.literal("private"), z.literal("public")]),
quadraticFundingConfig: z.object({
matchingFundsAvailable: z.number(),
}),
})
.passthrough();
10 changes: 10 additions & 0 deletions src/indexer/allo/v1/handleEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ describe("handleEvent", () => {
matchAmount: 0n,
matchTokenAddress: parseAddress(addressZero),
matchAmountInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
applicationMetadataCid: "",
applicationMetadata: null,
roundMetadataCid: "",
Expand Down Expand Up @@ -609,6 +611,8 @@ describe("handleEvent", () => {
matchAmount: 0n,
matchTokenAddress: parseAddress(addressZero),
matchAmountInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
applicationMetadataCid: "",
applicationMetadata: null,
roundMetadataCid: "",
Expand Down Expand Up @@ -672,6 +676,8 @@ describe("handleEvent", () => {
matchAmount: 0n,
matchTokenAddress: parseAddress(addressZero),
matchAmountInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
applicationMetadataCid: "",
applicationMetadata: null,
roundMetadataCid: "",
Expand Down Expand Up @@ -733,6 +739,8 @@ describe("handleEvent", () => {
matchAmount: 0n,
matchTokenAddress: parseAddress(addressZero),
matchAmountInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
applicationMetadataCid: "",
applicationMetadata: null,
roundMetadataCid: "",
Expand Down Expand Up @@ -953,6 +961,8 @@ describe("handleEvent", () => {
tags: ["allo-v1"],
totalDonationsCount: 0,
totalAmountDonatedInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
uniqueDonorsCount: 0,
matchTokenAddress: "0x0000000000000000000000000000000000000000",
matchAmount: 0n,
Expand Down
2 changes: 2 additions & 0 deletions src/indexer/allo/v1/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ export async function handleEvent(
tags: ["allo-v1"],
totalDonationsCount: 0,
totalAmountDonatedInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
uniqueDonorsCount: 0,
matchTokenAddress,
matchAmount: 0n,
Expand Down
8 changes: 7 additions & 1 deletion src/indexer/allo/v2/handleEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const round: Round = {
matchAmount: 0n,
matchTokenAddress: parseAddress(addressZero),
matchAmountInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
applicationMetadataCid: "",
applicationMetadata: null,
roundMetadataCid: "",
Expand Down Expand Up @@ -634,7 +636,9 @@ describe("handleEvent", () => {
totalAmountDonatedInUsd: 0,
uniqueDonorsCount: 0,
matchTokenAddress: "0x0000000000000000000000000000000000000000",
matchAmount: 10n,
fundedAmount: 10n,
fundedAmountInUsd: 1,
matchAmount: 0n,
matchAmountInUsd: 0,
applicationMetadataCid: "test-cid",
applicationMetadata: { applicationMetadata: "application metadata" },
Expand Down Expand Up @@ -847,6 +851,8 @@ describe("handleEvent", () => {
matchAmount: 0n,
matchTokenAddress: parseAddress(addressZero),
matchAmountInUsd: 0,
fundedAmount: 0n,
fundedAmountInUsd: 0,
applicationMetadataCid: "",
applicationMetadata: null,
roundMetadataCid: "",
Expand Down
Loading

0 comments on commit 5368db6

Please sign in to comment.