Created
January 25, 2025 17:56
-
-
Save nicola/eb77a4c887cd8e7612133326b15663f9 to your computer and use it in GitHub Desktop.
glif apy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ethers } = require('ethers') | |
const GLIF_CONTRACT = { | |
address: '0xe764Acf02D8B7c21d2B6A8f0a96C78541e0DC3fd', // GLIF contract address | |
abi: [ | |
'function getRate() view returns(uint256 rate)', | |
'function totalBorrowed() view returns(uint256)', | |
'function totalAssets() view returns(uint256)' | |
] | |
} | |
const glifContract = new ethers.Contract( | |
GLIF_CONTRACT.address, | |
GLIF_CONTRACT.abi, | |
provider | |
) | |
// Fetch APY and TVL from the contract | |
const [perEpochRate, borrowed, assets] = await Promise.all([ | |
glifContract.getRate(), | |
glifContract.totalBorrowed(), | |
glifContract.totalAssets() | |
]) | |
const epochsInYear = 2n * 60n * 24n * 365n // epochs in a year | |
let baseApr = BigInt(perEpochRate) * epochsInYear | |
// Treasury fee is typically 10% (0.1 * 1e18 in WAD) | |
const treasuryFeeRate = ethers.parseUnits('0.1', 18) | |
const oneWAD = ethers.parseUnits('1', 18) | |
// Calculate APR after treasury fee: apr * (1 - tFeeRate) | |
baseApr = (baseApr * (oneWAD - treasuryFeeRate)) / oneWAD | |
// Calculate utilization rate (borrowed * 1e18 / assets) | |
const utilizationRate = (borrowed * oneWAD) / assets | |
// Calculate current APY | |
let currentApy = (baseApr * utilizationRate) / oneWAD / oneWAD | |
// Convert to human-readable percentage | |
const rate = Number(currentApy) / 1e16 // Convert to percentage | |
const tvl = Number(ethers.formatUnits(borrowed, 18)) // Convert to FIL | |
const stakingData = [ | |
{ | |
protocol: 'GLIF', | |
rate, | |
tvl, | |
description: 'GLIF Staking Protocol' | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment