-
Notifications
You must be signed in to change notification settings - Fork 45
/
admin.ts
342 lines (273 loc) · 8.92 KB
/
admin.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import * as anchor from '@project-serum/anchor';
import { Program } from '@project-serum/anchor';
import { BN } from '../sdk';
import { assert } from 'chai';
import {
Admin,
FeeStructure,
OracleGuardRails,
OrderFillerRewardStructure,
} from '../sdk/src';
import { OracleSource } from '../sdk';
import { mockOracle, mockUSDCMint } from './testHelpers';
import { PublicKey } from '@solana/web3.js';
import { Markets } from '../sdk/src/constants/markets';
describe('admin', () => {
const provider = anchor.Provider.local(undefined, {
commitment: 'confirmed',
preflightCommitment: 'confirmed',
});
const connection = provider.connection;
anchor.setProvider(provider);
const chProgram = anchor.workspace.ClearingHouse as Program;
let clearingHouse: Admin;
let usdcMint;
before(async () => {
usdcMint = await mockUSDCMint(provider);
clearingHouse = Admin.from(
connection,
provider.wallet,
chProgram.programId,
{
commitment: 'confirmed',
}
);
await clearingHouse.initialize(usdcMint.publicKey, true);
await clearingHouse.subscribe();
const solUsd = await mockOracle(1);
const periodicity = new BN(60 * 60); // 1 HOUR
await clearingHouse.initializeMarket(
Markets[0].marketIndex,
solUsd,
new BN(1000),
new BN(1000),
periodicity
);
});
it('Update Margin Ratio', async () => {
const marginRatioInitial = 3000;
const marginRatioPartial = 2000;
const marginRatioMaintenance = 1000;
await clearingHouse.updateMarginRatio(
Markets[0].marketIndex,
marginRatioInitial,
marginRatioPartial,
marginRatioMaintenance
);
await clearingHouse.fetchAccounts();
const market = clearingHouse.getMarket(0);
assert(market.marginRatioInitial === marginRatioInitial);
assert(market.marginRatioPartial === marginRatioPartial);
assert(market.marginRatioMaintenance === marginRatioMaintenance);
});
it('Update Partial Liquidation Close Percentages', async () => {
const numerator = new BN(1);
const denominator = new BN(10);
await clearingHouse.updatePartialLiquidationClosePercentage(
numerator,
denominator
);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.partialLiquidationClosePercentageNumerator.eq(numerator));
assert(state.partialLiquidationClosePercentageDenominator.eq(denominator));
});
it('Update Partial Liquidation Penalty Percentages', async () => {
const numerator = new BN(1);
const denominator = new BN(10);
await clearingHouse.updatePartialLiquidationPenaltyPercentage(
numerator,
denominator
);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.partialLiquidationPenaltyPercentageNumerator.eq(numerator));
assert(
state.partialLiquidationPenaltyPercentageDenominator.eq(denominator)
);
});
it('Update Full Liquidation Penalty Percentages', async () => {
const numerator = new BN(1);
const denominator = new BN(10);
await clearingHouse.updateFullLiquidationPenaltyPercentage(
numerator,
denominator
);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.fullLiquidationPenaltyPercentageNumerator.eq(numerator));
assert(state.fullLiquidationPenaltyPercentageDenominator.eq(denominator));
});
it('Update Partial Liquidation Share Denominator', async () => {
const denominator = new BN(10);
await clearingHouse.updatePartialLiquidationShareDenominator(denominator);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.partialLiquidationLiquidatorShareDenominator.eq(denominator));
});
it('Update Full Liquidation Share Denominator', async () => {
const denominator = new BN(10);
await clearingHouse.updateFullLiquidationShareDenominator(denominator);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.fullLiquidationLiquidatorShareDenominator.eq(denominator));
});
it('Update fee', async () => {
const newFeeStructure: FeeStructure = {
feeNumerator: new BN(10),
feeDenominator: new BN(10),
discountTokenTiers: {
firstTier: {
minimumBalance: new BN(1),
discountNumerator: new BN(1),
discountDenominator: new BN(1),
},
secondTier: {
minimumBalance: new BN(1),
discountNumerator: new BN(1),
discountDenominator: new BN(1),
},
thirdTier: {
minimumBalance: new BN(1),
discountNumerator: new BN(1),
discountDenominator: new BN(1),
},
fourthTier: {
minimumBalance: new BN(1),
discountNumerator: new BN(1),
discountDenominator: new BN(1),
},
},
referralDiscount: {
referrerRewardNumerator: new BN(1),
referrerRewardDenominator: new BN(1),
refereeDiscountNumerator: new BN(1),
refereeDiscountDenominator: new BN(1),
},
};
await clearingHouse.updateFee(newFeeStructure);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(
JSON.stringify(newFeeStructure) === JSON.stringify(state.feeStructure)
);
});
it('Update order filler reward structure', async () => {
const newStructure: OrderFillerRewardStructure = {
rewardNumerator: new BN(1),
rewardDenominator: new BN(1),
timeBasedRewardLowerBound: new BN(1),
};
await clearingHouse.updateOrderFillerRewardStructure(newStructure);
await clearingHouse.fetchAccounts();
const orderState = clearingHouse.getOrderStateAccount();
assert(
JSON.stringify(newStructure) ===
JSON.stringify(orderState.orderFillerRewardStructure)
);
});
it('Update oracle guard rails', async () => {
const oracleGuardRails: OracleGuardRails = {
priceDivergence: {
markOracleDivergenceNumerator: new BN(1),
markOracleDivergenceDenominator: new BN(1),
},
validity: {
slotsBeforeStale: new BN(1),
confidenceIntervalMaxSize: new BN(1),
tooVolatileRatio: new BN(1),
},
useForLiquidations: false,
};
await clearingHouse.updateOracleGuardRails(oracleGuardRails);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(
JSON.stringify(oracleGuardRails) ===
JSON.stringify(state.oracleGuardRails)
);
});
it('Update protocol mint', async () => {
const mint = new PublicKey('2fvh6hkCYfpNqke9N48x6HcrW92uZVU3QSiXZX4A5L27');
await clearingHouse.updateDiscountMint(mint);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.discountMint.equals(mint));
});
it('Update max deposit', async () => {
const maxDeposit = new BN(10);
await clearingHouse.updateMaxDeposit(maxDeposit);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.maxDeposit.eq(maxDeposit));
});
it('Update market oracle', async () => {
const newOracle = PublicKey.default;
const newOracleSource = OracleSource.SWITCHBOARD;
await clearingHouse.updateMarketOracle(
Markets[0].marketIndex,
newOracle,
newOracleSource
);
await clearingHouse.fetchAccounts();
const market =
clearingHouse.getMarketsAccount().markets[
Markets[0].marketIndex.toNumber()
];
assert(market.amm.oracle.equals(PublicKey.default));
assert(
JSON.stringify(market.amm.oracleSource) ===
JSON.stringify(newOracleSource)
);
});
it('Update market minimum quote asset trade size', async () => {
const minimumTradeSize = new BN(1);
await clearingHouse.updateMarketMinimumQuoteAssetTradeSize(
Markets[0].marketIndex,
minimumTradeSize
);
await clearingHouse.fetchAccounts();
const market =
clearingHouse.getMarketsAccount().markets[
Markets[0].marketIndex.toNumber()
];
assert(market.amm.minimumQuoteAssetTradeSize.eq(minimumTradeSize));
});
it('Update market minimum base asset trade size', async () => {
const minimumTradeSize = new BN(2);
await clearingHouse.updateMarketMinimumBaseAssetTradeSize(
Markets[0].marketIndex,
minimumTradeSize
);
await clearingHouse.fetchAccounts();
const market =
clearingHouse.getMarketsAccount().markets[
Markets[0].marketIndex.toNumber()
];
assert(market.amm.minimumBaseAssetTradeSize.eq(minimumTradeSize));
});
it('Pause funding', async () => {
await clearingHouse.updateFundingPaused(true);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.fundingPaused);
});
it('Disable admin controls prices', async () => {
let state = clearingHouse.getStateAccount();
assert(state.adminControlsPrices);
await clearingHouse.disableAdminControlsPrices();
await clearingHouse.fetchAccounts();
state = clearingHouse.getStateAccount();
assert(!state.adminControlsPrices);
});
it('Update admin', async () => {
const newAdminKey = PublicKey.default;
await clearingHouse.updateAdmin(newAdminKey);
await clearingHouse.fetchAccounts();
const state = clearingHouse.getStateAccount();
assert(state.admin.equals(newAdminKey));
});
after(async () => {
await clearingHouse.unsubscribe();
});
});