hedge-web3 0.1.4 → 0.1.10

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 (57) hide show
  1. package/lib/index.js +394 -242
  2. package/lib/index.js.map +1 -1
  3. package/lib/types/src/Constants.d.ts +2 -1
  4. package/lib/types/src/Constants.d.ts.map +1 -1
  5. package/lib/types/src/index.d.ts +19 -3
  6. package/lib/types/src/index.d.ts.map +1 -1
  7. package/lib/types/src/instructions/createStakingPool.d.ts +4 -3
  8. package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -1
  9. package/lib/types/src/instructions/createVault.d.ts +4 -3
  10. package/lib/types/src/instructions/createVault.d.ts.map +1 -1
  11. package/lib/types/src/instructions/depositStakingPool.d.ts +4 -3
  12. package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -1
  13. package/lib/types/src/instructions/depositVault.d.ts +4 -3
  14. package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
  15. package/lib/types/src/instructions/liquidateVault.d.ts +4 -3
  16. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -1
  17. package/lib/types/src/instructions/loanVault.d.ts +4 -3
  18. package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
  19. package/lib/types/src/instructions/redeemVault.d.ts +4 -3
  20. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
  21. package/lib/types/src/instructions/refreshOraclePrice.d.ts +5 -0
  22. package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +1 -0
  23. package/lib/types/src/instructions/repayVault.d.ts +4 -3
  24. package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
  25. package/lib/types/src/instructions/withdrawStakingPool.d.ts +4 -3
  26. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -1
  27. package/lib/types/src/instructions/withdrawVault.d.ts +4 -3
  28. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
  29. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
  30. package/lib/types/src/state/LiquidationPosition.d.ts +1 -0
  31. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
  32. package/lib/types/src/state/StakingPool.d.ts +1 -2
  33. package/lib/types/src/state/StakingPool.d.ts.map +1 -1
  34. package/lib/types/src/state/StakingPoolPosition.d.ts +1 -0
  35. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -1
  36. package/lib/types/src/state/VaultAccount.d.ts.map +1 -1
  37. package/lib/types/tsconfig.base.tsbuildinfo +1 -1
  38. package/package.json +1 -1
  39. package/src/Constants.ts +6 -7
  40. package/src/index.ts +21 -3
  41. package/src/instructions/createStakingPool.ts +9 -10
  42. package/src/instructions/createVault.ts +10 -11
  43. package/src/instructions/depositStakingPool.ts +9 -10
  44. package/src/instructions/depositVault.ts +10 -11
  45. package/src/instructions/liquidateVault.ts +14 -18
  46. package/src/instructions/loanVault.ts +14 -18
  47. package/src/instructions/redeemVault.ts +12 -16
  48. package/src/instructions/refreshOraclePrice.ts +45 -0
  49. package/src/instructions/repayVault.ts +10 -11
  50. package/src/instructions/withdrawStakingPool.ts +9 -10
  51. package/src/instructions/withdrawVault.ts +17 -21
  52. package/src/state/LiquidationPoolEra.ts +5 -5
  53. package/src/state/LiquidationPosition.ts +26 -18
  54. package/src/state/StakingPool.ts +4 -5
  55. package/src/state/StakingPoolPosition.ts +4 -5
  56. package/src/state/VaultAccount.ts +0 -1
  57. package/src/state/VaultHistoryEvent.ts +1 -1
package/lib/index.js CHANGED
@@ -11,64 +11,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
11
11
 
12
12
  var Decimal__default = /*#__PURE__*/_interopDefaultLegacy(Decimal);
13
13
 
14
- const HEDGE_PROGRAM_ID = 'fff6FuPWrBPzFHeWwtBQaPjKvMvW27w8Jf4P7SSoueX';
15
- const HEDGE_PROGRAM_PUBLICKEY = new web3_js.PublicKey(HEDGE_PROGRAM_ID);
16
- const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf';
17
- const CHAINLINK_SOL_USD_PUBLICKEY = new web3_js.PublicKey(CHAINLINK_SOL_USD_ID);
18
- async function getLiquidationPoolStatePublicKey() {
19
- const enc = new TextEncoder();
20
- const [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolStateV1')], HEDGE_PROGRAM_PUBLICKEY);
21
- return [poolPublicKey, bump];
22
- }
23
- async function getLiquidationPoolUsdhAccountPublicKey() {
24
- const enc = new TextEncoder();
25
- const [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolUSDHAccountV1')], HEDGE_PROGRAM_PUBLICKEY);
26
- return [poolPublicKey, bump];
27
- }
28
- async function getUsdhMintPublicKey() {
29
- const enc = new TextEncoder();
30
- const [findMintPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('UsdhMintV1')], HEDGE_PROGRAM_PUBLICKEY);
31
- return [findMintPublicKey, bump];
32
- }
33
- async function getVaultSystemStatePublicKey() {
34
- const enc = new TextEncoder();
35
- const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('VaultSystemStateV1')], HEDGE_PROGRAM_PUBLICKEY);
36
- return [publicKey, bump];
37
- }
38
- async function getHedgeMintPublicKey() {
39
- const enc = new TextEncoder();
40
- const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('HEDGEMintV1')], HEDGE_PROGRAM_PUBLICKEY);
41
- return [publicKey, bump];
42
- }
43
- async function getPoolPublicKeyForMint(mintPublicKey) {
44
- const enc = new TextEncoder();
45
- const strToEncode = (mintPublicKey.toString().substring(0, 12));
46
- const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode(strToEncode)], HEDGE_PROGRAM_PUBLICKEY);
47
- return [publicKey, bump, strToEncode];
48
- }
49
- async function findAssociatedTokenAddress(walletAddress, tokenMintAddress) {
50
- return (await web3_js.PublicKey.findProgramAddress([
51
- walletAddress.toBuffer(),
52
- splToken.TOKEN_PROGRAM_ID.toBuffer(),
53
- tokenMintAddress.toBuffer()
54
- ], splToken.ASSOCIATED_TOKEN_PROGRAM_ID))[0];
55
- }
56
-
57
- var Constants = /*#__PURE__*/Object.freeze({
58
- __proto__: null,
59
- HEDGE_PROGRAM_ID: HEDGE_PROGRAM_ID,
60
- HEDGE_PROGRAM_PUBLICKEY: HEDGE_PROGRAM_PUBLICKEY,
61
- CHAINLINK_SOL_USD_ID: CHAINLINK_SOL_USD_ID,
62
- CHAINLINK_SOL_USD_PUBLICKEY: CHAINLINK_SOL_USD_PUBLICKEY,
63
- getLiquidationPoolStatePublicKey: getLiquidationPoolStatePublicKey,
64
- getLiquidationPoolUsdhAccountPublicKey: getLiquidationPoolUsdhAccountPublicKey,
65
- getUsdhMintPublicKey: getUsdhMintPublicKey,
66
- getVaultSystemStatePublicKey: getVaultSystemStatePublicKey,
67
- getHedgeMintPublicKey: getHedgeMintPublicKey,
68
- getPoolPublicKeyForMint: getPoolPublicKeyForMint,
69
- findAssociatedTokenAddress: findAssociatedTokenAddress
70
- });
71
-
72
14
  const vaultIdl = {
73
15
  version: '0.1.0',
74
16
  name: 'vault',
@@ -1542,20 +1484,168 @@ const vaultIdl = {
1542
1484
  ]
1543
1485
  };
