hedge-web3 0.1.3 → 0.1.9

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