From 8696da47a49320127b80b07fa13dd41d5f3b8f34 Mon Sep 17 00:00:00 2001 From: treeoflife2 Date: Fri, 13 Jun 2025 21:04:47 +0530 Subject: [PATCH 1/2] solana: kamino maple pools --- projects/kamino-lending/market.js | 65 +++++++++++++++++++++++++++++++ projects/maple/index.js | 6 ++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 projects/kamino-lending/market.js diff --git a/projects/kamino-lending/market.js b/projects/kamino-lending/market.js new file mode 100644 index 0000000000..f86d4b7489 --- /dev/null +++ b/projects/kamino-lending/market.js @@ -0,0 +1,65 @@ +const { PublicKey } = require('@solana/web3.js'); +const { getConnection, sumTokens2 } = require('../helper/solana.js'); +const { Program } = require('@project-serum/anchor'); +const kaminoIdl = require('./kamino-lending-idl.json'); +const { MintLayout } = require("../helper/utils/solana/layouts/mixed-layout"); + +async function isKToken(mint, connection) { + const mintInfo = await connection.getAccountInfo(new PublicKey(mint.toString())); + const rawMint = MintLayout.decode(mintInfo.data.slice(0, MintLayout.span)); + const KAMINO_PROGRAM_ID = new PublicKey('6LtLpnUFNByNXLyCoK9wA2MykKAmQNZKBdY8s47dehDc'); + const [expectedMintAuthority] = PublicKey.findProgramAddressSync( + [Buffer.from('authority'), mint.toBuffer()], + KAMINO_PROGRAM_ID + ); + return rawMint.mintAuthority !== null && rawMint.mintAuthority.equals(expectedMintAuthority); +} + +async function tvlForMarkets(api, market, borrowed = false) { + const connection = getConnection(); + const programId = new PublicKey('KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD'); + const lendingMarketAuthSeed = 'lma'; + const tokensAndOwners = []; + const ktokens = {}; + + const kaminoLendProgram = new Program(kaminoIdl, programId, { connection, publicKey: PublicKey.unique() }); + const reserves = await kaminoLendProgram.account.reserve.all([ + { dataSize: 8624 }, + { memcmp: { offset: 32, bytes: market } }, + ]); + + for (const reserveData of reserves) { + const reserve = reserveData.account; + if ( + ktokens[reserve.liquidity.mintPubkey] || + (await isKToken(new PublicKey(reserve.liquidity.mintPubkey), connection)) + ) { + ktokens[reserve.liquidity.mintPubkey] = true; + } else { + ktokens[reserve.liquidity.mintPubkey] = false; + if (borrowed) { + // Calculate borrowed amount using this formula + // liquidity.borrowedAmountSf / 2**60 / 10**liquidity.mintDecimals + const borrowedAmountSf = reserve.liquidity.borrowedAmountSf; + const mintDecimals = reserve.liquidity.mintDecimals; + const borrowedAmount = borrowedAmountSf / Math.pow(2, 60) / Math.pow(10, mintDecimals); + api.add(reserve.liquidity.mintPubkey.toString(), borrowedAmount); + } else { + const [authority] = PublicKey.findProgramAddressSync( + [Buffer.from(lendingMarketAuthSeed), new PublicKey(market).toBuffer()], + programId + ); + tokensAndOwners.push([reserve.liquidity.mintPubkey.toString(), authority]); + } + } + } + if (borrowed) { + return api.balances; + } else { + return sumTokens2({ tokensAndOwners, api }); + } +} + +module.exports = { + tvlForMarkets, +} \ No newline at end of file diff --git a/projects/maple/index.js b/projects/maple/index.js index 7be9e41a67..66eac52df0 100644 --- a/projects/maple/index.js +++ b/projects/maple/index.js @@ -14,6 +14,7 @@ const ADDRESSES = require('../helper/coreAssets.json'); const axios = require('axios'); +const { tvlForMarkets } = require('../kamino-lending/market.js') const stSYRUP = "0xc7E8b36E0766D9B04c93De68A9D47dD11f260B45"; @@ -96,7 +97,10 @@ const staking = async (api) => { module.exports = { hallmarks: [[1670976000, 'V2 Deployment']], - solana: { tvl: () => ({})}, + solana: { + tvl: (api) => tvlForMarkets(api, '6WEGfej9B9wjxRs6t4BYpb9iCXd8CpTpJ8fVSNzHCC5y', false), + borrowed: (api) => tvlForMarkets(api, '6WEGfej9B9wjxRs6t4BYpb9iCXd8CpTpJ8fVSNzHCC5y', true), + }, ethereum: { tvl: async (api) => processPools(api, "collateralValue"), borrowed: async (api) => processPools(api), From 2779bcaa0ed92a208856f1b09c07929e1db02442 Mon Sep 17 00:00:00 2001 From: treeoflife2 Date: Fri, 13 Jun 2025 21:11:28 +0530 Subject: [PATCH 2/2] fix solana borrows --- projects/kamino-lending/market.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/projects/kamino-lending/market.js b/projects/kamino-lending/market.js index f86d4b7489..e739fd2ecd 100644 --- a/projects/kamino-lending/market.js +++ b/projects/kamino-lending/market.js @@ -41,8 +41,7 @@ async function tvlForMarkets(api, market, borrowed = false) { // Calculate borrowed amount using this formula // liquidity.borrowedAmountSf / 2**60 / 10**liquidity.mintDecimals const borrowedAmountSf = reserve.liquidity.borrowedAmountSf; - const mintDecimals = reserve.liquidity.mintDecimals; - const borrowedAmount = borrowedAmountSf / Math.pow(2, 60) / Math.pow(10, mintDecimals); + const borrowedAmount = borrowedAmountSf / Math.pow(2, 60) api.add(reserve.liquidity.mintPubkey.toString(), borrowedAmount); } else { const [authority] = PublicKey.findProgramAddressSync(