hedge-web3 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/lib/index.js +356 -230
  2. package/lib/index.js.map +1 -1
  3. package/lib/types/src/index.d.ts +18 -3
  4. package/lib/types/src/index.d.ts.map +1 -1
  5. package/lib/types/src/instructions/createStakingPool.d.ts +4 -3
  6. package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -1
  7. package/lib/types/src/instructions/createVault.d.ts +4 -3
  8. package/lib/types/src/instructions/createVault.d.ts.map +1 -1
  9. package/lib/types/src/instructions/depositStakingPool.d.ts +4 -3
  10. package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -1
  11. package/lib/types/src/instructions/depositVault.d.ts +4 -3
  12. package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
  13. package/lib/types/src/instructions/liquidateVault.d.ts +4 -3
  14. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -1
  15. package/lib/types/src/instructions/loanVault.d.ts +4 -3
  16. package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
  17. package/lib/types/src/instructions/redeemVault.d.ts +4 -3
  18. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
  19. package/lib/types/src/instructions/repayVault.d.ts +4 -3
  20. package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
  21. package/lib/types/src/instructions/withdrawStakingPool.d.ts +4 -3
  22. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -1
  23. package/lib/types/src/instructions/withdrawVault.d.ts +4 -3
  24. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
  25. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
  26. package/lib/types/src/state/LiquidationPosition.d.ts +1 -0
  27. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
  28. package/lib/types/src/state/StakingPool.d.ts +1 -2
  29. package/lib/types/src/state/StakingPool.d.ts.map +1 -1
  30. package/lib/types/src/state/StakingPoolPosition.d.ts +1 -0
  31. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -1
  32. package/lib/types/src/state/VaultAccount.d.ts.map +1 -1
  33. package/lib/types/tsconfig.base.tsbuildinfo +1 -1
  34. package/package.json +1 -1
  35. package/src/index.ts +20 -3
  36. package/src/instructions/createStakingPool.ts +9 -10
  37. package/src/instructions/createVault.ts +10 -11
  38. package/src/instructions/depositStakingPool.ts +9 -10
  39. package/src/instructions/depositVault.ts +10 -11
  40. package/src/instructions/liquidateVault.ts +9 -10
  41. package/src/instructions/loanVault.ts +10 -11
  42. package/src/instructions/redeemVault.ts +10 -11
  43. package/src/instructions/repayVault.ts +10 -11
  44. package/src/instructions/withdrawStakingPool.ts +9 -10
  45. package/src/instructions/withdrawVault.ts +10 -11
  46. package/src/state/LiquidationPoolEra.ts +5 -5
  47. package/src/state/LiquidationPosition.ts +26 -18
  48. package/src/state/StakingPool.ts +4 -5
  49. package/src/state/StakingPoolPosition.ts +4 -5
  50. package/src/state/VaultAccount.ts +0 -1
  51. 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,169 @@ 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 = 'fff6FuPWrBPzFHeWwtBQaPjKvMvW27w8Jf4P7SSoueX';
