2022-10-28 08:24:25 -07:00
|
|
|
import { grabTerraCustodyData } from "./getTerraCustody";
|
|
|
|
import { grabSolanaCustodyData } from "./getSolanaCustody";
|
|
|
|
import { grabEvmCustodyData } from "./getEvmCustody";
|
|
|
|
import { MongoClient } from "mongodb";
|
2022-11-07 07:49:49 -08:00
|
|
|
import { grabNearCustodyData } from "./getNearCustody";
|
|
|
|
import { grabAlgoCustodyData } from "./getAlgorandCustody";
|
|
|
|
import { grabAptosCustodyData } from "./getAptosCustody";
|
|
|
|
import { sleepFor } from "./utils";
|
2022-10-28 08:24:25 -07:00
|
|
|
interface Token {
|
|
|
|
tokenAddress: string;
|
|
|
|
name: string;
|
|
|
|
decimals: number;
|
|
|
|
symbol: string;
|
|
|
|
balance: BigInt;
|
|
|
|
qty: number;
|
|
|
|
tokenPrice: number;
|
|
|
|
tokenBalanceUSD: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CustodyInfo {
|
|
|
|
_id: string;
|
2022-10-28 13:44:10 -07:00
|
|
|
updatedAt: string;
|
2022-10-28 08:24:25 -07:00
|
|
|
chainName: string;
|
|
|
|
chainId: number;
|
|
|
|
emitterAddress: string;
|
|
|
|
custodyUSD: number;
|
|
|
|
tokens: Token[];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateTable(chainInfo, client: MongoClient) {
|
2022-11-07 07:49:49 -08:00
|
|
|
if (chainInfo === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-28 08:24:25 -07:00
|
|
|
const custodyList = chainInfo.balances;
|
2022-10-28 13:44:10 -07:00
|
|
|
if (custodyList.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-28 08:24:25 -07:00
|
|
|
try {
|
|
|
|
const totalCustodyUSD = custodyList
|
|
|
|
.map((x) => x.tokenBalanceUSD)
|
|
|
|
.reduce((partialSum, a) => partialSum + a, 0);
|
|
|
|
console.log("totalCustodyUSD=", totalCustodyUSD);
|
|
|
|
|
|
|
|
const database = client.db("onchain_data");
|
|
|
|
// Specifying a Schema is optional, but it enables type hints on
|
|
|
|
// finds and inserts
|
|
|
|
const chainId = chainInfo.chain_id;
|
|
|
|
const emitterAddress = chainInfo.emitter_address;
|
|
|
|
const custody = database.collection<CustodyInfo>("custody");
|
|
|
|
const result = await custody.updateOne(
|
|
|
|
{ _id: `${chainId}/${emitterAddress}` },
|
|
|
|
{
|
|
|
|
$set: {
|
2022-10-28 13:44:10 -07:00
|
|
|
updatedAt: new Date().toISOString(),
|
2022-10-28 08:24:25 -07:00
|
|
|
chainName: chainInfo.name,
|
|
|
|
chainId: chainId,
|
|
|
|
emitterAddress: emitterAddress,
|
|
|
|
custodyUSD: totalCustodyUSD,
|
|
|
|
tokens: custodyList,
|
|
|
|
_id: `${chainId}/${emitterAddress}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ upsert: true }
|
|
|
|
);
|
|
|
|
console.log(`A document was inserted with the _id: ${result.upsertedId}`);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(encodeURIComponent);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const useAllowListstr = process.env.allowlist || "false";
|
|
|
|
|
2022-10-28 13:44:10 -07:00
|
|
|
export async function getCustodyData() {
|
2022-10-28 08:24:25 -07:00
|
|
|
const uri = process.env.MONGODB_URI;
|
|
|
|
if (uri === "" || uri === undefined) {
|
|
|
|
console.log("No mongodb uri supplied");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const client = new MongoClient(uri);
|
|
|
|
|
|
|
|
const useAllowList = true ? useAllowListstr === "true" : false;
|
2022-11-07 07:49:49 -08:00
|
|
|
const timeout = 5000;
|
2022-10-28 08:24:25 -07:00
|
|
|
const promises = [
|
|
|
|
grabSolanaCustodyData("1", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("2", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabTerraCustodyData("3", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("4", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("5", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("6", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("7", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
|
|
|
grabAlgoCustodyData("8", useAllowList),
|
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("9", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("10", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("11", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("12", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("13", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("14", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
|
|
|
grabNearCustodyData("15", useAllowList),
|
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabEvmCustodyData("16", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
2022-10-28 08:24:25 -07:00
|
|
|
grabTerraCustodyData("18", useAllowList),
|
2022-11-07 07:49:49 -08:00
|
|
|
await new Promise((res) => setTimeout(res, timeout)),
|
|
|
|
grabAptosCustodyData("22", useAllowList),
|
2022-10-28 08:24:25 -07:00
|
|
|
// grabTerraCustodyData("28", useAllowList),
|
|
|
|
];
|
|
|
|
|
|
|
|
const output = await Promise.all(promises);
|
|
|
|
// iterate through chains & insert into mongodb
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < output.length; i++) {
|
|
|
|
const data = output[i];
|
|
|
|
await updateTable(data, client);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
} finally {
|
|
|
|
await client.close();
|
|
|
|
}
|
2022-10-28 13:44:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default getCustodyData;
|