1544
1486
 
1545
- async function createVault(connection, payer, depositAmount, collateralRatio, confirmOptions) {
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 = 'hhhJmQPMmb2sNoTQK1ip7ZRGpRdGfskEEgjacf9XFWv';
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
+ const enc = new TextEncoder();
1501
+ async function getLiquidationPoolStatePublicKey() {
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 [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolUSDHAccountV1')], HEDGE_PROGRAM_PUBLICKEY);
1507
+ return [poolPublicKey, bump];
1508
+ }
1509
+ async function getUsdhMintPublicKey() {
1510
+ const [findMintPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('UsdhMintV1')], HEDGE_PROGRAM_PUBLICKEY);
1511
+ return [findMintPublicKey, bump];
1512
+ }
1513
+ async function getVaultSystemStatePublicKey() {
1514
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('VaultSystemStateV1')], HEDGE_PROGRAM_PUBLICKEY);
1515
+ return [publicKey, bump];
1516
+ }
1517
+ async function getHedgeMintPublicKey() {
1518
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('HEDGEMintV1')], HEDGE_PROGRAM_PUBLICKEY);
1519
+ return [publicKey, bump];
1520
+ }
1521
+ async function getPoolPublicKeyForMint(mintPublicKey) {
1522
+ const strToEncode = (mintPublicKey.toString().substring(0, 12));
1523
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode(strToEncode)], HEDGE_PROGRAM_PUBLICKEY);
1524
+ return [publicKey, bump, strToEncode];
1525
+ }
1526
+ async function getSolCollateralPriceAccountPublicKey() {
1527
+ const [collateralPriceAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Price')], HEDGE_PROGRAM_PUBLICKEY);
1528
+ return collateralPriceAccount;
1529
+ }
1530
+ async function findAssociatedTokenAddress(walletAddress, tokenMintAddress) {
1531
+ return (await web3_js.PublicKey.findProgramAddress([
1532
+ walletAddress.toBuffer(),
1533
+ splToken.TOKEN_PROGRAM_ID.toBuffer(),
1534
+ tokenMintAddress.toBuffer()
1535
+ ], splToken.ASSOCIATED_TOKEN_PROGRAM_ID))[0];
1536
+ }
1537
+
1538
+ async function createStakingPool(program, provider, payer, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1539
+ const transaction = new web3_js.Transaction().add(await createStakingPoolInstruction(program, payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime));
1540
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
1541
+ const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey);
1542
+ return poolPublickey;
1543
+ }
1544
+ async function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1545
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1546
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1547
+ const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey);
1548
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey);
1549
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1550
+ return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor.BN(hedgeTokensToBeMinted), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), {
1551
+ accounts: {
1552
+ signer: payerPublicKey,
1553
+ vaultSystemState: vaultSystemStatePublicKey,
1554
+ pool: poolPublickey,
1555
+ stakedTokenMintInfo: mintPublicKey,
1556
+ usdhMint: usdhMintPublickey,
1557
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1558
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1559
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1560
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1561
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1562
+ systemProgram: web3_js.SystemProgram.programId
1563
+ },
1564
+ signers: []
1565
+ });
1566
+ }
1567
+
1568
+ async function depositStakingPool(program, provider, payer, mintPublicKey, depositAmount, overrideStartTime) {
1569
+ const poolPosition = web3_js.Keypair.generate();
1570
+ const transaction = new web3_js.Transaction().add(await depositStakingPoolInstruction(program, payer.publicKey, poolPosition.publicKey, mintPublicKey, depositAmount, overrideStartTime));
1571
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors);
1572
+ return poolPosition.publicKey;
1573
+ }
1574
+ async function depositStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, depositAmount, overrideStartTime) {
1575
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1576
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1577
+ const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1578
+ return program.instruction.depositStakingPool(new anchor.BN(depositAmount), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1579
+ {
1580
+ accounts: {
1581
+ payer: payerPublicKey,
1582
+ pool: poolPublickey,
1583
+ poolPosition: poolPositionPublicKey,
1584
+ stakedTokenMintInfo: stakedTokenMintPublicKey,
1585
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1586
+ payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
1587
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1588
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1589
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1590
+ systemProgram: web3_js.SystemProgram.programId
1591
+ },
1592
+ signers: []
1593
+ });
1594
+ }
1595
+
1596
+ async function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1597
+ const poolPosition = web3_js.Keypair.generate();
1598
+ const transaction = new web3_js.Transaction().add(await withdrawStakingPoolInstruction(program, payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime));
1599
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
1600
+ return poolPosition.publicKey;
1601
+ }
1602
+ async function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1603
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1604
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1605
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1606
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1607
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1608
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1609
+ const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1610
+ const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey);
1611
+ const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey);
1612
+ return program.instruction.withdrawStakingPool(new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1613
+ {
1614
+ accounts: {
1615
+ payer: payerPublicKey,
1616
+ vaultSystemState: vaultSystemStatePublicKey,
1617
+ pool: poolPublickey,
1618
+ poolPosition: poolPositionPublicKey,
1619
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1620
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1621
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
1622
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
1623
+ payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
1624
+ hedgeMint: hedgeMintPublickey,
1625
+ stakedTokenMint: stakedTokenMintPublicKey,
1626
+ usdhMint: usdhMintPublickey,
1627
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1628
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1629
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1630
+ systemProgram: web3_js.SystemProgram.programId
1631
+ },
1632
+ signers: []
1633
+ });
1634
+ }
1635
+
1636
+ async function createVault(program, provider, payer, depositAmount, collateralRatio) {
1546
1637
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1547
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1638
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1548
1639
  // Prep the user to get USDH back out at some point
1549
1640
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1550
1641
  const newVault = web3_js.Keypair.generate();
1551
1642
  const history = web3_js.Keypair.generate();
1552
1643
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1553
- const transaction = new web3_js.Transaction().add(createVaultInstruction(vaultSystemStatePublicKey, payer.publicKey, newVault.publicKey, history.publicKey, depositAmount, collateralRatio));
1554
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, newVault, history], confirmOptions);
1644
+ const transaction = new web3_js.Transaction().add(createVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, newVault.publicKey, history.publicKey, depositAmount, collateralRatio));
1645
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, newVault, history], provider === null || provider === void 0 ? void 0 : provider.opts);
1555
1646
  return newVault.publicKey;