1497
+ const HEDGE_PROGRAM_PUBLICKEY = new web3_js.PublicKey(HEDGE_PROGRAM_ID);
1498
+ const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf';
1499
+ const CHAINLINK_SOL_USD_PUBLICKEY = new web3_js.PublicKey(CHAINLINK_SOL_USD_ID);
1500
+ async function getLiquidationPoolStatePublicKey() {
1501
+ const enc = new TextEncoder();
1502
+ const [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolStateV1')], HEDGE_PROGRAM_PUBLICKEY);
1503
+ return [poolPublicKey, bump];
1504
+ }
1505
+ async function getLiquidationPoolUsdhAccountPublicKey() {
1506
+ const enc = new TextEncoder();
1507
+ const [poolPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolUSDHAccountV1')], HEDGE_PROGRAM_PUBLICKEY);
1508
+ return [poolPublicKey, bump];
1509
+ }
1510
+ async function getUsdhMintPublicKey() {
1511
+ const enc = new TextEncoder();
1512
+ const [findMintPublicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('UsdhMintV1')], HEDGE_PROGRAM_PUBLICKEY);
1513
+ return [findMintPublicKey, bump];
1514
+ }
1515
+ async function getVaultSystemStatePublicKey() {
1516
+ const enc = new TextEncoder();
1517
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('VaultSystemStateV1')], HEDGE_PROGRAM_PUBLICKEY);
1518
+ return [publicKey, bump];
1519
+ }
1520
+ async function getHedgeMintPublicKey() {
1521
+ const enc = new TextEncoder();
1522
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode('HEDGEMintV1')], HEDGE_PROGRAM_PUBLICKEY);
1523
+ return [publicKey, bump];
1524
+ }
1525
+ async function getPoolPublicKeyForMint(mintPublicKey) {
1526
+ const enc = new TextEncoder();
1527
+ const strToEncode = (mintPublicKey.toString().substring(0, 12));
1528
+ const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode(strToEncode)], HEDGE_PROGRAM_PUBLICKEY);
1529
+ return [publicKey, bump, strToEncode];
1530
+ }
1531
+ async function findAssociatedTokenAddress(walletAddress, tokenMintAddress) {
1532
+ return (await web3_js.PublicKey.findProgramAddress([
1533
+ walletAddress.toBuffer(),
1534
+ splToken.TOKEN_PROGRAM_ID.toBuffer(),
1535
+ tokenMintAddress.toBuffer()
1536
+ ], splToken.ASSOCIATED_TOKEN_PROGRAM_ID))[0];
1537
+ }
1538
+
1539
+ async function createStakingPool(program, provider, payer, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1540
+ const transaction = new web3_js.Transaction().add(await createStakingPoolInstruction(program, payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime));
1541
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
1542
+ const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey);
1543
+ return poolPublickey;
1544
+ }
1545
+ async function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
1546
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1546
1547
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1547
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1548
+ const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey);
1549
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey);
1550
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1551
+ return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor.BN(hedgeTokensToBeMinted), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), {
1552
+ accounts: {
1553
+ signer: payerPublicKey,
1554
+ vaultSystemState: vaultSystemStatePublicKey,
1555
+ pool: poolPublickey,
1556
+ stakedTokenMintInfo: mintPublicKey,
1557
+ usdhMint: usdhMintPublickey,
1558
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1559
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1560
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1561
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1562
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1563
+ systemProgram: web3_js.SystemProgram.programId
1564
+ },
1565
+ signers: []
1566
+ });
1567
+ }
1568
+
1569
+ async function depositStakingPool(program, provider, payer, mintPublicKey, depositAmount, overrideStartTime) {
1570
+ const poolPosition = web3_js.Keypair.generate();
1571
+ const transaction = new web3_js.Transaction().add(await depositStakingPoolInstruction(program, payer.publicKey, poolPosition.publicKey, mintPublicKey, depositAmount, overrideStartTime));
1572
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors);
1573
+ return poolPosition.publicKey;
1574
+ }
1575
+ async function depositStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, depositAmount, overrideStartTime) {
1576
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1577
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1578
+ const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1579
+ return program.instruction.depositStakingPool(new anchor.BN(depositAmount), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1580
+ {
1581
+ accounts: {
1582
+ payer: payerPublicKey,
1583
+ pool: poolPublickey,
1584
+ poolPosition: poolPositionPublicKey,
1585
+ stakedTokenMintInfo: stakedTokenMintPublicKey,
1586
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1587
+ payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
1588
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1589
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1590
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1591
+ systemProgram: web3_js.SystemProgram.programId
1592
+ },
1593
+ signers: []
1594
+ });
1595
+ }
1596
+
1597
+ async function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1598
+ const poolPosition = web3_js.Keypair.generate();
1599
+ const transaction = new web3_js.Transaction().add(await withdrawStakingPoolInstruction(program, payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime));
1600
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
1601
+ return poolPosition.publicKey;
1602
+ }
1603
+ async function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
1604
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1605
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1606
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey();
1607
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
1608
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
1609
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
1610
+ const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey);
1611
+ const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey);
1612
+ const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey);
1613
+ return program.instruction.withdrawStakingPool(new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
1614
+ {
1615
+ accounts: {
1616
+ payer: payerPublicKey,
1617
+ vaultSystemState: vaultSystemStatePublicKey,
1618
+ pool: poolPublickey,
1619
+ poolPosition: poolPositionPublicKey,
1620
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
1621
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
1622
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
1623
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
1624
+ payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
1625
+ hedgeMint: hedgeMintPublickey,
1626
+ stakedTokenMint: stakedTokenMintPublicKey,
1627
+ usdhMint: usdhMintPublickey,
1628
+ rent: web3_js.SYSVAR_RENT_PUBKEY,
1629
+ splTokenProgramInfo: splToken.TOKEN_PROGRAM_ID,
1630
+ splAssociatedTokenProgramInfo: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
1631
+ systemProgram: web3_js.SystemProgram.programId
1632
+ },
1633
+ signers: []
1634
+ });
1635
+ }
1636
+
1637
+ async function createVault(program, provider, payer, depositAmount, collateralRatio) {
1638
+ const [usdhMintPublickey] = await getUsdhMintPublicKey();
1639
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1548
1640
  // Prep the user to get USDH back out at some point
1549
1641
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1550
1642
  const newVault = web3_js.Keypair.generate();
1551
1643
  const history = web3_js.Keypair.generate();
1552
1644
  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);
