From 674d5791a927c0b344bed73c3f72bd270d21bb02 Mon Sep 17 00:00:00 2001 From: bruce-bitty Date: Wed, 15 Oct 2025 19:15:23 +0800 Subject: [PATCH] feat(bitty): add btc chain (#16681) --- projects/bitty/bitcoin/index.js | 61 +++++++++++++++++++ projects/bitty/{helper => ethereum}/abis.js | 2 +- .../bitty/{helper => ethereum}/address.js | 2 +- projects/bitty/{helper => ethereum}/index.js | 2 +- projects/bitty/index.js | 12 ++-- 5 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 projects/bitty/bitcoin/index.js rename projects/bitty/{helper => ethereum}/abis.js (99%) rename projects/bitty/{helper => ethereum}/address.js (99%) rename projects/bitty/{helper => ethereum}/index.js (99%) diff --git a/projects/bitty/bitcoin/index.js b/projects/bitty/bitcoin/index.js new file mode 100644 index 000000000..eeca54cb7 --- /dev/null +++ b/projects/bitty/bitcoin/index.js @@ -0,0 +1,61 @@ +const { getConfig } = require("../../helper/cache"); +const sdk = require("@defillama/sdk"); + +const API = 'https://api.bitty.io/lending/asset/explore' + +function aggregateBalances(list, pickAmount) { + const balances = {} + list = Array.isArray(list) ? list : [] + for (const item of list) { + const id = String(item?.assetId || '').toUpperCase() + if (id !== 'BTC') continue + const amount = pickAmount(item) + if (amount > 0n) + sdk.util.sumSingleBalance(balances, 'bitcoin', Number(amount / 100000000n)) + } + return balances +} + +async function tvl() { + const data = await getConfig('bitty/bitcoin-tvl', API) + if (!data) return {} + + // Sum available liquidity (supply - borrowed) + const balances = aggregateBalances(data.liquidityList, (item) => { + const totalSupply = BigInt(item?.totalSupply ?? '0') + const totalBorrowed = BigInt(item?.totalBorrowed ?? '0') + const available = totalSupply - totalBorrowed + return available > 0n ? available : 0n + }) + + // Add BTC value of collateral across pools with active debt: collateralAmount * collateralPrice (both in sats) + const collateralBalances = aggregateBalances(data.poolList, (item) => { + const totalDebt = BigInt(item?.totalDebt ?? '0') + if (totalDebt <= 0n) return 0n // Only count collateral for pools with active debt + + const amount = BigInt(item?.collateralAmount ?? '0') + const price = BigInt(item?.collateralPrice ?? '0') + const total = amount * price + return total > 0n ? total : 0n + }) + + Object.entries(collateralBalances).forEach(([token, amount]) => { + sdk.util.sumSingleBalance(balances, token, amount) + }) + + return balances +} + +async function borrowed() { + const data = await getConfig('bitty/bitcoin-tvl', API) + if (!data) return {} + + // Sum total borrowed amounts from liquidity pools + // Note: totalBorrowed <= 0 indicates no active borrows + return aggregateBalances(data.liquidityList, (item) => { + const totalBorrowed = BigInt(item?.totalBorrowed ?? '0') + return totalBorrowed > 0n ? totalBorrowed : 0n + }) +} + +module.exports = { tvl, borrowed } diff --git a/projects/bitty/helper/abis.js b/projects/bitty/ethereum/abis.js similarity index 99% rename from projects/bitty/helper/abis.js rename to projects/bitty/ethereum/abis.js index 924cc8f71..8f828de20 100644 --- a/projects/bitty/helper/abis.js +++ b/projects/bitty/ethereum/abis.js @@ -6,4 +6,4 @@ module.exports = { getBNFTAssetList: "function getBNFTAssetList() external view returns (address[] memory)", bNftProxys: "function bNftProxys(address) view returns (address)" }, -}; \ No newline at end of file +}; diff --git a/projects/bitty/helper/address.js b/projects/bitty/ethereum/address.js similarity index 99% rename from projects/bitty/helper/address.js rename to projects/bitty/ethereum/address.js index 001e1fd02..7ab3dffef 100644 --- a/projects/bitty/helper/address.js +++ b/projects/bitty/ethereum/address.js @@ -6,4 +6,4 @@ const ChainEthereum = { module.exports = { ethereum: ChainEthereum, -} \ No newline at end of file +} diff --git a/projects/bitty/helper/index.js b/projects/bitty/ethereum/index.js similarity index 99% rename from projects/bitty/helper/index.js rename to projects/bitty/ethereum/index.js index e91497bba..dd6e67715 100644 --- a/projects/bitty/helper/index.js +++ b/projects/bitty/ethereum/index.js @@ -66,4 +66,4 @@ async function borrowed(api) { module.exports = { tvl, borrowed, -} \ No newline at end of file +} diff --git a/projects/bitty/index.js b/projects/bitty/index.js index 105c8c372..0628209dd 100644 --- a/projects/bitty/index.js +++ b/projects/bitty/index.js @@ -1,10 +1,10 @@ -const { tvl, borrowed } = require("./helper"); +const ethereum = require("./ethereum"); +const bitcoin = require("./bitcoin"); const methodologies = require("../helper/methodologies"); module.exports = { methodology: methodologies.lendingMarket, - ethereum: { - tvl, - borrowed, - } -}; \ No newline at end of file + timetravel: false, + ethereum, + bitcoin, +};