From 573a86180f798b8d3665fbb0b030efe1a0575bce Mon Sep 17 00:00:00 2001 From: Bhargava Sai Macha <40587507+Bhargavamacha@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:09:15 -0500 Subject: [PATCH] Value DAWN deal collateral from its on-chain Beam USD price map --- projects/loopscale/index.js | 43 ++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/projects/loopscale/index.js b/projects/loopscale/index.js index d733e0869ab..b75163a8251 100644 --- a/projects/loopscale/index.js +++ b/projects/loopscale/index.js @@ -182,6 +182,42 @@ const loopscaleIdl = { const WHIRLPOOL_PROGRAM = new PublicKey('whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc') const WHIRLPOOL_POSITION_SIZE = 216 const WHIRLPOOL_SIZE = 653 +const DAWN_BEAM = new PublicKey('H84M3es89eLfGRDhUFxo7xgTQmhzAx4h2oBexU7WNiGN') +const BEAM_PROGRAM = new PublicKey('beamVVkNmKeXcuZ6zLpC9eM5YgVyAn4Z9xdPrz3gCW2') +const BEAM_DISCRIMINATOR = Buffer.from('f32dd19543bfbd92', 'hex') +const PRICE_NODES_OFFSET = 120 +const PRICE_NODE_SIZE = 152 +const MAX_PRICE_ENTRIES = 255 + +async function addDawnCollateral(api, connection, collateral) { + const account = await connection.getAccountInfo(DAWN_BEAM) + if (!account || !account.owner.equals(BEAM_PROGRAM)) throw new Error('loopscale: missing DAWN Beam account') + const { data } = account + if (data.length < PRICE_NODES_OFFSET + MAX_PRICE_ENTRIES * PRICE_NODE_SIZE || + !data.subarray(0, 8).equals(BEAM_DISCRIMINATOR) || data[8] !== 1 || data.toString('ascii', 41, 49) !== 'USD ') + throw new Error('loopscale: unexpected DAWN Beam account format or currency') + + // BeamAccount v1: the price map starts at byte 88. Its Sokoban tree has a 32-byte + // header and 255 one-based nodes. Follow the child links to skip freed entries. + const stack = [data.readUInt32LE(88)] + const visited = new Set() + let totalUsdScaled = 0n + while (stack.length) { + const index = stack.pop() + if (index === 0) continue + if (index > MAX_PRICE_ENTRIES || visited.has(index)) throw new Error('loopscale: invalid DAWN Beam price tree') + visited.add(index) + const node = PRICE_NODES_OFFSET + (index - 1) * PRICE_NODE_SIZE + stack.push(data.readUInt32LE(node), data.readUInt32LE(node + 4)) + const mint = new PublicKey(data.subarray(node + 16, node + 48)).toBase58() + if (collateral[mint] !== 1n) continue // Each deal has one indivisible NFT. + + // BeamPricingData begins with unit_spot_price, an 18-decimal little-endian u192. + totalUsdScaled += bytesToBigIntLE(data.subarray(node + 48, node + 72)) + delete collateral[mint] + } + api.addUSDValue(Number(totalUsdScaled) / 1e18) +} function whirlpoolAccountData(account, size, key) { if (account.data.length !== size) throw new Error(`loopscale: whirlpool account ${key} is ${account.data.length} bytes, expected ${size}`) @@ -315,9 +351,10 @@ function getIdleBalanceForStrategy(strategy) { return amountExternallySupplied + amountHeldByStrategy; } -async function getDeposits(connection) { +async function getDeposits(connection, api) { const loans = await getLoans(connection); const collateralBalances = getCollateralValuesForLoans(loans); + await addDawnCollateral(api, connection, collateralBalances); const strats = await getStrategies(connection); const idleCapitalBalances = getIdleCapital(strats); @@ -334,7 +371,7 @@ async function getDeposits(connection) { } async function tvl(api) { - const balances = await getDeposits(getConnection()) + const balances = await getDeposits(getConnection(), api) for (const mint of await addWhirlpoolPositions(api, balances)) delete balances[mint] const mints = Object.keys(balances).filter(mint => balances[mint] !== 0n) api.addTokens(mints, mints.map(mint => balances[mint])) @@ -352,6 +389,6 @@ module.exports = { doublecounted: false, timetravel: false, methodology: - 'TVL sums idle lender capital in strategies and collateral supplied to loans, read on-chain. Orca position NFTs are valued from their pool liquidity. Collateral with no on-chain value, such as DAWN private-credit deal NFTs, is not counted.', + 'TVL sums idle lender capital in strategies and collateral supplied to loans, read on-chain. Orca position NFTs are valued from their pool liquidity. DAWN private-credit deal NFTs are valued at the USD spot price recorded for their mint in the DAWN Beam account.', solana: { tvl, borrowed, }, }