1645
+ const transaction = new web3_js.Transaction().add(createVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, newVault.publicKey, history.publicKey, depositAmount, collateralRatio));
1646
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, newVault, history], provider === null || provider === void 0 ? void 0 : provider.opts);
1555
1647
  return newVault.publicKey;
1556
1648
  }
1557
- function createVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount, collateralRatio) {
1558
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1649
+ function createVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount, collateralRatio) {
1559
1650
  const ix = program.instruction.createVault(new anchor.BN(depositAmount), new anchor.BN(collateralRatio), {
1560
1651
  accounts: {
1561
1652
  vaultSystemState: vaultSystemStatePublicKey,
@@ -1569,19 +1660,18 @@ function createVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vault
1569
1660
  return ix;
1570
1661
  }
1571
1662
 
1572
- async function depositVault(connection, payer, vaultPublicKey, depositAmount, confirmOptions) {
1663
+ async function depositVault(program, provider, payer, vaultPublicKey, depositAmount) {
1573
1664
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1574
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1665
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1575
1666
  // Prep the user to get USDH back out at some point
1576
1667
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1577
1668
  const history = web3_js.Keypair.generate();
1578
1669
  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);
1670
+ const transaction = new web3_js.Transaction().add(depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, depositAmount));
1671
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1581
1672
  return vaultPublicKey;
1582
1673
  }
1583
- function depositVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount) {
1584
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1674
+ function depositVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount) {
1585
1675
  return program.instruction.depositVault(new anchor.BN(depositAmount), {
1586
1676
  accounts: {
1587
1677
  vaultSystemState: vaultSystemStatePublicKey,
@@ -1594,19 +1684,18 @@ function depositVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaul
1594
1684
  });
1595
1685
  }
1596
1686
 
1597
- async function withdrawVault(connection, payer, vaultPublicKey, withdrawAmount, chainlinkOverridePrice, confirmOptions) {
1687
+ async function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount, chainlinkOverridePrice) {
1598
1688
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1599
- const USDH = new splToken.Token(connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1689
+ const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
1600
1690
  // Prep the user to get USDH back out at some point
1601
1691
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
1602
1692
  const history = web3_js.Keypair.generate();
1603
1693
  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);
1694
+ const transaction = new web3_js.Transaction().add(withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultPublicKey, history.publicKey, withdrawAmount, chainlinkOverridePrice));
1695
+ await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
1606
1696
  return vaultPublicKey;
1607
1697
  }
1608
- function withdrawVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vaultPublickey, historyPublicKey, withdrawAmount, chainlinkOverridePrice) {
1609
- const program = new anchor.Program(vaultIdl, HEDGE_PROGRAM_ID);
1698
+ function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublickey, historyPublicKey, withdrawAmount, chainlinkOverridePrice) {
1610
1699
  return program.instruction.withdrawVault(new anchor.BN(withdrawAmount), new anchor.BN(chainlinkOverridePrice !== null && chainlinkOverridePrice !== void 0 ? chainlinkOverridePrice : 200 * web3_js.LAMPORTS_PER_SOL), {
1611
1700
  accounts: {
1612
1701
  vaultSystemState: vaultSystemStatePublicKey,
@@ -1620,18 +1709,17 @@ function withdrawVaultInstruction(vaultSystemStatePublicKey, payerPublicKey, vau
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, chainlinkOverridePrice) {
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, chainlinkOverridePrice));
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, chainlinkOverridePrice) {
1635
1723
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1636
1724
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1637
1725
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
@@ -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,18 +1777,17 @@ 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, chainlinkOverridePrice, 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, chainlinkOverridePrice, 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, chainlinkOverridePrice, transactionOverrideTime) {
1705
1791
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1706
1792
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1707
1793
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
@@ -1727,15 +1813,14 @@ 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, chainlinkOverridePrice) {
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, chainlinkOverridePrice));
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, chainlinkOverridePrice) {
1739
1824
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey();
1740
1825
  const [usdhMintPublickey] = await getUsdhMintPublicKey();
1741
1826
  const [hedgeMintPublickey] = await getHedgeMintPublicKey();
@@ -1792,7 +1877,6 @@ class VaultAccount {
1792
1877
  this.deposited = vault.deposited.toNumber();
1793
1878
  this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio);
1794
1879
  this.liquidationPrice = vault.liquidationPrice.toNumber();
1795
- this.minCollateralRatio = vault.minCollateralRatio.toNumber();
1796
1880
  this.vaultStatus = Object.keys(vault.vaultStatus)[0];
1797
1881
  }
