Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coins/src/adapters/bridges/fraxtal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default async function bridge(): Promise<Token[]> {
from: `fraxtal:${token}`,
to: `ethereum:${l1Tokens[idx]}`,
symbol: symbols[idx],
decimals: decimals[idx],
decimals: Number(decimals[idx]),
});
});
const response = [tokens]
Expand Down
6 changes: 3 additions & 3 deletions coins/src/adapters/bridges/layerzero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default async function main() {
from: `${destinationChainSlug}:${destinationAddress}`,
to: `${sourceChainSlug}:${originAddress}`,
symbol,
decimals,
decimals: Number(decimals),
});
});
});
Expand Down Expand Up @@ -217,13 +217,13 @@ async function getMoreLayerZeroMappings(mappings: any[]) {
to: oftTokens[to] ?? to,
from: oftTokens[from] ?? from,
symbol,
decimals: decimals[from],
decimals: Number(decimals[from]),
});
mappings.push({
to: oftTokens[from] ?? from,
from: oftTokens[to] ?? to,
symbol,
decimals: decimals[to],
decimals: Number(decimals[to]),
});
});
});
Expand Down
48 changes: 48 additions & 0 deletions coins/src/cli/editEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { batchWrite, DELETE as deleteDDB } from "../utils/shared/dynamodb";
import { deleteRecords } from "../utils/coins3/es";
import { normalizeCoinId } from "../utils/coins3/utils";
import produce from "../utils/coins3/produce";

export async function deleteCoins(keys: { PK: string; SK: number }[]) {
if (keys.length === 0) return;

const esRecordsToDelete: { [index: string]: string[] } = {};
keys.forEach(({ SK, PK }) => {
const pid = normalizeCoinId(PK);
if (SK === 0) {
const index = "coins-metadata";
if (!esRecordsToDelete[index]) esRecordsToDelete[index] = [];
esRecordsToDelete[index].push(pid);
return;
}

const date = new Date(SK * 1000);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const index = `coins-timeseries-${year}-${month}`;
if (!esRecordsToDelete[index]) esRecordsToDelete[index] = [];
esRecordsToDelete[index].push(`${pid}_${SK}`);
});

await Promise.all(
Object.keys(esRecordsToDelete).map((index) =>
deleteRecords(index, esRecordsToDelete[index])
)
);

await deleteDDB(keys);
}

export async function updateCoins(items: any[]) {
items.map((item) => {
if (item.SK === 0 && item.symbol == undefined)
throw new Error("Symbol is required for coins-metadata");
if (item.price == undefined && !item.redirect)
throw new Error("Price or redirect is required");
if (item.adapter != "coingecko" && !item.decimals)
throw new Error("Decimals are required for non-coingecko coins");
});

await produce(items);
await batchWrite(items, true);
}
3 changes: 2 additions & 1 deletion coins/src/cli/markdownCoinHistorically.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DELETE,
getHistoricalValues,
} from "../utils/shared/dynamodb";
import { deleteCoins } from "./editEntry";

const start: number = 1690761600;
const coins: string[] = [
Expand Down Expand Up @@ -42,7 +43,7 @@ async function main() {
coins.slice(i, i + batchStep).map((t) => getHistoricalValues(t, start)),
);

await DELETE(current.flat());
await deleteCoins(current.flat());
}

const timestampArray = createTimestampArray(
Expand Down
2 changes: 1 addition & 1 deletion coins/src/cli/refill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function handler(adapterTorefill: string, timestamp: number) {
// .map((t) => getHistoricalValues(t.PK, start)),
// );

// await DELETE(current.flat());
// await deleteCoins(current.flat());
// }

for (let i = 0; i < resultsWithoutDuplicates.length; i += batchStep) {
Expand Down
11 changes: 6 additions & 5 deletions coins/src/scripts/coingecko.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setTimer } from "../utils/shared/coingeckoLocks";
import ddb, { batchGet, batchWrite, DELETE } from "../utils/shared/dynamodb";
import ddb, { batchGet, batchWrite } from "../utils/shared/dynamodb";
import {
Coin,
CoinMetadata,
Expand All @@ -20,6 +20,7 @@ import { storeAllTokens } from "../utils/shared/bridgedTvlPostgres";
import { sendMessage } from "../../../defi/src/utils/discord";
import { chainsThatShouldNotBeLowerCased } from "../utils/shared/constants";
import { cacheSolanaTokens, getSymbolAndDecimals } from "./coingeckoUtils";
import { deleteCoins } from "../cli/editEntry";

// Kill the script after 5 minutes to prevent infinite execution
const TIMEOUT_MS = 10 * 60 * 1000; // 5 minutes in milliseconds
Expand Down Expand Up @@ -131,9 +132,9 @@ async function getAndStoreCoins(coins: Coin[], rejected: Coin[]) {
SK: 0,
})),
)
).filter((c) => !c.adapter && c.confidence == 0.99);
).filter((c) => c.adapter == 'coingecko');

const deleteStaleKeysPromise = DELETE(
const deleteStaleKeysPromise = deleteCoins(
staleEntries.map((e) => ({
PK: e.PK,
SK: 0,
Expand Down Expand Up @@ -434,10 +435,10 @@ async function triggerFetchCoingeckoData(hourly: boolean, coinType?: string) {

if (coinType || hourly) {
const metadatas = await getCGCoinMetadatas(
coins.map((coin) => coin.id),
coins.map((coin: any) => coin.id),
coinType,
);
coins = coins.filter((coin) => {
coins = coins.filter((coin: any) => {
const metadata = metadatas[coin.id];
if (!metadata) return true; // if we don't have metadata, we don't know if it's over 10m
if (hourly) {
Expand Down
2 changes: 1 addition & 1 deletion coins/src/scripts/coingeckoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export async function getSymbolAndDecimals(
const tronApi = new sdk.ChainApi({ chain: "tron" });
return {
symbol: await tronApi.call({ target: originalAddress!, abi: "erc20:symbol" }),
decimals: await tronApi.call({ target: originalAddress!, abi: "erc20:decimals" }),
decimals: await tronApi.call({ target: originalAddress!, abi: "erc20:decimals" }).then(r => Number(r)),
};

case 'stellar':
Expand Down
18 changes: 18 additions & 0 deletions coins/src/utils/coins3/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ export function getMetadataRecord(json: any): MetadataRecord | undefined {
return recordClone;
}

// deletes records
export async function deleteRecords(index: string,keys: string[]): Promise<void> {
const client = getClient();
if (!client) throw new Error("Elasticsearch client not configured");

try {
const body: any[] = [];
for (const pid of keys) {
body.push({ delete: { _index: index, _id: pid } });
}

if (body.length) await esClient.bulk({ body });
} catch (err) {
console.error("Bulk delete from coins-metadata failed:", err);
throw err;
}
}

function normalizeRecord(record: any): MetadataRecord {
const pid = normalizeCoinId(record.PK);
if (record.redirect) {
Expand Down