Last active
June 20, 2023 18:56
-
-
Save rithvikvibhu/8541adcf90ea94d8127a38b91dfad8d3 to your computer and use it in GitHub Desktop.
missing-blind-bids.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { WalletClient } = require('hs-client'); | |
const { Network } = require('hsd'); | |
// Edit these: | |
const NETWORK = 'main'; | |
const API_KEY = 'apikey'; | |
const WALLET_NAME = 'walletname'; | |
/** @type {import('hsd/lib/protocol/network')} */ | |
const network = Network.get(NETWORK); | |
const walletClient = new WalletClient({ | |
network: network.type, | |
port: network.walletPort, | |
apiKey: API_KEY, | |
}); | |
const wallet = walletClient.wallet(WALLET_NAME); | |
(async () => { | |
const lostBids = []; | |
const repairableBids = []; | |
console.log('[*] Looking for bids without values...') | |
await walletClient.open(); | |
// Get all own bids | |
const bids = await wallet.getBids({ own: true }); | |
// console.log(bids.map(({ name, value, lockup }) => ({ name, value, lockup }))); | |
for (const bid of bids) { | |
// We only care about own bids | |
if (!bid.own) continue; | |
// Skip if bid already knows its true value | |
if (bid.value !== undefined) continue; | |
// BID coin for height | |
const bidCoin = await wallet.getCoin(bid.prevout.hash, bid.prevout.index); | |
const bidHeight = bidCoin.height; | |
// Namestate JSON (not hsd object) | |
// /** @type {import('hsd/lib/covenants/namestate')} */ | |
const ns = await wallet.getName(bid.name); | |
// Ignore bids from old auctions | |
if (bidHeight < ns.height) continue; | |
// Only consider bids for auctions in BIDDING or REVEAL periods | |
if (!['BIDDING', 'REVEAL'].includes(ns.state)) { | |
const lostBid = { | |
name: bid.name, | |
lockup: (bid.lockup / 1e6).toFixed(6), | |
bidHeight, | |
state: ns.state, | |
shouldHaveRevealedByBlock: ns.height + network.names.treeInterval + 1 + network.names.biddingPeriod + network.names.revealPeriod, | |
} | |
lostBids.push(lostBid); | |
console.log(`lost bid #${lostBids.length}:`, lostBid); | |
continue; | |
}; | |
const repairableBid = { | |
name: bid.name, | |
lockup: (bid.lockup / 1e6).toFixed(6), | |
bidHeight, | |
state: ns.state, | |
revealByBlock: ns.state === 'BIDDING' ? ns.stats.bidPeriodEnd + network.names.revealPeriod : ns.stats.revealPeriodEnd, | |
blocksRemaining: ns.state === 'BIDDING' ? ns.stats.blocksUntilReveal + network.names.revealPeriod : ns.stats.blocksUntilClose | |
} | |
repairableBids.push(repairableBid) | |
console.log(`repairable bid #${repairableBids.length}:`, repairableBid); | |
} | |
// Sort by blocks remaining | |
repairableBids.sort((a, b) => a.blocksRemaining - b.blocksRemaining) | |
console.log('\n Lost bids with unknown true bid values (too late to reveal):'); | |
console.table(lostBids) | |
console.log('\n Active bids with unknown true bid values (repair them to reveal!):'); | |
console.table(repairableBids) | |
// await nodeClient.close(); | |
await walletClient.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment