From 10663cd72eb8c94bbb810049b04ffbcb01c7253a Mon Sep 17 00:00:00 2001 From: Chase Moran Date: Fri, 29 Oct 2021 14:38:09 -0400 Subject: [PATCH] bridge_ui: total transacted value script Change-Id: Ib357ac62b32b1b1a30f53007c1e09b4971443fb0 --- bridge_ui/src/hooks/useTVL.ts | 4 + .../src/hooks/useTotalTransactedAmount.ts | 126 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 bridge_ui/src/hooks/useTotalTransactedAmount.ts diff --git a/bridge_ui/src/hooks/useTVL.ts b/bridge_ui/src/hooks/useTVL.ts index b9719656..23207ba5 100644 --- a/bridge_ui/src/hooks/useTVL.ts +++ b/bridge_ui/src/hooks/useTVL.ts @@ -47,6 +47,7 @@ export type TVL = { assetAddress: string; originChainId: ChainId; originChain: string; + decimals?: number; }; const calcEvmTVL = (covalentReport: any, chainId: ChainId): TVL[] => { @@ -67,6 +68,7 @@ const calcEvmTVL = (covalentReport: any, chainId: ChainId): TVL[] => { assetAddress: item.contract_address, originChainId: chainId, originChain: CHAINS_BY_ID[chainId].name, + decimals: item.contract_decimals, }); } }); @@ -113,6 +115,7 @@ const calcSolanaTVL = ( assetAddress: mint, originChainId: CHAIN_ID_SOLANA, originChain: "Solana", + decimals: item.account.data.parsed?.info?.tokenAmount?.decimals, }); }); @@ -176,6 +179,7 @@ const useTerraTVL = () => { totalValue, logo: getNativeTerraIcon(symbol), symbol, + decimals: NATIVE_TERRA_DECIMALS, }); }); } diff --git a/bridge_ui/src/hooks/useTotalTransactedAmount.ts b/bridge_ui/src/hooks/useTotalTransactedAmount.ts new file mode 100644 index 00000000..3dffbdb7 --- /dev/null +++ b/bridge_ui/src/hooks/useTotalTransactedAmount.ts @@ -0,0 +1,126 @@ +import { + hexToNativeString, + parseTransferPayload, +} from "@certusone/wormhole-sdk"; +import { formatUnits } from "@ethersproject/units"; +import axios from "axios"; +import { useEffect, useMemo, useState } from "react"; +import { DataWrapper } from "../store/helpers"; +import useTVL from "./useTVL"; + +function convertbase64ToBinary(base64: string) { + var raw = window.atob(base64); + var rawLength = raw.length; + var array = new Uint8Array(new ArrayBuffer(rawLength)); + + console.log(rawLength, "rawlength"); + + for (let i = 0; i < rawLength; i++) { + array[i] = raw.charCodeAt(i); + } + return array; +} + +//Don't actually mount this hook, it's way to expensive for the prod site. +const useTotalTransactedAmount = (): DataWrapper => { + const tvl = useTVL(); + const [everyVaaPayloadInHistory, setEveryVaaPayloadInHistory] = useState< + { EmitterChain: string; EmitterAddress: string; Payload: string }[] | null + >(null); + + useEffect(() => { + const URL = "http://localhost:8080/recent?numRows=15000"; + let response: { + EmitterChain: string; + EmitterAddress: string; + Payload: string; + }[] = []; + + axios.get(URL).then((result) => { + const payload = result?.data["*"]; + response = payload; + setEveryVaaPayloadInHistory(response as any); + }); + }, []); + + const output = useMemo(() => { + const emittersThatMatter = [ + `ec7372995d5cc8732397fb0ad35c0121e0eaa90d26f828a534cab54391b3a4f5`, //SOLANA TOKEN + `0000000000000000000000003ee18b2214aff97000d974cf647e7c347e8fa585`, //ETH token + `0000000000000000000000007cf7b764e38a0a5e967972c1df77d432510564e2`, //terra + `000000000000000000000000b6f6d86a8f9879a9c87f643768d9efc38c1da6e7`, //bsc + ]; + + if (!everyVaaPayloadInHistory || tvl.isFetching || !tvl.data) { + return 0; + } + + let total = 0; + + everyVaaPayloadInHistory.forEach((result) => { + const isImportant = emittersThatMatter.find( + (x) => x.toLowerCase() === result.EmitterAddress.toLowerCase() + ); + + if (!isImportant) { + return; + } + + console.log("about to parse", result.Payload); + let payload; + try { + payload = parseTransferPayload( + Buffer.from(convertbase64ToBinary(result.Payload)) + ); + } catch (e) { + console.log("parse fail"); + console.log(e); + } + + if (!payload) { + return; + } + + const assetAddress = + hexToNativeString(payload.originAddress, payload.originChain) || ""; + + const tvlItem = tvl.data?.find((item) => { + return ( + assetAddress && + item.assetAddress.toLowerCase() === assetAddress.toLowerCase() + ); + }); + + if (!assetAddress || !tvlItem) { + return; + } + + const quote = tvlItem?.quotePrice; + const decimals = + tvlItem?.decimals === undefined || tvlItem?.decimals === null + ? null + : tvlItem.decimals > 8 + ? 8 + : tvlItem.decimals; + const amount = + decimals != null && formatUnits(payload.amount.toString(), decimals); + + const valueAdd = + quote && amount && parseFloat(amount) && quote * parseFloat(amount); + console.log("value add", valueAdd); + + total = total + (valueAdd || 0); + }); + + return total; + }, [everyVaaPayloadInHistory, tvl.isFetching, tvl.data]); + + return { + data: output, + isFetching: tvl.isFetching || output === 0, + error: "", + receivedAt: null, + }; +}; + +export default useTotalTransactedAmount;