Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Format based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Fixed
- `usePayments` now extracts real transfers from Soroban `invoke_host_function` operations instead of returning blank 0 XLM rows.
- `usePayments` reports the actual merged amount for `account_merge` operations by reading operation effects.
- Unhandled payment operation types are filtered out instead of being returned as fabricated zero-amount rows.

### Added

- Typed wallet adapter, payment, asset, trustline, and Soroban simulation error codes.
Expand Down
100 changes: 100 additions & 0 deletions packages/core/src/__tests__/fixtures/horizon-payments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Realistic testnet Horizon fixtures for the six PaymentRecord union members.
// Addresses are placeholder testnet addresses, not mainnet.

export const TARGET = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
export const SENDER = "GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
export const RECEIVER = "GCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
export const ISSUER = "GDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"

export const nativePayment = {
id: "1001",
type: "payment",
transaction_hash: "tx1001",
created_at: "2026-01-01T00:00:00Z",
from: SENDER,
to: TARGET,
amount: "10.5",
asset_type: "native",
}

export const createAccount = {
id: "1002",
type: "create_account",
transaction_hash: "tx1002",
created_at: "2026-01-01T00:00:01Z",
funder: SENDER,
account: TARGET,
starting_balance: "1.5",
}

export const accountMerge = {
id: "1003",
type: "account_merge",
transaction_hash: "tx1003",
created_at: "2026-01-01T00:00:02Z",
account: TARGET,
into: RECEIVER,
}

export const accountMergeEffects = [
{
id: "1003-1",
type: "account_debited",
account: TARGET,
amount: "25.5",
asset_type: "native",
},
{
id: "1003-2",
type: "account_credited",
account: RECEIVER,
amount: "25.5",
asset_type: "native",
},
]

export const pathPaymentStrictReceive = {
id: "1004",
type: "path_payment_strict_receive",
transaction_hash: "tx1004",
created_at: "2026-01-01T00:00:03Z",
from: SENDER,
to: TARGET,
amount: "7.25",
asset_type: "credit_alphanum4",
asset_code: "USDC",
asset_issuer: ISSUER,
source_amount: "7.25",
source_asset_type: "native",
}

export const pathPaymentStrictSend = {
id: "1005",
type: "path_payment_strict_send",
transaction_hash: "tx1005",
created_at: "2026-01-01T00:00:04Z",
from: TARGET,
to: RECEIVER,
amount: "3.5",
asset_type: "native",
source_amount: "3.5",
source_asset_type: "native",
}

export const invokeHostFunction = {
id: "1006",
type: "invoke_host_function",
transaction_hash: "tx1006",
created_at: "2026-01-01T00:00:05Z",
asset_balance_changes: [
{
type: "asset_balance_change",
from: SENDER,
to: TARGET,
amount: "12.0",
asset_type: "credit_alphanum4",
asset_code: "USDC",
asset_issuer: ISSUER,
},
],
}
166 changes: 165 additions & 1 deletion packages/core/src/hooks/usePayments.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import React from "react"
import { renderHook, act, waitFor } from "@testing-library/react"
import { StellarProvider } from "../context/StellarProvider"
import { usePayments } from "./usePayments"
import {
nativePayment,
createAccount,
accountMerge,
pathPaymentStrictReceive,
pathPaymentStrictSend,
invokeHostFunction,
accountMergeEffects,
TARGET,
SENDER,
RECEIVER,
ISSUER,
} from "../__tests__/fixtures/horizon-payments"

jest.mock("../utils", () => ({
...jest.requireActual("../utils"),
getHorizonServer: jest.fn(),
}))
// packages/core/src/hooks/useTransactionHistory.ts

import { useCallback, useReducer } from "react"
Expand Down Expand Up @@ -33,6 +55,17 @@ function normalizeTransaction(record: TransactionRecord): NormalizedTransaction
const mockGetHorizonServer = getHorizonServer as jest.Mock

const mockCall = jest.fn()
const mockNext = jest.fn()
const mockPrev = jest.fn()
const mockEffectsCall = jest.fn()
const mockForOperation = jest.fn()

