|
| 1 | +const sdk = require('@defillama/sdk'); |
| 2 | +const utils = require('../utils'); |
| 3 | +const axios = require('axios'); |
| 4 | + |
| 5 | +const chain = 'plasma'; |
| 6 | + |
| 7 | +const yzUSD = '0x6695c0f8706c5ace3bdf8995073179cca47926dc'; |
| 8 | +const syzUSD = '0xc8a8df9b210243c55d31c73090f06787ad0a1bf6'; |
| 9 | + |
| 10 | +const yzUSD_unit = 1e18; |
| 11 | +const syzUSD_unit = 1e18; |
| 12 | + |
| 13 | +const YEAR_IN_DAYS = 365; |
| 14 | +const DAY_IN_SECONDS = 24 * 60 * 60; |
| 15 | + |
| 16 | +const APY_REFERENCE_PERIOD_IN_DAYS = 7; |
| 17 | + |
| 18 | +const getUsdPrice = async () => { |
| 19 | + try { |
| 20 | + const priceKey = `${chain}:${syzUSD}`; |
| 21 | + const { data } = await axios.get(`https://coins.llama.fi/prices/current/${priceKey}`); |
| 22 | + return data.coins[priceKey].price; |
| 23 | + } catch (error) { |
| 24 | + console.error('Error fetching USD price:', error); |
| 25 | + throw error; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const getTotalSupply = async () => { |
| 30 | + try { |
| 31 | + const { output } = await sdk.api.abi.call({ |
| 32 | + target: syzUSD, |
| 33 | + abi: 'erc20:totalSupply', |
| 34 | + chain, |
| 35 | + }); |
| 36 | + return output / syzUSD_unit; |
| 37 | + } catch (error) { |
| 38 | + console.error('Error fetching total supply:', error); |
| 39 | + throw error; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +const getRedemptionPrice = async (blockNumber) => { |
| 44 | + try { |
| 45 | + const { output } = await sdk.api.abi.call({ |
| 46 | + target: syzUSD, |
| 47 | + abi: 'function previewRedeem(uint256 shares) external view returns (uint256)', |
| 48 | + params: [BigInt(syzUSD_unit)], |
| 49 | + chain, |
| 50 | + block: blockNumber, |
| 51 | + }); |
| 52 | + return output / yzUSD_unit; |
| 53 | + } catch (error) { |
| 54 | + console.error('Error fetching redemption price:', error); |
| 55 | + throw error; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +const calculateUsdTvl = async () => { |
| 60 | + try { |
| 61 | + const totalSupply = await getTotalSupply(); |
| 62 | + const tokenPrice = await getUsdPrice(); |
| 63 | + return totalSupply * tokenPrice; |
| 64 | + } catch (error) { |
| 65 | + console.error('Error calculating USD TVL:', error); |
| 66 | + throw error; |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +const calculateApy = async () => { |
| 71 | + try { |
| 72 | + const currentBlock = await sdk.api.util.getLatestBlock(chain); |
| 73 | + const startTimestamp = currentBlock.timestamp - APY_REFERENCE_PERIOD_IN_DAYS * DAY_IN_SECONDS; |
| 74 | + const [startBlock] = await utils.getBlocksByTime([startTimestamp], chain); |
| 75 | + |
| 76 | + const [startPrice, currentPrice] = await Promise.all([getRedemptionPrice(startBlock), getRedemptionPrice(currentBlock.block)]); |
| 77 | + |
| 78 | + const appreciationRatio = currentPrice / startPrice; |
| 79 | + const apy = (Math.pow(appreciationRatio, YEAR_IN_DAYS / APY_REFERENCE_PERIOD_IN_DAYS) - 1) * 100; |
| 80 | + |
| 81 | + return apy; |
| 82 | + } catch (error) { |
| 83 | + console.error('Error calculating APY:', error); |
| 84 | + throw error; |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +const syzUSDPool = async () => { |
| 89 | + try { |
| 90 | + const [tvlUsd, apyBase] = await Promise.all([calculateUsdTvl(), calculateApy()]); |
| 91 | + return { |
| 92 | + tvlUsd, |
| 93 | + apyBase, |
| 94 | + pool: (`${syzUSD}-${chain}`).toLowerCase(), |
| 95 | + chain: 'Plasma', |
| 96 | + project: 'yuzu-money', |
| 97 | + symbol: 'yzUSD', |
| 98 | + underlyingTokens: [yzUSD], |
| 99 | + url: 'https://app.yuzu.money/syzusd', |
| 100 | + }; |
| 101 | + } catch (error) { |
| 102 | + console.error('Error fetching pool data:', error); |
| 103 | + throw error; |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +const apy = async () => { |
| 108 | + try { |
| 109 | + return [await syzUSDPool()]; |
| 110 | + } catch (error) { |
| 111 | + console.error('Error fetching pools:', error); |
| 112 | + throw error; |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +module.exports = { |
| 117 | + apy, |
| 118 | +}; |
0 commit comments