Skip to content

Commit

Permalink
feat(core): add transactionsByPaymentRequest query
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi committed Feb 19, 2024
1 parent 74ec06c commit 147c62a
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 22 deletions.
19 changes: 19 additions & 0 deletions bats/core/api/ln-receive.bats
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ usd_amount=50

transaction_id="$(graphql_output '.data.me.defaultAccount.walletById.transactionsByPaymentHash[0].id')"

# Get transaction by payment request
variables=$(
jq -n \
--arg wallet_id "$(read_value $btc_wallet_name)" \
--arg payment_request "$payment_request" \
'{walletId: $wallet_id, paymentRequest: $payment_request}'
)

exec_graphql "$token_name" 'transactions-for-wallet-by-payment-request' "$variables"

query_payment_hash="$(graphql_output '.data.me.defaultAccount.walletById.transactionsByPaymentRequest[0].initiationVia.paymentHash')"
[[ "${query_payment_hash}" == "${payment_hash}" ]] || exit 1

query_payment_request="$(graphql_output '.data.me.defaultAccount.walletById.transactionsByPaymentRequest[0].initiationVia.paymentRequest')"
[[ "${query_payment_request}" == "${payment_request}" ]] || exit 1

query_transaction_id="$(graphql_output '.data.me.defaultAccount.walletById.transactionsByPaymentRequest[0].id')"
[[ "${query_transaction_id}" == "${transaction_id}" ]] || exit 1

# Get transaction by tx id
variables=$(
jq -n \
Expand Down
58 changes: 58 additions & 0 deletions bats/gql/transactions-for-wallet-by-payment-request.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
query transactionsForWalletByPaymentRequest(
$walletId: WalletId!
$paymentRequest: PaymentRequest!
) {
me {
defaultAccount {
displayCurrency
walletById(walletId: $walletId) {
id
transactionsByPaymentRequest(paymentRequest: $paymentRequest) {
__typename
id
status
direction
memo
createdAt
settlementAmount
settlementFee
settlementDisplayAmount
settlementDisplayFee
settlementDisplayCurrency
settlementCurrency
settlementPrice {
base
offset
}
initiationVia {
__typename
... on InitiationViaIntraLedger {
counterPartyWalletId
counterPartyUsername
}
... on InitiationViaLn {
paymentHash
paymentRequest
}
... on InitiationViaOnChain {
address
}
}
settlementVia {
__typename
... on SettlementViaIntraLedger {
counterPartyWalletId
counterPartyUsername
}
... on SettlementViaLn {
preImage
}
... on SettlementViaOnChain {
transactionHash
}
}
}
}
}
}
}
33 changes: 33 additions & 0 deletions core/api/src/app/wallets/get-transactions-by-payment-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
getTransactionsByHash,
getTransactionsForWalletByPaymentHash,
} from "./get-transactions-by-hash"

import { decodeInvoice } from "@/domain/bitcoin/lightning"

export const getTransactionsForWalletByPaymentRequest = async ({
walletId,
uncheckedPaymentRequest,
}: {
walletId: WalletId
uncheckedPaymentRequest: string
}): Promise<WalletTransaction[] | ApplicationError> => {
const decodedInvoice = decodeInvoice(uncheckedPaymentRequest)
if (decodedInvoice instanceof Error) return decodedInvoice

return getTransactionsForWalletByPaymentHash({
walletId,
paymentHash: decodedInvoice.paymentHash,
})
}

export const getTransactionsByPaymentRequest = async ({
uncheckedPaymentRequest,
}: {
uncheckedPaymentRequest: string
}): Promise<WalletTransaction[] | ApplicationError> => {
const decodedInvoice = decodeInvoice(uncheckedPaymentRequest)
if (decodedInvoice instanceof Error) return decodedInvoice

return getTransactionsByHash(decodedInvoice.paymentHash)
}
1 change: 1 addition & 0 deletions core/api/src/app/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from "./get-transaction-by-id"
export * from "./get-transaction-by-journal-id"
export * from "./get-transactions-by-addresses"
export * from "./get-transactions-by-hash"
export * from "./get-transactions-by-payment-request"
export * from "./get-transactions-for-wallet"
export * from "./handle-held-invoices"
export * from "./register-broadcasted-payout-txn"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GT } from "@/graphql/index"

import { Wallets } from "@/app"
import { mapError } from "@/graphql/error-map"
import Transaction from "@/graphql/shared/types/object/transaction"
import LnPaymentRequest from "@/graphql/shared/types/scalar/ln-payment-request"

