Skip to content

Commit

Permalink
Update TVL and borrowed with USDC (#9990)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrykarpushkin authored Apr 29, 2024
1 parent 38947d5 commit 99e2171
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions projects/banx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,44 @@ async function getData() {
const idl = await getConfig('banx-idl', 'https://raw.githubusercontent.com/frakt-solana/banx-public-sdk/master/src/fbond-protocol/idl/bonds.json')
const program = new Program(idl, programId, provider)

const fraktBondsOffset = 8;
const bondOfferOffset = 32 + 8;
const bondTradeTxnOffset = 8;
const [
fraktBonds,
bondOffers,
bondTradeTxn,
] = await Promise.all([
getFilteredAccounts(program, 'fraktBond', fraktBondsOffset, [5,]),
getFilteredAccounts(program, 'bondOfferV2', bondOfferOffset, [5, 7,]),

getFilteredAccounts(program, 'bondTradeTransactionV3', bondTradeTxnOffset, [2, 6, 9]),
])

// OffersSum is sum of sol in pools not yet lent out. The borrowedSum is the sum of SOL which has been borrowed and overcollaterized by the value of locked NFTs
const offersSum = bondOffers.reduce((acc, offer) => acc + (+offer.account.fundsSolOrTokenBalance) + Math.max(0, +offer.account.bidSettlement), 0)
const { offersSum, offersSumUsdc } = bondOffers.reduce(({ offersSum, offersSumUsdc }, offer) => {
if (offer.account.bondingCurve.bondingType.linearUsdc || offer.account.bondingCurve.bondingType.exponentialUsdc) {
return { offersSumUsdc: offersSumUsdc + (+offer.account.fundsSolOrTokenBalance) + Math.max(0, +offer.account.bidSettlement), offersSum };
}
return { offersSum: offersSum + (+offer.account.fundsSolOrTokenBalance) + Math.max(0, +offer.account.bidSettlement), offersSumUsdc };
}, { offersSum: 0, offersSumUsdc: 0 });

const borrowedSum = fraktBonds.reduce((acc, bond) => acc + bond.account.borrowedAmount.toNumber(), 0)
const { borrowedSum, borrowedSumUsdc } = bondTradeTxn.reduce(({ borrowedSum, borrowedSumUsdc }, bondTxn) => {
if (bondTxn.account.lendingToken.usdc) {
return { borrowedSumUsdc: borrowedSumUsdc + (+bondTxn.account.solAmount), borrowedSum };
}
return { borrowedSum: borrowedSum + (+bondTxn.account.solAmount), borrowedSumUsdc };
}, { borrowedSum: 0, borrowedSumUsdc: 0 });


return { tvl: offersSum, borrowed: borrowedSum }
return { tvl: offersSum, tvlUsdc: offersSumUsdc, borrowed: borrowedSum, borrowedUsdc: borrowedSumUsdc }
}
}

const tvl = async () => {
return { ['solana:' + ADDRESSES.solana.SOL]: (await getData()).tvl }
const { tvl, tvlUsdc } = await getData();
return { ['solana:' + ADDRESSES.solana.SOL]: tvl, ['solana:' + ADDRESSES.solana.USDC]: tvlUsdc }
};

const borrowed = async () => {
return { ['solana:' + ADDRESSES.solana.SOL]: (await getData()).borrowed }
const { borrowed, borrowedUsdc } = await getData();
return { ['solana:' + ADDRESSES.solana.SOL]: borrowed, ['solana:' + ADDRESSES.solana.USDC]: borrowedUsdc }
};

const getFilteredAccounts = async (program, accountName, offset, indexes) => {
Expand Down

0 comments on commit 99e2171

Please sign in to comment.