-
Notifications
You must be signed in to change notification settings - Fork 2
Replace OKX and Panora SDK with thin client #100
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,84 @@ | ||
| import { buildHeaders, buildQueryString, sign } from "./auth"; | ||
|
|
||
| describe("OKX auth", () => { | ||
| describe("buildQueryString", () => { | ||
| it("returns empty string for empty params", () => { | ||
| expect(buildQueryString({})).toBe(""); | ||
| }); | ||
|
|
||
| it("builds query string from params", () => { | ||
| const qs = buildQueryString({ chainIndex: "501", amount: "1000" }); | ||
| expect(qs).toBe("?chainIndex=501&amount=1000"); | ||
| }); | ||
|
|
||
| it("filters out undefined and null values", () => { | ||
| const qs = buildQueryString({ chainIndex: "501", dexIds: undefined, feePercent: null }); | ||
| expect(qs).toBe("?chainIndex=501"); | ||
| }); | ||
|
|
||
| it("encodes special characters", () => { | ||
| const qs = buildQueryString({ key: "a b&c" }); | ||
| expect(qs).toBe("?key=a%20b%26c"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("sign", () => { | ||
| it("produces stable HMAC-SHA256 signature", () => { | ||
| const sig = sign("2026-01-01T00:00:00.000Z", "GET", "/api/v6/dex/aggregator/quote?chainIndex=501", "test-secret"); | ||
| expect(sig).toBe(sign("2026-01-01T00:00:00.000Z", "GET", "/api/v6/dex/aggregator/quote?chainIndex=501", "test-secret")); | ||
| expect(typeof sig).toBe("string"); | ||
| expect(sig.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("changes with different timestamps", () => { | ||
| const sig1 = sign("2026-01-01T00:00:00.000Z", "GET", "/path", "secret"); | ||
| const sig2 = sign("2026-01-02T00:00:00.000Z", "GET", "/path", "secret"); | ||
| expect(sig1).not.toBe(sig2); | ||
| }); | ||
|
|
||
| it("changes with different paths", () => { | ||
| const sig1 = sign("2026-01-01T00:00:00.000Z", "GET", "/path-a", "secret"); | ||
| const sig2 = sign("2026-01-01T00:00:00.000Z", "GET", "/path-b", "secret"); | ||
| expect(sig1).not.toBe(sig2); | ||
| }); | ||
|
|
||
| it("changes with different secrets", () => { | ||
| const sig1 = sign("2026-01-01T00:00:00.000Z", "GET", "/path", "secret-a"); | ||
| const sig2 = sign("2026-01-01T00:00:00.000Z", "GET", "/path", "secret-b"); | ||
| expect(sig1).not.toBe(sig2); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildHeaders", () => { | ||
| it("includes all required OKX headers", () => { | ||
| const config = { | ||
| apiKey: "test-key", | ||
| secretKey: "test-secret", | ||
| apiPassphrase: "test-pass", | ||
| projectId: "test-project", | ||
| }; | ||
| const headers = buildHeaders(config, "2026-01-01T00:00:00.000Z", "/api/v6/dex/aggregator/quote"); | ||
|
|
||
| expect(headers["OK-ACCESS-KEY"]).toBe("test-key"); | ||
| expect(headers["OK-ACCESS-PASSPHRASE"]).toBe("test-pass"); | ||
| expect(headers["OK-ACCESS-PROJECT"]).toBe("test-project"); | ||
| expect(headers["OK-ACCESS-TIMESTAMP"]).toBe("2026-01-01T00:00:00.000Z"); | ||
| expect(headers["OK-ACCESS-SIGN"]).toBeDefined(); | ||
| expect(headers["Content-Type"]).toBe("application/json"); | ||
| }); | ||
|
|
||
| it("sign matches HMAC-SHA256 of timestamp+method+path", () => { | ||
| const config = { | ||
| apiKey: "k", | ||
| secretKey: "my-secret", | ||
| apiPassphrase: "p", | ||
| projectId: "proj", | ||
| }; | ||
| const timestamp = "2026-01-01T00:00:00.000Z"; | ||
| const path = "/api/v6/dex/aggregator/quote?chainIndex=501"; | ||
| const headers = buildHeaders(config, timestamp, path); | ||
|
|
||
| expect(headers["OK-ACCESS-SIGN"]).toBe(sign(timestamp, "GET", path, "my-secret")); | ||
| }); | ||
| }); | ||
| }); |
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,35 @@ | ||
| import { createHmac } from "node:crypto"; | ||
|
|
||
| export interface OkxClientConfig { | ||
| apiKey: string; | ||
| secretKey: string; | ||
| apiPassphrase: string; | ||
| projectId: string; | ||
| } | ||
|
|
||
| export function buildQueryString(params: object): string { | ||
| const entries = Object.entries(params).filter(([, v]) => v !== undefined && v !== null); | ||
| if (entries.length === 0) return ""; | ||
| return "?" + entries.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`).join("&"); | ||
| } | ||
|
|
||
| export function sign(timestamp: string, method: string, path: string, secretKey: string): string { | ||
| return createHmac("sha256", secretKey).update(timestamp + method + path).digest("base64"); | ||
| } | ||
|
|
||
| const HEADER_KEY = "OK-ACCESS-KEY"; | ||
| const HEADER_SIGN = "OK-ACCESS-SIGN"; | ||
| const HEADER_TIMESTAMP = "OK-ACCESS-TIMESTAMP"; | ||
| const HEADER_PASSPHRASE = "OK-ACCESS-PASSPHRASE"; | ||
| const HEADER_PROJECT = "OK-ACCESS-PROJECT"; | ||
|
|
||
| export function buildHeaders(config: OkxClientConfig, timestamp: string, fullPath: string): Record<string, string> { | ||
| return { | ||
| "Content-Type": "application/json", | ||
| [HEADER_KEY]: config.apiKey, | ||
| [HEADER_SIGN]: sign(timestamp, "GET", fullPath, config.secretKey), | ||
| [HEADER_TIMESTAMP]: timestamp, | ||
| [HEADER_PASSPHRASE]: config.apiPassphrase, | ||
| [HEADER_PROJECT]: config.projectId, | ||
| }; | ||
| } | ||
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,44 @@ | ||
| import { buildHeaders, buildQueryString } from "./auth"; | ||
| import type { OkxClientConfig } from "./auth"; | ||
| import type { ChainData, OkxApiResponse, QuoteData, QuoteParams, SwapParams, TransactionData } from "./models"; | ||
|
|
||
| const BASE_URL = "https://web3.okx.com"; | ||
|
|
||
| export class OkxDexClient { | ||
| private readonly config: OkxClientConfig; | ||
|
|
||
| constructor(config: OkxClientConfig) { | ||
| this.config = config; | ||
| } | ||
|
|
||
| async getQuote(params: QuoteParams): Promise<OkxApiResponse<QuoteData>> { | ||
| return this.get("/api/v6/dex/aggregator/quote", params); | ||
| } | ||
|
|
||
| async getSwapData(params: SwapParams): Promise<OkxApiResponse<{ routerResult: QuoteData; tx: TransactionData }>> { | ||
| return this.get("/api/v6/dex/aggregator/swap", params); | ||
| } | ||
|
|
||
| async getChainData(chainIndex: string): Promise<OkxApiResponse<ChainData>> { | ||
| return this.get("/api/v6/dex/aggregator/supported/chain", { chainIndex }); | ||
| } | ||
|
|
||
| private async get<T>(path: string, params: object): Promise<T> { | ||
| const queryString = buildQueryString(params); | ||
| const fullPath = path + queryString; | ||
| const timestamp = new Date().toISOString(); | ||
|
|
||
| const response = await fetch(BASE_URL + fullPath, { | ||
| method: "GET", | ||
| headers: buildHeaders(this.config, timestamp, fullPath), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const text = await response.text(); | ||
| throw new Error(`OKX API ${response.status}: ${text}`); | ||
| } | ||
|
|
||
| const json = await response.json() as T; | ||
| return json; | ||
| } | ||
| } |
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,63 @@ | ||
| export interface OkxApiResponse<T> { | ||
| code: string; | ||
| msg: string; | ||
| data: T[]; | ||
| } | ||
|
|
||
| export interface TokenInfo { | ||
| tokenContractAddress: string; | ||
| tokenSymbol: string; | ||
| decimal: string; | ||
| tokenUnitPrice: string; | ||
| } | ||
|
|
||
| export interface QuoteData { | ||
| fromToken: TokenInfo; | ||
| toToken: TokenInfo; | ||
| fromTokenAmount: string; | ||
| toTokenAmount: string; | ||
| estimateGasFee: string; | ||
| tx?: TransactionData; | ||
| } | ||
|
|
||
| export interface TransactionData { | ||
| data: string; | ||
| from: string; | ||
| to: string; | ||
| value: string; | ||
| gas: string; | ||
| minReceiveAmount: string; | ||
| slippagePercent: string; | ||
| signatureData?: string[]; | ||
| } | ||
|
|
||
| export interface ChainData { | ||
| chainIndex: string; | ||
| chainName: string; | ||
| dexTokenApproveAddress: string | null; | ||
| } | ||
|
|
||
| export interface QuoteParams { | ||
| chainIndex: string; | ||
| amount: string; | ||
| fromTokenAddress: string; | ||
| toTokenAddress: string; | ||
| slippagePercent: string; | ||
| dexIds?: string; | ||
| feePercent?: string; | ||
| userWalletAddress?: string; | ||
| } | ||
|
|
||
| export interface SwapParams { | ||
| chainIndex: string; | ||
| amount: string; | ||
| fromTokenAddress: string; | ||
| toTokenAddress: string; | ||
| userWalletAddress: string; | ||
| slippagePercent?: string; | ||
| autoSlippage?: boolean; | ||
| maxAutoSlippagePercent?: string; | ||
| dexIds?: string; | ||
| feePercent?: string; | ||
| fromTokenReferrerWalletAddress?: string; | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.