const TransactionsByPaymentRequestQuery = GT.Field({
type: GT.List(Transaction),
args: {
paymentRequest: { type: GT.NonNull(LnPaymentRequest) },
},
resolve: async (_, { paymentRequest }) => {
if (paymentRequest instanceof Error) throw paymentRequest

const ledgerTxs = await Wallets.getTransactionsByPaymentRequest({
uncheckedPaymentRequest: paymentRequest,
})
if (ledgerTxs instanceof Error) {
throw mapError(ledgerTxs)
}

return ledgerTxs
},
})

export default TransactionsByPaymentRequestQuery
38 changes: 28 additions & 10 deletions core/api/src/graphql/shared/types/object/btc-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import IWallet from "../abstract/wallet"

import PaymentHash from "../scalar/payment-hash"
import SignedAmount from "../scalar/signed-amount"

import WalletCurrency from "../scalar/wallet-currency"

import OnChainAddress from "../scalar/on-chain-address"

import PaymentHash from "../scalar/payment-hash"

import LnPaymentRequest from "../scalar/ln-payment-request"
import IInvoice, { IInvoiceConnection } from "../abstract/invoice"

import Transaction, { TransactionConnection } from "./transaction"

import { GT } from "@/graphql/index"
import { WalletCurrency as WalletCurrencyDomain } from "@/domain/shared"

import { normalizePaymentAmount } from "@/graphql/shared/root/mutation"
import { connectionArgs } from "@/graphql/connections"
import { GT } from "@/graphql/index"
import { mapError } from "@/graphql/error-map"

import { Wallets } from "@/app"

import { WalletCurrency as WalletCurrencyDomain } from "@/domain/shared"

const BtcWallet = GT.Object<Wallet>({
name: "BTCWallet",
description:
Expand Down Expand Up @@ -206,6 +201,29 @@ const BtcWallet = GT.Object<Wallet>({
return transactions
},
},
transactionsByPaymentRequest: {
type: GT.NonNullList(Transaction),
args: {
paymentRequest: {
type: GT.NonNull(LnPaymentRequest),
},
},
resolve: async (source, args) => {
const { paymentRequest } = args
if (paymentRequest instanceof Error) throw paymentRequest

const transactions = await Wallets.getTransactionsForWalletByPaymentRequest({
walletId: source.id,
uncheckedPaymentRequest: paymentRequest,
})

if (transactions instanceof Error) {
throw mapError(transactions)
}

return transactions
},
},
transactionById: {
type: GT.NonNull(Transaction),
args: {
Expand Down
42 changes: 30 additions & 12 deletions core/api/src/graphql/shared/types/object/usd-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import IWallet from "../abstract/wallet"

import WalletCurrency from "../scalar/wallet-currency"

import PaymentHash from "../scalar/payment-hash"
import SignedAmount from "../scalar/signed-amount"

import WalletCurrency from "../scalar/wallet-currency"
import OnChainAddress from "../scalar/on-chain-address"

import PaymentHash from "../scalar/payment-hash"

import LnPaymentRequest from "../scalar/ln-payment-request"
import IInvoice, { IInvoiceConnection } from "../abstract/invoice"

import Transaction, { TransactionConnection } from "./transaction"

import { GT } from "@/graphql/index"
import { connectionArgs } from "@/graphql/connections"
import { WalletCurrency as WalletCurrencyDomain } from "@/domain/shared"

import { normalizePaymentAmount } from "@/graphql/shared/root/mutation"
import { connectionArgs } from "@/graphql/connections"
import { GT } from "@/graphql/index"
import { mapError } from "@/graphql/error-map"

import { Wallets } from "@/app"

import { WalletCurrency as WalletCurrencyDomain } from "@/domain/shared"

const UsdWallet = GT.Object<Wallet>({
name: "UsdWallet",
description:
Expand Down Expand Up @@ -204,6 +199,29 @@ const UsdWallet = GT.Object<Wallet>({
return transactions
},
},
transactionsByPaymentRequest: {
type: GT.NonNullList(Transaction),
args: {
paymentRequest: {
type: GT.NonNull(LnPaymentRequest),
},
},
resolve: async (source, args) => {
const { paymentRequest } = args
if (paymentRequest instanceof Error) throw paymentRequest

const transactions = await Wallets.getTransactionsForWalletByPaymentRequest({
walletId: source.id,
uncheckedPaymentRequest: paymentRequest,
})

if (transactions instanceof Error) {
throw mapError(transactions)
}

return transactions
},
},
transactionById: {
type: GT.NonNull(Transaction),
args: {
Expand Down

0 comments on commit 147c62a

Please sign in to comment.