Skip to content

Commit

Permalink
feat: add spot price in usd to epoch details
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight committed Oct 25, 2024
1 parent 9aa6749 commit 238e98b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cli/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +145,36 @@ export class Client {
}
}

async getPrice(): Promise<Price> {
try {
const { data } = await axios.get<Pool>(
'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,
Expand Down
10 changes: 10 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -93,6 +95,8 @@ const processEpoch = async () => {
totalRewardUnits,
distributionRate: metadata.distributionRate,
burnRate: metadata.burnRate,
assetPriceUsd,
runePriceUsd,
treasuryAddress: metadata.treasuryAddress,
distributionStatus: 'pending',
distributionsByStakingAddress,
Expand Down Expand Up @@ -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',
})

Expand Down
4 changes: 4 additions & 0 deletions cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 238e98b

Please sign in to comment.