forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiAsset.test.js
More file actions
152 lines (133 loc) · 4.75 KB
/
Copy pathmultiAsset.test.js
File metadata and controls
152 lines (133 loc) · 4.75 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
import { jest } from "@jest/globals";
import * as StellarSdk from "@stellar/stellar-sdk";
import { getAssetConfig } from "../../config/assets.js";
const TEST_PUBLIC_KEY = StellarSdk.Keypair.random().publicKey();
const makeFakeAccount = (publicKey, balances, sequence = "12345") => {
const account = new StellarSdk.Account(publicKey, sequence);
account.balances = balances;
account.subentry_count = 0;
account.data_attr = {};
return account;
};
const mockServer = {
loadAccount: jest.fn(async () =>
makeFakeAccount(TEST_PUBLIC_KEY, [{ asset_type: "native", balance: "1000" }])
),
};
const mockExecute = jest.fn(async (fn) => fn(mockServer));
jest.unstable_mockModule("./horizonClient.js", () => ({
client: { execute: mockExecute, endpoints: [{ server: mockServer }] },
}));
const {
buildPaymentTransaction,
hasTrustline,
hasUsdcTrustline,
getAccountBalance,
USDC,
} = await import("./stellarService.js");
describe("buildPaymentTransaction - native asset (XLM)", () => {
it("builds a payment op with a native asset and no issuer", async () => {
const destination = StellarSdk.Keypair.random().publicKey();
const result = await buildPaymentTransaction({
sourcePublicKey: TEST_PUBLIC_KEY,
destinationPublicKey: destination,
amount: "50",
assetCode: "XLM",
});
const parsed = StellarSdk.TransactionBuilder.fromXDR(
result.xdr,
StellarSdk.Networks.TESTNET
);
const op = parsed.operations[0];
expect(op.type).toBe("payment");
expect(op.asset.isNative()).toBe(true);
expect(op.destination).toBe(destination);
expect(result.assetCode).toBe("XLM");
});
});
describe("buildPaymentTransaction - USDC default path (unchanged)", () => {
it("builds a payment op in USDC when assetCode is omitted", async () => {
const destination = StellarSdk.Keypair.random().publicKey();
const result = await buildPaymentTransaction({
sourcePublicKey: TEST_PUBLIC_KEY,
destinationPublicKey: destination,
amount: "10",
});
const parsed = StellarSdk.TransactionBuilder.fromXDR(
result.xdr,
StellarSdk.Networks.TESTNET
);
const op = parsed.operations[0];
expect(op.asset.getCode()).toBe("USDC");
expect(op.asset.getIssuer()).toBe(USDC.getIssuer());
expect(result.assetCode).toBe("USDC");
});
});
describe("trustline check for a non-USDC asset (EURC)", () => {
const eurcConfig = getAssetConfig("EURC", "testnet");
it("reports no EURC trustline when the account only holds USDC", async () => {
mockServer.loadAccount.mockResolvedValueOnce(
makeFakeAccount(TEST_PUBLIC_KEY, [
{ asset_type: "native", balance: "1000" },
{
asset_type: "credit_alphanum4",
asset_code: "USDC",
asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
balance: "20",
},
])
);
const result = await hasTrustline(TEST_PUBLIC_KEY, "EURC");
expect(result).toBe(false);
});
it("reports an EURC trustline when the account holds an EURC balance line", async () => {
mockServer.loadAccount.mockResolvedValueOnce(
makeFakeAccount(TEST_PUBLIC_KEY, [
{ asset_type: "native", balance: "1000" },
{
asset_type: "credit_alphanum4",
asset_code: "EURC",
asset_issuer: eurcConfig.issuer,
balance: "5",
},
])
);
const result = await hasTrustline(TEST_PUBLIC_KEY, "EURC");
expect(result).toBe(true);
});
it("hasUsdcTrustline (back-compat wrapper) still checks USDC specifically", async () => {
mockServer.loadAccount.mockResolvedValueOnce(
makeFakeAccount(TEST_PUBLIC_KEY, [
{ asset_type: "native", balance: "1000" },
{
asset_type: "credit_alphanum4",
asset_code: "USDC",
asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
balance: "20",
},
])
);
expect(await hasUsdcTrustline(TEST_PUBLIC_KEY)).toBe(true);
});
});
describe("getAccountBalance - multi-asset shape", () => {
it("returns per-asset balances and trustlines alongside back-compat fields", async () => {
mockServer.loadAccount.mockResolvedValueOnce(
makeFakeAccount(TEST_PUBLIC_KEY, [
{ asset_type: "native", balance: "1000" },
{
asset_type: "credit_alphanum4",
asset_code: "USDC",
asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
balance: "20",
},
])
);
const result = await getAccountBalance(TEST_PUBLIC_KEY);
expect(result.usdcBalance).toBe("20");
expect(result.hasTrustline).toBe(true);
expect(result.balances.USDC).toBe("20");
expect(result.balances.EURC).toBe("0");
expect(result.trustlines.EURC).toBe(false);
});
});