forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarService.test.js
More file actions
211 lines (193 loc) · 6.43 KB
/
Copy pathstellarService.test.js
File metadata and controls
211 lines (193 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { jest } from "@jest/globals";
import * as StellarSdk from "@stellar/stellar-sdk";
import {
buildPaymentTransaction,
getAccountBalance,
hasUsdcTrustline,
isValidPublicKey,
networkPassphrase,
server,
submitTransaction,
USDC_ISSUER,
verifyPaymentOperations,
} from "../src/services/stellar/stellarService.js";
const makeHorizonError = (operationCode) => {
const error = new Error("Horizon rejected transaction");
error.response = {
data: {
extras: {
result_codes: {
operations: [operationCode],
},
},
},
};
return error;
};
const buildSignedXdr = () => {
const source = StellarSdk.Keypair.random();
const destination = StellarSdk.Keypair.random();
const account = new StellarSdk.Account(source.publicKey(), "1");
const tx = new StellarSdk.TransactionBuilder(account, {
fee: StellarSdk.BASE_FEE,
networkPassphrase,
})
.addOperation(
StellarSdk.Operation.payment({
destination: destination.publicKey(),
asset: StellarSdk.Asset.native(),
amount: "1",
})
)
.setTimeout(30)
.build();
tx.sign(source);
return tx.toXDR();
};
describe("Stellar service payment flow", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("builds an unsigned USDC payment transaction with the expected operation", async () => {
const source = StellarSdk.Keypair.random();
const destination = StellarSdk.Keypair.random();
jest.spyOn(server, "loadAccount").mockResolvedValue(
new StellarSdk.Account(source.publicKey(), "100")
);
const payment = await buildPaymentTransaction({
sourcePublicKey: source.publicKey(),
destinationPublicKey: destination.publicKey(),
amount: "12.345",
memo: "DNB-BOOK-1234",
});
const tx = StellarSdk.TransactionBuilder.fromXDR(
payment.xdr,
networkPassphrase
);
expect(tx.signatures).toHaveLength(0);
expect(tx.operations).toHaveLength(1);
expect(tx.operations[0]).toMatchObject({
type: "payment",
destination: destination.publicKey(),
amount: "12.3450000",
});
expect(tx.operations[0].asset.code).toBe("USDC");
expect(tx.operations[0].asset.issuer).toBe(USDC_ISSUER);
});
it.each([
["op_underfunded", "Insufficient balance"],
[
"op_no_trust",
"Recipient does not have a trustline for this asset. They need to add it to their wallet first.",
],
["op_no_destination", "Destination account does not exist"],
])("maps %s to a clear submit error", async (operationCode, message) => {
jest
.spyOn(server, "submitTransaction")
.mockRejectedValue(makeHorizonError(operationCode));
await expect(submitTransaction(buildSignedXdr())).rejects.toThrow(message);
});
it("verifies matching USDC payment operations", async () => {
const destination = StellarSdk.Keypair.random().publicKey();
jest.spyOn(server, "transactions").mockReturnValue({
transaction: () => ({
call: async () => ({
successful: true,
ledger: 123,
created_at: "2026-07-21T00:00:00Z",
}),
}),
});
jest.spyOn(server, "operations").mockReturnValue({
forTransaction: () => ({
call: async () => ({
records: [
{
type: "payment",
asset_code: "USDC",
asset_issuer: USDC_ISSUER,
to: destination,
amount: "9.5000000",
},
],
}),
}),
});
await expect(
verifyPaymentOperations("tx_hash", [
{ destination, amount: "9.5" },
])
).resolves.toEqual({ verified: true });
});
it.each([
{ name: "wrong amount", expectedAmount: "10" },
{
name: "wrong destination",
expectedAmount: "9.5",
expectedDestination: StellarSdk.Keypair.random().publicKey(),
},
{ name: "wrong asset", expectedAmount: "9.5", assetCode: "XLM" },
])(
"rejects a payment with $name",
async ({ expectedAmount, expectedDestination, assetCode, assetIssuer }) => {
const destination = StellarSdk.Keypair.random().publicKey();
jest.spyOn(server, "transactions").mockReturnValue({
transaction: () => ({
call: async () => ({
successful: true,
ledger: 123,
created_at: "2026-07-21T00:00:00Z",
}),
}),
});
jest.spyOn(server, "operations").mockReturnValue({
forTransaction: () => ({
call: async () => ({
records: [
{
type: "payment",
asset_code: assetCode || "USDC",
asset_issuer: assetIssuer || USDC_ISSUER,
to: destination,
amount: "9.5000000",
},
],
}),
}),
});
const result = await verifyPaymentOperations("tx_hash", [
{
destination: expectedDestination || destination,
amount: expectedAmount,
},
]);
expect(result.verified).toBe(false);
expect(result.reason).toContain("Missing expected USDC payment");
}
);
it("returns balance and trustline information from Horizon", async () => {
jest.spyOn(server, "loadAccount").mockResolvedValue({
balances: [
{ asset_type: "native", balance: "3.25" },
{
asset_code: "USDC",
asset_issuer: USDC_ISSUER,
balance: "44.5",
},
],
});
await expect(getAccountBalance("GACCOUNT")).resolves.toEqual({ exists: true, xlmBalance: "3.25", usdcBalance: "44.5", hasTrustline: true, balances: { USDC: "44.5", EURC: "0" }, trustlines: { USDC: true, EURC: false } });
await expect(hasUsdcTrustline("GACCOUNT")).resolves.toBe(true);
});
it("treats missing accounts and Horizon failures as no trustline", async () => {
const notFound = new Error("not found");
notFound.response = { status: 404 };
jest.spyOn(server, "loadAccount").mockRejectedValue(notFound);
await expect(getAccountBalance("GMISSING")).resolves.toEqual({ exists: false, xlmBalance: "0", usdcBalance: "0", hasTrustline: false, balances: { USDC: "0", EURC: "0" }, trustlines: { USDC: false, EURC: false } });
await expect(hasUsdcTrustline("GMISSING")).resolves.toBe(false);
});
it("validates Stellar public keys without network calls", () => {
expect(isValidPublicKey(StellarSdk.Keypair.random().publicKey())).toBe(true);
expect(isValidPublicKey("not-a-stellar-key")).toBe(false);
});
});