|
| 1 | +const BigNumber = require('bignumber.js'); |
| 2 | +const { default: axios } = require('axios'); |
| 3 | +const { request } = require('graphql-request'); |
| 4 | + |
| 5 | +const { queryPrices, queryMoneyMarkets, queryRewards } = require('./queries') |
| 6 | +const { calcLiquidStakingExchangeRate, calcSimulateExchangeRate } = require('./math'); |
| 7 | + |
| 8 | +const API_URL = 'https://mainnet-api.hatom.com/graphql'; |
| 9 | + |
| 10 | +async function getMoneyMarkets() { |
| 11 | + const response = await request(API_URL, queryMoneyMarkets, {}); |
| 12 | + return response.queryMoneyMarket.reduce((prev, market) => { |
| 13 | + const symbol = market.underlying.symbol; |
| 14 | + const value = { |
| 15 | + address: market.address, |
| 16 | + decimals: market.underlying.decimals, |
| 17 | + cash: market.stateHistory[0].cash, |
| 18 | + borrows: market.stateHistory[0].borrows, |
| 19 | + reserves: market.stateHistory[0].reserves, |
| 20 | + rate: market.stateHistory[0].supplyRatePerSecond, |
| 21 | + timestamp: market.stateHistory[0].timestamp, |
| 22 | + totalSupply: market.stateHistory[0].totalSupply, |
| 23 | + borrowRatePerSecond: market.stateHistory[0].borrowRatePerSecond, |
| 24 | + supplyAPY: market.stateHistory[0].supplyAPY, |
| 25 | + supplyRatePerSecond: market.stateHistory[0].supplyRatePerSecond, |
| 26 | + totalColateral: market.totalCollateral, |
| 27 | + } |
| 28 | + return { |
| 29 | + ...prev, |
| 30 | + [symbol]: value, |
| 31 | + }; |
| 32 | + }, {}) |
| 33 | +} |
| 34 | + |
| 35 | +async function getTokenPrices() { |
| 36 | + const { queryToken, queryLiquidStaking } = |
| 37 | + await request(API_URL, queryPrices, {}); |
| 38 | + |
| 39 | + const liquidStakingExchangeRate = calcLiquidStakingExchangeRate( |
| 40 | + queryLiquidStaking?.[0]?.state?.cashReserve, |
| 41 | + queryLiquidStaking?.[0]?.state?.totalShares, |
| 42 | + ); |
| 43 | + |
| 44 | + const queryTokenPopulated = queryToken |
| 45 | + .filter( |
| 46 | + ({ dailyPriceHistory, symbol }) => |
| 47 | + dailyPriceHistory.length > 0 || symbol === 'EGLD', |
| 48 | + ) |
| 49 | + .map((tokenItem) => { |
| 50 | + const filteredToken = tokenItem.dailyPriceHistory; |
| 51 | + |
| 52 | + const priceEgld = filteredToken?.[0]?.quote?.priceInEgld || '0'; |
| 53 | + |
| 54 | + let dailyPriceInEgld = '0'; |
| 55 | + |
| 56 | + if (tokenItem.symbol == 'EGLD') { |
| 57 | + dailyPriceInEgld = '1'; |
| 58 | + } else if (tokenItem.symbol == 'SEGLD') { |
| 59 | + dailyPriceInEgld = new BigNumber(1).multipliedBy(liquidStakingExchangeRate).dividedBy(BigNumber.WAD).toString(); |
| 60 | + } else { |
| 61 | + dailyPriceInEgld = priceEgld; |
| 62 | + } |
| 63 | + |
| 64 | + const dailyPriceInUSD = filteredToken?.[0]?.price?.price || '0'; |
| 65 | + |
| 66 | + return { |
| 67 | + ...tokenItem, |
| 68 | + dailyPriceInEgld, |
| 69 | + dailyPriceInUSD, |
| 70 | + }; |
| 71 | + }); |
| 72 | + |
| 73 | + const itemEgldInUSD = queryTokenPopulated.find( |
| 74 | + ({ symbol }) => symbol === 'EGLD', |
| 75 | + ); |
| 76 | + const itemEgldInUSDC = queryTokenPopulated.find( |
| 77 | + ({ symbol }) => symbol === 'USDC', |
| 78 | + ); |
| 79 | + |
| 80 | + const agregatorEGLDInUSD = new BigNumber( |
| 81 | + itemEgldInUSD?.dailyPriceInUSD || '0', |
| 82 | + ) |
| 83 | + .dividedBy(`1e${18}`) |
| 84 | + .toString(); |
| 85 | + |
| 86 | + const priceHistoryEGLDInUSDC = |
| 87 | + new BigNumber(1) |
| 88 | + .dividedBy(itemEgldInUSDC?.dailyPriceInEgld || 0) |
| 89 | + .toString() || '0'; |
| 90 | + |
| 91 | + const usdcPriceInEgld = new BigNumber(agregatorEGLDInUSD).isZero() |
| 92 | + ? priceHistoryEGLDInUSDC |
| 93 | + : agregatorEGLDInUSD; |
| 94 | + |
| 95 | + const egldInUsdc = usdcPriceInEgld !== '0' ? usdcPriceInEgld : '0'; |
| 96 | + |
| 97 | + return queryTokenPopulated.reduce( |
| 98 | + (prev, { dailyPriceInEgld, dailyPriceInUSD, symbol }) => { |
| 99 | + const priceUSD = |
| 100 | + !new BigNumber(egldInUsdc).isEqualTo('0') || |
| 101 | + !new BigNumber(dailyPriceInEgld).isEqualTo('0') |
| 102 | + ? new BigNumber(egldInUsdc) |
| 103 | + .multipliedBy(dailyPriceInEgld) |
| 104 | + .toString() |
| 105 | + : '0'; |
| 106 | + |
| 107 | + const value = !new BigNumber(dailyPriceInUSD).isZero() |
| 108 | + ? new BigNumber(dailyPriceInUSD).dividedBy(`1e${18}`).toString() |
| 109 | + : priceUSD; |
| 110 | + |
| 111 | + return { |
| 112 | + ...prev, |
| 113 | + [symbol]: value, |
| 114 | + }; |
| 115 | + }, |
| 116 | + {}, |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +function getExchangeRates(moneyMarkets) { |
| 121 | + const symbols = Object.keys(moneyMarkets); |
| 122 | + return symbols.reduce( |
| 123 | + (prev, symbol) => { |
| 124 | + const value = calcSimulateExchangeRate({ |
| 125 | + cash: moneyMarkets[symbol].cash || '0', |
| 126 | + borrows: |
| 127 | + moneyMarkets[symbol].borrows || '0', |
| 128 | + reserves: |
| 129 | + moneyMarkets[symbol].reserves || '0', |
| 130 | + totalSupply: |
| 131 | + moneyMarkets[symbol].totalSupply || '0', |
| 132 | + rate: |
| 133 | + moneyMarkets[symbol].supplyRatePerSecond || '0', |
| 134 | + timestamp: |
| 135 | + moneyMarkets[symbol].timestamp || new Date().toISOString(), |
| 136 | + }) |
| 137 | + return { |
| 138 | + ...prev, |
| 139 | + [symbol]: value |
| 140 | + } |
| 141 | + }, |
| 142 | + {}, |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +async function getRewardsBatches() { |
| 147 | + const response = await request(API_URL, queryRewards, {}); |
| 148 | + return response.queryRewardsBatchState.reduce((prev, batch) => { |
| 149 | + const symbol = batch.moneyMarket.underlying.symbol; |
| 150 | + const value = { |
| 151 | + id: batch.id, |
| 152 | + speed: batch.speed, |
| 153 | + type: batch.type, |
| 154 | + endTime: batch.endTime, |
| 155 | + fullyDistributed: batch.fullyDistributed, |
| 156 | + totalAmount: batch.totalAmount, |
| 157 | + rewardsToken: batch.rewardsToken, |
| 158 | + } |
| 159 | + return { |
| 160 | + ...prev, |
| 161 | + [symbol]: value, |
| 162 | + }; |
| 163 | + }, {}) |
| 164 | +} |
| 165 | + |
| 166 | +module.exports = { |
| 167 | + getMoneyMarkets, |
| 168 | + getTokenPrices, |
| 169 | + getExchangeRates, |
| 170 | + getRewardsBatches, |
| 171 | +} |
0 commit comments