1556
1647
  }
1557
- function createVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount, collateralRatio) {
1558
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1648
+ function createVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount, collateralRatio) {
1559
1649
  const ix = program.instruction.createVault(new anchor.BN(depositAmount), new anchor.BN(collateralRatio), {
1560
1650
  accounts: {
1561
1651
  vaultSystemState: vaultSystemStatePublicKey,
@@ -1569,19 +1659,18 @@ function createVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vault
1569
1659
  return ix;
1570
1660
  }
1571
1661
 
1572
- async function depositVault(connection, payer, vaultPublicKey, depositAmount, confirmOptions) {
1662
+ async function depositVault(program, provider, payer, vaultPublicKey, depositAmount) {
1573
1663
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1574
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1664
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1575
1665
  // Prep the user to get USDH back out at some point
1576
1666
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1577
1667
  const history = web3_js.Keypair.generate();
1578
1668
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1579
- const transaction = new web3_js.Transaction().add(depositVaultInstruction(vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, depositAmount));
1580
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions);
1669
+ const transaction = new web3_js.Transaction().add(depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, depositAmount));
1670
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1581
1671
  return vaultPublicKey;
1582
1672
  }
1583
- function depositVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount) {
1584
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1673
+ function depositVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount) {
1585
1674
  return program.instruction.depositVault(new anchor.BN(depositAmount), {
1586
1675
  accounts: {
1587
1676
  vaultSystemState: vaultSystemStatePublicKey,
@@ -1594,51 +1683,50 @@ function depositVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaul
1594
1683
  });
1595
1684
  }
1596
1685
 
1597
- async function withdrawVault(connection, payer, vaultPublicKey, withdrawAmount, chainlinkOverridePrice, confirmOptions) {
1686
+ async function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount) {
1598
1687
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1599
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1688
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1600
1689
  // Prep the user to get USDH back out at some point
1601
1690
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1602
1691
  const history = web3_js.Keypair.generate();
1603
1692
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1604
- const transaction = new web3_js.Transaction().add(withdrawVaultInstruction(vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, withdrawAmount, chainlinkOverridePrice));
1605
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions);
1693
+ const transaction = new web3_js.Transaction().add(await withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, withdrawAmount));
1694
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1606
1695
  return vaultPublicKey;
1607
1696
  }
