Skip to content

Commit

Permalink
feat: add txByHash and txById
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Oct 10, 2023
1 parent 54ba62a commit d9ecce8
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 7 deletions.
6 changes: 6 additions & 0 deletions core/api/dev/apollo-federation/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ type BTCWallet implements Wallet

"""An unconfirmed incoming onchain balance."""
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!

"""A list of BTC transactions associated with this wallet."""
transactions(
Expand Down Expand Up @@ -1593,6 +1595,8 @@ type UsdWallet implements Wallet

"""An unconfirmed incoming onchain balance."""
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!
transactions(
"""Returns the items in the list that come after the specified cursor."""
after: String
Expand Down Expand Up @@ -1904,6 +1908,8 @@ interface Wallet
id: ID!
invoiceByHash(paymentHash: PaymentHash!): LnInvoice!
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!

"""
Transactions are ordered anti-chronologically,
Expand Down
18 changes: 17 additions & 1 deletion core/api/src/app/wallets/get-transaction-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { memoSharingConfig } from "@/config"
import { WalletTransactionHistory } from "@/domain/wallets"
import { checkedToLedgerTransactionId } from "@/domain/ledger"
import {
checkedToLedgerTransactionId,
CouldNotFindTransactionError,
} from "@/domain/ledger"

import { getNonEndUserWalletIds, LedgerService } from "@/services/ledger"

export const getTransactionForWalletById = async ({
walletId,
transactionId: uncheckedTransactionId,
}: {
walletId: WalletId
transactionId: string
}): Promise<WalletTransaction | ApplicationError> => {
const transaction = await getTransactionById(uncheckedTransactionId)
if (transaction instanceof Error) return transaction
if (transaction.walletId !== walletId) return new CouldNotFindTransactionError()
return transaction
}

export const getTransactionById = async (
id: string,
): Promise<WalletTransaction | ApplicationError> => {
Expand Down
19 changes: 19 additions & 0 deletions core/api/src/app/wallets/get-transactions-by-hash.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { memoSharingConfig } from "@/config"
import { CouldNotFindTransactionError } from "@/domain/ledger"
import { WalletTransactionHistory } from "@/domain/wallets"

import { getNonEndUserWalletIds, LedgerService } from "@/services/ledger"

export const getTransactionForWalletByHash = async ({
walletId,
hash,
}: {
walletId: WalletId
hash: PaymentHash | OnChainTxHash
}): Promise<WalletTransaction | ApplicationError> => {
const transactions = await getTransactionsByHash(hash)
if (transactions instanceof Error) return transactions
const transaction = transactions.find(
(transaction) => transaction.walletId === walletId,
)

if (!transaction) return new CouldNotFindTransactionError()

return transaction
}

export const getTransactionsByHash = async (
hash: PaymentHash | OnChainTxHash,
): Promise<WalletTransaction[] | ApplicationError> => {
Expand Down
6 changes: 6 additions & 0 deletions core/api/src/graphql/admin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type BTCWallet implements Wallet {

"""An unconfirmed incoming onchain balance."""
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!

"""A list of BTC transactions associated with this wallet."""
transactions(
Expand Down Expand Up @@ -409,6 +411,8 @@ type UsdWallet implements Wallet {

"""An unconfirmed incoming onchain balance."""
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!
transactions(
"""Returns the items in the list that come after the specified cursor."""
after: String
Expand Down Expand Up @@ -458,6 +462,8 @@ interface Wallet {
id: ID!
invoiceByHash(paymentHash: PaymentHash!): LnInvoice!
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!

"""
Transactions are ordered anti-chronologically,
Expand Down
6 changes: 6 additions & 0 deletions core/api/src/graphql/public/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ type BTCWallet implements Wallet {

"""An unconfirmed incoming onchain balance."""
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!

"""A list of BTC transactions associated with this wallet."""
transactions(
Expand Down Expand Up @@ -1254,6 +1256,8 @@ type UsdWallet implements Wallet {

"""An unconfirmed incoming onchain balance."""
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!
transactions(
"""Returns the items in the list that come after the specified cursor."""
after: String
Expand Down Expand Up @@ -1497,6 +1501,8 @@ interface Wallet {
id: ID!
invoiceByHash(paymentHash: PaymentHash!): LnInvoice!
pendingIncomingBalance: SignedAmount!
transactionByHash(hash: PaymentHash!): Transaction!
transactionById(transactionId: ID!): Transaction!

"""
Transactions are ordered anti-chronologically,
Expand Down
18 changes: 17 additions & 1 deletion core/api/src/graphql/shared/types/abstract/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dedent from "dedent"

import { TransactionConnection } from "../object/transaction"
import Transaction, { TransactionConnection } from "../object/transaction"
import WalletCurrency from "../scalar/wallet-currency"
import SignedAmount from "../scalar/signed-amount"
import OnChainAddress from "../scalar/on-chain-address"
Expand Down Expand Up @@ -55,6 +55,22 @@ const IWallet = GT.Interface({
},
},
},
transactionByHash: {
type: GT.NonNull(Transaction),
args: {
hash: {
type: GT.NonNull(PaymentHash) || GT.NonNull(OnChainAddress),
},
},
},
transactionById: {
type: GT.NonNull(Transaction),
args: {
transactionId: {
type: GT.NonNullID,
},
},
},
}),
})

Expand Down
48 changes: 47 additions & 1 deletion core/api/src/graphql/shared/types/object/btc-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import OnChainAddress from "../scalar/on-chain-address"

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

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

import { GT } from "@/graphql/index"
import { normalizePaymentAmount } from "@/graphql/shared/root/mutation"
Expand Down Expand Up @@ -153,6 +153,52 @@ const BtcWallet = GT.Object<Wallet>({
return invoice
},
},
transactionByHash: {
type: GT.NonNull(Transaction),
args: {
hash: {
type: GT.NonNull(PaymentHash) || GT.NonNull(OnChainAddress),
},
},
resolve: async (source, args) => {
const { hash } = args
if (hash instanceof Error) throw hash

const transaction = await Wallets.getTransactionForWalletByHash({
walletId: source.id,
hash,
})

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

return transaction
},
},
transactionById: {
type: GT.NonNull(Transaction),
args: {
transactionId: {
type: GT.NonNullID,
},
},
resolve: async (source, args) => {
const { transactionId } = args
if (transactionId instanceof Error) throw transactionId

const transaction = await Wallets.getTransactionForWalletById({
walletId: source.id,
transactionId,
})

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

return transaction
},
},
}),
})

Expand Down
48 changes: 47 additions & 1 deletion core/api/src/graphql/shared/types/object/usd-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import OnChainAddress from "../scalar/on-chain-address"

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

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

import { GT } from "@/graphql/index"
import {
Expand Down Expand Up @@ -149,6 +149,52 @@ const UsdWallet = GT.Object<Wallet>({
return invoice
},
},
transactionByHash: {
type: GT.NonNull(Transaction),
args: {
hash: {
type: GT.NonNull(PaymentHash) || GT.NonNull(OnChainAddress),
},
},
resolve: async (source, args) => {
const { hash } = args
if (hash instanceof Error) throw hash

const transaction = await Wallets.getTransactionForWalletByHash({
walletId: source.id,
hash,
})

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

return transaction
},
},
transactionById: {
type: GT.NonNull(Transaction),
args: {
transactionId: {
type: GT.NonNullID,
},
},
resolve: async (source, args) => {
const { transactionId } = args
if (transactionId instanceof Error) throw transactionId

const transaction = await Wallets.getTransactionForWalletById({
walletId: source.id,
transactionId,
})

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

return transaction
},
},
}),
})

Expand Down
3 changes: 2 additions & 1 deletion core/api/test/bats/gql/transaction-for-wallet-by-hash.gql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
query transactionForWalletByHash($walletId: WalletId!, $hash: PaymentHash! | OnChainTxHash!) {
query transactionForWalletByHash($walletId: WalletId!, $hash: PaymentHash!) {
me {
defaultAccount {
displayCurrency
walletById(walletId: $walletId) {
id
transactionByHash(hash: $hash) {
__typename
id
Expand Down
3 changes: 2 additions & 1 deletion core/api/test/bats/gql/transaction-for-wallet-by-id.gql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
query transactionForWalletById($walletId: WalletId!, $transactionId: ID!) {
me {
defaultAccount {
id
displayCurrency
walletById(walletId: $walletId) {
transactionById(id: $transactionId) {
transactionById(transactionId: $transactionId) {
__typename
id
status
Expand Down
2 changes: 1 addition & 1 deletion core/api/test/bats/ln-receive.bats
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ usd_amount=50
jq -n \
--arg wallet_id "$(read_value $btc_wallet_name)" \
--arg payment_hash "$payment_hash" \
'{walletId: $wallet_id, paymentHash: $payment_hash}'
'{walletId: $wallet_id, hash: $payment_hash}'
)

exec_graphql "$token_name" 'transaction-for-wallet-by-hash' "$variables"
Expand Down

0 comments on commit d9ecce8

Please sign in to comment.