Skip to content

Commit

Permalink
add okx
Browse files Browse the repository at this point in the history
  • Loading branch information
0xngmi committed Oct 2, 2024
1 parent 9e32a04 commit a74ff0e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
84 changes: 84 additions & 0 deletions aggregators/okx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { CHAIN } from "../../helpers/chains";
import { FetchOptions } from "../../adapters/types";
import { getEnv } from "../../helpers/env";
import axios from "axios";
import { createHmac } from "crypto";
const plimit = require('p-limit');
const limits = plimit(1);

type TChain = {
[key: string]: number;
};
const CHAINS: TChain = {
[CHAIN.ETHEREUM]: 1,
[CHAIN.BSC]: 56,
[CHAIN.OKEXCHAIN]: 66,
[CHAIN.POLYGON]: 137,
[CHAIN.TRON]: 195,
[CHAIN.AVAX]: 43114,
[CHAIN.FANTOM]: 250,
[CHAIN.ARBITRUM]: 42161,
[CHAIN.OPTIMISM]: 10,
[CHAIN.CRONOS]: 25,
[CHAIN.SOLANA]: 501,
[CHAIN.OSMOSIS]: 706,
// 10001, "EthereumPoW"
[CHAIN.APTOS]: 637,
//[CHAIN.FLARE]: 14, // broken
[CHAIN.ERA]: 324,
[CHAIN.CONFLUX]: 1030, // Conflux eSpace
[CHAIN.SUI]: 784,
//[CHAIN.BITCOIN]: 0, // broken
[CHAIN.POLYGON_ZKEVM]: 1101,
[CHAIN.SEI]: 70000029,
[CHAIN.LINEA]: 59144,
[CHAIN.MANTLE]: 5000,
[CHAIN.BASE]: 8453,
[CHAIN.STACKS]: 5757,
[CHAIN.STARKNET]: 9004,
[CHAIN.SCROLL]: 534352,
[CHAIN.XLAYER]: 196,
[CHAIN.MANTA]: 169,
[CHAIN.METIS]: 1088,
[CHAIN.ZETA]: 7000,
[CHAIN.MERLIN]: 4200,
[CHAIN.BLAST]: 81457,
[CHAIN.MODE]: 34443,
[CHAIN.TON]: 607,
};

const fetch = async (_timestampParam: number, block: any, options: FetchOptions) => {
const timestamp = new Date().toISOString()
const path = `/api/v5/dex/aggregator/volume?timestamp=${options.endTimestamp * 1e3}&chainId=${CHAINS[options.chain]}`
const [secretKey, passphrase] = getEnv("0KX_API_KEY").split(":")
const data = await limits(() => axios.get(`https://www.okx.com${path}`, {
headers: {
'OK-ACCESS-PROJECT': 'be0ee327bbc230c3977c6868a77cd894',
'OK-ACCESS-KEY': 'feb1a319-69e0-4c00-96df-d1188d8a616a',
'OK-ACCESS-SIGN': createHmac('sha256', secretKey)
.update(timestamp + 'GET' + path)
.digest('base64'),
'OK-ACCESS-PASSPHRASE': passphrase,
'OK-ACCESS-TIMESTAMP': timestamp
}
}));
return {
dailyVolume: data.data.data.volumeUsdLast24hour,
timestamp: options.endTimestamp,
};
};

const adapter: any = {
version: 1, // api supports other timestamps but if you try using current timestamps, it breaks, so sticking to v1 even though it should be able to support v2
adapter: Object.keys(CHAINS).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: fetch,
start: 1652745600,
},
};
}, {}),
};

export default adapter;
3 changes: 2 additions & 1 deletion helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const ENV_KEYS = new Set([
'ZEROx_API_KEY',
'ZEROX_API_KEY',
'AGGREGATOR_0X_API_KEY',
'SUI_RPC'
'SUI_RPC',
'0KX_API_KEY'
])

// This is done to support both ZEROx_API_KEY and ZEROX_API_KEY
Expand Down

0 comments on commit a74ff0e

Please sign in to comment.