support add BTC archive balance from utxos (#15593)

This commit is contained in:
eden
2025-07-21 20:00:19 +07:00
committed by GitHub
parent 109989f418
commit ff5d51ebad
3 changed files with 36 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ const sdk = require('@defillama/sdk');
const { sumTokens } = require('../helper/chain/bitcoin.js');
const bitcoinAddressBook = require('../helper/bitcoin-book/index.js')
async function tvl() {
async function tvl(api) {
const response = await fetch("https://www.coinbase.com/cbbtc/proof-of-reserves.json", {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
@@ -33,7 +33,7 @@ async function tvl() {
const bitcoinWallets = res.reserveAddresses.map(item => item.address)
return sumTokens({ owners: bitcoinWallets })
return sumTokens({ timestamp: api.timestamp, owners: bitcoinWallets })
}
module.exports = {

View File

@@ -118,17 +118,42 @@ async function sumTokens({ balances = {}, owners = [], timestamp, forceCacheUse,
return balances
}
// async function getBalance(addr, timestamp) {
// const now = Date.now() / 1e3
// let balance = await getBalanceNow(addr)
// if (!timestamp || (now - timestamp) < delay) return balance
// let endpoint = `https://btc.getblock.io/${getEnv('GETBLOCK_KEY')}/mainnet/blockbook/api/v2/balancehistory/${addr}?fiatcurrency=btc&groupBy=86400&from=${timestamp}`
// const response = await get(endpoint)
// response.forEach(({ sent, received }) => balance += sent / 1e8 - received / 1e8)
// sdk.log('bitcoin balance', addr, balance)
// return balance
// }
// get archive BTC balance
async function getBalance(addr, timestamp) {
const now = Date.now() / 1e3
let balance = await getBalanceNow(addr)
const endpoint = url(addr) + '/txs'
const txs = await get(endpoint)
let balance = 0
for (const tx of txs) {
if (tx.status.block_time <= timestamp) {
for (const vin of tx.vin) {
if (vin.prevout.scriptpubkey_address === addr) {
balance -= vin.prevout.value / 1e8
}
}
if (!timestamp || (now - timestamp) < delay) return balance
for (const vout of tx.vout) {
if (vout.scriptpubkey_address === addr) {
balance += vout.value / 1e8
}
}
}
}
let endpoint = `https://btc.getblock.io/${getEnv('GETBLOCK_KEY')}/mainnet/blockbook/api/v2/balancehistory/${addr}?fiatcurrency=btc&groupBy=86400&from=${timestamp}`
const response = await get(endpoint)
response.forEach(({ sent, received }) => balance += sent / 1e8 - received / 1e8)
sdk.log('bitcoin balance', addr, balance)
return balance
}

View File

@@ -136,7 +136,7 @@ function validateHallmarks(hallmark) {
let unixTimestamp = Math.round(Date.now() / 1000) - 60;
let chainBlocks = {}
const passedTimestamp = process.argv[3]
const passedTimestamp = process.argv[3] ? Math.floor(new Date(process.argv[3]) / 1000) : undefined
if (passedTimestamp !== undefined) {
unixTimestamp = Number(passedTimestamp)