Skip to content

Commit

Permalink
test: fix on chain bats test
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Nov 15, 2023
1 parent c049e0a commit 1e63441
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
26 changes: 24 additions & 2 deletions core/api/test/bats/helpers/onchain.bash
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ get_from_transaction_by_address() {
get_from_pending_transaction_by_address() {
property_query=$2

jq_query='.data.me.defaultAccount.pendingTransactions[] | select(.initiationVia.address == $address)'
jq_query='.data.me.defaultAccount.pendingIncomingTransactions[] | select(.initiationVia.address == $address)'
echo $output \
| jq -r --arg address "$1" "$jq_query" \
| jq -r "$property_query"
Expand All @@ -35,7 +35,7 @@ check_for_broadcast() {
local token_name=$1
local address=$2

exec_graphql "$token_name" 'pending-transactions'
exec_graphql "$token_name" 'pending-incoming-transactions'

tx="$(get_from_pending_transaction_by_address "$address" '.')"
[[ -n "${tx}" && "${tx}" != "null" ]] || exit 1
Expand All @@ -47,6 +47,28 @@ check_for_broadcast() {
bitcoin_cli gettransaction "$txid" || exit 1
}

check_for_outgoing_broadcast() {
local token_name=$1
local address=$2
local first=${3:-"1"}

variables=$(
jq -n \
--argjson first "$first" \
'{"first": $first}'
)
exec_graphql "$token_name" 'transactions' "$variables"

tx="$(get_from_transaction_by_address "$address" '.')"
[[ -n "${tx}" && "${tx}" != "null" ]] || exit 1
txid="$(echo $tx | jq -r '.settlementVia.transactionHash')"
[[ "${txid}" != "null" ]] || exit 1
status="$(echo $tx | jq -r '.status')"
[[ "${status}" == "PENDING" ]] || exit 1

bitcoin_cli gettransaction "$txid" || exit 1
}

check_for_onchain_initiated_settled() {
local token_name=$1
local address=$2
Expand Down
18 changes: 9 additions & 9 deletions core/api/test/bats/onchain-receive.bats
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ create_new_lnd_onchain_address() {
[[ "${on_chain_address_created_1}" != "null" ]] || exit 1

bitcoin_cli sendtoaddress "$on_chain_address_created_1" "$amount"
retry 15 1 check_for_broadcast "$token_name" "$on_chain_address_created_1" 1
retry 15 1 check_for_broadcast "$token_name" "$on_chain_address_created_1"

# Create address and broadcast transaction 2
exec_graphql "$token_name" 'on-chain-address-create' "$variables"
on_chain_address_created_2="$(graphql_output '.data.onChainAddressCreate.address')"
[[ "${on_chain_address_created_2}" != "null" ]] || exit 1

bitcoin_cli sendtoaddress "$on_chain_address_created_2" "$amount"
retry 15 1 check_for_broadcast "$token_name" "$on_chain_address_created_2" 1
retry 15 1 check_for_broadcast "$token_name" "$on_chain_address_created_2"

# Check pending transactions for address 1

Expand Down Expand Up @@ -273,7 +273,7 @@ create_new_lnd_onchain_address() {

# Execute onchain send and check for transaction
bitcoin_cli sendtoaddress "$on_chain_address_created" "$amount"
retry 15 1 check_for_broadcast "$token_name" "$on_chain_address_created" 1
retry 15 1 check_for_broadcast "$token_name" "$on_chain_address_created"

# Check pending transactions for address

Expand Down Expand Up @@ -370,9 +370,9 @@ create_new_lnd_onchain_address() {
tx_hex=$(bitcoin_cli finalizepsbt "$signed_psbt" | jq -r '.hex')
txid=$(bitcoin_cli sendrawtransaction "$tx_hex")

retry 15 1 check_for_broadcast "$alice_token_name" "$alice_address_1" 2
retry 3 1 check_for_broadcast "$alice_token_name" "$alice_address_2" 2
retry 3 1 check_for_broadcast "$bob_token_name" "$bob_address_1" 1
retry 15 1 check_for_broadcast "$alice_token_name" "$alice_address_1"
retry 3 1 check_for_broadcast "$alice_token_name" "$alice_address_2"
retry 3 1 check_for_broadcast "$bob_token_name" "$bob_address_1"

# Check 'pendingIncomingBalance' query
exec_graphql "$alice_token_name" 'wallets-for-account'
Expand Down Expand Up @@ -465,9 +465,9 @@ create_new_lnd_onchain_address() {
tx_hex=$(bitcoin_cli finalizepsbt "$signed_psbt" | jq -r '.hex')
txid=$(bitcoin_cli sendrawtransaction "$tx_hex")

retry 15 1 check_for_broadcast "$alice_token_name" "$alice_btc_address" 10
retry 3 1 check_for_broadcast "$alice_token_name" "$alice_usd_address" 10
retry 3 1 check_for_broadcast "$bob_token_name" "$bob_btc_address" 10
retry 15 1 check_for_broadcast "$alice_token_name" "$alice_btc_address"
retry 3 1 check_for_broadcast "$alice_token_name" "$alice_usd_address"
retry 3 1 check_for_broadcast "$bob_token_name" "$bob_btc_address"

# Mine transactions
# Note: subscription event operates in a delayed way from lnd1 state
Expand Down
8 changes: 4 additions & 4 deletions core/api/test/bats/onchain-send.bats
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ teardown() {
# ----------

# Check for broadcast of last send
retry 15 1 check_for_broadcast "$token_name" "$on_chain_payment_send_all_address" 4
retry 3 1 check_for_broadcast "$token_name" "$on_chain_usd_payment_send_as_btc_denominated_address" 4
retry 3 1 check_for_broadcast "$token_name" "$on_chain_usd_payment_send_address" 4
retry 3 1 check_for_broadcast "$token_name" "$on_chain_payment_send_address" 4
retry 15 1 check_for_outgoing_broadcast "$token_name" "$on_chain_payment_send_all_address" 4
retry 3 1 check_for_outgoing_broadcast "$token_name" "$on_chain_usd_payment_send_as_btc_denominated_address" 4
retry 3 1 check_for_outgoing_broadcast "$token_name" "$on_chain_usd_payment_send_address" 4
retry 3 1 check_for_outgoing_broadcast "$token_name" "$on_chain_payment_send_address" 4

# Mine all
bitcoin_cli -generate 2
Expand Down
4 changes: 2 additions & 2 deletions core/api/test/helpers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createChainAddress } from "lightning"

import {
getBalanceForWallet,
getPendingOnChainTransactionsForWallets,
getPendingIncomingOnChainTransactionsForWallets,
getTransactionsForWallets,
} from "@/app/wallets"
import { RepositoryError } from "@/domain/errors"
Expand Down Expand Up @@ -32,7 +32,7 @@ export const getPendingTransactionsForWalletId = async (
const wallets = WalletsRepository()
const wallet = await wallets.findById(walletId)
if (wallet instanceof RepositoryError) return wallet
return getPendingOnChainTransactionsForWallets({
return getPendingIncomingOnChainTransactionsForWallets({
wallets: [wallet],
})
}
Expand Down

0 comments on commit 1e63441

Please sign in to comment.