forked from V-SK/synthlaunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathokx.ts
More file actions
230 lines (204 loc) · 6.48 KB
/
Copy pathokx.ts
File metadata and controls
230 lines (204 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { createHmac } from 'node:crypto';
export const OKX_BASE_URL = 'https://web3.okx.com';
export const OKX_X_LAYER_CHAIN_INDEX = '196';
export const OKX_NATIVE_TOKEN_ADDRESS =
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
type Primitive = string | number | boolean | null | undefined;
interface OkxRequestOptions {
method?: 'GET' | 'POST';
path: string;
query?: Record<string, Primitive>;
body?: unknown;
}
export interface OkxTokenSearchResult {
chainIndex: string;
tokenName: string;
tokenSymbol: string;
tokenLogoUrl?: string;
tokenContractAddress: string;
decimal: string;
explorerUrl?: string;
change?: string;
holders?: string;
liquidity?: string;
marketCap?: string;
price?: string;
tagList?: {
communityRecognized?: boolean;
};
}
export interface OkxBalanceItem {
tokenContractAddress: string;
symbol: string;
tokenName?: string;
tokenLogoUrl?: string;
balance: string;
rawBalance?: string;
decimal?: string;
price?: string;
valueUsd?: string;
change24h?: string;
}
function normalizeSlippagePercent(value?: string): string {
const raw = value?.trim() || '0.5';
const numeric = Number(raw);
if (!Number.isFinite(numeric) || numeric <= 0 || numeric > 100) {
throw new Error('Invalid slippage. Use a percentage between 0 and 100.');
}
return numeric.toString();
}
export function mapOkxErrorMessage(error: unknown): string {
const message = error instanceof Error ? error.message : 'OKX request failed';
if (/slippagePercent/i.test(message)) {
return 'OKX rejected the slippage value for this route.';
}
if (/insufficient/i.test(message)) {
return 'Insufficient balance for this X Layer route.';
}
if (/route/i.test(message) || /liquidity/i.test(message)) {
return 'No executable OKX route is available for this token pair right now.';
}
return message;
}
function getOkxCredentials() {
const apiKey = process.env.OKX_API_KEY ?? process.env.OK_ACCESS_KEY;
const secretKey = process.env.OKX_SECRET_KEY ?? process.env.OK_ACCESS_SECRET;
const passphrase =
process.env.OKX_API_PASSPHRASE ??
process.env.OKX_PASSPHRASE ??
process.env.OK_ACCESS_PASSPHRASE;
const projectId = process.env.OKX_PROJECT_ID ?? process.env.OK_ACCESS_PROJECT;
return { apiKey, secretKey, passphrase, projectId };
}
export function isOkxConfigured(): boolean {
const { apiKey, secretKey, passphrase } = getOkxCredentials();
return Boolean(apiKey && secretKey && passphrase);
}
function buildQueryString(query?: Record<string, Primitive>): string {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(query ?? {})) {
if (value === undefined || value === null || value === '') {
continue;
}
params.set(key, String(value));
}
const serialized = params.toString();
return serialized ? `?${serialized}` : '';
}
async function okxRequest<T>({
method = 'GET',
path,
query,
body,
}: OkxRequestOptions): Promise<T> {
const { apiKey, secretKey, passphrase, projectId } = getOkxCredentials();
if (!apiKey || !secretKey || !passphrase) {
throw new Error('OKX credentials are not configured');
}
const queryString = buildQueryString(query);
const requestPath = `${path}${method === 'GET' ? queryString : ''}`;
const bodyString =
method === 'POST' && body !== undefined ? JSON.stringify(body) : '';
const timestamp = new Date().toISOString();
const prehash = `${timestamp}${method}${requestPath}${bodyString}`;
const sign = createHmac('sha256', secretKey).update(prehash).digest('base64');
const response = await fetch(`${OKX_BASE_URL}${requestPath}`, {
method,
headers: {
'Content-Type': 'application/json',
'OK-ACCESS-KEY': apiKey,
'OK-ACCESS-SIGN': sign,
'OK-ACCESS-TIMESTAMP': timestamp,
'OK-ACCESS-PASSPHRASE': passphrase,
...(projectId ? { 'OK-ACCESS-PROJECT': projectId } : {}),
},
body: method === 'POST' ? bodyString : undefined,
cache: 'no-store',
});
const text = await response.text();
const parsed = text
? ((() => {
try {
return JSON.parse(text);
} catch {
return { msg: text };
}
})())
: {};
if (!response.ok) {
throw new Error(parsed?.msg || parsed?.message || response.statusText);
}
if (parsed?.code && parsed.code !== '0') {
throw new Error(parsed?.msg || 'OKX request failed');
}
return (parsed?.data ?? parsed) as T;
}
export async function okxTokenSearch(
search: string,
chains = OKX_X_LAYER_CHAIN_INDEX,
): Promise<OkxTokenSearchResult[]> {
return await okxRequest<OkxTokenSearchResult[]>({
path: '/api/v6/dex/market/token/search',
query: { chains, search },
});
}
export async function okxBalances(address: string): Promise<OkxBalanceItem[]> {
return await okxRequest<OkxBalanceItem[]>({
path: '/api/v6/dex/balance/all-token-balances-by-address',
query: {
chains: OKX_X_LAYER_CHAIN_INDEX,
address,
},
});
}
export async function okxTotalValue(
address: string,
): Promise<Array<{ totalValue?: string; tokenValueList?: Array<{ value?: string }> }>> {
return await okxRequest<
Array<{ totalValue?: string; tokenValueList?: Array<{ value?: string }> }>
>({
path: '/api/v6/dex/balance/total-value-by-address',
query: {
chains: OKX_X_LAYER_CHAIN_INDEX,
address,
},
});
}
export async function okxQuote(query: {
fromTokenAddress: string;
toTokenAddress: string;
amount: string;
slippage?: string;
userWalletAddress?: string;
}): Promise<Record<string, unknown>[]> {
return await okxRequest<Record<string, unknown>[]>({
path: '/api/v6/dex/aggregator/quote',
query: {
chainIndex: OKX_X_LAYER_CHAIN_INDEX,
fromTokenAddress: query.fromTokenAddress,
toTokenAddress: query.toTokenAddress,
amount: query.amount,
slippagePercent: normalizeSlippagePercent(query.slippage),
userWalletAddress: query.userWalletAddress,
},
});
}
export async function okxSwap(query: {
fromTokenAddress: string;
toTokenAddress: string;
amount: string;
userWalletAddress: string;
slippage?: string;
}): Promise<Record<string, unknown>[]> {
return await okxRequest<Record<string, unknown>[]>({
path: '/api/v6/dex/aggregator/swap',
query: {
chainIndex: OKX_X_LAYER_CHAIN_INDEX,
fromTokenAddress: query.fromTokenAddress,
toTokenAddress: query.toTokenAddress,
amount: query.amount,
slippagePercent: normalizeSlippagePercent(query.slippage),
userWalletAddress: query.userWalletAddress,
},
});
}