1608
- function withdrawVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaultPublickey, historyPublicKey, withdrawAmount, chainlinkOverridePrice) {
1609
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1610
- return program.instruction.withdrawVault(new anchor.BN(withdrawAmount), new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : 200 * web3_js.LAMPORTS_PER_SOL), {
1697
+ async function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublickey, historyPublicKey, withdrawAmount) {
1698
+ const solPriceAccount = await getSolCollateralPriceAccountPublicKey();
1699
+ return program.instruction.withdrawVault(new anchor.BN(withdrawAmount), {
1611
1700
  accounts: {
1612
1701
  vaultSystemState: vaultSystemStatePublicKey,
1613
1702
  vaultAccount: vaultPublickey,
1614
1703
  history: historyPublicKey,
1615
1704
  vaultOwner: payerPublicKey,
1616
- chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1705
+ priceForCollateral: solPriceAccount,
1617
1706
  systemProgram: web3_js.SystemProgram.programId
1618
1707
  },
1619
1708
  signers: []
1620
1709
  });
1621
1710
  }
1622
1711
 
1623
- async function loanVault(connection, payer, vaultPublicKey, loanAmount, chainlinkOverridePrice, confirmOptions) {
1712
+ async function loanVault(program, provider, payer, vaultPublicKey, loanAmount) {
1624
1713
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1625
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1714
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1626
1715
  // Prep the user to get USDH back out at some point
1627
1716
  const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1628
1717
  const history = web3_js.Keypair.generate();
1629
- const transaction = new web3_js.Transaction().add(await loanVaultInstruction(payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, loanAmount, chainlinkOverridePrice));
1630
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions);
1718
+ const transaction = new web3_js.Transaction().add(await loanVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, loanAmount));
1719
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1631
1720
  return vaultPublicKey;
1632
1721
  }
1633
- async function loanVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, loanAmount, chainlinkOverridePrice) {
1634
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1722
+ async function loanVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, loanAmount) {
1635
1723
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1636
1724
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1637
1725
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1638
1726
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1639
1727
  const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
1640
- 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
1641
- {
1728
+ const solPriceAccount = await getSolCollateralPriceAccountPublicKey();
1729
+ return program.instruction.loanVault(new anchor.BN(loanAmount), {
1642
1730
  accounts: {
1643
1731
  vaultSystemState: vaultSystemStatePublicKey,
1644
1732
  vaultAccount: vaultPublickey,
@@ -1648,7 +1736,7 @@ async function loanVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPubli
1648
1736
  usdhMint: usdhMintPublickey,
1649
1737
  vaultOwner: payerPublicKey,
1650
1738
  ownerUsdhAccount: ownerUsdhAccount,
1651
- chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1739
+ priceForCollateral: solPriceAccount,
1652
1740
  splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1653
1741
  systemProgram: web3_js.SystemProgram.programId
1654
1742
  },
@@ -1656,18 +1744,17 @@ async function loanVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPubli
1656
1744
  });
1657
1745
  }
1658
1746
 
1659
- async function repayVault(connection, payer, vaultPublicKey, repayAmount, confirmOptions) {
1747
+ async function repayVault(program, provider, payer, vaultPublicKey, repayAmount) {
1660
1748
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1661
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1749
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1662
1750
  // Prep the user to get USDH back out at some point
1663
1751
  const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1664
1752
  const history = web3_js.Keypair.generate();
1665
- const transaction = new web3_js.Transaction().add(await repayVaultInstruction(payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, repayAmount));
1666
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions);
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);
1667
1755
  return vaultPublicKey;
1668
1756
  }
1669
- async function repayVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount) {
1670
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1757
+ async function repayVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount) {
1671
1758
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1672
1759
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1673
1760
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
@@ -1690,25 +1777,24 @@ async function repayVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPubl
1690
1777
  });
1691
1778
  }
1692
1779
 
1693
- async function redeemVault(connection, payer, vaultPublicKey, repayAmount, chainlinkOverridePrice, transactionOverrideTime, confirmOptions) {
1780
+ async function redeemVault(program, provider, payer, vaultPublicKey, repayAmount, transactionOverrideTime) {
1694
1781
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1695
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1782
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1696
1783
  // Prep the user to get USDH back out at some point
1697
1784
  const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1698
1785
  const history = web3_js.Keypair.generate();
1699
- const transaction = new web3_js.Transaction().add(await redeemVaultInstruction(payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, repayAmount, chainlinkOverridePrice, transactionOverrideTime));
1700
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions);
1786
+ const transaction = new web3_js.Transaction().add(await redeemVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, repayAmount, transactionOverrideTime));
1787
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1701
1788
  return vaultPublicKey;
1702
1789
  }
1703
- async function redeemVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount, chainlinkOverridePrice, transactionOverrideTime) {
1704
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1790
+ async function redeemVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount, transactionOverrideTime) {
1705
1791
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1706
1792
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1707
1793
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1708
1794
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1709
1795
  const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
1710
- 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
1711
- new anchor.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : (Date.now() / 1000)), // override start time
1796
+ const solPriceAccount = await getSolCollateralPriceAccountPublicKey();
1797
+ return program.instruction.redeemVault(new anchor.BN(repayAmount), new anchor.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : (Date.now() / 1000)), // override start time
1712
1798
  {
1713
1799
  accounts: {
1714
1800
  vaultSystemState: vaultSystemStatePublicKey,
@@ -1719,7 +1805,7 @@ async function redeemVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPub
1719
1805
  usdhMint: usdhMintPublickey,
1720
1806
  payer: payerPublicKey,
1721
1807
  payerUsdhAccount: ownerUsdhAccount,
1722
- chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1808
+ priceForCollateral: solPriceAccount,
1723
1809
  splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1724
1810
  systemProgram: web3_js.SystemProgram.programId
1725
1811
  },
@@ -1727,43 +1813,65 @@ async function redeemVaultInstruction(payerPublicKey, ownerUsdhAccount, vaultPub
1727
1813
  });
1728
1814
  }
1729
1815
 