1798
1882
  /**
@@ -1830,147 +1914,189 @@ class VaultAccount {
1830
1914
  }
1831
1915
  }
1832
1916
 
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;
1917
+ class VaultHistoryEvent {
1918
+ constructor(account, publicKey) {
1919
+ this.account = account;
1920
+ this.publicKey = publicKey;
1921
+ this.actorAccount = account.actorAccount;
1922
+ this.usdSolPrice = DecimalFromU128(account.usdSolPrice.toString());
1923
+ this.usdhDebtBefore = account.usdhDebtBefore.toNumber();
1924
+ this.usdhDebtAfter = account.usdhDebtAfter.toNumber();
1925
+ this.solBalanceBefore = account.solBalanceBefore.toNumber();
1926
+ this.solBalanceAfter = account.solBalanceAfter.toNumber();
1927
+ this.minCollateralRatioBefore = DecimalFromU128(account.minCollateralRatioBefore);
1928
+ this.minCollateralRatioAfter = DecimalFromU128(account.minCollateralRatioAfter);
1929
+ this.vaultStateBefore = account.vaultStateBefore;
1930
+ this.vaultStateAfter = account.vaultStateAfter;
1931
+ this.timestamp = account.timestamp.toNumber();
1932
+ this.action = account.action;
1857
1933
  }
1858
- throw error;
1859
1934
  }
1935
+ exports.VaultStatus = void 0;
1936
+ (function (VaultStatus) {
1937
+ VaultStatus[VaultStatus["Open"] = 0] = "Open";
1938
+ VaultStatus[VaultStatus["Closed"] = 1] = "Closed";
1939
+ VaultStatus[VaultStatus["Liquidated"] = 2] = "Liquidated";
1940
+ VaultStatus[VaultStatus["Distributed"] = 3] = "Distributed";
1941
+ VaultStatus[VaultStatus["Redeemed"] = 4] = "Redeemed";
1942
+ })(exports.VaultStatus || (exports.VaultStatus = {}));
1943
+ exports.VaultHistoryAction = void 0;
1944
+ (function (VaultHistoryAction) {
1945
+ VaultHistoryAction[VaultHistoryAction["Create"] = 0] = "Create";
1946
+ VaultHistoryAction[VaultHistoryAction["Close"] = 1] = "Close";
1947
+ VaultHistoryAction[VaultHistoryAction["Liquidate"] = 2] = "Liquidate";
1948
+ VaultHistoryAction[VaultHistoryAction["PartialLiquidate"] = 3] = "PartialLiquidate";
1949
+ VaultHistoryAction[VaultHistoryAction["Distributed"] = 4] = "Distributed";
1950
+ VaultHistoryAction[VaultHistoryAction["Redeem"] = 5] = "Redeem";
1951
+ VaultHistoryAction[VaultHistoryAction["TransferOwnership"] = 6] = "TransferOwnership";
1952
+ VaultHistoryAction[VaultHistoryAction["Deposit"] = 7] = "Deposit";
1953
+ VaultHistoryAction[VaultHistoryAction["Withdraw"] = 8] = "Withdraw";
1954
+ VaultHistoryAction[VaultHistoryAction["Loan"] = 9] = "Loan";
1955
+ VaultHistoryAction[VaultHistoryAction["RepayCredit"] = 10] = "RepayCredit";
1956
+ })(exports.VaultHistoryAction || (exports.VaultHistoryAction = {}));
1860
1957
 
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;
1958
+ class StakingPool {
1959
+ constructor(poolInfo, publicKey) {
1960
+ this.poolInfo = poolInfo;
1961
+ this.publicKey = publicKey;
1962
+ this.deposits = poolInfo.deposits.toNumber();
1963
+ // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
1964
+ // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
1965
+ this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber();
1966
+ this.startTime = poolInfo.startTime.toNumber();
1967
+ this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber();
1968
+ this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber();
1969
+ this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator);
1970
+ this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator);
1971
+ this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator);
1972
+ // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
1973
+ }
1866
1974
  }
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
- });
1975
+
1976
+ class StakingPoolPosition {
1977
+ constructor(poolPositionInfo, key, stakingPool) {
1978
+ this.poolPositionInfo = poolPositionInfo;
1979
+ this.publicKey = key;
1980
+ this.pool = stakingPool;
1981
+ this.owner = poolPositionInfo.owner;
1982
+ this.deposited = poolPositionInfo.deposited.toNumber();
1983
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
1984
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
1985
+ this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber();
1986
+ this.startHedgeRewardAccumulator = DecimalFromU128(poolPositionInfo.startHedgeRewardAccumulator);
1987
+ this.startUsdhFeeAccumulator = DecimalFromU128(poolPositionInfo.startUsdhFeeAccumulator);
1988
+ this.startSolFeeAccumulator = DecimalFromU128(poolPositionInfo.startSolFeeAccumulator);
1989
+ this.open = poolPositionInfo.state.open !== undefined;
1990
+ }
1991
+ getCurrentUsdhFeeReward() {
1992
+ return this.pool.usdhFeeAccumulator.minus(this.startUsdhFeeAccumulator).times(new Decimal__default["default"](this.deposited));
1993
+ }
1890
1994
  }
1891
1995
 
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;
1996
+ /**
1997
+ * Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
1998
+ * created to maintain each users contribution to the pool.
1999
+ */
2000
+ class LiquidationPoolEra {
2001
+ constructor(liquidyPoolEra) {
2002
+ this.liquidyPoolEra = liquidyPoolEra;
2003
+ this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber();
2004
+ this.product = DecimalFromU128(liquidyPoolEra.productBytes);
2005
+ this.sum = DecimalFromU128(liquidyPoolEra.sumBytes);
2006
+ this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes);
2007
+ this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber();
2008
+ }
1897
2009
  }
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
- });
2010
+
2011
+ class LiquidationPoolState {
2012
+ constructor(liquidationPoolState) {
2013
+ this.liquidationPoolState = liquidationPoolState;
2014
+ this.lifetimeDeposits = liquidationPoolState.lifetimeDeposits.toNumber();
2015
+ this.hedgeInitRewardsTimestamp = liquidationPoolState.hedgeInitRewardsTimestamp.toNumber();
2016
+ // TODO Add the rest that are missing. Do we need them?
2017
+ }
1919
2018
  }
