hedge-web3 0.1.1 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +44 -0
  2. package/lib/index.js +2102 -0
  3. package/lib/index.js.map +1 -0
  4. package/lib/types/src/Constants.d.ts +13 -0
  5. package/lib/types/src/Constants.d.ts.map +1 -0
  6. package/lib/types/src/HedgeDecimal.d.ts +15 -0
  7. package/lib/types/src/HedgeDecimal.d.ts.map +1 -0
  8. package/lib/types/src/StakingPools.d.ts +4 -0
  9. package/lib/types/src/StakingPools.d.ts.map +1 -0
  10. package/lib/types/src/Vaults.d.ts +9 -0
  11. package/lib/types/src/Vaults.d.ts.map +1 -0
  12. package/lib/types/src/idl/idl.d.ts +3 -0
  13. package/lib/types/src/idl/idl.d.ts.map +1 -0
  14. package/lib/types/src/index.d.ts +20 -0
  15. package/lib/types/src/index.d.ts.map +1 -0
  16. package/lib/types/src/instructions/createStakingPool.d.ts +5 -0
  17. package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -0
  18. package/lib/types/src/instructions/createVault.d.ts +5 -0
  19. package/lib/types/src/instructions/createVault.d.ts.map +1 -0
  20. package/lib/types/src/instructions/depositStakingPool.d.ts +5 -0
  21. package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -0
  22. package/lib/types/src/instructions/depositVault.d.ts +5 -0
  23. package/lib/types/src/instructions/depositVault.d.ts.map +1 -0
  24. package/lib/types/src/instructions/liquidateVault.d.ts +5 -0
  25. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -0
  26. package/lib/types/src/instructions/loanVault.d.ts +5 -0
  27. package/lib/types/src/instructions/loanVault.d.ts.map +1 -0
  28. package/lib/types/src/instructions/redeemVault.d.ts +5 -0
  29. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -0
  30. package/lib/types/src/instructions/repayVault.d.ts +5 -0
  31. package/lib/types/src/instructions/repayVault.d.ts.map +1 -0
  32. package/lib/types/src/instructions/withdrawStakingPool.d.ts +5 -0
  33. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -0
  34. package/lib/types/src/instructions/withdrawVault.d.ts +5 -0
  35. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -0
  36. package/lib/types/src/state/LiquidationPoolEra.d.ts +15 -0
  37. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -0
  38. package/lib/types/src/state/LiquidationPoolState.d.ts +7 -0
  39. package/lib/types/src/state/LiquidationPoolState.d.ts.map +1 -0
  40. package/lib/types/src/state/LiquidationPosition.d.ts +25 -0
  41. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -0
  42. package/lib/types/src/state/StakingPool.d.ts +16 -0
  43. package/lib/types/src/state/StakingPool.d.ts.map +1 -0
  44. package/lib/types/src/state/StakingPoolPosition.d.ts +20 -0
  45. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -0
  46. package/lib/types/src/state/VaultAccount.d.ts +48 -0
  47. package/lib/types/src/state/VaultAccount.d.ts.map +1 -0
  48. package/lib/types/src/state/VaultHistoryEvent.d.ts +40 -0
  49. package/lib/types/src/state/VaultHistoryEvent.d.ts.map +1 -0
  50. package/lib/types/src/utils/Errors.d.ts +2 -0
  51. package/lib/types/src/utils/Errors.d.ts.map +1 -0
  52. package/lib/types/tsconfig.base.tsbuildinfo +1 -0
  53. package/package.json +6 -5
  54. package/rollup.config.js +14 -8
  55. package/src/Constants.ts +56 -0
  56. package/src/HedgeDecimal.ts +15 -7
  57. package/src/StakingPools.ts +3 -0
  58. package/src/Vaults.ts +8 -0
  59. package/src/idl/idl.ts +1474 -0
  60. package/src/index.ts +19 -4
  61. package/src/instructions/createStakingPool.ts +64 -0
  62. package/src/instructions/createVault.ts +65 -0
  63. package/src/instructions/depositStakingPool.ts +60 -0
  64. package/src/instructions/depositVault.ts +60 -0
  65. package/src/instructions/liquidateVault.ts +66 -0
  66. package/src/instructions/loanVault.ts +78 -0
  67. package/src/instructions/redeemVault.ts +82 -0
  68. package/src/instructions/repayVault.ts +73 -0
  69. package/src/instructions/withdrawStakingPool.ts +69 -0
  70. package/src/instructions/withdrawVault.ts +65 -0
  71. package/src/state/LiquidationPoolEra.ts +29 -0
  72. package/src/state/LiquidationPoolState.ts +10 -0
  73. package/src/state/LiquidationPosition.ts +77 -0
  74. package/src/state/StakingPool.ts +38 -0
  75. package/src/state/StakingPoolPosition.ts +39 -0
  76. package/src/{accounts → state}/VaultAccount.ts +5 -6
  77. package/src/state/VaultHistoryEvent.ts +61 -0
  78. package/src/types/buffer-layout/index.d.ts +88 -0
  79. package/src/utils/Errors.ts +11 -0
  80. package/tsconfig.base.json +3 -3
  81. package/tsconfig.d.json +3 -1
  82. package/tsconfig.json +1 -1
  83. package/src/accounts/LiquidationPoolEra.ts +0 -16
  84. package/src/accounts/LiquidationPoolState.ts +0 -22
  85. package/src/accounts/LiquidationPosition.ts +0 -72
  86. package/src/accounts/StakingPool.ts +0 -31
  87. package/src/accounts/StakingPoolPosition.ts +0 -29
  88. package/src/accounts/VaultHistoryEvent.ts +0 -27
