-
Notifications
You must be signed in to change notification settings - Fork 9
/
fund
executable file
·45 lines (32 loc) · 1.42 KB
/
fund
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
set -e -o pipefail
source _toolbox_lib.sh
PERSON=${1:-alice}
AMOUNT_BTC=${2:-100}
ADDRESS=$(newaddr "$PERSON")
ORIGINAL_BALANCE_BTC=$(onchain_balance "$PERSON")
echo "generated new address $ADDRESS for $PERSON (original balance ${ORIGINAL_BALANCE_BTC} BTC)"
AVAILABLE_BALANCE_BTC=$(faucet_balance)
if is "${AVAILABLE_BALANCE_BTC} < ${AMOUNT_BTC}"; then
echo "insufficient balance $AVAILABLE_BALANCE_BTC BTC, earning more..."
earn "${AMOUNT_BTC}"
fi
echo "sending $AMOUNT_BTC BTC to $PERSON"
if is_btcd_master; then
# a simple direct command would be:
# btcctl --wallet sendfrom imported ${ADDRESS} ${AMOUNT_BTC}
# btcctl can be flaky and report "-32603: insufficient funds available to construct transaction"
# see https://travis-ci.org/darwin/simverse/builds/531193450#L3941
# let's try it in a loop and hope it succeeds at some point, also mine some blocks between trials
SEND_CMD="btcctl --wallet sendfrom imported ${ADDRESS} ${AMOUNT_BTC}"
wait_for "sending funds to $PERSON" "$SEND_CMD" "generate 1"
else
bitcoin-cli sendtoaddress "${ADDRESS}" "${AMOUNT_BTC}"
fi
generate "${TX_CONF_COUNT}"
# c-lightning is pretty lazy in updating its state
# we have to resort to polling...
EXPECTED_BALANCE=$(compute "${ORIGINAL_BALANCE_BTC} + ${AMOUNT_BTC}")
wait_for_onchain_balance "$PERSON" "$EXPECTED_BALANCE"
NEW_BALANCE_BTC=$(onchain_balance "$PERSON")
echo "$PERSON now has ${NEW_BALANCE_BTC} BTC"