|
| 1 | +import axios from "axios"; |
| 2 | +import { getEnvValue } from "./env"; |
| 3 | +import runInPromisePool from "./promisePool"; |
| 4 | + |
| 5 | +type CoinsApiData = { |
| 6 | + decimals: number; |
| 7 | + price: number; |
| 8 | + symbol: string; |
| 9 | + timestamp: number; |
| 10 | + PK?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type McapsApiData = { |
| 14 | + mcap: number; |
| 15 | + timestamp: number; |
| 16 | +}; |
| 17 | + |
| 18 | +const coinsApiKey = getEnvValue("COINS_API_KEY") |
| 19 | +const bodySize = 2; // 100; |
| 20 | + |
| 21 | +function getBodies(readKeys: string[], timestamp: number | "now") { |
| 22 | + const bodies: string[] = []; |
| 23 | + for (let i = 0; i < readKeys.length; i += bodySize) { |
| 24 | + const body = { |
| 25 | + coins: readKeys.slice(i, i + bodySize), |
| 26 | + } as any; |
| 27 | + if (timestamp !== "now") body.timestamp = timestamp; |
| 28 | + bodies.push(JSON.stringify(body)); |
| 29 | + } |
| 30 | + |
| 31 | + return bodies; |
| 32 | +} |
| 33 | + |
| 34 | +function sleep(ms: number) { |
| 35 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 36 | +} |
| 37 | + |
| 38 | +async function restCallWrapper( |
| 39 | + request: () => Promise<any>, |
| 40 | + retries: number = 3, |
| 41 | + name: string = "-" |
| 42 | +) { |
| 43 | + while (retries > 0) { |
| 44 | + try { |
| 45 | + const res = await request(); |
| 46 | + return res; |
| 47 | + } catch { |
| 48 | + await sleep(5_000 + 10_000 * Math.random()); |
| 49 | + restCallWrapper(request, retries--, name); |
| 50 | + } |
| 51 | + } |
| 52 | + throw new Error(`couldnt work ${name} call after retries!`); |
| 53 | +} |
| 54 | + |
| 55 | +const priceCache: { [PK: string]: any } = { |
| 56 | + "coingecko:tether": { |
| 57 | + price: 1, |
| 58 | + symbol: "USDT", |
| 59 | + timestamp: Math.floor(Date.now() / 1e3 + 3600), // an hour from script start time |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +export async function getPrices( |
| 64 | + readKeys: string[], |
| 65 | + timestamp: number | "now" |
| 66 | +): Promise<{ [address: string]: CoinsApiData }> { |
| 67 | + if (!readKeys.length) return {}; |
| 68 | + |
| 69 | + const aggregatedRes: { [address: string]: CoinsApiData } = {}; |
| 70 | + |
| 71 | + // read data from cache where possible |
| 72 | + readKeys = readKeys.filter((PK: string) => { |
| 73 | + if (timestamp !== "now") return true; |
| 74 | + if (priceCache[PK]) { |
| 75 | + aggregatedRes[PK] = { ...priceCache[PK], PK }; |
| 76 | + return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | + }); |
| 80 | + |
| 81 | + const bodies = getBodies(readKeys, timestamp); |
| 82 | + const tokenData: CoinsApiData[][] = []; |
| 83 | + await runInPromisePool({ |
| 84 | + items: bodies, |
| 85 | + concurrency: 10, |
| 86 | + processor: async (body: string) => { |
| 87 | + const res = await restCallWrapper(() => |
| 88 | + axios.post( |
| 89 | + `https://coins.llama.fi/prices?source=internal${coinsApiKey ? `?apikey=${coinsApiKey}` : "" |
| 90 | + }`, |
| 91 | + body, |
| 92 | + { |
| 93 | + headers: { "Content-Type": "application/json" }, |
| 94 | + params: { source: "internal", apikey: coinsApiKey }, |
| 95 | + }, |
| 96 | + ) |
| 97 | + ); |
| 98 | + |
| 99 | + const data = (res.data.coins = Object.entries(res.data.coins).map( |
| 100 | + ([PK, value]) => ({ |
| 101 | + ...(value as CoinsApiData), |
| 102 | + PK, |
| 103 | + }) |
| 104 | + )); |
| 105 | + |
| 106 | + tokenData.push(data); |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + const normalizedReadKeys = readKeys.map((k: string) => k.toLowerCase()); |
| 111 | + tokenData.map((batch: CoinsApiData[]) => { |
| 112 | + batch.map((a: CoinsApiData) => { |
| 113 | + if (!a.PK) return; |
| 114 | + const i = normalizedReadKeys.indexOf(a.PK.toLowerCase()); |
| 115 | + aggregatedRes[readKeys[i]] = a; |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + return aggregatedRes; |
| 120 | +} |
| 121 | + |
| 122 | +const mcapCache: { [PK: string]: any } = {}; |
| 123 | + |
| 124 | +export async function getMcaps( |
| 125 | + readKeys: string[], |
| 126 | + timestamp: number | "now" |
| 127 | +): Promise<{ [address: string]: McapsApiData }> { |
| 128 | + if (!readKeys.length) return {}; |
| 129 | + |
| 130 | + const aggregatedRes: { [address: string]: McapsApiData } = {}; |
| 131 | + |
| 132 | + // read data from cache where possible |
| 133 | + readKeys = readKeys.filter((PK: string) => { |
| 134 | + if (timestamp !== "now") return true; |
| 135 | + if (mcapCache[PK]) { |
| 136 | + aggregatedRes[PK] = { ...mcapCache[PK], PK }; |
| 137 | + return false; |
| 138 | + } |
| 139 | + return true; |
| 140 | + }); |
| 141 | + |
| 142 | + const bodies = getBodies(readKeys, timestamp); |
| 143 | + const tokenData: { [key: string]: McapsApiData }[] = []; |
| 144 | + await runInPromisePool({ |
| 145 | + items: bodies, |
| 146 | + concurrency: 10, |
| 147 | + processor: async (body: string) => { |
| 148 | + const res = await restCallWrapper(() => |
| 149 | + axios.post( |
| 150 | + `https://coins.llama.fi/mcaps${coinsApiKey ? `?apikey=${coinsApiKey}` : "" |
| 151 | + }`, |
| 152 | + body, |
| 153 | + { |
| 154 | + headers: { "Content-Type": "application/json" }, |
| 155 | + } |
| 156 | + ) |
| 157 | + ); |
| 158 | + tokenData.push(res.data as any); |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const normalizedReadKeys = readKeys.map((k: string) => k.toLowerCase()); |
| 163 | + tokenData.map((batch: { [key: string]: McapsApiData }) => { |
| 164 | + Object.keys(batch).map((a: string) => { |
| 165 | + if (!batch[a].mcap) return; |
| 166 | + const i = normalizedReadKeys.indexOf(a.toLowerCase()); |
| 167 | + aggregatedRes[readKeys[i]] = batch[a]; |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + return aggregatedRes; |
| 172 | +} |
0 commit comments