|
| 1 | +const sdk = require('@defillama/sdk'); |
| 2 | +const { ethers } = require('ethers'); |
| 3 | +const axios = require('axios'); |
| 4 | +const BigNumber = require('bignumber.js'); |
| 5 | + |
| 6 | +const utils = require('../utils'); |
| 7 | + |
| 8 | +const abiLendingPool = require('./abi/abiLendingPool.json'); |
| 9 | +const abiOracle = require('./abi/abiOracle.json'); |
| 10 | +const abiGMIncentives = require('./abi/abiGMIncentives.json'); |
| 11 | +const abiInterestRateModel = require('./abi/abiInterestRateModel.json'); |
| 12 | +const abiUniswapV3Pool = require('./abi/abiUniswapV3Pool.json'); |
| 13 | +const abiGMI = require('./abi/abiGMI.json'); |
| 14 | + |
| 15 | +const { getGloopPrice } = require('./getGloopPrice'); |
| 16 | +const { getSupplyAPY } = require('./getSupplyAPY'); |
| 17 | +const { getRewardAPY } = require('./getRewardAPY'); |
| 18 | + |
| 19 | +// Contract addresses on Arbitrum |
| 20 | +const {ADDRESSES, CHAIN} = require('./Constants'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Main function to fetch pool data |
| 24 | + */ |
| 25 | +const apy = async () => { |
| 26 | + try { |
| 27 | + // Fetch total underlying USDC in the lending pool |
| 28 | + const totalUnderlyingResult = await sdk.api.abi.call({ |
| 29 | + target: ADDRESSES.LENDING_POOL, |
| 30 | + abi: abiLendingPool.find((m) => m.name === 'totalUnderlying'), |
| 31 | + params: [ADDRESSES.USDC], |
| 32 | + chain: CHAIN, |
| 33 | + }); |
| 34 | + |
| 35 | + const totalUnderlying = BigInt(totalUnderlyingResult.output); |
| 36 | + |
| 37 | + // Fetch total borrows |
| 38 | + const totalBorrowsResult = await sdk.api.abi.call({ |
| 39 | + target: ADDRESSES.LENDING_POOL, |
| 40 | + abi: abiLendingPool.find((m) => m.name === 'totalBorrows'), |
| 41 | + params: [ADDRESSES.USDC], |
| 42 | + chain: CHAIN, |
| 43 | + }); |
| 44 | + |
| 45 | + const totalBorrows = BigInt(totalBorrowsResult.output); |
| 46 | + |
| 47 | + // Fetch available liquidity |
| 48 | + const availableLiquidityResult = await sdk.api.abi.call({ |
| 49 | + target: ADDRESSES.LENDING_POOL, |
| 50 | + abi: abiLendingPool.find((m) => m.name === 'availableLiquidity'), |
| 51 | + params: [ADDRESSES.USDC], |
| 52 | + chain: CHAIN, |
| 53 | + }); |
| 54 | + |
| 55 | + const availableLiquidity = BigInt(availableLiquidityResult.output); |
| 56 | + |
| 57 | + // Get USDC price (should be ~$1, but fetch from oracle to be accurate) |
| 58 | + const usdcPriceResult = await sdk.api.abi.call({ |
| 59 | + target: ADDRESSES.ORACLE, |
| 60 | + abi: abiOracle.find((m) => m.name === 'getUnderlyingPrice'), |
| 61 | + params: [ADDRESSES.USDC], |
| 62 | + chain: CHAIN, |
| 63 | + }); |
| 64 | + |
| 65 | + // Oracle returns price in 1e18 format, convert to decimal |
| 66 | + const usdcPrice = Number(BigInt(usdcPriceResult.output)) / 1e18; |
| 67 | + |
| 68 | + // Calculate TVL in USD |
| 69 | + // For lending protocols: tvlUsd = totalSupplyUsd - totalBorrowUsd |
| 70 | + // totalSupplyUsd = totalUnderlying * price |
| 71 | + // totalBorrowUsd = totalBorrows * price |
| 72 | + const totalUnderlyingDecimal = Number(totalUnderlying) / 1e6; // USDC has 6 decimals |
| 73 | + const totalBorrowsDecimal = Number(totalBorrows) / 1e6; |
| 74 | + |
| 75 | + const totalSupplyUsd = totalUnderlyingDecimal * usdcPrice; |
| 76 | + const totalBorrowUsd = totalBorrowsDecimal * usdcPrice; |
| 77 | + const tvlUsd = totalSupplyUsd - totalBorrowUsd; |
| 78 | + |
| 79 | + // Get GLOOP price |
| 80 | + const gloopPrice = await getGloopPrice(); |
| 81 | + |
| 82 | + // Calculate supply APY |
| 83 | + const apyBase = await getSupplyAPY( |
| 84 | + availableLiquidity.toString(), |
| 85 | + totalBorrows.toString() |
| 86 | + ); |
| 87 | + |
| 88 | + // Calculate reward APY |
| 89 | + const apyReward = await getRewardAPY(gloopPrice, totalUnderlyingDecimal); |
| 90 | + |
| 91 | + // Create pool identifier |
| 92 | + const poolIdentifier = `${ADDRESSES.RECEIVED_TOKEN}-${CHAIN}`.toLowerCase(); |
| 93 | + |
| 94 | + const pool = { |
| 95 | + pool: poolIdentifier, |
| 96 | + chain: utils.formatChain(CHAIN), |
| 97 | + project: 'gloop', |
| 98 | + symbol: 'USDC', |
| 99 | + tvlUsd: tvlUsd, |
| 100 | + apyBase: apyBase, |
| 101 | + apyReward: apyReward > 0 ? apyReward : null, |
| 102 | + rewardTokens: apyReward > 0 ? [ADDRESSES.GLOOP] : null, |
| 103 | + underlyingTokens: [ADDRESSES.USDC], |
| 104 | + poolMeta: 'V1 market', |
| 105 | + url: 'https://gloop.finance/', |
| 106 | + // Lending protocol specific fields |
| 107 | + totalSupplyUsd: totalSupplyUsd, |
| 108 | + totalBorrowUsd: totalBorrowUsd, |
| 109 | + }; |
| 110 | + |
| 111 | + return [pool]; |
| 112 | + } catch (error) { |
| 113 | + console.error('Error in Gloop adapter:', error); |
| 114 | + return []; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +module.exports = { |
| 119 | + timetravel: false, |
| 120 | + apy, |
| 121 | + url: 'https://app.gloop.finance/loop', |
| 122 | +}; |
| 123 | + |
| 124 | +// async function main() { |
| 125 | +// console.log(await apy()); |
| 126 | +// } |
| 127 | + |
| 128 | +// main() |
0 commit comments