1730
- async function liquidateVault(connection, payer, vaultPublicKey, chainlinkOverridePrice, confirmOptions) {
1816
+ async function liquidateVault(program, provider, payer, vaultPublicKey) {
1731
1817
  const history = web3_js.Keypair.generate();
1732
1818
  const newEra = web3_js.Keypair.generate();
1733
- const transaction = new web3_js.Transaction().add(await liquidateVaultInstruction(payer.publicKey, vaultPublicKey, history.publicKey, newEra.publicKey, chainlinkOverridePrice));
1734
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, history, newEra], confirmOptions);
1819
+ const transaction = new web3_js.Transaction().add(await liquidateVaultInstruction(program, payer.publicKey, vaultPublicKey, history.publicKey, newEra.publicKey));
1820
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts);
1735
1821
  return vaultPublicKey;
1736
1822
  }
1737
- async function liquidateVaultInstruction(payerPublicKey, vaultPublickey, historyPublicKey, newEraPublicKey, chainlinkOverridePrice) {
1738
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1823
+ async function liquidateVaultInstruction(program, payerPublicKey, vaultPublickey, historyPublicKey, newEraPublicKey) {
1739
1824
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1740
1825
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1741
1826
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1742
1827
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
1743
1828
  const [liquidationPoolStatePublicKey] = await getLiquidationPoolStatePublicKey();
1744
1829
  const [liquidationPoolUsdhAccountPublickey] = await getLiquidationPoolUsdhAccountPublicKey();
1830
+ const solPriceAccount = await getSolCollateralPriceAccountPublicKey();
1745
1831
  const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey);
1746
- return program.instruction.liquidateVault(new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : web3_js.LAMPORTS_PER_SOL * 150), // override usd/sol price
1747
- {
1832
+ return program.instruction.liquidateVault({
1748
1833
  accounts: {
1749
1834
  vaultSystemState: vaultSystemStatePublicKey,
1750
1835
  poolEra: poolStateInfo.currentEra,
1751
1836
  vaultAccount: vaultPublickey,
1752
1837
  poolState: liquidationPoolStatePublicKey,
1753
1838
  usdhMint: usdhMintPublickey,
1754
- chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
1839
+ priceForCollateral: solPriceAccount,
1755
1840
  history: historyPublicKey,
1756
1841
  payer: payerPublicKey,
1757
1842
  splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1758
1843
  systemProgram: web3_js.SystemProgram.programId,
1759
1844
  feePool: hedgeStakingPoolPublicKey,
1760
- poolUsdhAccount: liquidationPoolUsdhAccountPublickey,
1845
+ liquidationPoolUsdhAccount: liquidationPoolUsdhAccountPublickey,
1761
1846
  newEra: newEraPublicKey
1762
1847
  },
1763
1848
  signers: []
1764
1849
  });
1765
1850
  }
1766
1851
 
1852
+ async function refreshOraclePrice(program, provider, payer, overridePrice, overrideTime) {
1853
+ const transaction = new web3_js.Transaction().add(await refreshOraclePriceInstruction(program, overridePrice, overrideTime));
1854
+ return await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts);
1855
+ }
1856
+ async function refreshOraclePriceInstruction(program, overridePrice, overrideTime) {
1857
+ const enc = new TextEncoder();
1858
+ const [oracleInfoAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY);
1859
+ const [collateralPriceAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Price')], HEDGE_PROGRAM_PUBLICKEY);
1860
+ return program.instruction.refreshOraclePrice(new anchor.BN(overridePrice !== null && overridePrice !== void 0 ? overridePrice : web3_js.LAMPORTS_PER_SOL * 150), // override usd/sol price
1861
+ new anchor.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
1862
+ {
1863
+ accounts: {
1864
+ oracleInfoAccount: oracleInfoAccount,
1865
+ collateralPriceAccount: collateralPriceAccount,
1866
+ oracleChainlink: web3_js.SystemProgram.programId,
1867
+ oraclePyth: web3_js.SystemProgram.programId,
1868
+ oracleSwitchboard: web3_js.SystemProgram.programId,
1869
+ systemProgram: web3_js.SystemProgram.programId
1870
+ },
1871
+ signers: []
1872
+ });
1873
+ }
1874
+
1767
1875
  /**
1768
1876
  * Convert a U128 to a Decimal
1769
1877
  *
@@ -1792,7 +1900,6 @@ class VaultAccount {
1792
1900
  this.deposited = vault.deposited.toNumber();
1793
1901
  this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio);
1794
1902
  this.liquidationPrice = vault.liquidationPrice.toNumber();
1795
- this.minCollateralRatio = vault.minCollateralRatio.toNumber();
1796
1903
  this.vaultStatus = Object.keys(vault.vaultStatus)[0];
1797
1904
  }
1798
1905
  /**
@@ -1830,147 +1937,192 @@ class VaultAccount {
1830
1937
  }
1831
1938
  }
1832
1939
 
1833
- var Vaults = /*#__PURE__*/Object.freeze({
1834
- __proto__: null,
1835
- createVault: createVault,
1836
- createVaultInstruction: createVaultInstruction,
1837
- depositVault: depositVault,
1838
- depositVaultInstruction: depositVaultInstruction,
1839
- withdrawVault: withdrawVault,
1840
- withdrawVaultInstruction: withdrawVaultInstruction,
1841
- loanVault: loanVault,
1842
- loanVaultInstruction: loanVaultInstruction,
1843
- repayVault: repayVault,
1844
- repayVaultInstruction: repayVaultInstruction,
1845
- redeemVault: redeemVault,
1846
- redeemVaultInstruction: redeemVaultInstruction,
1847
- liquidateVault: liquidateVault,
1848
- liquidateVaultInstruction: liquidateVaultInstruction,
1849
- VaultAccount: VaultAccount
1850
- });
1851
-
1852
- function parseAnchorErrors(error) {
1853
- const idlErrors = anchor.parseIdlErrors(vaultIdl);
1854
- const parsedError = anchor.ProgramError.parse(error, idlErrors);
1855
- if (parsedError !== null) {
1856
- throw parsedError;
1940
+ class VaultHistoryEvent {
1941
+ constructor(account, publicKey) {
1942
+ this.account = account;
1943
+ this.publicKey = publicKey;
1944
+ this.actorAccount = account.actorAccount;
1945
+ this.usdSolPrice = DecimalFromU128(account.usdSolPrice.toString());
1946
+ this.usdhDebtBefore = account.usdhDebtBefore.toNumber();
1947
+ this.usdhDebtAfter = account.usdhDebtAfter.toNumber();
1948
+ this.solBalanceBefore = account.solBalanceBefore.toNumber();
1949
+ this.solBalanceAfter = account.solBalanceAfter.toNumber();
1950
+ this.minCollateralRatioBefore = DecimalFromU128(account.minCollateralRatioBefore);
1951
+ this.minCollateralRatioAfter = DecimalFromU128(account.minCollateralRatioAfter);
1952
+ this.vaultStateBefore = account.vaultStateBefore;
1953
+ this.vaultStateAfter = account.vaultStateAfter;
1954
+ this.timestamp = account.timestamp.toNumber();
1955
+ this.action = account.action;
1857
1956
  }
1858
- throw error;
1859
1957
  }
1958
+ exports.VaultStatus = void 0;
1959
+ (function (VaultStatus) {
1960
+ VaultStatus[VaultStatus["Open"] = 0] = "Open";
1961
+ VaultStatus[VaultStatus["Closed"] = 1] = "Closed";
1962
+ VaultStatus[VaultStatus["Liquidated"] = 2] = "Liquidated";
1963
+ VaultStatus[VaultStatus["Distributed"] = 3] = "Distributed";
1964
+ VaultStatus[VaultStatus["Redeemed"] = 4] = "Redeemed";
1965
+ })(exports.VaultStatus || (exports.VaultStatus = {}));
1966
+ exports.VaultHistoryAction = void 0;
1967
+ (function (VaultHistoryAction) {
1968
+ VaultHistoryAction[VaultHistoryAction["Create"] = 0] = "Create";
1969
+ VaultHistoryAction[VaultHistoryAction["Close"] = 1] = "Close";
1970
+ VaultHistoryAction[VaultHistoryAction["Liquidate"] = 2] = "Liquidate";
1971
+ VaultHistoryAction[VaultHistoryAction["PartialLiquidate"] = 3] = "PartialLiquidate";
1972
+ VaultHistoryAction[VaultHistoryAction["Distributed"] = 4] = "Distributed";
1973
+ VaultHistoryAction[VaultHistoryAction["Redeem"] = 5] = "Redeem";
1974
+ VaultHistoryAction[VaultHistoryAction["TransferOwnership"] = 6] = "TransferOwnership";
1975
+ VaultHistoryAction[VaultHistoryAction["Deposit"] = 7] = "Deposit";
1976
+ VaultHistoryAction[VaultHistoryAction["Withdraw"] = 8] = "Withdraw";
1977
+ VaultHistoryAction[VaultHistoryAction["Loan"] = 9] = "Loan";
1978
+ VaultHistoryAction[VaultHistoryAction["RepayCredit"] = 10] = "RepayCredit";
1979
+ })(exports.VaultHistoryAction || (exports.VaultHistoryAction = {}));
1860
1980
 
1861
- async function createStakingPool(connection, payer, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime, confirmOptions) {
1862
- const transaction = new web3_js.Transaction().add(await createStakingPoolInstruction(payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime));
1863
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions).catch(parseAnchorErrors);
1864
- const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey);
1865
- return poolPublickey;
1981
+ class StakingPool {
1982
+ constructor(poolInfo, publicKey) {
1983
+ this.poolInfo = poolInfo;
1984
+ this.publicKey = publicKey;
1985
+ this.deposits = poolInfo.deposits.toNumber();
1986
+ // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
1987
+ // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
1988
+ this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber();
1989
+ this.startTime = poolInfo.startTime.toNumber();
1990
+ this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber();
1991
+ this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber();
1992
+ this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator);
1993
+ this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator);
1994
+ this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator);
1995
+ // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
1996
+ }
1866
1997
  }
