diff --git a/src/lib/client.ts b/src/lib/client.ts index 244a72b..1efb1d2 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -160,8 +160,9 @@ export class CoreClient { if (!names.includes(label)) { if (!files.includes(label)) { await wallet._create() + } else { + await wallet._load() } - await wallet._load() } wallets[label] = wallet } diff --git a/src/lib/const.ts b/src/lib/const.ts index 002a282..837d095 100644 --- a/src/lib/const.ts +++ b/src/lib/const.ts @@ -1,6 +1,7 @@ export const DUST_LIMIT = 1_000 export const FALLBACK_FEE = 5_000 -export const FAUCET_MIN_BAL = 10_000_000_000 +export const FAUCET_MIN_BAL = 100_000_000 * 50 * 25 +export const INIT_BLOCK_CT = 125 export const MIN_TX_FEE = 1_000 export const RATE_LIMIT = 0 export const SAT_MULTI = 100_000_000 diff --git a/src/lib/core.ts b/src/lib/core.ts index ffc6f4d..15f3b6a 100644 --- a/src/lib/core.ts +++ b/src/lib/core.ts @@ -22,7 +22,7 @@ import { import * as CONST from './const.js' -const { FALLBACK_FEE, FAUCET_MIN_BAL, SAT_MULTI, RANDOM_PORT } = CONST +const { FALLBACK_FEE, FAUCET_MIN_BAL, INIT_BLOCK_CT, SAT_MULTI, RANDOM_PORT } = CONST export class CoreDaemon extends EventEmitter { readonly _client : CoreClient @@ -151,8 +151,18 @@ export class CoreDaemon extends EventEmitter { } async _init () { - this._faucet = await this.client.load_wallet('faucet') - return this.faucet.ensure_funds(FAUCET_MIN_BAL / SAT_MULTI) + const debug = this.opt.debug + const min_bal = FAUCET_MIN_BAL / SAT_MULTI + this._faucet = await this.client.load_wallet('faucet') + if (this.opt.network === 'regtest') { + const addr = await this.faucet.get_address('faucet') + let bal = await this.faucet.balance + while (bal <= min_bal) { + if (debug) console.log('[core] Faucet generating blocks. Balance:', bal) + await this.client.mine_blocks(INIT_BLOCK_CT, addr) + bal = await this.faucet.balance + } + } } async startup (params : string[] = []) { diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index a11ddd0..8b26fab 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -173,13 +173,13 @@ export class CoreWallet { if (debug) console.log('[wallet] using cached address:', addr) return addr } - const addr_book = await this.cmd('getaddressesbylabel', label, { cache : true }) - const addr_list = Object.keys(addr_book) - - addr = (addr_list.length !== 0) - ? addr_list[0] - : await this.gen_address({ label }) - + try { + const addr_book = await this.cmd('getaddressesbylabel', label, { cache : true }) + const addr_list = Object.keys(addr_book) + addr = addr_list[0] + } catch { + addr = await this.gen_address({ label }) + } this._addrs.set(label, addr) return addr }