mirror of
https://github.com/alexgo-io/DefiLlama-Adapters.git
synced 2026-01-12 16:53:02 +08:00
support add BTC archive balance from utxos (#15593)
This commit is contained in:
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
2
test.js
2
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user