package/lib/index.js ADDED
@@ -0,0 +1,2102 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var anchor = require('@project-serum/anchor');
6
+ var splToken = require('@solana/spl-token');
7
+ var web3_js = require('@solana/web3.js');
8
+ var Decimal = require('decimal.js');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var Decimal__default = /*#__PURE__*/_interopDefaultLegacy(Decimal);
13
+
14
+ const vaultIdl = {
15
+ version: '0.1.0',
16
+ name: 'vault',
17
+ instructions: [
18
+ {
19
+ name: 'initHedgeFoundation',
20
+ accounts: [
21
+ {
22
+ name: 'vaultSystemState',
23
+ isMut: true,
24
+ isSigner: false
25
+ },
26
+ {
27
+ name: 'poolState',
28
+ isMut: true,
29
+ isSigner: false
30
+ },
31
+ {
32
+ name: 'poolEra',
33
+ isMut: true,
34
+ isSigner: true
35
+ },
36
+ {
37
+ name: 'poolUsdhAccount',
38
+ isMut: true,
39
+ isSigner: false
40
+ },
41
+ {
42
+ name: 'founder',
43
+ isMut: true,
44
+ isSigner: true
45
+ },
46
+ {
47
+ name: 'usdhMint',
48
+ isMut: true,
49
+ isSigner: false
50
+ },
51
+ {
52
+ name: 'hedgeMint',
53
+ isMut: true,
54
+ isSigner: false
55
+ },
56
+ {
57
+ name: 'founderAssociatedHedgeTokenAccount',
58
+ isMut: true,
59
+ isSigner: false
60
+ },
61
+ {
62
+ name: 'oracleChainlink',
63
+ isMut: false,
64
+ isSigner: false
65
+ },
66
+ {
67
+ name: 'oraclePyth',
68
+ isMut: false,
69
+ isSigner: false
70
+ },
71
+ {
72
+ name: 'oracleSwitchboard',
73
+ isMut: false,
74
+ isSigner: false
75
+ },
76
+ {
77
+ name: 'rent',
78
+ isMut: false,
79
+ isSigner: false
80
+ },
81
+ {
82
+ name: 'splTokenProgramInfo',
83
+ isMut: false,
84
+ isSigner: false
85
+ },
86
+ {
87
+ name: 'splAssociatedTokenProgramInfo',
88
+ isMut: false,
89
+ isSigner: false
90
+ },
91
+ {
92
+ name: 'systemProgram',
93
+ isMut: false,
94
+ isSigner: false
95
+ }
96
+ ],
97
+ args: [
98
+ {
99
+ name: 'bump1',
100
+ type: 'u8'
101
+ },
102
+ {
103
+ name: 'bump2',
104
+ type: 'u8'
105
+ }
106
+ ]
107
+ },
108
+ {
109
+ name: 'createStakingPool',
110
+ accounts: [
111
+ {
112
+ name: 'signer',
113
+ isMut: true,
114
+ isSigner: true
115
+ },
116
+ {
117
+ name: 'vaultSystemState',
118
+ isMut: true,
119
+ isSigner: false
120
+ },
121
+ {
122
+ name: 'pool',
123
+ isMut: true,
124
+ isSigner: false
125
+ },
126
+ {
127
+ name: 'stakedTokenMintInfo',
128
+ isMut: false,
129
+ isSigner: false
130
+ },
131
+ {
132
+ name: 'usdhMint',
133
+ isMut: false,
134
+ isSigner: false
135
+ },
136
+ {
137
+ name: 'poolAssociatedStakedTokenAccount',
138
+ isMut: true,
139
+ isSigner: false
140
+ },
141
+ {
142
+ name: 'poolAssociatedUsdhTokenAccount',
143
+ isMut: true,
144
+ isSigner: false
145
+ },
146
+ {
147
+ name: 'rent',
148
+ isMut: false,
149
+ isSigner: false
150
+ },
151
+ {
152
+ name: 'splTokenProgramInfo',
153
+ isMut: false,
154
+ isSigner: false
155
+ },
156
+ {
157
+ name: 'splAssociatedTokenProgramInfo',
158
+ isMut: false,
159
+ isSigner: false
160
+ },
161
+ {
162
+ name: 'systemProgram',
163
+ isMut: false,
164
+ isSigner: false
165
+ }
166
+ ],
167
+ args: [
168
+ {
169
+ name: 'bump1',
170
+ type: 'u8'
171
+ },
172
+ {
173
+ name: 'poolSeedPhraseInput',
174
+ type: 'string'
175
+ },
176
+ {
177
+ name: 'totalRewards',
178
+ type: 'u64'
179
+ },
180
+ {
181
+ name: 'overrideCurrentTime',
182
+ type: 'i64'
183
+ }
184
+ ]
185
+ },
186
+ {
187
+ name: 'depositStakingPool',
188
+ accounts: [
189
+ {
190
+ name: 'payer',
191
+ isMut: true,
192
+ isSigner: true
193
+ },
194
+ {
195
+ name: 'pool',
196
+ isMut: true,
197
+ isSigner: false
198
+ },
199
+ {
200
+ name: 'poolPosition',
201
+ isMut: true,
202
+ isSigner: true
203
+ },
204
+ {
205
+ name: 'payerAssociatedStakedTokenAccount',
206
+ isMut: true,
207
+ isSigner: false
208
+ },
209
+ {
210
+ name: 'poolAssociatedStakedTokenAccount',
211
+ isMut: true,
212
+ isSigner: false
213
+ },
214
+ {
215
+ name: 'splTokenProgramInfo',
216
+ isMut: false,
217
+ isSigner: false
218
+ },
219
+ {
220
+ name: 'systemProgram',
221
+ isMut: false,
222
+ isSigner: false
223
+ }
224
+ ],
225
+ args: [
226
+ {
227
+ name: 'stakeAmount',
228
+ type: 'u64'
229
+ },
230
+ {
231
+ name: 'overrideCurrentTime',
232
+ type: 'i64'
233
+ }
234
+ ]
235
+ },
236
+ {
237
+ name: 'withdrawStakingPool',
238
+ accounts: [
239
+ {
240
+ name: 'payer',
241
+ isMut: true,
242
+ isSigner: true
243
+ },
244
+ {
245
+ name: 'vaultSystemState',
246
+ isMut: true,
247
+ isSigner: false
248
+ },
249
+ {
250
+ name: 'pool',
251
+ isMut: true,
252
+ isSigner: false
253
+ },
254
+ {
255
+ name: 'poolPosition',
256
+ isMut: true,
257
+ isSigner: false
258
+ },
259
+ {
260
+ name: 'hedgeMint',
261
+ isMut: true,
262
+ isSigner: false
263
+ },
264
+ {
265
+ name: 'stakedTokenMint',
266
+ isMut: true,
267
+ isSigner: false
268
+ },
269
+ {
270
+ name: 'usdhMint',
271
+ isMut: false,
272
+ isSigner: false
273
+ },
274
+ {
275
+ name: 'payerAssociatedStakedTokenAccount',
276
+ isMut: true,
277
+ isSigner: false
278
+ },
279
+ {
280
+ name: 'payerAssociatedHedgeAccount',
281
+ isMut: true,
282
+ isSigner: false
283
+ },
284
+ {
285
+ name: 'payerAssociatedUsdhAccount',
286
+ isMut: true,
287
+ isSigner: false
288
+ },
289
+ {
290
+ name: 'poolAssociatedStakedTokenAccount',
291
+ isMut: true,
292
+ isSigner: false
293
+ },
294
+ {
295
+ name: 'poolAssociatedUsdhTokenAccount',
296
+ isMut: true,
297
+ isSigner: false
298
+ },
299
+ {
300
+ name: 'splTokenProgramInfo',
301
+ isMut: false,
302
+ isSigner: false
303
+ },
304
+ {
305
+ name: 'systemProgram',
306
+ isMut: false,
307
+ isSigner: false
308
+ }
309
+ ],
310
+ args: [
311
+ {
312
+ name: 'overrideCurrentTime',
313
+ type: 'i64'
314
+ }
315
+ ]
316
+ },
317
+ {
318
+ name: 'createVault',
319
+ accounts: [
320
+ {
321
+ name: 'vaultSystemState',
322
+ isMut: true,
323
+ isSigner: false
324
+ },
325
+ {
326
+ name: 'vault',
327
+ isMut: true,
328
+ isSigner: true
329
+ },
330
+ {
331
+ name: 'history',
332
+ isMut: true,
333
+ isSigner: true
334
+ },
335
+ {
336
+ name: 'payer',
337
+ isMut: true,
338
+ isSigner: true
339
+ },
340
+ {
341
+ name: 'systemProgram',
342
+ isMut: false,
343
+ isSigner: false
344
+ }
345
+ ],
346
+ args: [
347
+ {
348
+ name: 'depositAmount',
349
+ type: 'u64'
350
+ },
351
+ {
352
+ name: 'minCollateralRatio',
353
+ type: 'u64'
354
+ }
355
+ ]
356
+ },
357
+ {
358
+ name: 'depositVault',
359
+ accounts: [
360
+ {
361
+ name: 'vaultSystemState',
362
+ isMut: true,
363
+ isSigner: false
364
+ },
365
+ {
366
+ name: 'vaultAccount',
367
+ isMut: true,
368
+ isSigner: false
369
+ },
370
+ {
371
+ name: 'history',
372
+ isMut: true,
373
+ isSigner: true
374
+ },
375
+ {
376
+ name: 'vaultOwner',
377
+ isMut: true,
378
+ isSigner: true
379
+ },
380
+ {
381
+ name: 'systemProgram',
382
+ isMut: false,
383
+ isSigner: false
384
+ }
385
+ ],
386
+ args: [
387
+ {
388
+ name: 'depositAmount',
389
+ type: 'u64'
390
+ }
391
+ ]
392
+ },
393
+ {
394
+ name: 'withdrawVault',
395
+ accounts: [
396
+ {
397
+ name: 'vaultSystemState',
398
+ isMut: true,
399
+ isSigner: false
400
+ },
401
+ {
402
+ name: 'vaultAccount',
403
+ isMut: true,
404
+ isSigner: false
405
+ },
406
+ {
407
+ name: 'history',
408
+ isMut: true,
409
+ isSigner: true
410
+ },
411
+ {
412
+ name: 'chainlinkFeedAccount',
413
+ isMut: false,
414
+ isSigner: false
415
+ },
416
+ {
417
+ name: 'vaultOwner',
418
+ isMut: true,
419
+ isSigner: true
420
+ },
421
+ {
422
+ name: 'systemProgram',
423
+ isMut: false,
424
+ isSigner: false
425
+ }
426
+ ],
427
+ args: [
428
+ {
429
+ name: 'withdrawAmount',
430
+ type: 'u64'
431
+ },
432
+ {
433
+ name: 'overrideSolPrice',
434
+ type: 'u64'
435
+ }
436
+ ]
437
+ },
438
+ {
439
+ name: 'loanVault',
440
+ accounts: [
441
+ {
442
+ name: 'vaultSystemState',
443
+ isMut: true,
444
+ isSigner: false
445
+ },
446
+ {
447
+ name: 'vaultAccount',
448
+ isMut: true,
449
+ isSigner: false
450
+ },
451
+ {
452
+ name: 'history',
453
+ isMut: true,
454
+ isSigner: true
455
+ },
456
+ {
457
+ name: 'feePool',
458
+ isMut: true,
459
+ isSigner: false
460
+ },
461
+ {
462
+ name: 'feePoolAssociatedUsdhTokenAccount',
463
+ isMut: true,
464
+ isSigner: false
465
+ },
466
+ {
467
+ name: 'usdhMint',
468
+ isMut: true,
469
+ isSigner: false
470
+ },
471
+ {
472
+ name: 'vaultOwner',
473
+ isMut: true,
474
+ isSigner: true
475
+ },
476
+ {
477
+ name: 'ownerUsdhAccount',
478
+ isMut: true,
479
+ isSigner: false
480
+ },
481
+ {
482
+ name: 'chainlinkFeedAccount',
483
+ isMut: false,
484
+ isSigner: false
485
+ },
486
+ {
487
+ name: 'splTokenProgramInfo',
488
+ isMut: false,
489
+ isSigner: false
490
+ },
491
+ {
492
+ name: 'systemProgram',
493
+ isMut: false,
494
+ isSigner: false
495
+ }
496
+ ],
497
+ args: [
498
+ {
499
+ name: 'loanAmount',
500
+ type: 'u64'
501
+ },
502
+ {
503
+ name: 'overrideSolPrice',
504
+ type: 'u64'
505
+ }
506
+ ]
507
+ },
508
+ {
509
+ name: 'repayVault',
510
+ accounts: [
511
+ {
512
+ name: 'vaultSystemState',
513
+ isMut: true,
514
+ isSigner: false
515
+ },
516
+ {
517
+ name: 'vaultAccount',
518
+ isMut: true,
519
+ isSigner: false
520
+ },
521
+ {
522
+ name: 'history',
523
+ isMut: true,
524
+ isSigner: true
525
+ },
526
+ {
527
+ name: 'usdhMint',
528
+ isMut: true,
529
+ isSigner: false
530
+ },
531
+ {
532
+ name: 'vaultOwner',
533
+ isMut: true,
534
+ isSigner: true
535
+ },
536
+ {
537
+ name: 'ownerUsdhAccount',
538
+ isMut: true,
539
+ isSigner: false
540
+ },
541
+ {
542
+ name: 'splTokenProgramInfo',
543
+ isMut: false,
544
+ isSigner: false
545
+ },
546
+ {
547
+ name: 'systemProgram',
548
+ isMut: false,
549
+ isSigner: false
550
+ }
551
+ ],
552
+ args: [
553
+ {
554
+ name: 'repayAmount',
555
+ type: 'u64'
556
+ }
557
+ ]
558
+ },
559
+ {
560
+ name: 'redeemVault',
561
+ accounts: [
562
+ {
563
+ name: 'vaultSystemState',
564
+ isMut: true,
565
+ isSigner: false
566
+ },
567
+ {
568
+ name: 'vaultAccount',
569
+ isMut: true,
570
+ isSigner: false
571
+ },
572
+ {
573
+ name: 'history',
574
+ isMut: true,
575
+ isSigner: true
576
+ },
577
+ {
578
+ name: 'usdhMint',
579
+ isMut: true,
580
+ isSigner: false
581
+ },
582
+ {
583
+ name: 'feePool',
584
+ isMut: true,
585
+ isSigner: false
586
+ },
587
+ {
588
+ name: 'feePoolAssociatedUsdhTokenAccount',
589
+ isMut: true,
590
+ isSigner: false
591
+ },
592
+ {
593
+ name: 'payer',
594
+ isMut: true,
595
+ isSigner: true
596
+ },
597
+ {
598
+ name: 'payerUsdhAccount',
599
+ isMut: true,
600
+ isSigner: false
601
+ },
602
+ {
603
+ name: 'chainlinkFeedAccount',
604
+ isMut: false,
605
+ isSigner: false
606
+ },
607
+ {
608
+ name: 'splTokenProgramInfo',
609
+ isMut: false,
610
+ isSigner: false
611
+ },
612
+ {
613
+ name: 'systemProgram',
614
+ isMut: false,
615
+ isSigner: false
616
+ }
617
+ ],
618
+ args: [
619
+ {
620
+ name: 'redeemPayUsdh',
621
+ type: 'u64'
622
+ },
623
+ {
624
+ name: 'overrideSolPrice',
625
+ type: 'u64'
626
+ },
627
+ {
628
+ name: 'overrideCurrentTime',
629
+ type: 'i64'
630
+ }
631
+ ]
632
+ },
633
+ {
634
+ name: 'depositLiquidityPool',
635
+ accounts: [
636
+ {
637
+ name: 'poolState',
638
+ isMut: true,
639
+ isSigner: false
640
+ },
641
+ {
642
+ name: 'poolUsdhAccount',
643
+ isMut: true,
644
+ isSigner: false
645
+ },
646
+ {
647
+ name: 'poolEra',
648
+ isMut: true,
649
+ isSigner: false
650
+ },
651
+ {
652
+ name: 'poolPosition',
653
+ isMut: true,
654
+ isSigner: true
655
+ },
656
+ {
657
+ name: 'payer',
658
+ isMut: true,
659
+ isSigner: true
660
+ },
661
+ {
662
+ name: 'ownerUsdhAccount',
663
+ isMut: true,
664
+ isSigner: false
665
+ },
666
+ {
667
+ name: 'splTokenProgramInfo',
668
+ isMut: false,
669
+ isSigner: false
670
+ },
671
+ {
672
+ name: 'systemProgram',
673
+ isMut: false,
674
+ isSigner: false
675
+ }
676
+ ],
677
+ args: [
678
+ {
679
+ name: 'depositAmount',
680
+ type: 'u64'
681
+ },
682
+ {
683
+ name: 'overrideCurrentTime',
684
+ type: 'i64'
685
+ }
686
+ ]
687
+ },
688
+ {
689
+ name: 'liquidateVault',
690
+ accounts: [
691
+ {
692
+ name: 'vaultSystemState',
693
+ isMut: true,
694
+ isSigner: false
695
+ },
696
+ {
697
+ name: 'vaultAccount',
698
+ isMut: true,
699
+ isSigner: false
700
+ },
701
+ {
702
+ name: 'poolState',
703
+ isMut: true,
704
+ isSigner: false
705
+ },
706
+ {
707
+ name: 'feePool',
708
+ isMut: true,
709
+ isSigner: false
710
+ },
711
+ {
712
+ name: 'poolUsdhAccount',
713
+ isMut: true,
714
+ isSigner: false
715
+ },
716
+ {
717
+ name: 'usdhMint',
718
+ isMut: true,
719
+ isSigner: false
720
+ },
721
+ {
722
+ name: 'poolEra',
723
+ isMut: true,
724
+ isSigner: false
725
+ },
726
+ {
727
+ name: 'newEra',
728
+ isMut: true,
729
+ isSigner: true
730
+ },
731
+ {
732
+ name: 'chainlinkFeedAccount',
733
+ isMut: false,
734
+ isSigner: false
735
+ },
736
+ {
737
+ name: 'history',
738
+ isMut: true,
739
+ isSigner: true
740
+ },
741
+ {
742
+ name: 'payer',
743
+ isMut: true,
744
+ isSigner: true
745
+ },
746
+ {
747
+ name: 'splTokenProgramInfo',
748
+ isMut: false,
749
+ isSigner: false
750
+ },
751
+ {
752
+ name: 'systemProgram',
753
+ isMut: false,
754
+ isSigner: false
755
+ }
756
+ ],
757
+ args: [
758
+ {
759
+ name: 'overrideSolPrice',
760
+ type: 'u64'
761
+ }
762
+ ]
763
+ },
764
+ {
765
+ name: 'withdrawLiquidity',
766
+ accounts: [
767
+ {
768
+ name: 'poolState',
769
+ isMut: true,
770
+ isSigner: false
771
+ },
772
+ {
773
+ name: 'poolEra',
774
+ isMut: true,
775
+ isSigner: false
776
+ },
777
+ {
778
+ name: 'poolPosition',
779
+ isMut: true,
780
+ isSigner: false
781
+ },
782
+ {
783
+ name: 'poolUsdhAccount',
784
+ isMut: true,
785
+ isSigner: false
786
+ },
787
+ {
788
+ name: 'payer',
789
+ isMut: true,
790
+ isSigner: true
791
+ },
792
+ {
793
+ name: 'ownerUsdhAccount',
794
+ isMut: true,
795
+ isSigner: false
796
+ },
797
+ {
798
+ name: 'hedgeMint',
799
+ isMut: true,
800
+ isSigner: false
801
+ },
802
+ {
803
+ name: 'payerAssociatedHedgeAccount',
804
+ isMut: true,
805
+ isSigner: false
806
+ },
807
+ {
808
+ name: 'rent',
809
+ isMut: false,
810
+ isSigner: false
811
+ },
812
+ {
813
+ name: 'splTokenProgramInfo',
814
+ isMut: false,
815
+ isSigner: false
816
+ },
817
+ {
818
+ name: 'splAssociatedTokenProgramInfo',
819
+ isMut: false,
820
+ isSigner: false
821
+ },
822
+ {
823
+ name: 'systemProgram',
824
+ isMut: false,
825
+ isSigner: false
826
+ }
827
+ ],
828
+ args: [
829
+ {
830
+ name: 'overrideCurrentTime',
831
+ type: 'i64'
832
+ }
833
+ ]
834
+ }
835
+ ],
836
+ accounts: [
837
+ {
838
+ name: 'StakingPool',
839
+ type: {
840
+ kind: 'struct',
841
+ fields: [
842
+ {
843
+ name: 'deposits',
844
+ type: 'u64'
845
+ },
846
+ {
847
+ name: 'bump',
848
+ type: 'u8'
849
+ },
850
+ {
851
+ name: 'seedPhrase',
852
+ type: 'string'
853
+ },
854
+ {
855
+ name: 'hedgeRewardAccumulator',
856
+ type: 'u128'
857
+ },
858
+ {
859
+ name: 'usdhFeeAccumulator',
860
+ type: 'u128'
861
+ },
862
+ {
863
+ name: 'solFeeAccumulator',
864
+ type: 'u128'
865
+ },
866
+ {
867
+ name: 'startTime',
868
+ type: 'i64'
869
+ },
870
+ {
871
+ name: 'lastTransactionTime',
872
+ type: 'i64'
873
+ },
874
+ {
875
+ name: 'halfLifeInDays',
876
+ type: 'u64'
877
+ },
878
+ {
879
+ name: 'totalHedgeReward',
880
+ type: 'u64'
881
+ },
882
+ {
883
+ name: 'stakedTokenMint',
884
+ type: 'publicKey'
885
+ }
886
+ ]
887
+ }
888
+ },
889
+ {
890
+ name: 'StakingPoolPosition',
891
+ type: {
892
+ kind: 'struct',
893
+ fields: [
894
+ {
895
+ name: 'state',
896
+ type: {
897
+ defined: 'PositionState'
898
+ }
899
+ },
900
+ {
901
+ name: 'owner',
902
+ type: 'publicKey'
903
+ },
904
+ {
905
+ name: 'pool',
906
+ type: 'publicKey'
907
+ },
908
+ {
909
+ name: 'deposited',
910
+ type: 'u64'
911
+ },
912
+ {
913
+ name: 'startHedgeRewardAccumulator',
914
+ type: 'u128'
915
+ },
916
+ {
917
+ name: 'startUsdhFeeAccumulator',
918
+ type: 'u128'
919
+ },
920
+ {
921
+ name: 'startSolFeeAccumulator',
922
+ type: 'u128'
923
+ },
924
+ {
925
+ name: 'timestampOpened',
926
+ type: 'i64'
927
+ },
928
+ {
929
+ name: 'timestampClosed',
930
+ type: 'i64'
931
+ },
932
+ {
933
+ name: 'closedRewardedTokens',
934
+ type: 'u64'
935
+ }
936
+ ]
937
+ }
938
+ },
939
+ {
940
+ name: 'VaultSystemState',
941
+ type: {
942
+ kind: 'struct',
943
+ fields: [
944
+ {
945
+ name: 'lastRedeemFeeBytes',
946
+ type: 'u128'
947
+ },
948
+ {
949
+ name: 'lastRedeemTimestamp',
950
+ type: 'i64'
951
+ },
952
+ {
953
+ name: 'totalCollateral',
954
+ type: 'u64'
955
+ },
956
+ {
957
+ name: 'totalUsdhSupply',
958
+ type: 'u64'
959
+ },
960
+ {
961
+ name: 'totalVaults',
962
+ type: 'u64'
963
+ },
964
+ {
965
+ name: 'totalVaultsClosed',
966
+ type: 'u64'
967
+ },
968
+ {
969
+ name: 'debtRedistributionProduct',
970
+ type: 'u128'
971
+ },
972
+ {
973
+ name: 'collateralRedistributionAccumulator',
974
+ type: 'u128'
975
+ },
976
+ {
977
+ name: 'debtRedistributionError',
978
+ type: 'u64'
979
+ },
980
+ {
981
+ name: 'bumpVaultState',
982
+ type: 'u8'
983
+ },
984
+ {
985
+ name: 'bumpPoolState',
986
+ type: 'u8'
987
+ },
988
+ {
989
+ name: 'bumpMint',
990
+ type: 'u8'
991
+ },
992
+ {
993
+ name: 'bumpUsdhPoolAccount',
994
+ type: 'u8'
995
+ },
996
+ {
997
+ name: 'authority',
998
+ type: 'publicKey'
999
+ },
1000
+ {
1001
+ name: 'oracleChainlink',
1002
+ type: 'publicKey'
1003
+ },
1004
+ {
1005
+ name: 'oraclePyth',
1006
+ type: 'publicKey'
1007
+ },
1008
+ {
1009
+ name: 'oracleSwitchboard',
1010
+ type: 'publicKey'
1011
+ },
1012
+ {
1013
+ name: 'hedgeStakingPool',
1014
+ type: 'publicKey'
1015
+ }
1016
+ ]
1017
+ }
1018
+ },
1019
+ {
1020
+ name: 'Vault',
1021
+ type: {
1022
+ kind: 'struct',
1023
+ fields: [
1024
+ {
1025
+ name: 'vaultOwner',
1026
+ type: 'publicKey'
1027
+ },
1028
+ {
1029
+ name: 'deposited',
1030
+ type: 'u64'
1031
+ },
1032
+ {
1033
+ name: 'debt',
1034
+ type: 'u64'
1035
+ },
1036
+ {
1037
+ name: 'vaultVersion',
1038
+ type: 'u64'
1039
+ },
1040
+ {
1041
+ name: 'liquidationPrice',
1042
+ type: 'u64'
1043
+ },
1044
+ {
1045
+ name: 'minCollateralRatio',
1046
+ type: 'u128'
1047
+ },
1048
+ {
1049
+ name: 'debtProductSnapshotBytes',
1050
+ type: 'u128'
1051
+ },
1052
+ {
1053
+ name: 'collateralAccumulatorSnapshotBytes',
1054
+ type: 'u128'
1055
+ },
1056
+ {
1057
+ name: 'collateralType',
1058
+ type: {
1059
+ defined: 'CurrencyType'
1060
+ }
1061
+ },
1062
+ {
1063
+ name: 'debtType',
1064
+ type: {
1065
+ defined: 'CurrencyType'
1066
+ }
1067
+ },
1068
+ {
1069
+ name: 'vaultStatus',
1070
+ type: {
1071
+ defined: 'VaultStatus'
1072
+ }
1073
+ }
1074
+ ]
1075
+ }
1076
+ },
1077
+ {
1078
+ name: 'VaultHistoryEvent',
1079
+ type: {
1080
+ kind: 'struct',
1081
+ fields: [
1082
+ {
1083
+ name: 'vaultAccount',
1084
+ type: 'publicKey'
1085
+ },
1086
+ {
1087
+ name: 'actorAccount',
1088
+ type: 'publicKey'
1089
+ },
1090
+ {
1091
+ name: 'usdSolPrice',
1092
+ type: 'u128'
1093
+ },
1094
+ {
1095
+ name: 'usdhDebtBefore',
1096
+ type: 'u64'
1097
+ },
1098
+ {
1099
+ name: 'usdhDebtAfter',
1100
+ type: 'u64'
1101
+ },
1102
+ {
1103
+ name: 'solBalanceBefore',
1104
+ type: 'u64'
1105
+ },
1106
+ {
1107
+ name: 'solBalanceAfter',
1108
+ type: 'u64'
1109
+ },
1110
+ {
1111
+ name: 'minCollateralRatioBefore',
1112
+ type: 'u128'
1113
+ },
1114
+ {
1115
+ name: 'minCollateralRatioAfter',
1116
+ type: 'u128'
1117
+ },
1118
+ {
1119
+ name: 'vaultStateBefore',
1120
+ type: {
1121
+ defined: 'VaultStatus'
1122
+ }
1123
+ },
1124
+ {
1125
+ name: 'vaultStateAfter',
1126
+ type: {
1127
+ defined: 'VaultStatus'
1128
+ }
1129
+ },
1130
+ {
1131
+ name: 'timestamp',
1132
+ type: 'i64'
1133
+ },
1134
+ {
1135
+ name: 'action',
1136
+ type: {
1137
+ defined: 'VaultHistoryAction'
1138
+ }
1139
+ }
1140
+ ]
1141
+ }
1142
+ },
1143
+ {
1144
+ name: 'LiquidationPoolState',
1145
+ type: {
1146
+ kind: 'struct',
1147
+ fields: [
1148
+ {
1149
+ name: 'currentEra',
1150
+ type: 'publicKey'
1151
+ },
1152
+ {
1153
+ name: 'lifetimeDeposits',
1154
+ type: 'u64'
1155
+ },
1156
+ {
1157
+ name: 'lifetimeOpenedPositions',
1158
+ type: 'u64'
1159
+ },
1160
+ {
1161
+ name: 'lifetimeClosedPositions',
1162
+ type: 'u64'
1163
+ },
1164
+ {
1165
+ name: 'lifetimeVaultsLiquidated',
1166
+ type: 'u64'
1167
+ },
1168
+ {
1169
+ name: 'lifetimeVaultsRedistributed',
1170
+ type: 'u64'
1171
+ },
1172
+ {
1173
+ name: 'lifetimeSolPurchased',
1174
+ type: 'u64'
1175
+ },
1176
+ {
1177
+ name: 'lifetimeSolClaimed',
1178
+ type: 'u64'
1179
+ },
1180
+ {
1181
+ name: 'hedgeInitRewardsTimestamp',
1182
+ type: 'i64'
1183
+ }
1184
+ ]
1185
+ }
1186
+ },
1187
+ {
1188
+ name: 'LiquidationPoolEra',
1189
+ type: {
1190
+ kind: 'struct',
1191
+ fields: [
1192
+ {
1193
+ name: 'totalDeposits',
1194
+ type: 'u64'
1195
+ },
1196
+ {
1197
+ name: 'productBytes',
1198
+ type: 'u128'
1199
+ },
1200
+ {
1201
+ name: 'sumBytes',
1202
+ type: 'u128'
1203
+ },
1204
+ {
1205
+ name: 'hedgeRewardsAccumulatorBytes',
1206
+ type: 'u128'
1207
+ },
1208
+ {
1209
+ name: 'hedgeRewardsTimestamp',
1210
+ type: 'i64'
1211
+ }
1212
+ ]
1213
+ }
1214
+ },
1215
+ {
1216
+ name: 'LiquidationPosition',
1217
+ type: {
1218
+ kind: 'struct',
1219
+ fields: [
1220
+ {
1221
+ name: 'state',
1222
+ type: {
1223
+ defined: 'PositionState'
1224
+ }
1225
+ },
1226
+ {
1227
+ name: 'era',
1228
+ type: 'publicKey'
1229
+ },
1230
+ {
1231
+ name: 'ownerAccount',
1232
+ type: 'publicKey'
1233
+ },
1234
+ {
1235
+ name: 'deposit',
1236
+ type: 'u64'
1237
+ },
1238
+ {
1239
+ name: 'closedSol',
1240
+ type: 'u64'
1241
+ },
1242
+ {
1243
+ name: 'closedUsdh',
1244
+ type: 'u64'
1245
+ },
1246
+ {
1247
+ name: 'productSnapshotBytes',
1248
+ type: 'u128'
1249
+ },
1250
+ {
1251
+ name: 'sumSnapshotBytes',
1252
+ type: 'u128'
1253
+ },
1254
+ {
1255
+ name: 'hedgeRewardsSnapshotAccum',
1256
+ type: 'u128'
1257
+ },
1258
+ {
1259
+ name: 'timestampOpened',
1260
+ type: 'i64'
1261
+ },
1262
+ {
1263
+ name: 'timestampClosed',
1264
+ type: 'i64'
1265
+ }
1266
+ ]
1267
+ }
1268
+ },
1269
+ {
1270
+ name: 'Aggregator',
1271
+ type: {
1272
+ kind: 'struct',
1273
+ fields: [
1274
+ {
1275
+ name: 'isInitialized',
1276
+ type: 'bool'
1277
+ },
1278
+ {
1279
+ name: 'version',
1280
+ type: 'u32'
1281
+ },
1282
+ {
1283
+ name: 'config',
1284
+ type: {
1285
+ defined: 'Config'
1286
+ }
1287
+ },
1288
+ {
1289
+ name: 'updatedAt',
1290
+ type: 'i64'
1291
+ },
1292
+ {
1293
+ name: 'owner',
1294
+ type: 'publicKey'
1295
+ },
1296
+ {
1297
+ name: 'submissions',
1298
+ type: {
1299
+ array: [
1300
+ {
1301
+ defined: 'Submission'
1302
+ },
1303
+ 8
1304
+ ]
1305
+ }
1306
+ },
1307
+ {
1308
+ name: 'answer',
1309
+ type: {
1310
+ option: 'u128'
1311
+ }
1312
+ }
1313
+ ]
1314
+ }
1315
+ }
1316
+ ],
1317
+ types: [
1318
+ {
1319
+ name: 'Config',
1320
+ type: {
1321
+ kind: 'struct',
1322
+ fields: [
1323
+ {
1324
+ name: 'oracles',
1325
+ type: {
1326
+ vec: 'publicKey'
1327
+ }
1328
+ },
1329
+ {
1330
+ name: 'minAnswerThreshold',
1331
+ type: 'u8'
1332
+ },
1333
+ {
1334
+ name: 'stalenessThreshold',
1335
+ type: 'u8'
1336
+ },
1337
+ {
1338
+ name: 'decimals',
1339
+ type: 'u8'
1340
+ }
1341
+ ]
1342
+ }
1343
+ },
1344
+ {
1345
+ name: 'Submission',
1346
+ type: {
1347
+ kind: 'struct',
1348
+ fields: [
1349
+ {
1350
+ name: 'timestamp',
1351
+ type: 'i64'
1352
+ },
1353
+ {
1354
+ name: 'value',
1355
+ type: 'u128'
1356
+ }
1357
+ ]
1358
+ }
1359
+ },
1360
+ {
1361
+ name: 'PositionState',
1362
+ type: {
1363
+ kind: 'enum',
1364
+ variants: [
1365
+ {
1366
+ name: 'Open'
1367
+ },
1368
+ {
1369
+ name: 'Closed'
1370
+ }
1371
+ ]
1372
+ }
1373
+ },
1374
+ {
1375
+ name: 'CurrencyType',
1376
+ type: {
1377
+ kind: 'enum',
1378
+ variants: [
1379
+ {
1380
+ name: 'Solana'
1381
+ },
1382
+ {
1383
+ name: 'Usdh'
1384
+ }
1385
+ ]
1386
+ }
1387
+ },
1388
+ {
1389
+ name: 'VaultStatus',
1390
+ type: {
1391
+ kind: 'enum',
1392
+ variants: [
1393
+ {
1394
+ name: 'Open'
1395
+ },
1396
+ {
1397
+ name: 'Closed'
1398
+ },
1399
+ {
1400
+ name: 'Liquidated'
1401
+ },
1402
+ {
1403
+ name: 'Distributed'
1404
+ },
1405
+ {
1406
+ name: 'Redeemed'
1407
+ }
1408
+ ]
1409
+ }
1410
+ },
1411
+ {
1412
+ name: 'VaultHistoryAction',
1413
+ type: {
1414
+ kind: 'enum',
1415
+ variants: [
1416
+ {
1417
+ name: 'Create'
1418
+ },
1419
+ {
1420
+ name: 'Close'
1421
+ },
1422
+ {
1423
+ name: 'Liquidate'
1424
+ },
1425
+ {
1426
+ name: 'PartialLiquidate'
1427
+ },
1428
+ {
1429
+ name: 'Distributed'
1430
+ },
1431
+ {
1432
+ name: 'Redeem'
1433
+ },
1434
+ {
1435
+ name: 'TransferOwnership'
1436
+ },
1437
+ {
1438
+ name: 'Deposit'
1439
+ },
1440
+ {
1441
+ name: 'Withdraw'
1442
+ },
1443
+ {
1444
+ name: 'Loan'
1445
+ },
1446
+ {
1447
+ name: 'RepayCredit'
1448
+ }
1449
+ ]
1450
+ }
1451
+ }
1452
+ ],
1453
+ errors: [
1454
+ {
1455
+ code: 6000,
1456
+ name: 'AlreadyInitialized',
1457
+ msg: 'This vault was already initialized'
1458
+ },
1459
+ {
1460
+ code: 6001,
1461
+ name: 'NotInitialized',
1462
+ msg: 'This vault was not initialized'
1463
+ },
1464
+ {
1465
+ code: 6002,
1466
+ name: 'Overdraw',
1467
+ msg: 'Cannot withdraw more than deposited'
1468
+ },
1469
+ {
1470
+ code: 6003,
1471
+ name: 'Overpaid',
1472
+ msg: 'Cannot repay more than loaned'
1473
+ },
1474
+ {
1475
+ code: 6004,
1476
+ name: 'NotAuthorizedToCreateStakingPools',
1477
+ msg: 'Signer not authorized to create new staking pools'
1478
+ },
1479
+ {
1480
+ code: 6005,
1481
+ name: 'BadSeed',
1482
+ msg: 'Bad Seed'
1483
+ }
1484
+ ]
1485
+ };
1486
+
1487
+ function parseAnchorErrors(error) {
1488
+ const idlErrors = anchor.parseIdlErrors(vaultIdl);
1489
+ const parsedError = anchor.ProgramError.parse(error, idlErrors);
1490
+ if (parsedError !== null) {
1491
+ throw parsedError;
1492
+ }
1493
+ throw error;
1494
+ }
1495
+
1496
+ const HEDGE_PROGRAM_ID = 'fff6FuPWrBPzFHeWwtBQaPjKvMvW27w8Jf4P7SSoueX';
1497
+ const HEDGE_PROGRAM_PUBLICKEY = new web3_js.PublicKey(HEDGE_PROGRAM_ID);
1498
+ const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf';
1499
+ const CHAINLINK_SOL_USD_PUBLICKEY = new web3_js.PublicKey(CHAINLINK_SOL_USD_ID);
1500
+ async function getLiquidationPoolStatePublicKey() {
1501
+ const enc = new TextEncoder();
1502
+ const [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolStateV1')], HEDGE_PROGRAM_PUBLICKEY);
1503
+ return [poolPublicKey, bump];
1504
+ }
1505
+ async function getLiquidationPoolUsdhAccountPublicKey() {
1506
+ const enc = new TextEncoder();
1507
+ const [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolUSDHAccountV1')], HEDGE_PROGRAM_PUBLICKEY);
1508
+ return [poolPublicKey, bump];
1509
+ }
1510
+ async function getUsdhMintPublicKey() {
1511
+ const enc = new TextEncoder();
1512
+ const [findMintPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('UsdhMintV1')], HEDGE_PROGRAM_PUBLICKEY);
1513
+ return [findMintPublicKey, bump];
1514
+ }
1515
+ async function getVaultSystemStatePublicKey() {
1516
+ const enc = new TextEncoder();
1517
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('VaultSystemStateV1')], HEDGE_PROGRAM_PUBLICKEY);
1518
+ return [publicKey, bump];
1519
+ }
1520
+ async function getHedgeMintPublicKey() {
1521
+ const enc = new TextEncoder();
1522
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('HEDGEMintV1')], HEDGE_PROGRAM_PUBLICKEY);
1523
+ return [publicKey, bump];
1524
+ }
1525
+ async function getPoolPublicKeyForMint(mintPublicKey) {
1526
+ const enc = new TextEncoder();
1527
+ const strToEncode = (mintPublicKey.toString().substring(0, 12));
1528
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode(strToEncode)], HEDGE_PROGRAM_PUBLICKEY);
1529
+ return [publicKey, bump, strToEncode];
1530
+ }
1531
+ async function findAssociatedTokenAddress(walletAddress, tokenMintAddress) {
1532
+ return (await web3_js.PublicKey.findProgramAddress([
1533
+ walletAddress.toBuffer(),
1534
+ splToken.TOKEN_PROGRAM_ID.toBuffer(),
1535
+ tokenMintAddress.toBuffer()
1536
+ ], splToken.ASSOCIATED_TOKEN_PROGRAM_ID))[0];
1537
+ }
1538
+
1539
+ async function createStakingPool(program, provider, payer, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1540
+ const transaction = new web3_js.Transaction().add(await createStakingPoolInstruction(program, payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime));
1541
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
1542
+ const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey);
1543
+ return poolPublickey;
1544
+ }
1545
+ async function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1546
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1547
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1548
+ const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey);
1549
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey);
1550
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1551
+ return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor.BN(hedgeTokensToBeMinted), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), {
1552
+ accounts: {
1553
+ signer: payerPublicKey,
1554
+ vaultSystemState: vaultSystemStatePublicKey,
1555
+ pool: poolPublickey,
1556
+ stakedTokenMintInfo: mintPublicKey,
1557
+ usdhMint: usdhMintPublickey,
1558
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1559
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1560
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1561
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1562
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1563
+ systemProgram: web3_js.SystemProgram.programId
1564
+ },
1565
+ signers: []
1566
+ });
1567
+ }
1568
+
1569
+ async function depositStakingPool(program, provider, payer, mintPublicKey, depositAmount, overrideStartTime) {
1570
+ const poolPosition = web3_js.Keypair.generate();
1571
+ const transaction = new web3_js.Transaction().add(await depositStakingPoolInstruction(program, payer.publicKey, poolPosition.publicKey, mintPublicKey, depositAmount, overrideStartTime));
1572
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors);
1573
+ return poolPosition.publicKey;
1574
+ }
1575
+ async function depositStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, depositAmount, overrideStartTime) {
1576
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1577
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1578
+ const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1579
+ return program.instruction.depositStakingPool(new anchor.BN(depositAmount), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1580
+ {
1581
+ accounts: {
1582
+ payer: payerPublicKey,
1583
+ pool: poolPublickey,
1584
+ poolPosition: poolPositionPublicKey,
1585
+ stakedTokenMintInfo: stakedTokenMintPublicKey,
1586
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1587
+ payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
1588
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1589
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1590
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1591
+ systemProgram: web3_js.SystemProgram.programId
1592
+ },
1593
+ signers: []
1594
+ });
1595
+ }
1596
+
1597
+ async function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1598
+ const poolPosition = web3_js.Keypair.generate();
1599
+ const transaction = new web3_js.Transaction().add(await withdrawStakingPoolInstruction(program, payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime));
1600
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
1601
+ return poolPosition.publicKey;
1602
+ }
1603
+ async function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1604
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1605
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1606
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1607
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1608
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1609
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1610
+ const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1611
+ const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey);
1612
+ const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey);
1613
+ return program.instruction.withdrawStakingPool(new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1614
+ {
1615
+ accounts: {
1616
+ payer: payerPublicKey,
1617
+ vaultSystemState: vaultSystemStatePublicKey,
1618
+ pool: poolPublickey,
1619
+ poolPosition: poolPositionPublicKey,
1620
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1621
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1622
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
1623
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
1624
+ payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
1625
+ hedgeMint: hedgeMintPublickey,
1626
+ stakedTokenMint: stakedTokenMintPublicKey,
1627
+ usdhMint: usdhMintPublickey,
1628
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1629
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1630
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1631
+ systemProgram: web3_js.SystemProgram.programId
1632
+ },
1633
+ signers: []
1634
+ });
1635
+ }
1636
+
1637
+ async function createVault(program, provider, payer, depositAmount, collateralRatio) {
1638
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1639
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1640
+ // Prep the user to get USDH back out at some point
1641
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1642
+ const newVault = web3_js.Keypair.generate();
1643
+ const history = web3_js.Keypair.generate();
1644
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1645
+ const transaction = new web3_js.Transaction().add(createVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, newVault.publicKey, history.publicKey, depositAmount, collateralRatio));
1646
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, newVault, history], provider === null || provider === void 0 ? void 0 : provider.opts);
1647
+ return newVault.publicKey;
1648
+ }
1649
+ function createVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount, collateralRatio) {
1650
+ const ix = program.instruction.createVault(new anchor.BN(depositAmount), new anchor.BN(collateralRatio), {
1651
+ accounts: {
1652
+ vaultSystemState: vaultSystemStatePublicKey,
1653
+ vault: vaultPublicKey,
1654
+ history: historyPublicKey,
1655
+ payer: payerPublicKey,
1656
+ systemProgram: web3_js.SystemProgram.programId
1657
+ },
1658
+ signers: []
1659
+ });
1660
+ return ix;
1661
+ }
1662
+
1663
+ async function depositVault(program, provider, payer, vaultPublicKey, depositAmount) {
1664
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1665
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1666
+ // Prep the user to get USDH back out at some point
1667
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1668
+ const history = web3_js.Keypair.generate();
1669
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1670
+ const transaction = new web3_js.Transaction().add(depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, depositAmount));
1671
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1672
+ return vaultPublicKey;
1673
+ }
1674
+ function depositVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount) {
1675
+ return program.instruction.depositVault(new anchor.BN(depositAmount), {
1676
+ accounts: {
1677
+ vaultSystemState: vaultSystemStatePublicKey,
1678
+ vaultAccount: vaultPublicKey,
1679
+ history: historyPublicKey,
1680
+ vaultOwner: payerPublicKey,
1681
+ systemProgram: web3_js.SystemProgram.programId
1682
+ },
1683
+ signers: []
1684
+ });
1685
+ }
1686
+
1687
+ async function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount, chainlinkOverridePrice) {
1688
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1689
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1690
+ // Prep the user to get USDH back out at some point
1691
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1692
+ const history = web3_js.Keypair.generate();
1693
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1694
+ const transaction = new web3_js.Transaction().add(withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, withdrawAmount, chainlinkOverridePrice));
1695
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1696
+ return vaultPublicKey;
1697
+ }
1698
+ function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublickey, historyPublicKey, withdrawAmount, chainlinkOverridePrice) {
1699
+ return program.instruction.withdrawVault(new anchor.BN(withdrawAmount), new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : 200 * web3_js.LAMPORTS_PER_SOL), {
1700
+ accounts: {
1701
+ vaultSystemState: vaultSystemStatePublicKey,
1702
+ vaultAccount: vaultPublickey,
1703
+ history: historyPublicKey,
1704
+ vaultOwner: payerPublicKey,
1705
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1706
+ systemProgram: web3_js.SystemProgram.programId
1707
+ },
1708
+ signers: []
1709
+ });
1710
+ }
1711
+
1712
+ async function loanVault(program, provider, payer, vaultPublicKey, loanAmount, chainlinkOverridePrice) {
1713
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1714
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1715
+ // Prep the user to get USDH back out at some point
1716
+ const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1717
+ const history = web3_js.Keypair.generate();
1718
+ const transaction = new web3_js.Transaction().add(await loanVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, loanAmount, chainlinkOverridePrice));
1719
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1720
+ return vaultPublicKey;
1721
+ }
1722
+ async function loanVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, loanAmount, chainlinkOverridePrice) {
1723
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1724
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1725
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1726
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1727
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
1728
+ return program.instruction.loanVault(new anchor.BN(loanAmount), new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : web3_js.LAMPORTS_PER_SOL * 150), // override usd/sol price
1729
+ {
1730
+ accounts: {
1731
+ vaultSystemState: vaultSystemStatePublicKey,
1732
+ vaultAccount: vaultPublickey,
1733
+ history: historyPublicKey,
1734
+ feePool: hedgeStakingPoolPublicKey,
1735
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
1736
+ usdhMint: usdhMintPublickey,
1737
+ vaultOwner: payerPublicKey,
1738
+ ownerUsdhAccount: ownerUsdhAccount,
1739
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1740
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1741
+ systemProgram: web3_js.SystemProgram.programId
1742
+ },
1743
+ signers: []
1744
+ });
1745
+ }
1746
+
1747
+ async function repayVault(program, provider, payer, vaultPublicKey, repayAmount) {
1748
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1749
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1750
+ // Prep the user to get USDH back out at some point
1751
+ const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1752
+ const history = web3_js.Keypair.generate();
1753
+ const transaction = new web3_js.Transaction().add(await repayVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, repayAmount));
1754
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1755
+ return vaultPublicKey;
1756
+ }
1757
+ async function repayVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount) {
1758
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1759
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1760
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1761
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1762
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
1763
+ return program.instruction.repayVault(new anchor.BN(repayAmount), {
1764
+ accounts: {
1765
+ vaultSystemState: vaultSystemStatePublicKey,
1766
+ vaultAccount: vaultPublickey,
1767
+ history: historyPublicKey,
1768
+ feePool: hedgeStakingPoolPublicKey,
1769
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
1770
+ usdhMint: usdhMintPublickey,
1771
+ vaultOwner: payerPublicKey,
1772
+ ownerUsdhAccount: ownerUsdhAccount,
1773
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1774
+ systemProgram: web3_js.SystemProgram.programId
1775
+ },
1776
+ signers: []
1777
+ });
1778
+ }
1779
+
1780
+ async function redeemVault(program, provider, payer, vaultPublicKey, repayAmount, chainlinkOverridePrice, transactionOverrideTime) {
1781
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1782
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1783
+ // Prep the user to get USDH back out at some point
1784
+ const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1785
+ const history = web3_js.Keypair.generate();
1786
+ const transaction = new web3_js.Transaction().add(await redeemVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, repayAmount, chainlinkOverridePrice, transactionOverrideTime));
1787
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1788
+ return vaultPublicKey;
1789
+ }
1790
+ async function redeemVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount, chainlinkOverridePrice, transactionOverrideTime) {
1791
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1792
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1793
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1794
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1795
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
1796
+ return program.instruction.redeemVault(new anchor.BN(repayAmount), new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : 150 * web3_js.LAMPORTS_PER_SOL), // override usd/sol price
1797
+ new anchor.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : (Date.now() / 1000)), // override start time
1798
+ {
1799
+ accounts: {
1800
+ vaultSystemState: vaultSystemStatePublicKey,
1801
+ vaultAccount: vaultPublickey,
1802
+ history: historyPublicKey,
1803
+ feePool: hedgeStakingPoolPublicKey,
1804
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
1805
+ usdhMint: usdhMintPublickey,
1806
+ payer: payerPublicKey,
1807
+ payerUsdhAccount: ownerUsdhAccount,
1808
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1809
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1810
+ systemProgram: web3_js.SystemProgram.programId
1811
+ },
1812
+ signers: []
1813
+ });
1814
+ }
1815
+
1816
+ async function liquidateVault(program, provider, payer, vaultPublicKey, chainlinkOverridePrice) {
1817
+ const history = web3_js.Keypair.generate();
1818
+ const newEra = web3_js.Keypair.generate();
1819
+ const transaction = new web3_js.Transaction().add(await liquidateVaultInstruction(program, payer.publicKey, vaultPublicKey, history.publicKey, newEra.publicKey, chainlinkOverridePrice));
1820
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts);
1821
+ return vaultPublicKey;
1822
+ }
1823
+ async function liquidateVaultInstruction(program, payerPublicKey, vaultPublickey, historyPublicKey, newEraPublicKey, chainlinkOverridePrice) {
1824
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1825
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1826
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1827
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1828
+ const [liquidationPoolStatePublicKey] = await getLiquidationPoolStatePublicKey();
1829
+ const [liquidationPoolUsdhAccountPublickey] = await getLiquidationPoolUsdhAccountPublicKey();
1830
+ const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey);
1831
+ return program.instruction.liquidateVault(new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : web3_js.LAMPORTS_PER_SOL * 150), // override usd/sol price
1832
+ {
1833
+ accounts: {
1834
+ vaultSystemState: vaultSystemStatePublicKey,
1835
+ poolEra: poolStateInfo.currentEra,
1836
+ vaultAccount: vaultPublickey,
1837
+ poolState: liquidationPoolStatePublicKey,
1838
+ usdhMint: usdhMintPublickey,
1839
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1840
+ history: historyPublicKey,
1841
+ payer: payerPublicKey,
1842
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1843
+ systemProgram: web3_js.SystemProgram.programId,
1844
+ feePool: hedgeStakingPoolPublicKey,
1845
+ poolUsdhAccount: liquidationPoolUsdhAccountPublickey,
1846
+ newEra: newEraPublicKey
1847
+ },
1848
+ signers: []
1849
+ });
1850
+ }
1851
+
1852
+ /**
1853
+ * Convert a U128 to a Decimal
1854
+ *
1855
+ * On chain, u128s are used to store Decimal numbers. These values
1856
+ * are simply stored as u128 where the actual value is divided by
1857
+ * 1.0e+24. This gives 24 digits of percision on decimal numbers.
1858
+ *
1859
+ * Example: the number 1.5 is stored as a u128: 1.5 * 10^24
1860
+ *
1861
+ * @param value the value to be converted to Decimal
1862
+ * @returns the converted value as a decimal.js object
1863
+ */
1864
+ function DecimalFromU128(value) {
1865
+ const adjustedValue = new Decimal__default["default"](value.toString());
1866
+ return adjustedValue.div(new Decimal__default["default"]('1e+24'));
1867
+ }
1868
+
1869
+ /**
1870
+ * A class that represents an on-chian vault.
1871
+ */
1872
+ class VaultAccount {
1873
+ constructor(vault, publicKey) {
1874
+ this.publicKey = publicKey;
1875
+ this.vaultOwner = vault.vaultOwner;
1876
+ this.debt = vault.debt.toNumber();
1877
+ this.deposited = vault.deposited.toNumber();
1878
+ this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio);
1879
+ this.liquidationPrice = vault.liquidationPrice.toNumber();
1880
+ this.vaultStatus = Object.keys(vault.vaultStatus)[0];
1881
+ }
1882
+ /**
1883
+ * Check if some `PublicKey` is the owner
1884
+ *
1885
+ * @param publicKey the publicKey to check against the vault owner
1886
+ * @returns true if publicKey matches the owner publicKey
1887
+ */
1888
+ isOwnedBy(publicKey) {
1889
+ return publicKey.toString() === this.vaultOwner.toString();
1890
+ }
1891
+ /**
1892
+ * Get the collateral value in SOL
1893
+ *
1894
+ * @returns collateral value in SOL
1895
+ */
1896
+ inSol() {
1897
+ return this.deposited / web3_js.LAMPORTS_PER_SOL;
1898
+ }
1899
+ /**
1900
+ * Get the debt value in USDH
1901
+ *
1902
+ * @returns debt value in USDH
1903
+ */
1904
+ inUsd() {
1905
+ return this.debt / web3_js.LAMPORTS_PER_SOL;
1906
+ }
1907
+ /**
1908
+ * Pretty print the vault publickey for easy display
1909
+ *
1910
+ * @returns example: `1b6ca...azy71s`
1911
+ */
1912
+ toDisplayString() {
1913
+ return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`;
1914
+ }
1915
+ }
1916
+
1917
+ class VaultHistoryEvent {
1918
+ constructor(account, publicKey) {
1919
+ this.account = account;
1920
+ this.publicKey = publicKey;
1921
+ this.actorAccount = account.actorAccount;
1922
+ this.usdSolPrice = DecimalFromU128(account.usdSolPrice.toString());
1923
+ this.usdhDebtBefore = account.usdhDebtBefore.toNumber();
1924
+ this.usdhDebtAfter = account.usdhDebtAfter.toNumber();
1925
+ this.solBalanceBefore = account.solBalanceBefore.toNumber();
1926
+ this.solBalanceAfter = account.solBalanceAfter.toNumber();
1927
+ this.minCollateralRatioBefore = DecimalFromU128(account.minCollateralRatioBefore);
1928
+ this.minCollateralRatioAfter = DecimalFromU128(account.minCollateralRatioAfter);
1929
+ this.vaultStateBefore = account.vaultStateBefore;
1930
+ this.vaultStateAfter = account.vaultStateAfter;
1931
+ this.timestamp = account.timestamp.toNumber();
1932
+ this.action = account.action;
1933
+ }
1934
+ }
1935
+ exports.VaultStatus = void 0;
1936
+ (function (VaultStatus) {
1937
+ VaultStatus[VaultStatus["Open"] = 0] = "Open";
1938
+ VaultStatus[VaultStatus["Closed"] = 1] = "Closed";
1939
+ VaultStatus[VaultStatus["Liquidated"] = 2] = "Liquidated";
1940
+ VaultStatus[VaultStatus["Distributed"] = 3] = "Distributed";
1941
+ VaultStatus[VaultStatus["Redeemed"] = 4] = "Redeemed";
1942
+ })(exports.VaultStatus || (exports.VaultStatus = {}));
1943
+ exports.VaultHistoryAction = void 0;
1944
+ (function (VaultHistoryAction) {
1945
+ VaultHistoryAction[VaultHistoryAction["Create"] = 0] = "Create";
1946
+ VaultHistoryAction[VaultHistoryAction["Close"] = 1] = "Close";
1947
+ VaultHistoryAction[VaultHistoryAction["Liquidate"] = 2] = "Liquidate";
1948
+ VaultHistoryAction[VaultHistoryAction["PartialLiquidate"] = 3] = "PartialLiquidate";
1949
+ VaultHistoryAction[VaultHistoryAction["Distributed"] = 4] = "Distributed";
1950
+ VaultHistoryAction[VaultHistoryAction["Redeem"] = 5] = "Redeem";
1951
+ VaultHistoryAction[VaultHistoryAction["TransferOwnership"] = 6] = "TransferOwnership";
1952
+ VaultHistoryAction[VaultHistoryAction["Deposit"] = 7] = "Deposit";
1953
+ VaultHistoryAction[VaultHistoryAction["Withdraw"] = 8] = "Withdraw";
1954
+ VaultHistoryAction[VaultHistoryAction["Loan"] = 9] = "Loan";
1955
+ VaultHistoryAction[VaultHistoryAction["RepayCredit"] = 10] = "RepayCredit";
1956
+ })(exports.VaultHistoryAction || (exports.VaultHistoryAction = {}));
1957
+
1958
+ class StakingPool {
1959
+ constructor(poolInfo, publicKey) {
1960
+ this.poolInfo = poolInfo;
1961
+ this.publicKey = publicKey;
1962
+ this.deposits = poolInfo.deposits.toNumber();
1963
+ // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
1964
+ // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
1965
+ this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber();
1966
+ this.startTime = poolInfo.startTime.toNumber();
1967
+ this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber();
1968
+ this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber();
1969
+ this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator);
1970
+ this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator);
1971
+ this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator);
1972
+ // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
1973
+ }
1974
+ }
1975
+
1976
+ class StakingPoolPosition {
1977
+ constructor(poolPositionInfo, key, stakingPool) {
1978
+ this.poolPositionInfo = poolPositionInfo;
1979
+ this.publicKey = key;
1980
+ this.pool = stakingPool;
1981
+ this.owner = poolPositionInfo.owner;
1982
+ this.deposited = poolPositionInfo.deposited.toNumber();
1983
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
1984
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
1985
+ this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber();
1986
+ this.startHedgeRewardAccumulator = DecimalFromU128(poolPositionInfo.startHedgeRewardAccumulator);
1987
+ this.startUsdhFeeAccumulator = DecimalFromU128(poolPositionInfo.startUsdhFeeAccumulator);
1988
+ this.startSolFeeAccumulator = DecimalFromU128(poolPositionInfo.startSolFeeAccumulator);
1989
+ this.open = poolPositionInfo.state.open !== undefined;
1990
+ }
1991
+ getCurrentUsdhFeeReward() {
1992
+ return this.pool.usdhFeeAccumulator.minus(this.startUsdhFeeAccumulator).times(new Decimal__default["default"](this.deposited));
1993
+ }
1994
+ }
1995
+
1996
+ /**
1997
+ * Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
1998
+ * created to maintain each users contribution to the pool.
1999
+ */
2000
+ class LiquidationPoolEra {
2001
+ constructor(liquidyPoolEra) {
2002
+ this.liquidyPoolEra = liquidyPoolEra;
2003
+ this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber();
2004
+ this.product = DecimalFromU128(liquidyPoolEra.productBytes);
2005
+ this.sum = DecimalFromU128(liquidyPoolEra.sumBytes);
2006
+ this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes);
2007
+ this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber();
2008
+ }
2009
+ }
2010
+
2011
+ class LiquidationPoolState {
2012
+ constructor(liquidationPoolState) {
2013
+ this.liquidationPoolState = liquidationPoolState;
2014
+ this.lifetimeDeposits = liquidationPoolState.lifetimeDeposits.toNumber();
2015
+ this.hedgeInitRewardsTimestamp = liquidationPoolState.hedgeInitRewardsTimestamp.toNumber();
2016
+ // TODO Add the rest that are missing. Do we need them?
2017
+ }
2018
+ }
2019
+
2020
+ class LiquidationPosition {
2021
+ constructor(poolPositionInfo, key, era, liquidationPoolState) {
2022
+ this.publicKey = key;
2023
+ this.eraPublicKey = poolPositionInfo.era;
2024
+ this.era = era;
2025
+ this.liquidationPoolState = liquidationPoolState;
2026
+ this.ownerAccount = poolPositionInfo.ownerAccount;
2027
+ this.deposit = poolPositionInfo.deposit.toNumber();
2028
+ this.closedUsdh = poolPositionInfo.closedUsdh.toNumber();
2029
+ this.closedSol = poolPositionInfo.closedSol.toNumber();
2030
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
2031
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
2032
+ this.productSnapshot = DecimalFromU128(poolPositionInfo.productSnapshotBytes);
2033
+ this.sumSnapshot = DecimalFromU128(poolPositionInfo.sumSnapshotBytes);
2034
+ this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum);
2035
+ this.open = poolPositionInfo.state.open !== undefined;
2036
+ }
2037
+ getUsdhAvailable() {
2038
+ return (this.era.product.div(this.productSnapshot)).mul(new Decimal__default["default"](this.deposit));
2039
+ }
2040
+ getSolAvailable() {
2041
+ return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal__default["default"](this.deposit)).floor();
2042
+ }
2043
+ getHedgeAvailable() {
2044
+ const LiquidationPoolTotalSupply = 2000000.0 * web3_js.LAMPORTS_PER_SOL;
2045
+ const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(LiquidationPoolTotalSupply, this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000, this.era.hedgeRewardsTimestamp * 1000, Date.now(), 365 * 1000);
2046
+ if (this.era.totalDeposits === 0) {
2047
+ return new Decimal__default["default"](0);
2048
+ }
2049
+ const rewardsPerToken = this.era.product.mul(new Decimal__default["default"](hedgeRewardsSinceLastUpdate / this.era.totalDeposits));
2050
+ const newAccumulator = rewardsPerToken.add(new Decimal__default["default"](this.era.hedgeRewardsAccumulator));
2051
+ const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal__default["default"](this.deposit)).div(new Decimal__default["default"](this.productSnapshot));
2052
+ return hedgeAvailable;
2053
+ }
2054
+ }
2055
+ function hedgeRewardsDecay(supply, birthTime, timeIn, timeOut, halfLifeInDays) {
2056
+ const timeInOffsetStart = timeIn - birthTime;
2057
+ const timeOutOffsetStart = timeOut - birthTime;
2058
+ const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24);
2059
+ const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart));
2060
+ return awardedTokens;
2061
+ }
2062
+
2063
+ exports.CHAINLINK_SOL_USD_ID = CHAINLINK_SOL_USD_ID;
2064
+ exports.CHAINLINK_SOL_USD_PUBLICKEY = CHAINLINK_SOL_USD_PUBLICKEY;
2065
+ exports.DecimalFromU128 = DecimalFromU128;
2066
+ exports.HEDGE_PROGRAM_ID = HEDGE_PROGRAM_ID;
2067
+ exports.HEDGE_PROGRAM_PUBLICKEY = HEDGE_PROGRAM_PUBLICKEY;
2068
+ exports.LiquidationPoolEra = LiquidationPoolEra;
2069
+ exports.LiquidationPoolState = LiquidationPoolState;
2070
+ exports.LiquidationPosition = LiquidationPosition;
2071
+ exports.StakingPool = StakingPool;
2072
+ exports.StakingPoolPosition = StakingPoolPosition;
2073
+ exports.VaultAccount = VaultAccount;
2074
+ exports.VaultHistoryEvent = VaultHistoryEvent;
2075
+ exports.createStakingPool = createStakingPool;
2076
+ exports.createStakingPoolInstruction = createStakingPoolInstruction;
2077
+ exports.createVault = createVault;
2078
+ exports.createVaultInstruction = createVaultInstruction;
2079
+ exports.depositStakingPool = depositStakingPool;
2080
+ exports.depositStakingPoolInstruction = depositStakingPoolInstruction;
2081
+ exports.depositVault = depositVault;
2082
+ exports.depositVaultInstruction = depositVaultInstruction;
2083
+ exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
2084
+ exports.getHedgeMintPublicKey = getHedgeMintPublicKey;
2085
+ exports.getLiquidationPoolStatePublicKey = getLiquidationPoolStatePublicKey;
2086
+ exports.getLiquidationPoolUsdhAccountPublicKey = getLiquidationPoolUsdhAccountPublicKey;
2087
+ exports.getPoolPublicKeyForMint = getPoolPublicKeyForMint;
2088
+ exports.getUsdhMintPublicKey = getUsdhMintPublicKey;
2089
+ exports.getVaultSystemStatePublicKey = getVaultSystemStatePublicKey;
2090
+ exports.liquidateVault = liquidateVault;
2091
+ exports.liquidateVaultInstruction = liquidateVaultInstruction;
2092
+ exports.loanVault = loanVault;
2093
+ exports.loanVaultInstruction = loanVaultInstruction;
2094
+ exports.redeemVault = redeemVault;
2095
+ exports.redeemVaultInstruction = redeemVaultInstruction;
2096
+ exports.repayVault = repayVault;
2097
+ exports.repayVaultInstruction = repayVaultInstruction;
2098
+ exports.withdrawStakingPool = withdrawStakingPool;
2099
+ exports.withdrawStakingPoolInstruction = withdrawStakingPoolInstruction;
2100
+ exports.withdrawVault = withdrawVault;
2101
+ exports.withdrawVaultInstruction = withdrawVaultInstruction;
2102
+ //# sourceMappingURL=index.js.map