Skip to content

Commit

Permalink
refactor: finish onchain
Browse files Browse the repository at this point in the history
  • Loading branch information
sandipndev committed Nov 16, 2023
1 parent 6776ee8 commit 4ccd831
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 196 deletions.
164 changes: 164 additions & 0 deletions bats/core/api/onchain-receive.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load "../../helpers/cli.bash"
load "../../helpers/user.bash"
load "../../helpers/funding/onchain.bash"
load "../../helpers/funding/ln.bash"
load "../../helpers/funding/wallet.bash"

setup_file() {
Expand Down Expand Up @@ -341,3 +342,166 @@ setup_file() {
retry 3 1 check_for_onchain_initiated_settled 'alice' "$alice_address_2" 2
retry 3 1 check_for_onchain_initiated_settled 'bob' "$bob_address_1" 1
}

@test "onchain-receive: process received batch transaction" {
alice_btc_wallet_name="alice.btc_wallet_id"
bob_usd_wallet_name="bob.usd_wallet_id"
amount="0.01"

# Create Alice addresses
alice_variables=$(
jq -n \
--arg wallet_id "$(read_value $alice_btc_wallet_name)" \
'{input: {walletId: $wallet_id}}'
)

exec_graphql 'alice' 'on-chain-address-create' "$alice_variables"
alice_address_1="$(graphql_output '.data.onChainAddressCreate.address')"
[[ "${alice_address_1}" != "null" ]] || exit 1

exec_graphql 'alice' 'on-chain-address-create' "$alice_variables"
alice_address_2="$(graphql_output '.data.onChainAddressCreate.address')"
[[ "${alice_address_2}" != "null" ]] || exit 1

# Create Bob addresses
bob_variables=$(
jq -n \
--arg wallet_id "$(read_value $bob_usd_wallet_name)" \
'{input: {walletId: $wallet_id}}'
)

exec_graphql 'bob' 'on-chain-address-create' "$bob_variables"
bob_address_1="$(graphql_output '.data.onChainAddressCreate.address')"
[[ "${bob_address_1}" != "null" ]] || exit 1

# Create psbt & broadcast transaction
psbt_outputs=$(
jq -c -n \
--arg alice_address_1 "$alice_address_1" \
--arg alice_address_2 "$alice_address_2" \
--arg bob_address_1 "$bob_address_1" \
--argjson amount "$amount" \
'{
($alice_address_1): $amount,
($alice_address_2): $amount,
($bob_address_1): $amount
}'
)
unsigned_psbt=$(bitcoin_cli walletcreatefundedpsbt '[]' $psbt_outputs | jq -r '.psbt')
signed_psbt=$(bitcoin_cli walletprocesspsbt "$unsigned_psbt" | jq -r '.psbt')
tx_hex=$(bitcoin_cli finalizepsbt "$signed_psbt" | jq -r '.hex')
txid=$(bitcoin_cli sendrawtransaction "$tx_hex")

retry 15 1 check_for_broadcast 'alice' "$alice_address_1" 2
retry 3 1 check_for_broadcast 'alice' "$alice_address_2" 2
retry 3 1 check_for_broadcast 'bob' "$bob_address_1" 1

# Check 'pendingIncomingBalance' query
exec_graphql 'alice' 'wallets-for-account'
alice_btc_pending_incoming=$(graphql_output '
.data.me.defaultAccount.wallets[]
| select(.walletCurrency == "BTC")
.pendingIncomingBalance
')
[[ "$alice_btc_pending_incoming" -gt 0 ]] || exit 1

exec_graphql 'bob' 'wallets-for-account'
bob_usd_pending_incoming=$(graphql_output '
.data.me.defaultAccount.wallets[]
| select(.walletCurrency == "USD")
.pendingIncomingBalance
')
[[ "$bob_usd_pending_incoming" -gt 0 ]] || exit 1

# Mine transactions
bitcoin_cli -generate 2
retry 15 1 check_for_onchain_initiated_settled 'alice' "$alice_address_1" 2
retry 3 1 check_for_onchain_initiated_settled 'alice' "$alice_address_2" 2
retry 3 1 check_for_onchain_initiated_settled 'bob' "$bob_address_1" 1
}

@test "onchain-receive: process received batch transaction via legacy lnd" {
alice_btc_wallet_name="alice.btc_wallet_id"
alice_usd_wallet_name="alice.usd_wallet_id"
bob_btc_wallet_name="bob.btc_wallet_id"
amount="0.01"

# Get initial balances
lnd1_initial_balance=$(lnd_cli walletbalance | jq -r '.confirmed_balance')
bria_initial_balance=$(bria_cli wallet-balance -w dev-wallet | jq -r '.effectiveSettled')

# Create Alice addresses
alice_btc_address="$(create_new_lnd_onchain_address $alice_btc_wallet_name)"
current_btc_address_variables=$(
jq -n \
--arg wallet_id "$(read_value $alice_btc_wallet_name)" \
'{input: {walletId: $wallet_id}}'
)
exec_graphql 'alice' 'on-chain-address-current' "$current_btc_address_variables"
on_chain_address_current="$(graphql_output '.data.onChainAddressCurrent.address')"
[[ "${on_chain_address_current}" != "null" ]] || exit 1
[[ "${alice_btc_address}" == "${on_chain_address_current}" ]] || exit 1

alice_usd_address="$(create_new_lnd_onchain_address $alice_usd_wallet_name)"
current_usd_address_variables=$(
jq -n \
--arg wallet_id "$(read_value $alice_usd_wallet_name)" \
'{input: {walletId: $wallet_id}}'
)
exec_graphql 'alice' 'on-chain-address-current' "$current_usd_address_variables"
on_chain_address_current="$(graphql_output '.data.onChainAddressCurrent.address')"
[[ "${on_chain_address_current}" != "null" ]] || exit 1
[[ "${alice_usd_address}" == "${on_chain_address_current}" ]] || exit 1

# Create Bob addresses
bob_btc_address="$(create_new_lnd_onchain_address $bob_btc_wallet_name)"
current_btc_address_variables=$(
jq -n \
--arg wallet_id "$(read_value $bob_btc_wallet_name)" \
'{input: {walletId: $wallet_id}}'
)
exec_graphql 'bob' 'on-chain-address-current' "$current_btc_address_variables"
on_chain_address_current="$(graphql_output '.data.onChainAddressCurrent.address')"
[[ "${on_chain_address_current}" != "null" ]] || exit 1
[[ "${bob_btc_address}" == "${on_chain_address_current}" ]] || exit 1

# Create psbt & broadcast transaction
psbt_outputs=$(
jq -c -n \
--arg alice_btc_address "$alice_btc_address" \
--arg alice_usd_address "$alice_usd_address" \
--arg bob_btc_address "$bob_btc_address" \
--argjson amount "$amount" \
'{
($alice_btc_address): $amount,
($alice_usd_address): $amount,
($bob_btc_address): $amount
}'
)
unsigned_psbt=$(bitcoin_cli walletcreatefundedpsbt '[]' $psbt_outputs | jq -r '.psbt')
signed_psbt=$(bitcoin_cli walletprocesspsbt "$unsigned_psbt" | jq -r '.psbt')
tx_hex=$(bitcoin_cli finalizepsbt "$signed_psbt" | jq -r '.hex')
txid=$(bitcoin_cli sendrawtransaction "$tx_hex")

retry 15 1 check_for_broadcast 'alice' "$alice_btc_address" 10
retry 3 1 check_for_broadcast 'alice' "$alice_usd_address" 10
retry 3 1 check_for_broadcast 'bob' "$bob_btc_address" 10

# Mine transactions
# Note: subscription event operates in a delayed way from lnd1 state
bitcoin_cli -generate 2
sleep 1
bitcoin_cli -generate 2
sleep 1
bitcoin_cli -generate 2

retry 15 1 check_for_onchain_initiated_settled 'alice' "$alice_btc_address" 10
retry 3 1 check_for_onchain_initiated_settled 'alice' "$alice_usd_address" 10
retry 3 1 check_for_onchain_initiated_settled 'bob' "$bob_btc_address" 10

# Check final balances
lnd1_final_balance=$(lnd_cli walletbalance | jq -r '.confirmed_balance')
[[ "$lnd1_final_balance" -gt "$lnd1_initial_balance" ]] || exit 1
bria_final_balance=$( bria_cli wallet-balance -w dev-wallet | jq -r '.effectiveSettled')
[[ "$bria_final_balance" == "$bria_initial_balance" ]] || exit 1
}
12 changes: 12 additions & 0 deletions bats/helpers/cli.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ bitcoin_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-bitcoind-1" bitcoin-cli $@
}