1867
- async function createStakingPoolInstruction(payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1868
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1869
- const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1870
- const [usdhMintPublickey] = await getUsdhMintPublicKey();
1871
- const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey);
1872
- const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey);
1873
- const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1874
- return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor.BN(hedgeTokensToBeMinted), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), {
1875
- accounts: {
1876
- signer: payerPublicKey,
1877
- vaultSystemState: vaultSystemStatePublicKey,
1878
- pool: poolPublickey,
1879
- stakedTokenMintInfo: mintPublicKey,
1880
- usdhMint: usdhMintPublickey,
1881
- poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1882
- poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1883
- rent: web3_js.SYSVAR_RENT_PUBKEY,
1884
- splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1885
- splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1886
- systemProgram: web3_js.SystemProgram.programId
1887
- },
1888
- signers: []
1889
- });
1998
+
1999
+ class StakingPoolPosition {
2000
+ constructor(poolPositionInfo, key, stakingPool) {
2001
+ this.poolPositionInfo = poolPositionInfo;
2002
+ this.publicKey = key;
2003
+ this.pool = stakingPool;
2004
+ this.owner = poolPositionInfo.owner;
2005
+ this.deposited = poolPositionInfo.deposited.toNumber();
2006
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
2007
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
2008
+ this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber();
2009
+ this.startHedgeRewardAccumulator = DecimalFromU128(poolPositionInfo.startHedgeRewardAccumulator);
2010
+ this.startUsdhFeeAccumulator = DecimalFromU128(poolPositionInfo.startUsdhFeeAccumulator);
2011
+ this.startSolFeeAccumulator = DecimalFromU128(poolPositionInfo.startSolFeeAccumulator);
2012
+ this.open = poolPositionInfo.state.open !== undefined;
2013
+ }
2014
+ getCurrentUsdhFeeReward() {
2015
+ return this.pool.usdhFeeAccumulator.minus(this.startUsdhFeeAccumulator).times(new Decimal__default["default"](this.deposited));
2016
+ }
1890
2017
  }
