|
| 1 | +const { ChainApi } = require('@defillama/sdk'); |
| 2 | +const ADDRESSES = require('../helper/coreAssets.json'); |
| 3 | +const { FXRP, KINETIC_ISO_FXRP, SPARKDEX_V3_POOLS, SPARKDEX_V2_FXRP_WFLR, STXRP } = require('./addresses'); |
| 4 | + |
| 5 | +async function tvl(api) { |
| 6 | + // Create Flare API to call contracts on Flare chain |
| 7 | + const flareApi = new ChainApi({ chain: 'flare', timestamp: api.timestamp }); |
| 8 | + await flareApi.getBlock(); |
| 9 | + |
| 10 | + // Kinetic: getCash() + totalBorrows() = total FXRP supplied |
| 11 | + const [kineticCash, kineticBorrows] = await Promise.all([ |
| 12 | + flareApi.call({ abi: 'uint256:getCash', target: KINETIC_ISO_FXRP }), |
| 13 | + flareApi.call({ abi: 'uint256:totalBorrows', target: KINETIC_ISO_FXRP }), |
| 14 | + ]); |
| 15 | + |
| 16 | + // SparkDEX V3-1: FXRP balance in each pool |
| 17 | + const v3Balances = await flareApi.multiCall({ |
| 18 | + abi: 'erc20:balanceOf', |
| 19 | + calls: SPARKDEX_V3_POOLS.map(pool => ({ target: FXRP, params: [pool] })), |
| 20 | + }); |
| 21 | + |
| 22 | + // SparkDEX V2: FXRP balance in pool |
| 23 | + const v2Balance = await flareApi.call({ |
| 24 | + abi: 'erc20:balanceOf', |
| 25 | + target: FXRP, |
| 26 | + params: [SPARKDEX_V2_FXRP_WFLR], |
| 27 | + }); |
| 28 | + |
| 29 | + // Firelight: stXRP totalSupply (backed 1:1 by FXRP) |
| 30 | + const stxrpSupply = await flareApi.call({ abi: 'uint256:totalSupply', target: STXRP }); |
| 31 | + |
| 32 | + // Sum all FXRP and add as XRP to ripple chain |
| 33 | + const totalFxrp = BigInt(kineticCash) + BigInt(kineticBorrows) + |
| 34 | + v3Balances.reduce((sum, b) => sum + BigInt(b), 0n) + |
| 35 | + BigInt(v2Balance) + BigInt(stxrpSupply); |
| 36 | + |
| 37 | + api.add(ADDRESSES.ripple.XRP, totalFxrp.toString()); |
| 38 | + return api.getBalances(); |
| 39 | +} |
| 40 | + |
| 41 | +module.exports = { |
| 42 | + ripple: { |
| 43 | + tvl, |
| 44 | + }, |
| 45 | + methodology: "Counts total FXRP across Flare DeFi: Kinetic (lending), SparkDEX V2/V3 (liquidity pools), and Firelight (staking). FXRP is 1:1 backed by XRP.", |
| 46 | +}; |
0 commit comments