diff --git a/projects/coinbase-btc/index.js b/projects/coinbase-btc/index.js index 41d069f6f..51034a6ba 100644 --- a/projects/coinbase-btc/index.js +++ b/projects/coinbase-btc/index.js @@ -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 = { diff --git a/projects/helper/chain/bitcoin.js b/projects/helper/chain/bitcoin.js index c525eda89..99b90d94e 100644 --- a/projects/helper/chain/bitcoin.js +++ b/projects/helper/chain/bitcoin.js @@ -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 } diff --git a/test.js b/test.js index 911633fdf..e9bd21520 100644 --- a/test.js +++ b/test.js @@ -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)