1920
2019
 
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;
2020
+ class LiquidationPosition {
2021
+ constructor(poolPositionInfo, key, era, liquidationPoolState) {
2022
+ this.publicKey = key;
2023
+ this.eraPublicKey = poolPositionInfo.era;
2024
+ this.era = era;
2025
+ this.liquidationPoolState = liquidationPoolState;
2026
+ this.ownerAccount = poolPositionInfo.ownerAccount;
2027
+ this.deposit = poolPositionInfo.deposit.toNumber();
2028
+ this.closedUsdh = poolPositionInfo.closedUsdh.toNumber();
2029
+ this.closedSol = poolPositionInfo.closedSol.toNumber();
2030
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
2031
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
2032
+ this.productSnapshot = DecimalFromU128(poolPositionInfo.productSnapshotBytes);
2033
+ this.sumSnapshot = DecimalFromU128(poolPositionInfo.sumSnapshotBytes);
2034
+ this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum);
2035
+ this.open = poolPositionInfo.state.open !== undefined;
2036
+ }
2037
+ getUsdhAvailable() {
2038
+ return (this.era.product.div(this.productSnapshot)).mul(new Decimal__default["default"](this.deposit));
2039
+ }
2040
+ getSolAvailable() {
2041
+ return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal__default["default"](this.deposit)).floor();
2042
+ }
2043
+ getHedgeAvailable() {
2044
+ const LiquidationPoolTotalSupply = 2000000.0 * web3_js.LAMPORTS_PER_SOL;
2045
+ const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(LiquidationPoolTotalSupply, this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000, this.era.hedgeRewardsTimestamp * 1000, Date.now(), 365 * 1000);
2046
+ if (this.era.totalDeposits === 0) {
2047
+ return new Decimal__default["default"](0);
2048
+ }
2049
+ const rewardsPerToken = this.era.product.mul(new Decimal__default["default"](hedgeRewardsSinceLastUpdate / this.era.totalDeposits));
2050
+ const newAccumulator = rewardsPerToken.add(new Decimal__default["default"](this.era.hedgeRewardsAccumulator));
2051
+ const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal__default["default"](this.deposit)).div(new Decimal__default["default"](this.productSnapshot));
2052
+ return hedgeAvailable;
2053
+ }
1926
2054
  }
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
- });
2055
+ function hedgeRewardsDecay(supply, birthTime, timeIn, timeOut, halfLifeInDays) {
2056
+ const timeInOffsetStart = timeIn - birthTime;
2057
+ const timeOutOffsetStart = timeOut - birthTime;
2058
+ const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24);
2059
+ const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart));
2060
+ return awardedTokens;
1960
2061
  }