1891
2018
 
1892
- async function depositStakingPool(connection, payer, mintPublicKey, depositAmount, overrideStartTime, confirmOptions) {
1893
- const poolPosition = web3_js.Keypair.generate();
1894
- const transaction = new web3_js.Transaction().add(await depositStakingPoolInstruction(payer.publicKey, poolPosition.publicKey, mintPublicKey, depositAmount, overrideStartTime));
1895
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer, poolPosition], confirmOptions).catch(parseAnchorErrors);
1896
- return poolPosition.publicKey;
2019
+ /**
2020
+ * Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
2021
+ * created to maintain each users contribution to the pool.
2022
+ */
2023
+ class LiquidationPoolEra {
2024
+ constructor(liquidyPoolEra) {
2025
+ this.liquidyPoolEra = liquidyPoolEra;
2026
+ this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber();
2027
+ this.product = DecimalFromU128(liquidyPoolEra.productBytes);
2028
+ this.sum = DecimalFromU128(liquidyPoolEra.sumBytes);
2029
+ this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes);
2030
+ this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber();
2031
+ }
1897
2032
  }
1898
- async function depositStakingPoolInstruction(payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, depositAmount, overrideStartTime) {
1899
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1900
- const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1901
- const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1902
- const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1903
- return program.instruction.depositStakingPool(new anchor.BN(depositAmount), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1904
- {
1905
- accounts: {
1906
- payer: payerPublicKey,
1907
- pool: poolPublickey,
1908
- poolPosition: poolPositionPublicKey,
1909
- stakedTokenMintInfo: stakedTokenMintPublicKey,
1910
- poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1911
- payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
1912
- rent: web3_js.SYSVAR_RENT_PUBKEY,
1913
- splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1914
- splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1915
- systemProgram: web3_js.SystemProgram.programId
1916
- },
1917
- signers: []
1918
- });
2033
+
2034
+ class LiquidationPoolState {
2035
+ constructor(liquidationPoolState) {
2036
+ this.liquidationPoolState = liquidationPoolState;
2037
+ this.lifetimeDeposits = liquidationPoolState.lifetimeDeposits.toNumber();
2038
+ this.hedgeInitRewardsTimestamp = liquidationPoolState.hedgeInitRewardsTimestamp.toNumber();
2039
+ // TODO Add the rest that are missing. Do we need them?
2040
+ }
1919
2041
  }
1920
2042
 
1921
- async function withdrawStakingPool(connection, payer, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime, confirmOptions) {
1922
- const poolPosition = web3_js.Keypair.generate();
1923
- const transaction = new web3_js.Transaction().add(await withdrawStakingPoolInstruction(payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime));
1924
- await web3_js.sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions).catch(parseAnchorErrors);
1925
- return poolPosition.publicKey;
2043
+ class LiquidationPosition {
2044
+ constructor(poolPositionInfo, key, era, liquidationPoolState) {
2045
+ this.publicKey = key;
2046
+ this.eraPublicKey = poolPositionInfo.era;
2047
+ this.era = era;
2048
+ this.liquidationPoolState = liquidationPoolState;
2049
+ this.ownerAccount = poolPositionInfo.ownerAccount;
2050
+ this.deposit = poolPositionInfo.deposit.toNumber();
2051
+ this.closedUsdh = poolPositionInfo.closedUsdh.toNumber();
2052
+ this.closedSol = poolPositionInfo.closedSol.toNumber();
2053
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
2054
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
2055
+ this.productSnapshot = DecimalFromU128(poolPositionInfo.productSnapshotBytes);
2056
+ this.sumSnapshot = DecimalFromU128(poolPositionInfo.sumSnapshotBytes);
2057
+ this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum);
2058
+ this.open = poolPositionInfo.state.open !== undefined;
2059
+ }
2060
+ getUsdhAvailable() {
2061
+ return (this.era.product.div(this.productSnapshot)).mul(new Decimal__default["default"](this.deposit));
2062
+ }
2063
+ getSolAvailable() {
2064
+ return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal__default["default"](this.deposit)).floor();
2065
+ }
2066
+ getHedgeAvailable() {
2067
+ const LiquidationPoolTotalSupply = 2000000.0 * web3_js.LAMPORTS_PER_SOL;
2068
+ const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(LiquidationPoolTotalSupply, this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000, this.era.hedgeRewardsTimestamp * 1000, Date.now(), 365 * 1000);
2069
+ if (this.era.totalDeposits === 0) {
2070
+ return new Decimal__default["default"](0);
2071
+ }
2072
+ const rewardsPerToken = this.era.product.mul(new Decimal__default["default"](hedgeRewardsSinceLastUpdate / this.era.totalDeposits));
2073
+ const newAccumulator = rewardsPerToken.add(new Decimal__default["default"](this.era.hedgeRewardsAccumulator));
2074
+ const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal__default["default"](this.deposit)).div(new Decimal__default["default"](this.productSnapshot));
2075
+ return hedgeAvailable;
2076
+ }
1926
2077
  }
