Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add spot price in usd to epoch details #93

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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