generated from defi-wonderland/ts-turborepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dvmd direct transfer allocated handler (#29)
# 🤖 Linear Closes GIT-140 GIT-142 ## Description - `Allocated` event handler for DVMD Direct Transfer strategy - add `AllocatedWithX` events from Envio - adds `DonationRepository` ## Checklist before requesting a review - [x] I have conducted a self-review of my code. - [x] I have conducted a QA. - [x] If it is a core feature, I have included comprehensive tests.
- Loading branch information
Showing
40 changed files
with
1,009 additions
and
30 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
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
27 changes: 27 additions & 0 deletions
27
packages/data-flow/src/data-loader/handlers/donation.handlers.ts
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,27 @@ | ||
import { DonationChangeset, IDonationRepository } from "@grants-stack-indexer/repository"; | ||
|
||
import { ChangesetHandler } from "../types/index.js"; | ||
|
||
/** | ||
* Collection of handlers for application-related operations. | ||
* Each handler corresponds to a specific Application changeset type. | ||
*/ | ||
export type DonationHandlers = { | ||
[K in DonationChangeset["type"]]: ChangesetHandler<K>; | ||
}; | ||
|
||
/** | ||
* Creates handlers for managing application-related operations. | ||
* | ||
* @param repository - The application repository instance used for database operations | ||
* @returns An object containing all application-related handlers | ||
*/ | ||
export const createDonationHandlers = (repository: IDonationRepository): DonationHandlers => ({ | ||
InsertDonation: (async (changeset): Promise<void> => { | ||
await repository.insertDonation(changeset.args.donation); | ||
}) satisfies ChangesetHandler<"InsertDonation">, | ||
|
||
InsertManyDonations: (async (changeset): Promise<void> => { | ||
await repository.insertManyDonations(changeset.args.donations); | ||
}) satisfies ChangesetHandler<"InsertManyDonations">, | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./application.handlers.js"; | ||
export * from "./project.handlers.js"; | ||
export * from "./round.handlers.js"; | ||
export * from "./donation.handlers.js"; |
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
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
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
9 changes: 9 additions & 0 deletions
9
packages/processors/src/exceptions/applicationNotFound.exception.ts
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,9 @@ | ||
import { ChainId } from "@grants-stack-indexer/shared"; | ||
|
||
export class ApplicationNotFound extends Error { | ||
constructor(chainId: ChainId, roundId: string, recipientId: string) { | ||
super( | ||
`Application not found on chain ${chainId} for round ${roundId} and recipient ${recipientId}`, | ||
); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
packages/processors/src/exceptions/metadataParsingFailed.exception.ts
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,5 @@ | ||
export class MetadataParsingFailed extends Error { | ||
constructor(additionalInfo?: string) { | ||
super(`Failed to parse application metadata: ${additionalInfo}`); | ||
} | ||
} |
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 @@ | ||
import { ChainId } from "@grants-stack-indexer/shared"; | ||
|
||
export class UnknownToken extends Error { | ||
constructor(tokenAddress: string, chainId?: ChainId) { | ||
super(`Unknown token: ${tokenAddress} ${chainId ? `on chain ${chainId}` : ""}`); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./roles.js"; | ||
export * from "./utils.js"; | ||
export * from "./tokenMath.js"; | ||
export * from "./pricing.js"; |
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,71 @@ | ||
import { IPricingProvider } from "@grants-stack-indexer/pricing"; | ||
import { Token } from "@grants-stack-indexer/shared"; | ||
|
||
import { TokenPriceNotFoundError } from "../internal.js"; | ||
import { calculateAmountInToken, calculateAmountInUsd } from "./index.js"; | ||
|
||
// sometimes coingecko returns no prices for 1 hour range, 2 hours works better | ||
const TIMESTAMP_DELTA_RANGE = 2 * 60 * 60 * 1000; | ||
|
||
/** | ||
* Get the amount in USD for a given amount in the token | ||
* @param pricingProvider - The pricing provider to use | ||
* @param token - The token to get the amount in | ||
* @param amount - The amount in the token | ||
* @param timestamp - The timestamp to get the price at | ||
* @returns The amount in USD | ||
* @throws TokenPriceNotFoundError if the price is not found | ||
*/ | ||
export const getTokenAmountInUsd = async ( | ||
pricingProvider: IPricingProvider, | ||
token: Token, | ||
amount: bigint, | ||
timestamp: number, | ||
): Promise<{ amountInUsd: string; timestamp: number }> => { | ||
const tokenPrice = await pricingProvider.getTokenPrice( | ||
token.priceSourceCode, | ||
timestamp, | ||
timestamp + TIMESTAMP_DELTA_RANGE, | ||
); | ||
|
||
if (!tokenPrice) { | ||
throw new TokenPriceNotFoundError(token.address, timestamp); | ||
} | ||
|
||
return { | ||
amountInUsd: calculateAmountInUsd(amount, tokenPrice.priceUsd, token.decimals), | ||
timestamp: tokenPrice.timestampMs, | ||
}; | ||
}; | ||
|
||
/** | ||
* Get the amount in the token for a given amount in USD | ||
* @param pricingProvider - The pricing provider to use | ||
* @param token - The token to get the amount in | ||
* @param amountInUSD - The amount in USD | ||
* @param timestamp - The timestamp to get the price at | ||
* @returns The amount in the token | ||
* @throws TokenPriceNotFoundError if the price is not found | ||
*/ | ||
export const getUsdInTokenAmount = async ( | ||
pricingProvider: IPricingProvider, | ||
token: Token, | ||
amountInUSD: string, | ||
timestamp: number, | ||
): Promise<{ amount: bigint; price: number; timestamp: Date }> => { | ||
const closestPrice = await pricingProvider.getTokenPrice( | ||
token.priceSourceCode, | ||
timestamp, | ||
timestamp + TIMESTAMP_DELTA_RANGE, | ||
); | ||
|
||
if (!closestPrice) { | ||
throw new TokenPriceNotFoundError(token.address, timestamp); | ||
} | ||
|
||
return { | ||
amount: calculateAmountInToken(amountInUSD, closestPrice.priceUsd, token.decimals), | ||
timestamp: new Date(closestPrice.timestampMs), | ||
price: 1 / closestPrice.priceUsd, // price is the token price in USD, we return the inverse | ||
}; | ||
}; |
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
Oops, something went wrong.