From 6a9da175004924a61a5c6f7583187eec16f6f00b Mon Sep 17 00:00:00 2001 From: Jordan Kee Date: Thu, 27 Apr 2023 11:53:31 +0800 Subject: [PATCH 1/3] Do todos --- .../walletkit-bot/src/api/StateRelayerBot.ts | 101 ++++++++++++++++-- 1 file changed, 94 insertions(+), 7 deletions(-) diff --git a/packages/walletkit-bot/src/api/StateRelayerBot.ts b/packages/walletkit-bot/src/api/StateRelayerBot.ts index 878cf2c..3c9165a 100644 --- a/packages/walletkit-bot/src/api/StateRelayerBot.ts +++ b/packages/walletkit-bot/src/api/StateRelayerBot.ts @@ -4,25 +4,54 @@ import { BigNumber } from "bignumber.js"; import { getWhaleClient } from "./DeFiChainCore"; +type TokenSymbolStringPair = { [tokenSymbol: string]: string }; + type PairData = { [pairSymbol: string]: { primaryTokenPrice: string; volume24H: string; totalLiquidity: string; apr: string; + pooledTokensCount: TokenSymbolStringPair; + conversationRate: TokenSymbolStringPair; + rewards: string; + commission: string; }; }; +type VaultData = { + totalVaults: number; + totalLoanValue: number; + totalCollateralValue: number; + totalCollateralizationRatio: number; + activeAuctions: number; +}; + +type MasternodesData = { + totalValueLocked: number; + zeroYearLocked: number; + fiveYearsLocked: number; + tenYearsLocked: number; +}; + +type BurnData = { + totalDFIburned: number; +}; + type DataStore = { // /dex totalValueLockInPoolPair: string; total24HVolume: string; pair: PairData; + vaults: VaultData; + masternodes: MasternodesData; + burns: BurnData; }; type StateRelayerHandlerProps = { urlNetwork: string; envNetwork: EnvironmentNetwork; + previousBlockHeight?: number; }; const DENOMINATION = "USDT"; @@ -30,14 +59,24 @@ const DENOMINATION = "USDT"; export async function handler( props: StateRelayerHandlerProps ): Promise { - const { urlNetwork, envNetwork } = props; + const { urlNetwork, envNetwork, previousBlockHeight } = props; const dataStore = {} as DataStore; try { - // TODO: Check if Function should run (blockHeight > 30 from previous) // Get Data from OCEAN API const client = getWhaleClient(urlNetwork, envNetwork); const statsData = await client.stats.get(); + + // Check if Function should run (blockHeight > 30 from previous) + if ( + previousBlockHeight && + statsData.count.blocks - previousBlockHeight <= 30 + ) { + throw new Error("Less than 30 blocks have passed since last query"); + return; // Do we want to return nothing, cached data or an error? + } + const rawPoolpairData = await client.poolpairs.list(200); + const dexPriceData = await client.poolpairs.listDexPrices(DENOMINATION); // sanitise response data @@ -51,7 +90,7 @@ export async function handler( // total24HVolume const total24HVolume = poolpairData.reduce( - (acc, currPair) => acc + (currPair.volume?.h24 ?? 0), + (acc, currPair) => acc + (currPair.volume?.h24 ?? 0), // This is not accurate due to rounding, is it fine for volume? 0 ); dataStore.total24HVolume = total24HVolume.toString(); @@ -72,18 +111,66 @@ export async function handler( return { ...acc, [currPair.displaySymbol]: { + // Overview primaryTokenPrice: tokenPrice.toString(), volume24H: currPair?.volume?.h24.toString() ?? "0", totalLiquidity: currPair.totalLiquidity.usd ?? "0", apr: currPair?.apr?.total.toString(), + // Detail + pooledTokensCount: { + [currPair.tokenA.displaySymbol]: currPair.tokenA.reserve, + [currPair.tokenB.displaySymbol]: currPair.tokenB.reserve, + }, + conversionRate: { + [currPair.tokenA.displaySymbol]: currPair.priceRatio.ab, + [currPair.tokenB.displaySymbol]: currPair.priceRatio.ba, + }, + rewards: (currPair.apr?.reward + ? currPair.apr.reward * 100 + : 0 + ).toString(), + commission: (currPair.apr?.commission + ? currPair.apr.commission * 100 + : 0 + ).toString(), }, } as PairData; }, {} as PairData); dataStore.pair = pair; - // TODO: Get Data from /dex/[pool-pair] - // TODO: Get Data from /vaults - // TODO: Get Data from /masternodes - // TODO: Get Data from all burns in ecosystem + + const loanData = statsData.loan; + // Get Data from /vaults + const vaults = { + totalVaults: loanData.count.openVaults, + totalLoanValue: loanData.value.loan, + totalCollateralValue: loanData.value.collateral, + totalCollateralizationRatio: + (loanData.value.collateral / loanData.value.loan) * 100, + activeAuctions: loanData.count.openAuctions, + }; + dataStore.vaults = vaults; + + // Get Data from /masternodes + const lockedMasternodes = statsData.masternodes.locked; + + const masternodes = { + totalValueLocked: statsData.tvl.masternodes, // Alternatively, can reduce lockedMasternodes to derive this data + zeroYearLocked: lockedMasternodes.find((m) => m.weeks === 0)?.count ?? 0, // Can technically access element at 0 index for speed + fiveYearsLocked: + lockedMasternodes.find((m) => m.weeks === 260)?.count ?? 0, + tenYearsLocked: + lockedMasternodes.find((m) => m.weeks === 520)?.count ?? 0, + }; + dataStore.masternodes = masternodes; + + // Get Data from all burns in ecosystem + const burns = { + // Other keys in burned such as address, auction, emission, + // fee, and payback do not seem to be used in scan. + totalDFIburned: statsData.burned.total, + }; + dataStore.burns = burns; + // Interfacing with SC // TODO: Connect with SC // TODO: Call SC Function to update Collated Data From dc6a77400fde4b2c78471ab970f2dac07c0c9c09 Mon Sep 17 00:00:00 2001 From: Jordan Kee Date: Thu, 27 Apr 2023 14:55:08 +0800 Subject: [PATCH 2/3] Address feedback --- .../walletkit-bot/src/api/StateRelayerBot.ts | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/packages/walletkit-bot/src/api/StateRelayerBot.ts b/packages/walletkit-bot/src/api/StateRelayerBot.ts index 3c9165a..afe4b97 100644 --- a/packages/walletkit-bot/src/api/StateRelayerBot.ts +++ b/packages/walletkit-bot/src/api/StateRelayerBot.ts @@ -20,22 +20,27 @@ type PairData = { }; type VaultData = { - totalVaults: number; - totalLoanValue: number; - totalCollateralValue: number; - totalCollateralizationRatio: number; - activeAuctions: number; + totalVaults: string; + totalLoanValue: string; + totalCollateralValue: string; + totalCollateralizationRatio: string; + activeAuctions: string; }; type MasternodesData = { - totalValueLocked: number; - zeroYearLocked: number; - fiveYearsLocked: number; - tenYearsLocked: number; + totalValueLocked: string; + zeroYearLocked: string; + fiveYearsLocked: string; + tenYearsLocked: string; }; type BurnData = { - totalDFIburned: number; + address: string; + fee: string; + auction: string; + payback: string; + emission: string; + total: string; }; type DataStore = { @@ -51,7 +56,6 @@ type DataStore = { type StateRelayerHandlerProps = { urlNetwork: string; envNetwork: EnvironmentNetwork; - previousBlockHeight?: number; }; const DENOMINATION = "USDT"; @@ -59,22 +63,13 @@ const DENOMINATION = "USDT"; export async function handler( props: StateRelayerHandlerProps ): Promise { - const { urlNetwork, envNetwork, previousBlockHeight } = props; + const { urlNetwork, envNetwork } = props; const dataStore = {} as DataStore; try { // Get Data from OCEAN API const client = getWhaleClient(urlNetwork, envNetwork); const statsData = await client.stats.get(); - // Check if Function should run (blockHeight > 30 from previous) - if ( - previousBlockHeight && - statsData.count.blocks - previousBlockHeight <= 30 - ) { - throw new Error("Less than 30 blocks have passed since last query"); - return; // Do we want to return nothing, cached data or an error? - } - const rawPoolpairData = await client.poolpairs.list(200); const dexPriceData = await client.poolpairs.listDexPrices(DENOMINATION); @@ -89,10 +84,10 @@ export async function handler( dataStore.totalValueLockInPoolPair = statsData.tvl.dex.toString(); // total24HVolume - const total24HVolume = poolpairData.reduce( - (acc, currPair) => acc + (currPair.volume?.h24 ?? 0), // This is not accurate due to rounding, is it fine for volume? - 0 - ); + const total24HVolume = poolpairData.reduce((acc, currPair) => { + const volume = new BigNumber(currPair.volume?.h24 ?? 0); + return acc.plus(volume); + }, new BigNumber(0)); dataStore.total24HVolume = total24HVolume.toString(); // pair @@ -126,11 +121,11 @@ export async function handler( [currPair.tokenB.displaySymbol]: currPair.priceRatio.ba, }, rewards: (currPair.apr?.reward - ? currPair.apr.reward * 100 + ? currPair.apr.reward : 0 ).toString(), commission: (currPair.apr?.commission - ? currPair.apr.commission * 100 + ? currPair.apr.commission : 0 ).toString(), }, @@ -141,12 +136,12 @@ export async function handler( const loanData = statsData.loan; // Get Data from /vaults const vaults = { - totalVaults: loanData.count.openVaults, - totalLoanValue: loanData.value.loan, - totalCollateralValue: loanData.value.collateral, + totalVaults: loanData.count.openVaults.toString(), + totalLoanValue: loanData.value.loan.toString(), + totalCollateralValue: loanData.value.collateral.toString(), totalCollateralizationRatio: - (loanData.value.collateral / loanData.value.loan) * 100, - activeAuctions: loanData.count.openAuctions, + ((loanData.value.collateral / loanData.value.loan) * 100).toString(), + activeAuctions: loanData.count.openAuctions.toString(), }; dataStore.vaults = vaults; @@ -154,22 +149,24 @@ export async function handler( const lockedMasternodes = statsData.masternodes.locked; const masternodes = { - totalValueLocked: statsData.tvl.masternodes, // Alternatively, can reduce lockedMasternodes to derive this data - zeroYearLocked: lockedMasternodes.find((m) => m.weeks === 0)?.count ?? 0, // Can technically access element at 0 index for speed + totalValueLocked: statsData.tvl.masternodes.toString(), // Alternatively, can reduce lockedMasternodes to derive this data + zeroYearLocked: (lockedMasternodes.find((m) => m.weeks === 0)?.count ?? 0).toString(), // Can technically access element at 0 index for speed fiveYearsLocked: - lockedMasternodes.find((m) => m.weeks === 260)?.count ?? 0, + (lockedMasternodes.find((m) => m.weeks === 260)?.count ?? 0).toString(), tenYearsLocked: - lockedMasternodes.find((m) => m.weeks === 520)?.count ?? 0, + (lockedMasternodes.find((m) => m.weeks === 520)?.count ?? 0).toString(), }; dataStore.masternodes = masternodes; // Get Data from all burns in ecosystem - const burns = { - // Other keys in burned such as address, auction, emission, - // fee, and payback do not seem to be used in scan. - totalDFIburned: statsData.burned.total, + dataStore.burns = { + address: dataStore.burns.address.toString(), + fee: dataStore.burns.fee.toString(), + auction: dataStore.burns.auction.toString(), + payback: dataStore.burns.address.toString(), + emission: dataStore.burns.emission.toString(), + total: dataStore.burns.total.toString(), }; - dataStore.burns = burns; // Interfacing with SC // TODO: Connect with SC From d1c6c415bd2c4df8475d5d85aa5756bfce3db1a6 Mon Sep 17 00:00:00 2001 From: Jordan Kee Date: Thu, 27 Apr 2023 16:53:21 +0800 Subject: [PATCH 3/3] Rename wrong variable --- .../walletkit-bot/src/api/StateRelayerBot.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/walletkit-bot/src/api/StateRelayerBot.ts b/packages/walletkit-bot/src/api/StateRelayerBot.ts index afe4b97..47218ed 100644 --- a/packages/walletkit-bot/src/api/StateRelayerBot.ts +++ b/packages/walletkit-bot/src/api/StateRelayerBot.ts @@ -50,7 +50,7 @@ type DataStore = { pair: PairData; vaults: VaultData; masternodes: MasternodesData; - burns: BurnData; + burned: BurnData; }; type StateRelayerHandlerProps = { @@ -159,13 +159,13 @@ export async function handler( dataStore.masternodes = masternodes; // Get Data from all burns in ecosystem - dataStore.burns = { - address: dataStore.burns.address.toString(), - fee: dataStore.burns.fee.toString(), - auction: dataStore.burns.auction.toString(), - payback: dataStore.burns.address.toString(), - emission: dataStore.burns.emission.toString(), - total: dataStore.burns.total.toString(), + dataStore.burned = { + address: statsData.burned.address.toString(), + fee: statsData.burned.fee.toString(), + auction: statsData.burned.auction.toString(), + payback: statsData.burned.address.toString(), + emission: statsData.burned.emission.toString(), + total: statsData.burned.total.toString(), }; // Interfacing with SC