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.
# 🤖 Linear Closes GIT-136 GIT-144 ## Description - DummyPricingProvider for testing, dev environments to avoid rate limiting - write a Factory to instantiate a pricing provider from Env config - makes `timestampEnd` optional for `IPricingProvider` ## 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
24 changed files
with
273 additions
and
42 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class InvalidPricingSource extends Error { | ||
constructor() { | ||
super(`Invalid pricing source`); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/pricing/src/exceptions/missingDependencies.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 MissingDependencies extends Error { | ||
constructor() { | ||
super(`Missing dependencies`); | ||
} | ||
} |
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,9 +1,20 @@ | ||
export type { TokenPrice, IPricingProvider } from "./internal.js"; | ||
|
||
export { CoingeckoProvider } from "./internal.js"; | ||
export { CoingeckoProvider, DummyPricingProvider } from "./internal.js"; | ||
|
||
export { PricingProviderFactory } from "./internal.js"; | ||
export type { | ||
PricingConfig, | ||
PricingProvider, | ||
DummyPricingConfig, | ||
CoingeckoPricingConfig, | ||
} from "./internal.js"; | ||
|
||
export { | ||
UnsupportedChainException, | ||
NetworkException, | ||
UnknownPricingException, | ||
UnsupportedToken, | ||
InvalidPricingSource, | ||
MissingDependencies, | ||
} from "./internal.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,56 @@ | ||
import { ILogger } from "@grants-stack-indexer/shared"; | ||
|
||
import { | ||
CoingeckoProvider, | ||
DummyPricingProvider, | ||
InvalidPricingSource, | ||
IPricingProvider, | ||
MissingDependencies, | ||
PricingConfig, | ||
PricingProvider, | ||
} from "../internal.js"; | ||
|
||
/** | ||
* Factory class for creating pricing providers. | ||
*/ | ||
export class PricingProviderFactory { | ||
/** | ||
* Creates a pricing provider based on the provided configuration. | ||
* @param options - The pricing configuration. | ||
* @param deps - dependencies to inject into the pricing provider. | ||
* @returns The created pricing provider. | ||
* @throws {InvalidPricingSource} if the pricing source is invalid. | ||
* @throws {MissingDependencies} if the dependencies are missing. | ||
*/ | ||
static create( | ||
options: PricingConfig<PricingProvider>, | ||
deps?: { | ||
logger?: ILogger; | ||
}, | ||
): IPricingProvider { | ||
let pricingProvider: IPricingProvider; | ||
|
||
switch (options.pricingSource) { | ||
case "dummy": | ||
pricingProvider = new DummyPricingProvider(options.dummyPrice); | ||
break; | ||
case "coingecko": | ||
if (!deps?.logger) { | ||
throw new MissingDependencies(); | ||
} | ||
|
||
pricingProvider = new CoingeckoProvider( | ||
{ | ||
apiKey: options.apiKey, | ||
apiType: options.apiType, | ||
}, | ||
deps.logger, | ||
); | ||
break; | ||
default: | ||
throw new InvalidPricingSource(); | ||
} | ||
|
||
return pricingProvider; | ||
} | ||
} |
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 +1,2 @@ | ||
export * from "./pricing.interface.js"; | ||
export * from "./pricingConfig.interface.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
18 changes: 18 additions & 0 deletions
18
packages/pricing/src/interfaces/pricingConfig.interface.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,18 @@ | ||
import { PricingProvider } from "./index.js"; | ||
|
||
export type DummyPricingConfig = { | ||
pricingSource: "dummy"; | ||
dummyPrice?: number; | ||
}; | ||
|
||
export type CoingeckoPricingConfig = { | ||
pricingSource: "coingecko"; | ||
apiKey: string; | ||
apiType: "demo" | "pro"; | ||
}; | ||
|
||
export type PricingConfig<Source extends PricingProvider> = Source extends "dummy" | ||
? DummyPricingConfig | ||
: Source extends "coingecko" | ||
? CoingeckoPricingConfig | ||
: never; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { TokenCode } from "@grants-stack-indexer/shared"; | ||
|
||
import { IPricingProvider, TokenPrice } from "../internal.js"; | ||
|
||
/** | ||
* DummyPricingProvider class that implements the IPricingProvider interface. | ||
* This provider returns a configurable fixed price (defaults to 1) for any token code. | ||
* Used primarily for testing purposes when actual token prices are not needed. | ||
*/ | ||
export class DummyPricingProvider implements IPricingProvider { | ||
constructor(private readonly dummyPrice: number = 1) {} | ||
|
||
/* @inheritdoc */ | ||
async getTokenPrice( | ||
_tokenCode: TokenCode, | ||
startTimestampMs: number, | ||
_endTimestampMs?: number, | ||
): Promise<TokenPrice | undefined> { | ||
return Promise.resolve({ | ||
priceUsd: this.dummyPrice, | ||
timestampMs: startTimestampMs, | ||
}); | ||
} | ||
} |
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 +1,2 @@ | ||
export * from "./coingecko.provider.js"; | ||
export * from "./dummy.provider.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,56 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { ILogger } from "@grants-stack-indexer/shared"; | ||
|
||
import { CoingeckoProvider, PricingConfig } from "../../src/external.js"; | ||
import { PricingProviderFactory } from "../../src/factory/index.js"; | ||
import { InvalidPricingSource, MissingDependencies } from "../../src/internal.js"; | ||
import { DummyPricingProvider } from "../../src/providers/dummy.provider.js"; | ||
|
||
describe("PricingProviderFactory", () => { | ||
it("create a DummyPricingProvider", () => { | ||
const options: PricingConfig<"dummy"> = { | ||
pricingSource: "dummy", | ||
dummyPrice: 1, | ||
}; | ||
|
||
const pricingProvider = PricingProviderFactory.create(options); | ||
expect(pricingProvider).toBeInstanceOf(DummyPricingProvider); | ||
const dummyProvider = pricingProvider as DummyPricingProvider; | ||
expect(dummyProvider["dummyPrice"]).toBe(1); | ||
}); | ||
|
||
it("create a CoingeckoProvider", () => { | ||
const options: PricingConfig<"coingecko"> = { | ||
pricingSource: "coingecko", | ||
apiKey: "some-api-key", | ||
apiType: "pro", | ||
}; | ||
|
||
const pricingProvider = PricingProviderFactory.create(options, { | ||
logger: {} as unknown as ILogger, | ||
}); | ||
|
||
expect(pricingProvider).toBeInstanceOf(CoingeckoProvider); | ||
}); | ||
|
||
it("throws if logger instance is not provided for CoingeckoProvider", () => { | ||
const options: PricingConfig<"coingecko"> = { | ||
pricingSource: "coingecko", | ||
apiKey: "some-api-key", | ||
apiType: "demo", | ||
}; | ||
|
||
expect(() => PricingProviderFactory.create(options)).toThrowError(MissingDependencies); | ||
}); | ||
|
||
it("should throw an error for invalid pricing source", () => { | ||
const options = { | ||
source: "invalid", | ||
}; | ||
|
||
expect(() => { | ||
PricingProviderFactory.create(options as unknown as PricingConfig<"dummy">); | ||
}).toThrowError(InvalidPricingSource); | ||
}); | ||
}); |
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,18 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { TokenCode } from "@grants-stack-indexer/shared"; | ||
|
||
import { DummyPricingProvider } from "../../src/providers/dummy.provider.js"; | ||
|
||
describe("DummyPricingProvider", () => { | ||
it("return 1 for all token prices", async () => { | ||
const provider = new DummyPricingProvider(); | ||
|
||
const response = await provider.getTokenPrice("ETH" as TokenCode, 11111111); | ||
|
||
expect(response).toEqual({ | ||
priceUsd: 1, | ||
timestampMs: 11111111, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.