lnd_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-lnd1-1" \
lncli \
--macaroonpath /root/.lnd/admin.macaroon \
--tlscertpath /root/.lnd/tls.cert \
$@
}

bria_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-bria-1" bria $@
}

tilt_cli() {
tilt $@
}

mongo_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-mongodb-1" mongosh --quiet mongodb://localhost:27017/galoy --eval $@
}
33 changes: 33 additions & 0 deletions bats/helpers/funding/ln.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CURRENT_FILE=${BASH_SOURCE:-bats/helpers/.}
source "$(dirname "$CURRENT_FILE")/../_common.bash"
source "$(dirname "$CURRENT_FILE")/../cli.bash"

create_new_lnd_onchain_address() {
local wallet_name=$1
local wallet_id=$(read_value $wallet_name)

insert_lnd1_address() {
local wallet_id=$1
local address=$2
pubkey=$(lnd_cli getinfo | jq -r '.identity_pubkey')

mongo_command=$(echo "db.wallets.updateOne(
{ id: \"$wallet_id\" },
{
\$push: {
onchain: {
address: \"$address\",
pubkey: \"$pubkey\"
}
}
}
);" | tr -d '[:space:]')

mongo_cli "$mongo_command"
}

address=$(lnd_cli newaddress p2wkh | jq -r '.address')
insert_lnd1_address "$wallet_id" "$address" > /dev/null

echo $address
}
Loading

0 comments on commit 4ccd831

Please sign in to comment.