-
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.
- Loading branch information
Showing
10 changed files
with
215 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
export type { IPricingProvider, PriceResponse } from "./internal.js"; | ||
export type { | ||
IPricingProvider, | ||
PriceResponse, | ||
PricingConfig, | ||
PricingProvider, | ||
DummyPricingConfig, | ||
CoingeckoPricingConfig, | ||
} from "./internal.js"; | ||
|
||
export { RateLimitExceeded, ApiNotAvailable } from "./internal.js"; | ||
|
||
export { CoingeckoProvider, DummyPricingProvider } from "./internal.js"; | ||
|
||
export { PricingProviderFactory } 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,45 @@ | ||
import { Cache, ILogger } from "@zkchainhub/shared"; | ||
|
||
import { | ||
CoingeckoProvider, | ||
DummyPricingProvider, | ||
IPricingProvider, | ||
PricingConfig, | ||
PricingProvider, | ||
} from "../internal.js"; | ||
|
||
export class PricingProviderFactory { | ||
static create( | ||
options: PricingConfig<PricingProvider>, | ||
deps?: { | ||
logger?: ILogger; | ||
cache?: Cache; | ||
}, | ||
): IPricingProvider { | ||
let pricingProvider: IPricingProvider; | ||
|
||
switch (options.source) { | ||
case "dummy": | ||
pricingProvider = new DummyPricingProvider(options.dummyPrice); | ||
break; | ||
case "coingecko": | ||
if (!deps?.cache || !deps?.logger) { | ||
throw new Error("Missing dependencies"); | ||
} | ||
pricingProvider = new CoingeckoProvider( | ||
{ | ||
apiBaseUrl: options.apiBaseUrl, | ||
apiKey: options.apiKey, | ||
apiType: options.apiType, | ||
}, | ||
deps.cache, | ||
deps.logger, | ||
); | ||
break; | ||
default: | ||
throw new Error("Invalid pricing source"); | ||
} | ||
|
||
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
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
import { PricingProvider } from "./pricing.interface.js"; | ||
|
||
export interface DummyPricingConfig { | ||
source: "dummy"; | ||
dummyPrice?: number; | ||
} | ||
|
||
export interface CoingeckoPricingConfig { | ||
source: "coingecko"; | ||
apiKey: string; | ||
apiBaseUrl: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { | ||
CoingeckoProvider, | ||
DummyPricingProvider, | ||
PricingConfig, | ||
PricingProviderFactory, | ||
} from "../../../src/internal.js"; | ||
|
||
describe("PricingProviderFactory", () => { | ||
it("create a DummyPricingProvider", () => { | ||
const options: PricingConfig<"dummy"> = { | ||
source: "dummy", | ||
dummyPrice: 1, | ||
}; | ||
|
||
const pricingProvider = PricingProviderFactory.create(options); | ||
|
||
expect(pricingProvider).toBeInstanceOf(DummyPricingProvider); | ||
expect(pricingProvider["dummyPrice"]).toBe(1); | ||
}); | ||
|
||
it("create a CoingeckoProvider", () => { | ||
const options: PricingConfig<"coingecko"> = { | ||
source: "coingecko", | ||
apiKey: "some-api-key", | ||
apiBaseUrl: "some-base-url", | ||
apiType: "demo", | ||
}; | ||
|
||
const pricingProvider = PricingProviderFactory.create(options, { | ||
logger: {} as any, | ||
cache: {} as any, | ||
}); | ||
|
||
expect(pricingProvider).toBeInstanceOf(CoingeckoProvider); | ||
expect(pricingProvider["options"]).toEqual({ | ||
apiKey: "some-api-key", | ||
apiBaseUrl: "some-base-url", | ||
apiType: "demo", | ||
}); | ||
}); | ||
|
||
it("throws if cache instance is not provided for CoingeckoProvider", () => { | ||
const options: PricingConfig<"coingecko"> = { | ||
source: "coingecko", | ||
apiKey: "some-api-key", | ||
apiBaseUrl: "some-base-url", | ||
apiType: "demo", | ||
}; | ||
|
||
expect(() => | ||
PricingProviderFactory.create(options, { | ||
logger: {} as any, | ||
}), | ||
).toThrowError("Missing dependencies"); | ||
}); | ||
|
||
it("throws if logger instance is not provided for CoingeckoProvider", () => { | ||
const options: PricingConfig<"coingecko"> = { | ||
source: "coingecko", | ||
apiKey: "some-api-key", | ||
apiBaseUrl: "some-base-url", | ||
apiType: "demo", | ||
}; | ||
|
||
expect(() => | ||
PricingProviderFactory.create(options, { | ||
cache: {} as any, | ||
}), | ||
).toThrowError("Missing dependencies"); | ||
}); | ||
|
||
it("should throw an error for invalid pricing source", () => { | ||
const options = { | ||
source: "invalid", | ||
}; | ||
|
||
expect(() => { | ||
PricingProviderFactory.create(options as any); | ||
}).toThrowError("Invalid pricing source"); | ||
}); | ||
}); |