From 238e98b2b2d1df3e4a6c25dd6f5e60fb7d6e563a Mon Sep 17 00:00:00 2001 From: kaladinlight <35275952+kaladinlight@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:22:09 -0600 Subject: [PATCH] feat: add spot price in usd to epoch details --- cli/src/client.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ cli/src/index.ts | 10 ++++++++++ cli/src/types.ts | 4 ++++ 3 files changed, 56 insertions(+) diff --git a/cli/src/client.ts b/cli/src/client.ts index 523f7a5..8197465 100644 --- a/cli/src/client.ts +++ b/cli/src/client.ts @@ -18,12 +18,24 @@ if (!INFURA_API_KEY) { const AVERAGE_BLOCK_TIME_BLOCKS = 1000 const ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS = '0xac2a4fd70bcd8bab0662960455c363735f0e2b56' +const THORCHAIN_PRECISION = 8 type Revenue = { address: string amount: string } +type Pool = { + balance_asset: string + balance_rune: string + asset_tor_price: string +} + +type Price = { + assetPriceUsd: string + runePriceUsd: string +} + type ClosingState = { rewardUnits: bigint totalRewardUnits: bigint @@ -133,6 +145,36 @@ export class Client { } } + async getPrice(): Promise { + try { + const { data } = await axios.get( + 'https://daemon.thorchain.shapeshift.com/lcd/thorchain/pool/ETH.FOX-0XC770EEFAD204B5180DF6A14EE197D99D808EE52D', + ) + + const runeInAsset = new BigNumber(data.balance_asset).div(data.balance_rune) + const assetPriceUsd = new BigNumber(data.asset_tor_price) + .div(new BigNumber(10).pow(THORCHAIN_PRECISION)) + .toFixed(THORCHAIN_PRECISION) + const runePriceUsd = runeInAsset.times(assetPriceUsd).toFixed(THORCHAIN_PRECISION) + + info(`Current asset price (USD): ${assetPriceUsd}`) + info(`Current rune price (USD): ${runePriceUsd}`) + + return { + assetPriceUsd, + runePriceUsd, + } + } catch (err) { + if (isAxiosError(err)) { + error(`Failed to get price: ${err.message}, exiting.`) + } else { + error('Failed to get price, exiting.') + } + + process.exit(1) + } + } + private async getClosingStateByStakingAddress( addresses: Address[], startBlock: bigint, diff --git a/cli/src/index.ts b/cli/src/index.ts index f8d4bd9..670e191 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -83,6 +83,8 @@ const processEpoch = async () => { totalDistribution, ) + const { assetPriceUsd, runePriceUsd } = await client.getPrice() + const epochHash = await ipfs.addEpoch({ number: metadata.epoch, startTimestamp: metadata.epochStartTimestamp, @@ -93,6 +95,8 @@ const processEpoch = async () => { totalRewardUnits, distributionRate: metadata.distributionRate, burnRate: metadata.burnRate, + assetPriceUsd, + runePriceUsd, treasuryAddress: metadata.treasuryAddress, distributionStatus: 'pending', distributionsByStakingAddress, @@ -192,13 +196,19 @@ const update = async () => { } const processDistribution = async (metadata: RFOXMetadata, epoch: Epoch, wallet: Wallet, ipfs: IPFS) => { + const client = await Client.new() + const epochHash = metadata.ipfsHashByEpoch[epoch.number] await wallet.fund(epoch, epochHash) const processedEpoch = await wallet.distribute(epoch, epochHash) + const { assetPriceUsd, runePriceUsd } = await client.getPrice() + const processedEpochHash = await ipfs.addEpoch({ ...processedEpoch, + assetPriceUsd, + runePriceUsd, distributionStatus: 'complete', }) diff --git a/cli/src/types.ts b/cli/src/types.ts index 9ddcd72..0d6c727 100644 --- a/cli/src/types.ts +++ b/cli/src/types.ts @@ -81,6 +81,10 @@ export type Epoch = { distributionRate: number /** The percentage of revenue (RUNE) accumulated by the treasury to be used to buy FOX from the open market and subsequently burned for this epoch */ burnRate: number + /** The spot price of asset in USD */ + assetPriceUsd: string + /** The spot price of rune in USD */ + runePriceUsd: string /** The treasury address on THORChain used to determine revenue earned by the DAO for rFOX reward distributions and total burn */ treasuryAddress: string /** The status of the reward distribution */