1927
- async function withdrawStakingPoolInstruction(payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1928
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1929
- const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1930
- const [usdhMintPublickey] = await getUsdhMintPublicKey();
1931
- const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1932
- const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1933
- const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1934
- const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1935
- const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1936
- const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey);
1937
- const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey);
1938
- return program.instruction.withdrawStakingPool(new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1939
- {
1940
- accounts: {
1941
- payer: payerPublicKey,
1942
- vaultSystemState: vaultSystemStatePublicKey,
1943
- pool: poolPublickey,
1944
- poolPosition: poolPositionPublicKey,
1945
- poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1946
- poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1947
- payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
1948
- payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
1949
- payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
1950
- hedgeMint: hedgeMintPublickey,
1951
- stakedTokenMint: stakedTokenMintPublicKey,
1952
- usdhMint: usdhMintPublickey,
1953
- rent: web3_js.SYSVAR_RENT_PUBKEY,
1954
- splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1955
- splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1956
- systemProgram: web3_js.SystemProgram.programId
1957
- },
1958
- signers: []
1959
- });
2078
+ function hedgeRewardsDecay(supply, birthTime, timeIn, timeOut, halfLifeInDays) {
2079
+ const timeInOffsetStart = timeIn - birthTime;
2080
+ const timeOutOffsetStart = timeOut - birthTime;
2081
+ const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24);
2082
+ const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart));
2083
+ return awardedTokens;
1960
2084
  }
1961
2085
 
1962
- var StakingPools = /*#__PURE__*/Object.freeze({
1963
- __proto__: null,
1964
- createStakingPool: createStakingPool,
1965
- createStakingPoolInstruction: createStakingPoolInstruction,
1966
- depositStakingPool: depositStakingPool,
1967
- depositStakingPoolInstruction: depositStakingPoolInstruction,
1968
- withdrawStakingPool: withdrawStakingPool,
1969
- withdrawStakingPoolInstruction: withdrawStakingPoolInstruction
1970
- });
1971
-
1972
- exports.Constants = Constants;
2086
+ exports.CHAINLINK_SOL_USD_ID = CHAINLINK_SOL_USD_ID;
2087
+ exports.CHAINLINK_SOL_USD_PUBLICKEY = CHAINLINK_SOL_USD_PUBLICKEY;
1973
2088
  exports.DecimalFromU128 = DecimalFromU128;
1974
- exports.StakingPool = StakingPools;
1975
- exports.Vaults = Vaults;
2089
+ exports.HEDGE_PROGRAM_ID = HEDGE_PROGRAM_ID;
2090
+ exports.HEDGE_PROGRAM_PUBLICKEY = HEDGE_PROGRAM_PUBLICKEY;
2091
+ exports.LiquidationPoolEra = LiquidationPoolEra;
2092
+ exports.LiquidationPoolState = LiquidationPoolState;
2093
+ exports.LiquidationPosition = LiquidationPosition;
2094
+ exports.StakingPool = StakingPool;
2095
+ exports.StakingPoolPosition = StakingPoolPosition;
2096
+ exports.VaultAccount = VaultAccount;
2097
+ exports.VaultHistoryEvent = VaultHistoryEvent;
2098
+ exports.createStakingPool = createStakingPool;
2099
+ exports.createStakingPoolInstruction = createStakingPoolInstruction;
2100
+ exports.createVault = createVault;
2101
+ exports.createVaultInstruction = createVaultInstruction;
2102
+ exports.depositStakingPool = depositStakingPool;
2103
+ exports.depositStakingPoolInstruction = depositStakingPoolInstruction;
2104
+ exports.depositVault = depositVault;
2105
+ exports.depositVaultInstruction = depositVaultInstruction;
2106
+ exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
2107
+ exports.getHedgeMintPublicKey = getHedgeMintPublicKey;
2108
+ exports.getLiquidationPoolStatePublicKey = getLiquidationPoolStatePublicKey;
2109
+ exports.getLiquidationPoolUsdhAccountPublicKey = getLiquidationPoolUsdhAccountPublicKey;
2110
+ exports.getPoolPublicKeyForMint = getPoolPublicKeyForMint;
2111
+ exports.getSolCollateralPriceAccountPublicKey = getSolCollateralPriceAccountPublicKey;
2112
+ exports.getUsdhMintPublicKey = getUsdhMintPublicKey;
2113
+ exports.getVaultSystemStatePublicKey = getVaultSystemStatePublicKey;
2114
+ exports.liquidateVault = liquidateVault;
2115
+ exports.liquidateVaultInstruction = liquidateVaultInstruction;
2116
+ exports.loanVault = loanVault;
2117
+ exports.loanVaultInstruction = loanVaultInstruction;
2118
+ exports.redeemVault = redeemVault;
2119
+ exports.redeemVaultInstruction = redeemVaultInstruction;
2120
+ exports.refreshOraclePrice = refreshOraclePrice;
2121
+ exports.refreshOraclePriceInstruction = refreshOraclePriceInstruction;
2122
+ exports.repayVault = repayVault;
2123
+ exports.repayVaultInstruction = repayVaultInstruction;
2124
+ exports.withdrawStakingPool = withdrawStakingPool;
2125
+ exports.withdrawStakingPoolInstruction = withdrawStakingPoolInstruction;
2126
+ exports.withdrawVault = withdrawVault;
2127
+ exports.withdrawVaultInstruction = withdrawVaultInstruction;
1976
2128
  //# sourceMappingURL=index.js.map