1961
2062
 
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;
2063
+ exports.CHAINLINK_SOL_USD_ID = CHAINLINK_SOL_USD_ID;
2064
+ exports.CHAINLINK_SOL_USD_PUBLICKEY = CHAINLINK_SOL_USD_PUBLICKEY;
1973
2065
  exports.DecimalFromU128 = DecimalFromU128;
1974
- exports.StakingPool = StakingPools;
1975
- exports.Vaults = Vaults;
2066
+ exports.HEDGE_PROGRAM_ID = HEDGE_PROGRAM_ID;
2067
+ exports.HEDGE_PROGRAM_PUBLICKEY = HEDGE_PROGRAM_PUBLICKEY;
2068
+ exports.LiquidationPoolEra = LiquidationPoolEra;
2069
+ exports.LiquidationPoolState = LiquidationPoolState;
2070
+ exports.LiquidationPosition = LiquidationPosition;
2071
+ exports.StakingPool = StakingPool;
2072
+ exports.StakingPoolPosition = StakingPoolPosition;
2073
+ exports.VaultAccount = VaultAccount;
2074
+ exports.VaultHistoryEvent = VaultHistoryEvent;
2075
+ exports.createStakingPool = createStakingPool;
2076
+ exports.createStakingPoolInstruction = createStakingPoolInstruction;
2077
+ exports.createVault = createVault;
2078
+ exports.createVaultInstruction = createVaultInstruction;
2079
+ exports.depositStakingPool = depositStakingPool;
2080
+ exports.depositStakingPoolInstruction = depositStakingPoolInstruction;
2081
+ exports.depositVault = depositVault;
2082
+ exports.depositVaultInstruction = depositVaultInstruction;
2083
+ exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
2084
+ exports.getHedgeMintPublicKey = getHedgeMintPublicKey;
2085
+ exports.getLiquidationPoolStatePublicKey = getLiquidationPoolStatePublicKey;
2086
+ exports.getLiquidationPoolUsdhAccountPublicKey = getLiquidationPoolUsdhAccountPublicKey;
2087
+ exports.getPoolPublicKeyForMint = getPoolPublicKeyForMint;
2088
+ exports.getUsdhMintPublicKey = getUsdhMintPublicKey;
2089
+ exports.getVaultSystemStatePublicKey = getVaultSystemStatePublicKey;
2090
+ exports.liquidateVault = liquidateVault;
2091
+ exports.liquidateVaultInstruction = liquidateVaultInstruction;
2092
+ exports.loanVault = loanVault;
2093
+ exports.loanVaultInstruction = loanVaultInstruction;
2094
+ exports.redeemVault = redeemVault;
2095
+ exports.redeemVaultInstruction = redeemVaultInstruction;
2096
+ exports.repayVault = repayVault;
2097
+ exports.repayVaultInstruction = repayVaultInstruction;
2098
+ exports.withdrawStakingPool = withdrawStakingPool;
2099
+ exports.withdrawStakingPoolInstruction = withdrawStakingPoolInstruction;
2100
+ exports.withdrawVault = withdrawVault;
2101
+ exports.withdrawVaultInstruction = withdrawVaultInstruction;
1976
2102
  //# sourceMappingURL=index.js.map