|
| 1 | +/** |
| 2 | + * stellarWallet.test.ts |
| 3 | + * |
| 4 | + * Unit tests for the network fee bump helper used to "Speed Up" stuck |
| 5 | + * transaction envelopes (#707). |
| 6 | + * |
| 7 | + * Tests cover: |
| 8 | + * - buildFeeBumpEnvelope constructs a FeeBumpTransaction that wraps the |
| 9 | + * original (inner) transaction with a higher max fee, using |
| 10 | + * TransactionBuilder.buildFeeBumpTransaction. |
| 11 | + * - speedUpTransaction pays a bumped (multiplied) max fee and resubmits the |
| 12 | + * fee-bumped envelope to the Stellar network, returning the resulting hash. |
| 13 | + */ |
| 14 | + |
| 15 | +import AsyncStorage from "@react-native-async-storage/async-storage"; |
| 16 | + |
| 17 | +jest.mock("@react-native-async-storage/async-storage", () => |
| 18 | + require("@react-native-async-storage/async-storage/jest/async-storage-mock") |
| 19 | +); |
| 20 | + |
| 21 | +// ── Mock @stellar/stellar-sdk ───────────────────────────────────────────────── |
| 22 | + |
| 23 | +const mockBuildFeeBump = jest.fn(); |
| 24 | +const mockFeeBumpSign = jest.fn(); |
| 25 | +const mockFeeBumpToXDR = jest.fn(() => "fee-bump-xdr"); |
| 26 | +const mockFetchBaseFee = jest.fn(); |
| 27 | +const mockSubmitTransaction = jest.fn(); |
| 28 | +const mockKeypairFromSecret = jest.fn(); |
| 29 | + |
| 30 | +jest.mock("@stellar/stellar-sdk", () => { |
| 31 | + class MockTransaction { |
| 32 | + xdr: string; |
| 33 | + networkPassphrase: string; |
| 34 | + constructor(xdr: string, networkPassphrase: string) { |
| 35 | + this.xdr = xdr; |
| 36 | + this.networkPassphrase = networkPassphrase; |
| 37 | + } |
| 38 | + } |
| 39 | + return { |
| 40 | + __esModule: true, |
| 41 | + Transaction: MockTransaction, |
| 42 | + TransactionBuilder: { |
| 43 | + buildFeeBumpTransaction: (...args: unknown[]) => { |
| 44 | + mockBuildFeeBump(...args); |
| 45 | + return { |
| 46 | + sign: mockFeeBumpSign, |
| 47 | + toXDR: mockFeeBumpToXDR, |
| 48 | + }; |
| 49 | + }, |
| 50 | + // Auto-detects fee bump vs regular envelopes from the XDR and returns a |
| 51 | + // plain object the Horizon server accepts (mirrors the SDK's behaviour). |
| 52 | + fromXDR: (xdr: string) => ({ __fromXDR: xdr }), |
| 53 | + }, |
| 54 | + Horizon: { |
| 55 | + Server: jest.fn().mockImplementation(() => ({ |
| 56 | + fetchBaseFee: mockFetchBaseFee, |
| 57 | + submitTransaction: mockSubmitTransaction, |
| 58 | + })), |
| 59 | + }, |
| 60 | + Keypair: { |
| 61 | + random: jest.fn(), |
| 62 | + fromSecret: mockKeypairFromSecret, |
| 63 | + }, |
| 64 | + Networks: { PUBLIC: "public-passphrase", TESTNET: "testnet-passphrase" }, |
| 65 | + BASE_FEE: 100, |
| 66 | + xdr: {}, |
| 67 | + }; |
| 68 | +}); |
| 69 | + |
| 70 | +// ── Imports (after mocks) ───────────────────────────────────────────────────── |
| 71 | + |
| 72 | +import { |
| 73 | + buildFeeBumpEnvelope, |
| 74 | + speedUpTransaction, |
| 75 | + SPEED_UP_FEE_MULTIPLIER, |
| 76 | +} from "../src/services/stellarWallet"; |
| 77 | + |
| 78 | +const FEE_SOURCE = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"; |
| 79 | +const INNER_XDR = "AAAAAgAAAABh...inner-tx..."; |
| 80 | +const LOCAL_SECRET = "SAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBB"; |
| 81 | + |
| 82 | +const mockFeeKeypair = { |
| 83 | + publicKey: jest.fn(() => FEE_SOURCE), |
| 84 | + sign: jest.fn(() => "sig"), |
| 85 | +}; |
| 86 | + |
| 87 | +// ── Helpers ─────────────────────────────────────────────────────────────────── |
| 88 | + |
| 89 | +beforeEach(async () => { |
| 90 | + jest.clearAllMocks(); |
| 91 | + await AsyncStorage.clear(); |
| 92 | + mockFetchBaseFee.mockResolvedValue(100); |
| 93 | + mockSubmitTransaction.mockResolvedValue({ hash: "bumped-hash-123" }); |
| 94 | + mockKeypairFromSecret.mockReturnValue(mockFeeKeypair); |
| 95 | +}); |
| 96 | + |
| 97 | +// ─ buildFeeBumpEnvelope ─────────────────────────────────────────────────────── |
| 98 | + |
| 99 | +describe("buildFeeBumpEnvelope", () => { |
| 100 | + it("builds a FeeBumpTransaction wrapping the inner tx with a higher fee", () => { |
| 101 | + const result = buildFeeBumpEnvelope(INNER_XDR, FEE_SOURCE, "200"); |
| 102 | + |
| 103 | + // Constructed via the SDK's buildFeeBumpTransaction helper (#707 guidance). |
| 104 | + expect(mockBuildFeeBump).toHaveBeenCalledTimes(1); |
| 105 | + const [feeSource, baseFee, innerTx, passphrase] = mockBuildFeeBump.mock |
| 106 | + .calls[0] as [string, string, { xdr: string }, string]; |
| 107 | + |
| 108 | + expect(feeSource).toBe(FEE_SOURCE); |
| 109 | + expect(baseFee).toBe("200"); |
| 110 | + // The inner transaction is parsed from the original envelope XDR. |
| 111 | + expect(innerTx.xdr).toBe(INNER_XDR); |
| 112 | + expect(passphrase).toBe("testnet-passphrase"); |
| 113 | + // returns the FeeBumpTransaction object |
| 114 | + expect(result).toBeDefined(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("passes a higher max fee than the supplied base fee", () => { |
| 118 | + buildFeeBumpEnvelope(INNER_XDR, FEE_SOURCE, "500"); |
| 119 | + const [, baseFee] = mockBuildFeeBump.mock.calls[0] as [string, string]; |
| 120 | + expect(Number(baseFee)).toBeGreaterThan(100); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +// ─ speedUpTransaction ───────────────────────────────────────────────────────── |
| 125 | + |
| 126 | +describe("speedUpTransaction", () => { |
| 127 | + it("resubmits a fee-bumped envelope using a multiplied fee and returns the hash", async () => { |
| 128 | + mockFetchBaseFee.mockResolvedValue(150); |
| 129 | + await AsyncStorage.setItem("stellar_keypair", LOCAL_SECRET); |
| 130 | + |
| 131 | + const hash = await speedUpTransaction(INNER_XDR, "local"); |
| 132 | + |
| 133 | + // Fee is bumped by the multiplier over the network base fee. |
| 134 | + const [, baseFee] = mockBuildFeeBump.mock.calls[0] as [string, string]; |
| 135 | + expect(mockFetchBaseFee).toHaveBeenCalled(); |
| 136 | + expect(Number(baseFee)).toBe( |
| 137 | + 150 * SPEED_UP_FEE_MULTIPLIER |
| 138 | + ); |
| 139 | + |
| 140 | + // The fee-bump transaction is signed with the local keypair. |
| 141 | + expect(mockFeeBumpSign).toHaveBeenCalledWith(mockFeeKeypair); |
| 142 | + |
| 143 | + // The bumped envelope is resubmitted to the Stellar network. |
| 144 | + expect(mockSubmitTransaction).toHaveBeenCalledTimes(1); |
| 145 | + expect(hash).toBe("bumped-hash-123"); |
| 146 | + }); |
| 147 | + |
| 148 | + it("resolves the fee source from the local keypair when not provided", async () => { |
| 149 | + await AsyncStorage.setItem("stellar_keypair", LOCAL_SECRET); |
| 150 | + |
| 151 | + await speedUpTransaction(INNER_XDR, "local"); |
| 152 | + |
| 153 | + const [feeSource] = mockBuildFeeBump.mock.calls[0] as [string]; |
| 154 | + expect(feeSource).toBe(FEE_SOURCE); |
| 155 | + }); |
| 156 | +}); |
0 commit comments