Skip to content

solana: kamino maple pools #15059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions projects/kamino-lending/market.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 borrowedAmount = borrowedAmountSf / Math.pow(2, 60)
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,
}
6 changes: 5 additions & 1 deletion projects/maple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

const ADDRESSES = require('../helper/coreAssets.json');
const axios = require('axios');
const { tvlForMarkets } = require('../kamino-lending/market.js')

const stSYRUP = "0xc7E8b36E0766D9B04c93De68A9D47dD11f260B45";

Expand Down Expand Up @@ -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),
Expand Down
Loading