-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathtokenmanager.spec.ts
165 lines (144 loc) · 4.62 KB
/
tokenmanager.spec.ts
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
import { TokenConfiguration, TokenManager } from "./tokenmanager";
import { MinimalAccount } from "./types";
const dummyConfig: TokenConfiguration = {
bankTokens: ["utokenz", "mtrash"],
};
describe("TokenManager", () => {
describe("constructor", () => {
it("can be constructed", () => {
const tm = new TokenManager(dummyConfig);
expect(tm).toBeTruthy();
});
});
describe("creditAmount", () => {
const tm = new TokenManager(dummyConfig);
it("returns 10_000_000 base tokens by default", () => {
expect(tm.creditAmount("utokenz")).toEqual({
amount: "10000000",
denom: "utokenz",
});
expect(tm.creditAmount("mtrash")).toEqual({
amount: "10000000",
denom: "mtrash",
});
});
it("returns value from env variable when set", () => {
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "22";
expect(tm.creditAmount("mtrash")).toEqual({
amount: "22",
denom: "mtrash",
});
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "";
});
it("returns default when env variable is set to empty", () => {
process.env.FAUCET_CREDIT_AMOUNT_TRASH = "";
expect(tm.creditAmount("mtrash")).toEqual({
amount: "10000000",
denom: "mtrash",
});
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "";
});
});
describe("refillAmount", () => {
const tm = new TokenManager(dummyConfig);
beforeEach(() => {
process.env.FAUCET_REFILL_FACTOR = "";
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "";
});
it("returns 20*10_000_000' by default", () => {
expect(tm.refillAmount("mtrash")).toEqual({
amount: "200000000",
denom: "mtrash",
});
});
it("returns 20*22 when credit amount is 22", () => {
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "22";
expect(tm.refillAmount("mtrash")).toEqual({
amount: "440",
denom: "mtrash",
});
});
it("returns 30*10_000_000' when refill factor is 30", () => {
process.env.FAUCET_REFILL_FACTOR = "30";
expect(tm.refillAmount("mtrash")).toEqual({
amount: "300000000",
denom: "mtrash",
});
});
it("returns 30*22 when refill factor is 30 and credit amount is 22", () => {
process.env.FAUCET_REFILL_FACTOR = "30";
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "22";
expect(tm.refillAmount("mtrash")).toEqual({
amount: "660",
denom: "mtrash",
});
});
});
describe("refillThreshold", () => {
const tm = new TokenManager(dummyConfig);
beforeEach(() => {
process.env.FAUCET_REFILL_THRESHOLD = "";
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "";
});
it("returns 8*10_000_000 by default", () => {
expect(tm.refillThreshold("mtrash")).toEqual({
amount: "80000000",
denom: "mtrash",
});
});
it("returns 8*22 when credit amount is 22", () => {
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "22";
expect(tm.refillThreshold("mtrash")).toEqual({
amount: "176",
denom: "mtrash",
});
});
it("returns 5*10_000_000 when refill threshold is 5", () => {
process.env.FAUCET_REFILL_THRESHOLD = "5";
expect(tm.refillThreshold("mtrash")).toEqual({
amount: "50000000",
denom: "mtrash",
});
});
it("returns 5*22 when refill threshold is 5 and credit amount is 22", () => {
process.env.FAUCET_REFILL_THRESHOLD = "5";
process.env.FAUCET_CREDIT_AMOUNT_MTRASH = "22";
expect(tm.refillThreshold("mtrash")).toEqual({
amount: "110",
denom: "mtrash",
});
});
});
describe("needsRefill", () => {
const tm = new TokenManager(dummyConfig);
it("works for sufficient/insufficient balance", () => {
const brokeAccount: MinimalAccount = {
address: "cosmos1rtfrpqt3yd7c8g73m9rsaen7fft0h52m3v9v5a",
balance: [
{
denom: "utokenz",
amount: "3",
},
],
};
const richAccount: MinimalAccount = {
address: "cosmos1rtfrpqt3yd7c8g73m9rsaen7fft0h52m3v9v5a",
balance: [
{
denom: "utokenz",
amount: "3456789000000", // 3456789 TOKENZ
},
],
};
expect(tm.needsRefill(brokeAccount, "utokenz")).toEqual(true);
expect(tm.needsRefill(richAccount, "utokenz")).toEqual(false);
});
it("works for missing balance", () => {
const emptyAccount: MinimalAccount = {
address: "cosmos1rtfrpqt3yd7c8g73m9rsaen7fft0h52m3v9v5a",
balance: [],
};
expect(tm.needsRefill(emptyAccount, "utokenz")).toEqual(true);
});
});
});