-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathassetIdentity.test.ts
More file actions
160 lines (135 loc) · 5.02 KB
/
Copy pathassetIdentity.test.ts
File metadata and controls
160 lines (135 loc) · 5.02 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
import BigNumber from "bignumber.js";
import { Asset, Networks } from "stellar-sdk";
import { AssetType } from "@shared/api/types/account-balance";
import {
getAssetFromCanonical,
getCanonicalFromAsset,
} from "@shared/helpers/stellar";
import {
getNativeContractId,
isNativeAsset,
isNativeAssetId,
isNativeAssetPair,
isNativeBalance,
isNativeContract,
} from "@shared/helpers/assetIdentity";
// A classic asset that uses the native asset's display code but carries its
// own issuer. It is a different asset from the native lumen, and every
// predicate has to say so.
const XLM_CODED_ISSUER =
"GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN";
// The published native SAC addresses, pinned rather than re-derived, so these
// tests assert that deriving from the passphrase reproduces the known-good
// values rather than just agreeing with themselves.
const NATIVE_SAC_PUBLIC =
"CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA";
const NATIVE_SAC_TESTNET =
"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC";
const nativeBalance = {
token: { type: "native", code: "XLM" },
total: new BigNumber("100"),
available: new BigNumber("100"),
buyingLiabilities: "0",
sellingLiabilities: "0",
minimumBalance: "1",
blockaidData: {},
} as unknown as AssetType;
const xlmCodedClassicBalance = {
token: {
type: "credit_alphanum4",
code: "XLM",
issuer: { key: XLM_CODED_ISSUER },
},
total: new BigNumber("100"),
available: new BigNumber("100"),
buyingLiabilities: "0",
sellingLiabilities: "0",
blockaidData: {},
} as unknown as AssetType;
describe("isNativeAssetId", () => {
it("accepts the native identifier", () => {
expect(isNativeAssetId("native")).toBe(true);
});
it("rejects the display code, which is not an identifier", () => {
expect(isNativeAssetId("XLM")).toBe(false);
});
it("rejects a canonical pair that uses the native code", () => {
expect(isNativeAssetId(`XLM:${XLM_CODED_ISSUER}`)).toBe(false);
});
it("rejects an absent id", () => {
expect(isNativeAssetId(undefined)).toBe(false);
expect(isNativeAssetId(null)).toBe(false);
});
});
describe("isNativeAssetPair", () => {
it("accepts the native code with no issuer", () => {
expect(isNativeAssetPair("XLM", undefined)).toBe(true);
expect(isNativeAssetPair("XLM", "")).toBe(true);
});
it("rejects the native code paired with an issuer", () => {
expect(isNativeAssetPair("XLM", XLM_CODED_ISSUER)).toBe(false);
});
it("rejects a different code with no issuer", () => {
expect(isNativeAssetPair("USDC", undefined)).toBe(false);
});
});
describe("isNativeBalance", () => {
it("accepts a native-typed balance", () => {
expect(isNativeBalance(nativeBalance)).toBe(true);
});
it("rejects a classic balance that uses the native code", () => {
expect(isNativeBalance(xlmCodedClassicBalance)).toBe(false);
});
});
describe("isNativeAsset", () => {
it("accepts the SDK native asset", () => {
expect(isNativeAsset(Asset.native())).toBe(true);
});
it("rejects a classic asset that uses the native code", () => {
expect(isNativeAsset(new Asset("XLM", XLM_CODED_ISSUER))).toBe(false);
});
it("rejects the plain shape used for Soroban issuers", () => {
expect(
isNativeAsset({
code: "XLM",
issuer: "CCV3NAKLIBBNSJNNTV2AZVRX6VODUDWK4TVYILE5MW6R45SSQJS5VCAM",
}),
).toBe(false);
});
it("rejects a contract token whose symbol contains a colon", () => {
const contract = "CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA";
const canonical = getCanonicalFromAsset("XLM:", contract);
expect(isNativeAsset(getAssetFromCanonical(canonical))).toBe(false);
});
});
describe("getNativeContractId", () => {
it("reproduces the published mainnet native SAC address", () => {
expect(getNativeContractId(Networks.PUBLIC)).toBe(NATIVE_SAC_PUBLIC);
});
it("reproduces the published testnet native SAC address", () => {
expect(getNativeContractId(Networks.TESTNET)).toBe(NATIVE_SAC_TESTNET);
});
it("derives a distinct address on FUTURENET", () => {
const futurenet = getNativeContractId(Networks.FUTURENET);
expect(futurenet).toMatch(/^C[A-Z2-7]{55}$/);
expect(futurenet).not.toBe(NATIVE_SAC_PUBLIC);
});
});
describe("isNativeContract", () => {
it("accepts the native SAC for its own network", () => {
expect(isNativeContract(NATIVE_SAC_PUBLIC, Networks.PUBLIC)).toBe(true);
});
it("rejects the native SAC of a different network", () => {
expect(isNativeContract(NATIVE_SAC_TESTNET, Networks.PUBLIC)).toBe(false);
});
it("rejects the wrapped contract of a classic asset using the native code", () => {
const wrapped = new Asset("XLM", XLM_CODED_ISSUER).contractId(
Networks.PUBLIC,
);
expect(isNativeContract(wrapped, Networks.PUBLIC)).toBe(false);
});
it("rejects an empty contract id, which an absent lookup can produce", () => {
expect(isNativeContract("", Networks.PUBLIC)).toBe(false);
expect(isNativeContract(undefined, Networks.PUBLIC)).toBe(false);
});
});