const mockQuery = {
forAccount: jest.fn(),
limit: jest.fn(),
order: jest.fn(),
cursor: jest.fn(),
call: mockCall,
interface PageData {
transactions: NormalizedTransaction[]
hasNext: boolean
Expand Down Expand Up @@ -133,11 +166,17 @@ describe("usePayments", () => {
mockQuery.limit.mockReturnValue(mockQuery)
mockQuery.order.mockReturnValue(mockQuery)
mockQuery.cursor.mockReturnValue(mockQuery)
mockGetHorizonServer.mockReturnValue({ payments: () => mockQuery })
mockEffectsCall.mockResolvedValue({ records: accountMergeEffects })
mockForOperation.mockReturnValue({ call: mockEffectsCall })
mockGetHorizonServer.mockReturnValue({
payments: () => mockQuery,
effects: () => ({ forOperation: mockForOperation }),
})
})

// ── Basic behaviour ────────────────────────────────────────────────────

const { result } = renderHook(() => usePayments({ address: TARGET }), { wrapper })
it("handles empty state and returns empty array", async () => {
mockCall.mockResolvedValueOnce(pageOf([]))

Expand Down Expand Up @@ -225,6 +264,130 @@ export function useTransactionHistory({
enabled: Boolean(resolvedAddress),
})

it.each([
{
name: "native payment",
record: nativePayment,
expected: {
id: nativePayment.id,
txHash: nativePayment.transaction_hash,
type: "payment",
from: SENDER,
to: TARGET,
amount: "10.5",
asset: "XLM",
direction: "incoming",
createdAt: nativePayment.created_at,
},
},
{
name: "create account",
record: createAccount,
expected: {
id: createAccount.id,
txHash: createAccount.transaction_hash,
type: "create_account",
from: SENDER,
to: TARGET,
amount: "1.5",
asset: "XLM",
direction: "incoming",
createdAt: createAccount.created_at,
},
},
{
name: "account merge",
record: accountMerge,
expected: {
id: accountMerge.id,
txHash: accountMerge.transaction_hash,
type: "account_merge",
from: TARGET,
to: RECEIVER,
amount: "25.5",
asset: "XLM",
direction: "outgoing",
createdAt: accountMerge.created_at,
},
},
{
name: "path payment strict receive",
record: pathPaymentStrictReceive,
expected: {
id: pathPaymentStrictReceive.id,
txHash: pathPaymentStrictReceive.transaction_hash,
type: "path_payment_strict_receive",
from: SENDER,
to: TARGET,
amount: "7.25",
asset: { code: "USDC", issuer: ISSUER },
direction: "incoming",
createdAt: pathPaymentStrictReceive.created_at,
},
},
{
name: "path payment strict send",
record: pathPaymentStrictSend,
expected: {
id: pathPaymentStrictSend.id,
txHash: pathPaymentStrictSend.transaction_hash,
type: "path_payment_strict_send",
from: TARGET,
to: RECEIVER,
amount: "3.5",
asset: "XLM",
direction: "outgoing",
createdAt: pathPaymentStrictSend.created_at,
},
},
{
name: "invoke host function",
record: invokeHostFunction,
expected: {
id: invokeHostFunction.id,
txHash: invokeHostFunction.transaction_hash,
type: "invoke_host_function",
from: SENDER,
to: TARGET,
amount: "12.0",
asset: { code: "USDC", issuer: ISSUER },
direction: "incoming",
createdAt: invokeHostFunction.created_at,
},
},
])("normalizes $name", async ({ record, expected }) => {
mockCall.mockResolvedValueOnce({ records: [record] })

const { result } = renderHook(() => usePayments({ address: TARGET }), { wrapper })

await waitFor(() => expect(result.current.loading).toBe(false))

expect(result.current.payments).toEqual([expected])
})

it("handles pagination via fetchNext and fetchPrev", async () => {
const page1 = {
records: [{ ...nativePayment, id: "200" }],
next: mockNext,
prev: mockPrev,
}

const page2 = {
records: [{ ...nativePayment, id: "201" }],
next: mockNext,
prev: mockPrev,
}

mockCall.mockResolvedValueOnce(page1)
mockNext.mockResolvedValueOnce(page2)

const { result } = renderHook(() => usePayments({ address: TARGET, limit: 1 }), { wrapper })

await waitFor(() => expect(result.current.loading).toBe(false))

expect(result.current.payments[0].id).toBe("200")
expect(result.current.hasNext).toBe(true)

const fetchNext = useCallback(async () => {
if (pageState.queryKey !== currentQueryKey || !pageState.next) return

Expand Down Expand Up @@ -339,6 +502,7 @@ export function useTransactionHistory({
it("handles errors gracefully", async () => {
mockCall.mockRejectedValueOnce(new Error("Network Error"))

const { result } = renderHook(() => usePayments({ address: TARGET }), { wrapper })
const { result } = renderHook(() => usePayments({ address: ADDRESS }), { wrapper })

await waitFor(() => expect(result.current.loading).toBe(false))
Expand Down
Loading
Loading