-
Notifications
You must be signed in to change notification settings - Fork 16
feat(ensnode-sdk): currencies module #1250
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@ensnode/ensnode-sdk": minor | ||
| --- | ||
|
|
||
| Create `currencies` module in SDK. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| addPrices, | ||
| CurrencyIds, | ||
| type CurrencyInfo, | ||
| getCurrencyInfo, | ||
| isPriceCurrencyEqual, | ||
| isPriceEqual, | ||
| type Price, | ||
| type PriceDai, | ||
| type PriceEth, | ||
| type PriceUsdc, | ||
| priceDai, | ||
| priceEth, | ||
| priceUsdc, | ||
| } from "./currencies"; | ||
|
|
||
| describe("Currencies", () => { | ||
| describe("getCurrencyInfo", () => { | ||
| it("returns CurrencyInfo for requested CurrencyId", () => { | ||
| expect(getCurrencyInfo(CurrencyIds.ETH)).toStrictEqual({ | ||
| id: CurrencyIds.ETH, | ||
| name: "ETH", | ||
| decimals: 18, | ||
| } satisfies CurrencyInfo); | ||
| }); | ||
| }); | ||
|
|
||
| describe("priceEth", () => { | ||
| it("returns correct Price object", () => { | ||
| expect(priceEth(1n)).toStrictEqual({ | ||
| amount: 1n, | ||
| currency: CurrencyIds.ETH, | ||
| } satisfies PriceEth); | ||
| }); | ||
| }); | ||
|
|
||
| describe("priceUsdc", () => { | ||
| it("returns correct Price object", () => { | ||
| expect(priceUsdc(1n)).toStrictEqual({ | ||
| amount: 1n, | ||
| currency: CurrencyIds.USDC, | ||
| } satisfies PriceUsdc); | ||
| }); | ||
| }); | ||
|
|
||
| describe("priceDai", () => { | ||
| it("returns correct Price object", () => { | ||
| expect(priceDai(1n)).toStrictEqual({ | ||
| amount: 1n, | ||
| currency: CurrencyIds.DAI, | ||
| } satisfies PriceDai); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isPriceCurrencyEqual", () => { | ||
| it("returns true when two prices have the same currency", () => { | ||
| expect(isPriceCurrencyEqual(priceEth(1n), priceEth(1n))).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when two prices have different currency", () => { | ||
| expect(isPriceCurrencyEqual(priceEth(1n), priceUsdc(1n))).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isPriceEqual", () => { | ||
| it("returns true when two prices have the same currency and the same amount", () => { | ||
| const price = { | ||
| amount: 1n, | ||
| currency: CurrencyIds.ETH, | ||
| } satisfies Price; | ||
|
|
||
| expect(isPriceEqual(price, price)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when two prices have different currency", () => { | ||
| const priceA = { | ||
| amount: 1n, | ||
| currency: CurrencyIds.ETH, | ||
| } satisfies Price; | ||
|
|
||
| const priceB = { | ||
| amount: 1n, | ||
| currency: CurrencyIds.USDC, | ||
| } satisfies Price; | ||
|
|
||
| expect(isPriceEqual(priceA, priceB)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when two prices have different amount", () => { | ||
| const priceA = { | ||
| amount: 1n, | ||
| currency: CurrencyIds.ETH, | ||
| } satisfies Price; | ||
|
|
||
| const priceB = { | ||
| amount: 2n, | ||
| currency: CurrencyIds.ETH, | ||
| } satisfies Price; | ||
|
|
||
| expect(isPriceEqual(priceA, priceB)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("addPrices", () => { | ||
| it("returns a total of at prices which all have the same currency", () => { | ||
| expect(addPrices(priceEth(1n), priceEth(2n), priceEth(3n))).toEqual(priceEth(6n)); | ||
| }); | ||
| it("throws an error if all prices do not have the same currency", () => { | ||
| expect(() => addPrices(priceEth(1n), priceDai(2n), priceEth(3n))).toThrowError( | ||
| /All prices must have the same currency to be added together/i, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,153 @@ | ||
| /** | ||
| * Identifiers for supported currencies. | ||
| * | ||
| * TODO: Add support for WETH | ||
| */ | ||
| export const CurrencyIds = { | ||
| ETH: "ETH", | ||
| USDC: "USDC", | ||
| DAI: "DAI", | ||
| } as const; | ||
|
|
||
| export type CurrencyId = (typeof CurrencyIds)[keyof typeof CurrencyIds]; | ||
|
|
||
| /** | ||
| * The amount of the currency in the smallest unit of the currency | ||
| * (see {@link CurrencyInfo.decimals} for the currency). | ||
| * | ||
| * Guaranteed to be non-negative. | ||
| */ | ||
| export type CurrencyAmount = bigint; | ||
|
|
||
| export interface PriceEth { | ||
| currency: typeof CurrencyIds.ETH; | ||
|
|
||
| amount: CurrencyAmount; | ||
| } | ||
|
|
||
| export interface PriceDai { | ||
| currency: typeof CurrencyIds.DAI; | ||
|
|
||
| amount: CurrencyAmount; | ||
| } | ||
|
|
||
| export interface PriceUsdc { | ||
| currency: typeof CurrencyIds.USDC; | ||
|
|
||
| amount: CurrencyAmount; | ||
| } | ||
|
|
||
| export type Price = PriceEth | PriceDai | PriceUsdc; | ||
|
|
||
| /** | ||
| * Serialized representation of {@link Price}. | ||
| */ | ||
| export interface SerializedPrice extends Omit<Price, "amount"> { | ||
| currency: CurrencyId; | ||
|
|
||
| amount: string; | ||
| } | ||
|
|
||
| export interface CurrencyInfo { | ||
| id: CurrencyId; | ||
| name: string; | ||
| decimals: number; | ||
| } | ||
|
|
||
| const currencyInfo: Record<CurrencyId, CurrencyInfo> = { | ||
| [CurrencyIds.ETH]: { | ||
| id: CurrencyIds.ETH, | ||
| name: "ETH", | ||
| decimals: 18, | ||
| }, | ||
| [CurrencyIds.USDC]: { | ||
| id: CurrencyIds.USDC, | ||
| name: "USDC", | ||
| decimals: 6, | ||
| }, | ||
| [CurrencyIds.DAI]: { | ||
| id: CurrencyIds.DAI, | ||
| name: "Dai Stablecoin", | ||
| decimals: 18, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Get currency info for a provided currency. | ||
| */ | ||
| export function getCurrencyInfo(currencyId: CurrencyId): CurrencyInfo { | ||
| return currencyInfo[currencyId]; | ||
| } | ||
|
|
||
| /** | ||
| * Create price in ETH for given amount. | ||
| */ | ||
| export function priceEth(amount: Price["amount"]): PriceEth { | ||
| return { | ||
| amount, | ||
| currency: CurrencyIds.ETH, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Create price in USDC for given amount. | ||
| */ | ||
| export function priceUsdc(amount: Price["amount"]): PriceUsdc { | ||
| return { | ||
| amount, | ||
| currency: CurrencyIds.USDC, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Create price in DAI for given amount. | ||
| */ | ||
| export function priceDai(amount: Price["amount"]): PriceDai { | ||
| return { | ||
| amount, | ||
| currency: CurrencyIds.DAI, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Check if two prices have the same currency. | ||
| */ | ||
| export function isPriceCurrencyEqual(priceA: Price, priceB: Price): boolean { | ||
| return priceA.currency === priceB.currency; | ||
| } | ||
|
|
||
| /** | ||
| * Check if two {@link Price} values have the same currency and amount. | ||
| */ | ||
| export function isPriceEqual(priceA: Price, priceB: Price): boolean { | ||
| return isPriceCurrencyEqual(priceA, priceB) && priceA.amount === priceB.amount; | ||
| } | ||
|
|
||
| /** | ||
| * Add prices | ||
| * | ||
| * @param prices at least two {@link Price} values to be added together. | ||
| * @returns total of all prices. | ||
| * @throws if not all prices have the same currency. | ||
| */ | ||
| export function addPrices(...prices: [Price, Price, ...Price[]]): Price { | ||
| const firstPrice = prices[0]; | ||
| const allPricesInSameCurrency = prices.every((price) => isPriceCurrencyEqual(firstPrice, price)); | ||
|
|
||
| if (allPricesInSameCurrency === false) { | ||
| throw new Error("All prices must have the same currency to be added together."); | ||
| } | ||
|
|
||
| const { currency } = firstPrice; | ||
|
|
||
| return prices.reduce( | ||
| (acc, price) => ({ | ||
| amount: acc.amount + price.amount, | ||
| currency, | ||
| }), | ||
| { | ||
| amount: 0n, | ||
| currency: firstPrice.currency, | ||
| } satisfies Price, | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you double check this noting how this interface is defined to extend Price?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, this noting looks good to me. We take all serializable fileds of
Pricebut we omit the non-serializable ones.