-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: ecosystem configuration #61
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5834cd0
feat: configurable dummy price
0xnigir1 042cea1
feat: pricing provider factory
0xnigir1 52fa1e6
feat: add ecosystem picker variable
0xnigir1 16890d3
docs: update .env.example
0xnigir1 8689c5a
feat: remove l2 configuration
0xnigir1 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 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 |
---|---|---|
|
@@ -44,4 +44,6 @@ contracts/out | |
contracts/cache | ||
|
||
# Turborepo | ||
.turbo | ||
.turbo | ||
|
||
.tmp |
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 |
---|---|---|
@@ -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
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"); | ||
}); | ||
}); |
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.
Might want to indicate that
mainnet
is the default somewhere in the docs? (.env.example, README, etc)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.
will do it in another PR where I attack all documentation and read me detailed 🫡