Skip to content

Commit 8054901

Browse files
committed
fixed yarn tests
1 parent 4e3bc21 commit 8054901

File tree

13 files changed

+4216
-0
lines changed

13 files changed

+4216
-0
lines changed

packages/amino/src/coins.spec.ts

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
import { addCoins, coin, coins, parseCoins } from "./coins";
2+
3+
describe("coins", () => {
4+
describe("coin", () => {
5+
it("works for number amounts", () => {
6+
expect(coin(123, "utoken")).toEqual({ amount: "123", denom: "utoken" });
7+
expect(coin(123.0, "utoken")).toEqual({ amount: "123", denom: "utoken" });
8+
expect(coin(Number.MAX_SAFE_INTEGER, "utoken")).toEqual({
9+
amount: "9007199254740991",
10+
denom: "utoken",
11+
});
12+
expect(coin(+0, "utoken")).toEqual({ amount: "0", denom: "utoken" });
13+
expect(coin(-0, "utoken")).toEqual({ amount: "0", denom: "utoken" });
14+
});
15+
16+
it("throws for non-safe-integer values", () => {
17+
expect(() => coin(1.23, "utoken")).toThrow(/Given amount is not a safe integer/i);
18+
expect(() => coin(NaN, "utoken")).toThrow(/Given amount is not a safe integer/i);
19+
expect(() => coin(Number.POSITIVE_INFINITY, "utoken")).toThrow(
20+
/Given amount is not a safe integer/i,
21+
);
22+
expect(() => coin(Number.MAX_SAFE_INTEGER + 1, "utoken")).toThrow(
23+
/Given amount is not a safe integer/i,
24+
);
25+
});
26+
27+
it("throws for negative values", () => {
28+
expect(() => coin(-1, "utoken")).toThrow(/Given amount is not a safe integer/i);
29+
expect(() => coin(Number.MIN_SAFE_INTEGER, "utoken")).toThrow(
30+
/Given amount is not a safe integer/i,
31+
);
32+
expect(() => coin(Number.NEGATIVE_INFINITY, "utoken")).toThrow(
33+
/Given amount is not a safe integer/i,
34+
);
35+
});
36+
37+
it("works for string amounts", () => {
38+
expect(coin("0", "utoken")).toEqual({ amount: "0", denom: "utoken" });
39+
expect(coin("1", "utoken")).toEqual({ amount: "1", denom: "utoken" });
40+
expect(coin("00123", "utoken")).toEqual({ amount: "123", denom: "utoken" });
41+
expect(coin("12300", "utoken")).toEqual({ amount: "12300", denom: "utoken" });
42+
expect(coin("9007199254740992", "utoken")).toEqual({ amount: "9007199254740992", denom: "utoken" });
43+
// ETH supply (~118 mio ETH)
44+
expect(coin("118273505060000000000000000", "wei")).toEqual({
45+
amount: "118273505060000000000000000",
46+
denom: "wei",
47+
});
48+
});
49+
50+
it("throws for invalid amount strings", () => {
51+
expect(() => coin("-1", "utoken")).toThrow(/Invalid unsigned integer string format/i);
52+
expect(() => coin("0x01", "utoken")).toThrow(/Invalid unsigned integer string format/i);
53+
expect(() => coin("NaN", "utoken")).toThrow(/Invalid unsigned integer string format/i);
54+
expect(() => coin("1.0", "utoken")).toThrow(/Invalid unsigned integer string format/i);
55+
expect(() => coin("1 ", "utoken")).toThrow(/Invalid unsigned integer string format/i);
56+
expect(() => coin(" 1", "utoken")).toThrow(/Invalid unsigned integer string format/i);
57+
expect(() => coin("1.1827350506e+26", "utoken")).toThrow(
58+
/Invalid unsigned integer string format/i,
59+
);
60+
});
61+
});
62+
63+
describe("coins", () => {
64+
it("returns one element array of coin", () => {
65+
expect(coins(123, "utoken")).toEqual([{ amount: "123", denom: "utoken" }]);
66+
expect(coins("123", "utoken")).toEqual([{ amount: "123", denom: "utoken" }]);
67+
});
68+
});
69+
70+
describe("parseCoins", () => {
71+
it("works for empty", () => {
72+
expect(parseCoins("")).toEqual([]);
73+
});
74+
75+
it("works for one element", () => {
76+
expect(parseCoins("7643ureef")).toEqual([
77+
{
78+
amount: "7643",
79+
denom: "ureef",
80+
},
81+
]);
82+
});
83+
84+
it("works for various denoms", () => {
85+
// very short (3)
86+
expect(parseCoins("7643bar")).toEqual([
87+
{
88+
amount: "7643",
89+
denom: "bar",
90+
},
91+
]);
92+
93+
// very long (128)
94+
expect(
95+
parseCoins(
96+
"7643abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh",
97+
),
98+
).toEqual([
99+
{
100+
amount: "7643",
101+
denom:
102+
"abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh",
103+
},
104+
]);
105+
106+
// IBC denom (https://github.com/cosmos/cosmos-sdk/blob/v0.42.7/types/coin_test.go#L512-L519)
107+
expect(parseCoins("7643ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2")).toEqual([
108+
{
109+
amount: "7643",
110+
denom: "ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2",
111+
},
112+
]);
113+
114+
// Token factory denom (https://docs.osmosis.zone/osmosis-core/modules/tokenfactory/)
115+
expect(parseCoins("100000000000factory/osmo1c584m4lq25h83yp6ag8hh4htjr92d954vklzja/ufoo")).toEqual([
116+
{
117+
amount: "100000000000",
118+
denom: "factory/osmo1c584m4lq25h83yp6ag8hh4htjr92d954vklzja/ufoo",
119+
},
120+
]);
121+
});
122+
123+
it("trims leading zeros", () => {
124+
expect(parseCoins("07643ureef")).toEqual([
125+
{
126+
amount: "7643",
127+
denom: "ureef",
128+
},
129+
]);
130+
expect(parseCoins("007643ureef")).toEqual([
131+
{
132+
amount: "7643",
133+
denom: "ureef",
134+
},
135+
]);
136+
expect(parseCoins("0ureef")).toEqual([
137+
{
138+
amount: "0",
139+
denom: "ureef",
140+
},
141+
]);
142+
expect(parseCoins("0000ureef")).toEqual([
143+
{
144+
amount: "0",
145+
denom: "ureef",
146+
},
147+
]);
148+
});
149+
150+
it("works for large numbers", () => {
151+
expect(parseCoins(`${Number.MAX_SAFE_INTEGER}ureef`)).toEqual([
152+
{
153+
amount: "9007199254740991",
154+
denom: "ureef",
155+
},
156+
]);
157+
// 2**64-1
158+
expect(parseCoins("18446744073709551615ureef")).toEqual([
159+
{
160+
amount: "18446744073709551615",
161+
denom: "ureef",
162+
},
163+
]);
164+
// 2**128-1
165+
expect(parseCoins("340282366920938463463374607431768211455ureef")).toEqual([
166+
{
167+
amount: "340282366920938463463374607431768211455",
168+
denom: "ureef",
169+
},
170+
]);
171+
});
172+
173+
it("works for two", () => {
174+
expect(parseCoins("819966000ucosm,700000000ustake")).toEqual([
175+
{
176+
amount: "819966000",
177+
denom: "ucosm",
178+
},
179+
{
180+
amount: "700000000",
181+
denom: "ustake",
182+
},
183+
]);
184+
});
185+
186+
it("ignores empty elements", () => {
187+
// start
188+
expect(parseCoins(",819966000ucosm,700000000ustake")).toEqual([
189+
{
190+
amount: "819966000",
191+
denom: "ucosm",
192+
},
193+
{
194+
amount: "700000000",
195+
denom: "ustake",
196+
},
197+
]);
198+
// middle
199+
expect(parseCoins("819966000ucosm,,700000000ustake")).toEqual([
200+
{
201+
amount: "819966000",
202+
denom: "ucosm",
203+
},
204+
{
205+
amount: "700000000",
206+
denom: "ustake",
207+
},
208+
]);
209+
// end
210+
expect(parseCoins("819966000ucosm,700000000ustake,")).toEqual([
211+
{
212+
amount: "819966000",
213+
denom: "ucosm",
214+
},
215+
{
216+
amount: "700000000",
217+
denom: "ustake",
218+
},
219+
]);
220+
});
221+
222+
it("throws for invalid inputs", () => {
223+
// denom missing
224+
expect(() => parseCoins("3456")).toThrow(/invalid coin string/i);
225+
226+
// amount missing
227+
expect(() => parseCoins("ucosm")).toThrow(/invalid coin string/i);
228+
229+
// denom starting with slash
230+
expect(() => parseCoins("3456/ibc")).toThrow(/invalid coin string/i);
231+
232+
// denom too short
233+
expect(() => parseCoins("3456a")).toThrow(/invalid coin string/i);
234+
expect(() => parseCoins("3456aa")).toThrow(/invalid coin string/i);
235+
236+
// denom too long
237+
expect(() =>
238+
parseCoins(
239+
"3456abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgha",
240+
),
241+
).toThrow(/invalid coin string/i);
242+
});
243+
});
244+
245+
describe("addCoins", () => {
246+
it("works with same denom", () => {
247+
const balance1 = {
248+
amount: "10000",
249+
denom: "utest",
250+
};
251+
252+
const balance2 = {
253+
amount: "20000",
254+
denom: "utest",
255+
};
256+
257+
const expectedBalance = {
258+
amount: "30000",
259+
denom: "utest",
260+
};
261+
expect(addCoins(balance1, balance2)).toEqual(expectedBalance);
262+
});
263+
264+
it("works with different denoms", () => {
265+
const balance1 = {
266+
amount: "10000",
267+
denom: "utest",
268+
};
269+
270+
const balance2 = {
271+
amount: "20000",
272+
denom: "ucosm",
273+
};
274+
expect(() => addCoins(balance1, balance2)).toThrow(
275+
/Trying to add two coins with different denoms/i,
276+
);
277+
});
278+
});
279+
});

0 commit comments

Comments
 (0)