|
| 1 | +const sdk = require('@defillama/sdk'); |
| 2 | +const axios = require('axios'); |
| 3 | +const utils = require('../utils'); |
| 4 | +const { ethers } = require('ethers'); |
| 5 | + |
| 6 | +const xUSD = '0xe80772Eaf6e2E18B651F160Bc9158b2A5caFCA65'; |
| 7 | + |
| 8 | +const eventAbi = [ |
| 9 | + { |
| 10 | + anonymous: false, |
| 11 | + inputs: [ |
| 12 | + { |
| 13 | + indexed: false, |
| 14 | + internalType: 'uint256', |
| 15 | + name: 'profit', |
| 16 | + type: 'uint256', |
| 17 | + }, |
| 18 | + { |
| 19 | + indexed: false, |
| 20 | + internalType: 'uint256', |
| 21 | + name: 'newLiquidityIndex', |
| 22 | + type: 'uint256', |
| 23 | + }, |
| 24 | + { |
| 25 | + indexed: false, |
| 26 | + internalType: 'uint256', |
| 27 | + name: 'excessProfit', |
| 28 | + type: 'uint256', |
| 29 | + }, |
| 30 | + { |
| 31 | + indexed: false, |
| 32 | + internalType: 'uint256', |
| 33 | + name: 'insurancePremium', |
| 34 | + type: 'uint256', |
| 35 | + }, |
| 36 | + { |
| 37 | + indexed: false, |
| 38 | + internalType: 'uint256', |
| 39 | + name: 'insuranceLoss', |
| 40 | + type: 'uint256', |
| 41 | + }, |
| 42 | + ], |
| 43 | + name: 'PayoutEvent', |
| 44 | + type: 'event', |
| 45 | + }, |
| 46 | +]; |
| 47 | + |
| 48 | +const apy = async () => { |
| 49 | + const totalSupply = |
| 50 | + ( |
| 51 | + await sdk.api.abi.call({ |
| 52 | + target: xUSD, |
| 53 | + abi: 'erc20:totalSupply', |
| 54 | + chain: 'arbitrum', |
| 55 | + }) |
| 56 | + ).output / 1e6; |
| 57 | + |
| 58 | + const priceKey = `arbitrum:${xUSD}`; |
| 59 | + const price = ( |
| 60 | + await axios.get(`https://coins.llama.fi/prices/current/${priceKey}`) |
| 61 | + ).data.coins[priceKey].price; |
| 62 | + |
| 63 | + const tvlUsd = totalSupply * price; |
| 64 | + |
| 65 | + const currentBlock = await sdk.api.util.getLatestBlock('arbitrum'); |
| 66 | + const toBlock = currentBlock.number; |
| 67 | + const topic = |
| 68 | + '0x8dd3783ac3ed2cabfce0fa4347c2cab93b8273171a7e30b28a83147b099c4038'; |
| 69 | + const logs = ( |
| 70 | + await sdk.api.util.getLogs({ |
| 71 | + target: '0x73cb180bf0521828d8849bc8CF2B920918e23032', |
| 72 | + topic: '', |
| 73 | + toBlock, |
| 74 | + fromBlock: 63102109, |
| 75 | + keys: [], |
| 76 | + topics: [topic], |
| 77 | + chain: 'arbitrum', |
| 78 | + }) |
| 79 | + ).output.sort((a, b) => b.blockNumber - a.blockNumber); |
| 80 | + |
| 81 | + const elapsedTime = |
| 82 | + (await sdk.api.util.getTimestamp(logs[0].blockNumber, 'arbitrum')) - |
| 83 | + (await sdk.api.util.getTimestamp(logs[1].blockNumber, 'arbitrum')); |
| 84 | + |
| 85 | + const iface = new ethers.utils.Interface(eventAbi); |
| 86 | + const decoded = iface.parseLog(logs[0]); |
| 87 | + const rewardsReceived = parseInt(decoded.args.profit / 1e6); |
| 88 | + // daily compounding |
| 89 | + const apyBase = calcApy(rewardsReceived, tvlUsd, elapsedTime); |
| 90 | + return [ |
| 91 | + { |
| 92 | + pool: xUSD, |
| 93 | + symbol: 'xUSD', |
| 94 | + project: 'overnight-finance', |
| 95 | + chain: 'arbitrum', |
| 96 | + tvlUsd, |
| 97 | + apyBase, |
| 98 | + }, |
| 99 | + ]; |
| 100 | +}; |
| 101 | + |
| 102 | +function calcApy(profit, totalSupply, elapsedTime) { |
| 103 | + if (profit == 0) { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + const hourlyRate = (profit / totalSupply / elapsedTime) * 3600; |
| 107 | + const dailyRate = hourlyRate * 24; |
| 108 | + const apy = (dailyRate + 1) ** 365 - 1; |
| 109 | + return apy * 100; |
| 110 | +} |
| 111 | + |
| 112 | +module.exports = { |
| 113 | + apy, |
| 114 | + url: 'https://app.overnight.fi/', |
| 115 | +}; |
0 commit comments