hedge-web3 0.2.24 → 0.2.25

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 (70) hide show
  1. package/declarations/Constants.d.ts +13 -15
  2. package/declarations/idl/vault.d.ts +36 -0
  3. package/declarations/instructions/createReferralAccount.d.ts +1 -1
  4. package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
  5. package/declarations/instructions/referralClaimFees.d.ts +1 -1
  6. package/declarations/utils/getLinkedListAccounts.d.ts +2 -2
  7. package/lib/Constants.js +28 -34
  8. package/lib/idl/vault.js +36 -0
  9. package/lib/instructions/claimLiquidationPoolPosition.js +10 -7
  10. package/lib/instructions/claimStakingPoolPosition.js +5 -5
  11. package/lib/instructions/closeClaimedLiquidationPoolPosition.js +1 -1
  12. package/lib/instructions/closeLiquidationPoolPosition.js +13 -13
  13. package/lib/instructions/createReferralAccount.js +8 -8
  14. package/lib/instructions/createStakingPool.js +7 -7
  15. package/lib/instructions/createUserReferralAccount.js +6 -6
  16. package/lib/instructions/createVault.js +15 -15
  17. package/lib/instructions/depositLiquidationPool.js +9 -9
  18. package/lib/instructions/depositStakingPool.js +5 -5
  19. package/lib/instructions/depositVault.js +11 -9
  20. package/lib/instructions/initHedgeFoundation.js +8 -8
  21. package/lib/instructions/initHedgeFoundationTokens.js +5 -5
  22. package/lib/instructions/liquidateVault.js +8 -8
  23. package/lib/instructions/loanVault.js +12 -12
  24. package/lib/instructions/psmEditAccount.js +5 -5
  25. package/lib/instructions/psmMintUsh.js +14 -14
  26. package/lib/instructions/psmRedeemUsh.js +14 -14
  27. package/lib/instructions/redeemVault.js +7 -7
  28. package/lib/instructions/referralClaimFees.js +8 -8
  29. package/lib/instructions/refreshOraclePrice.js +3 -3
  30. package/lib/instructions/repayVault.js +9 -9
  31. package/lib/instructions/setHalted.js +1 -1
  32. package/lib/instructions/updateReferralAccount.js +2 -2
  33. package/lib/instructions/updateReferralState.js +2 -2
  34. package/lib/instructions/updateVaultType.js +1 -1
  35. package/lib/instructions/withdrawStakingPool.js +11 -11
  36. package/lib/instructions/withdrawVault.js +5 -5
  37. package/lib/utils/getLinkedListAccounts.js +2 -3
  38. package/package.json +1 -1
  39. package/src/Constants.ts +44 -85
  40. package/src/idl/vault.ts +72 -0
  41. package/src/instructions/claimLiquidationPoolPosition.ts +31 -29
  42. package/src/instructions/claimStakingPoolPosition.ts +10 -15
  43. package/src/instructions/closeClaimedLiquidationPoolPosition.ts +4 -2
  44. package/src/instructions/closeLiquidationPoolPosition.ts +18 -24
  45. package/src/instructions/createReferralAccount.ts +17 -29
  46. package/src/instructions/createStakingPool.ts +11 -13
  47. package/src/instructions/createUserReferralAccount.ts +13 -24
  48. package/src/instructions/createVault.ts +44 -26
  49. package/src/instructions/depositLiquidationPool.ts +16 -23
  50. package/src/instructions/depositStakingPool.ts +18 -14
  51. package/src/instructions/depositVault.ts +23 -18
  52. package/src/instructions/initHedgeFoundation.ts +16 -14
  53. package/src/instructions/initHedgeFoundationTokens.ts +12 -14
  54. package/src/instructions/liquidateVault.ts +15 -20
  55. package/src/instructions/loanVault.ts +18 -27
  56. package/src/instructions/psmEditAccount.ts +10 -18
  57. package/src/instructions/psmMintUsh.ts +19 -41
  58. package/src/instructions/psmRedeemUsh.ts +21 -45
  59. package/src/instructions/redeemVault.ts +12 -15
  60. package/src/instructions/referralClaimFees.ts +17 -31
  61. package/src/instructions/refreshOraclePrice.ts +6 -8
  62. package/src/instructions/repayVault.ts +18 -16
  63. package/src/instructions/setHalted.ts +5 -24
  64. package/src/instructions/transferVault.ts +4 -9
  65. package/src/instructions/updateReferralAccount.ts +7 -14
  66. package/src/instructions/updateReferralState.ts +7 -14
  67. package/src/instructions/updateVaultType.ts +9 -23
  68. package/src/instructions/withdrawStakingPool.ts +17 -21
  69. package/src/instructions/withdrawVault.ts +10 -16
  70. package/src/utils/getLinkedListAccounts.ts +4 -7
@@ -17,12 +17,12 @@ const anchor_1 = require("@project-serum/anchor");
17
17
  const spl_token_1 = require("@solana/spl-token");
18
18
  // import { TokenInstructions } from '@project-serum/serum'
19
19
  const web3_js_1 = require("@solana/web3.js");
20
- const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
21
20
  const Constants_1 = require("../Constants");
21
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
22
22
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
23
23
  function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, transactionOverrideTime) {
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
- const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
25
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
26
26
  // Prep the user to get USH back out at some point
27
27
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
28
28
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
@@ -40,11 +40,11 @@ function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, tra
40
40
  exports.redeemVault = redeemVault;
41
41
  function redeemVaultInstruction(program, payerPublicKey, payerUshAccount, destinationTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, redeemAmount, transactionOverrideTime) {
42
42
  return __awaiter(this, void 0, void 0, function* () {
43
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
44
- const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
45
- const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
46
- const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
47
- const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
43
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
44
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
45
+ const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)(program.programId);
46
+ const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(program.programId, hedgeMintPublickey);
47
+ const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, hedgeStakingPoolPublicKey, ushMintPublickey);
48
48
  return yield program.methods
49
49
  .redeemVault(redeemAmount, new anchor_1.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : Date.now() / 1000) // override start time
50
50
  )
@@ -28,28 +28,28 @@ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfi
28
28
  * - payer: Signer : who we are creating the referral account for
29
29
  * - poolPublicKey: PublicKey : a key to a pool position. If the position is closed or does not belong to the signer it will be ignored
30
30
  * - referrer: PublicKey
31
- */
31
+ */
32
32
  function referralClaimFees(program, provider, payer, poolPosition) {
33
33
  return __awaiter(this, void 0, void 0, function* () {
34
- // setup transaction
34
+ // setup transaction
35
35
  const transaction = new web3_js_1.Transaction();
36
36
  const signers = [payer];
37
37
  // General variables
38
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
38
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
39
39
  // Find referrer account
40
40
  const referrer = yield payer.publicKey;
41
- const referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
41
+ const referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(program.programId, referrer);
42
42
  // Derive the user referral account public key
43
- const referallStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
44
- const hedgeMintPublicKey = yield (0, Constants_1.getHedgeMintPublicKey)();
43
+ const referallStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)(program.programId);
44
+ const hedgeMintPublicKey = yield (0, Constants_1.getHedgeMintPublicKey)(program.programId);
45
45
  // Get HDG account of signer
46
46
  const payerHdgAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMintPublicKey, payer.publicKey);
47
- const ushMintPublicKey = yield (0, Constants_1.getUshMintPublicKey)();
47
+ const ushMintPublicKey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
48
48
  // Get USH account of signer
49
49
  const payerUshAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublicKey, payer.publicKey);
50
50
  // Get community_associated_hedge_token_account
51
51
  const communityAssociatedHedgeTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMintPublicKey, vaultSystemStatePublicKey, true);
52
- const [feePoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublicKey);
52
+ const [feePoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(program.programId, hedgeMintPublicKey);
53
53
  const feePoolAssociatedUshTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublicKey, feePoolPublicKey, true);
54
54
  transaction.add(yield referralClaimFeesInstruction(program, payer.publicKey, vaultSystemStatePublicKey, poolPosition, referralAccountPublicKey, referallStatePublicKey, hedgeMintPublicKey, payerHdgAssociatedTokenAccount.address, ushMintPublicKey, payerUshAssociatedTokenAccount.address, communityAssociatedHedgeTokenAccount.address, feePoolPublicKey, feePoolAssociatedUshTokenAccount.address));
55
55
  yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, signers).catch(Errors_1.parseAnchorErrors);
@@ -15,8 +15,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Cluster = exports.refreshOraclePriceInstruction = exports.refreshOraclePrice = void 0;
16
16
  const anchor_1 = require("@project-serum/anchor");
17
17
  const web3_js_1 = require("@solana/web3.js");
18
- const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
19
18
  const Constants_1 = require("../Constants");
19
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
20
20
  function refreshOraclePrice(program, provider, payer, collateralType, network, overridePrice, overrideTime) {
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
22
  const transaction = new web3_js_1.Transaction().add(yield refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime));
@@ -27,8 +27,8 @@ exports.refreshOraclePrice = refreshOraclePrice;
27
27
  function refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime) {
28
28
  return __awaiter(this, void 0, void 0, function* () {
29
29
  const enc = new TextEncoder();
30
- const [oracleInfoAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
31
- const [vaultTypeAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
30
+ const [oracleInfoAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], program.programId);
31
+ const [vaultTypeAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], program.programId);
32
32
  const oracleInfo = yield program.account.oracleInfoForCollateralType.fetch(oracleInfoAccount);
33
33
  return yield program.methods
34
34
  .refreshOraclePrice(new anchor_1.BN(overridePrice !== null && overridePrice !== void 0 ? overridePrice : web3_js_1.LAMPORTS_PER_SOL * 150), // override usd/sol price
@@ -16,18 +16,18 @@ exports.repayVaultInstruction = exports.repayVault = void 0;
16
16
  const anchor_1 = require("@project-serum/anchor");
17
17
  const spl_token_1 = require("@solana/spl-token");
18
18
  const web3_js_1 = require("@solana/web3.js");
19
- const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
20
19
  const Constants_1 = require("../Constants");
20
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
21
21
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
22
22
  function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overrideTime) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
- const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
24
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
25
25
  // Prep the user to get USH back out at some point
26
26
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
27
27
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
28
28
  const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
29
- const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultAccount.vaultType, vaultTypeAccount.collateralMint);
30
- const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultPublicKey, vaultTypeAccount.collateralMint);
29
+ const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, vaultAccount.vaultType, vaultTypeAccount.collateralMint);
30
+ const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, vaultPublicKey, vaultTypeAccount.collateralMint);
31
31
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(Math.abs(repayAmount)), false, false);
32
32
  const history = web3_js_1.Keypair.generate();
33
33
  const transaction = new web3_js_1.Transaction().add(yield repayVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, new anchor_1.BN(repayAmount), overrideTime));
@@ -38,11 +38,11 @@ function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overr
38
38
  exports.repayVault = repayVault;
39
39
  function repayVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, repayAmount, overrideTime) {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
42
- const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
43
- const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
44
- const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
45
- const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
41
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
42
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
43
+ const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)(program.programId);
44
+ const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(program.programId, hedgeMintPublickey);
45
+ const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, hedgeStakingPoolPublicKey, ushMintPublickey);
46
46
  return yield program.methods
47
47
  .repayVault(repayAmount, new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
48
48
  )
@@ -19,7 +19,7 @@ const Errors_1 = require("../utils/Errors");
19
19
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
20
20
  function setHalted(program, provider, payer, halted) {
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
22
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
23
23
  const transaction = new web3_js_1.Transaction().add(yield setHaltedInstruction(program, vaultSystemStatePublicKey, payer.publicKey, halted));
24
24
  yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
25
25
  return vaultSystemStatePublicKey;
@@ -18,8 +18,8 @@ const Constants_1 = require("../Constants");
18
18
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
19
19
  function updateReferralAccount(program, provider, payer, referrer, config) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
22
- const referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
21
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
22
+ const referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(program.programId, referrer);
23
23
  const transaction = new web3_js_1.Transaction().add(yield updateReferralAccountInstruction(program, vaultSystemStatePublicKey, referralAccountPublicKey, payer.publicKey, config));
24
24
  yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
25
25
  return referralAccountPublicKey;
@@ -18,8 +18,8 @@ const Constants_1 = require("../Constants");
18
18
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
19
19
  function updateReferralState(program, provider, payer, config) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
22
- const referralStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
21
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
22
+ const referralStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)(program.programId);
23
23
  const transaction = new web3_js_1.Transaction().add(yield updateReferralStateInstruction(program, vaultSystemStatePublicKey, referralStatePublicKey, payer.publicKey, config));
24
24
  yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
25
25
  return referralStatePublicKey;
@@ -18,7 +18,7 @@ const Constants_1 = require("../Constants");
18
18
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
19
19
  function updateVaultType(program, provider, payer, vaultTypeAccount, oracleInfoAccount, config) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
21
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
22
22
  const transaction = new web3_js_1.Transaction().add(yield updateVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, oracleInfoAccount, config));
23
23
  yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
24
24
  return vaultSystemStatePublicKey;
@@ -16,8 +16,8 @@ exports.withdrawStakingPoolInstruction = exports.withdrawStakingPool = void 0;
16
16
  const anchor_1 = require("@project-serum/anchor");
17
17
  const spl_token_1 = require("@solana/spl-token");
18
18
  const web3_js_1 = require("@solana/web3.js");
19
- const Errors_1 = require("../utils/Errors");
20
19
  const Constants_1 = require("../Constants");
20
+ const Errors_1 = require("../utils/Errors");
21
21
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
22
22
  function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
@@ -30,16 +30,16 @@ function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, st
30
30
  exports.withdrawStakingPool = withdrawStakingPool;
31
31
  function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
34
- const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
35
- const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
36
- const [poolPublickey] = yield (0, Constants_1.getPoolPublicKeyForMint)(stakedTokenMintPublicKey);
37
- const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, stakedTokenMintPublicKey);
38
- const poolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, ushMintPublickey);
39
- const payerAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, stakedTokenMintPublicKey);
40
- const payerAssociatedHedgeAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, hedgeMintPublickey);
41
- const payerAssociatedUshAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, ushMintPublickey);
42
- const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultSystemStatePublicKey, hedgeMintPublickey);
33
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
34
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
35
+ const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)(program.programId);
36
+ const [poolPublickey] = yield (0, Constants_1.getPoolPublicKeyForMint)(program.programId, stakedTokenMintPublicKey);
37
+ const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, poolPublickey, stakedTokenMintPublicKey);
38
+ const poolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, poolPublickey, ushMintPublickey);
39
+ const payerAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, payerPublicKey, stakedTokenMintPublicKey);
40
+ const payerAssociatedHedgeAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, payerPublicKey, hedgeMintPublickey);
41
+ const payerAssociatedUshAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, payerPublicKey, ushMintPublickey);
42
+ const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, vaultSystemStatePublicKey, hedgeMintPublickey);
43
43
  return yield program.methods
44
44
  .withdrawStakingPool(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)) // override current time
45
45
  )
@@ -14,26 +14,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.withdrawVaultInstruction = exports.withdrawVault = void 0;
16
16
  const anchor_1 = require("@project-serum/anchor");
17
- const spl_token_1 = require("@solana/spl-token");
18
17
  const serum_1 = require("@project-serum/serum");
18
+ const spl_token_1 = require("@solana/spl-token");
19
19
  const web3_js_1 = require("@solana/web3.js");
20
20
  const Constants_1 = require("../Constants");
21
21
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
22
22
  const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
23
23
  function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount, overrideTime) {
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
- const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
25
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
26
26
  // Prep the user to get USH back out at some point
27
27
  yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
28
28
  const history = web3_js_1.Keypair.generate();
29
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
29
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
30
30
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
31
31
  const vaultAssociatedCollateralAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, serum_1.TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, true);
32
32
  const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
33
33
  const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
34
34
  const destinationTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
35
- const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
36
- const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
35
+ const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(program.programId, yield (0, Constants_1.getHedgeMintPublicKey)(program.programId));
36
+ const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, hedgeStakingPoolPublicKey, ushMintPublickey);
37
37
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(Math.abs(withdrawAmount)), new anchor_1.BN(0), new anchor_1.BN(0), false, false);
38
38
  const transaction = new web3_js_1.Transaction().add(yield withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, destinationTokenAccount.address, vaultPublicKey, vaultAssociatedCollateralAccount.address, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMintPublickey, history.publicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, new anchor_1.BN(withdrawAmount), overrideTime));
39
39
  yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
@@ -15,9 +15,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getLinkedListAccounts = void 0;
16
16
  const anchor_1 = require("@project-serum/anchor");
17
17
  const underscore_1 = __importDefault(require("underscore"));
18
- const Constants_1 = require("../Constants");
19
- const VaultAccount_1 = require("../state/VaultAccount");
20
18
  const decimal_js_1 = __importDefault(require("decimal.js"));
19
+ const VaultAccount_1 = require("../state/VaultAccount");
21
20
  const bs58_1 = __importDefault(require("bs58"));
22
21
  const VaultType_1 = __importDefault(require("../state/VaultType"));
23
22
  /**
@@ -200,7 +199,7 @@ function getMiniVaults(program, vaultTypePublicKey) {
200
199
  },
201
200
  },
202
201
  ];
203
- const allAccounts = yield program.provider.connection.getProgramAccounts(Constants_1.HEDGE_PROGRAM_PUBLICKEY, {
202
+ const allAccounts = yield program.provider.connection.getProgramAccounts(program.programId, {
204
203
  filters: filters,
205
204
  // Slice the data only to grab the 3 u64's of size 8 bytes each
206
205
  dataSlice: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.2.24",
3
+ "version": "0.2.25",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "types": "declarations/index.d.ts",
package/src/Constants.ts CHANGED
@@ -1,194 +1,153 @@
1
- import {
2
- ASSOCIATED_TOKEN_PROGRAM_ID,
3
- TOKEN_PROGRAM_ID,
4
- } from '@solana/spl-token'
1
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
5
2
  import { PublicKey } from '@solana/web3.js'
6
3
 
7
- export const HEDGE_PROGRAM_ID = 'HedgeEohwU6RqokrvPU4Hb6XKPub8NuKbnPmY7FoMMtN'
8
- export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
9
-
10
- export const CHAINLINK_SOL_USD_ID =
11
- 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
4
+ export const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
12
5
  export const CHAINLINK_SOL_USD_PUBLICKEY = new PublicKey(CHAINLINK_SOL_USD_ID)
13
- export const CHAINLINK_PROGRAM_ID = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny";
6
+ export const CHAINLINK_PROGRAM_ID = 'HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny'
14
7
 
15
8
  const enc = new TextEncoder()
16
9
 
17
10
  /**
18
11
  * @returns The Liquidation pool public key
19
12
  */
20
- export async function getLiquidationPoolStatePublicKey(): Promise<PublicKey> {
21
- const [poolPublicKey] = await PublicKey.findProgramAddress(
22
- [enc.encode('LiquidationPoolStateV1')],
23
- HEDGE_PROGRAM_PUBLICKEY
24
- )
13
+ export async function getLiquidationPoolStatePublicKey(hedgeProgramId: PublicKey): Promise<PublicKey> {
14
+ const [poolPublicKey] = await PublicKey.findProgramAddress([enc.encode('LiquidationPoolStateV1')], hedgeProgramId)
25
15
  return poolPublicKey
26
16
  }
27
17
  /**
28
- *
18
+ *
29
19
  * @returns The liquidation pool ush account public key
30
20
  */
31
- export async function getLiquidationPoolUshAccountPublicKey(): Promise<PublicKey> {
32
- const [poolPublicKey] = await PublicKey.findProgramAddress(
33
- [enc.encode('LiquidationPoolUSHAccountV1')],
34
- HEDGE_PROGRAM_PUBLICKEY
35
- )
21
+ export async function getLiquidationPoolUshAccountPublicKey(hedgeProgramId: PublicKey): Promise<PublicKey> {
22
+ const [poolPublicKey] = await PublicKey.findProgramAddress([enc.encode('LiquidationPoolUSHAccountV1')], hedgeProgramId)
36
23
  return poolPublicKey
37
24
  }
38
25
  /**
39
- *
26
+ *
40
27
  * @returns The USH mint public key
41
28
  */
42
- export async function getUshMintPublicKey(): Promise<PublicKey> {
43
- const [findMintPublicKey] = await PublicKey.findProgramAddress(
44
- [enc.encode('UshMintV1')],
45
- HEDGE_PROGRAM_PUBLICKEY
46
- )
29
+ export async function getUshMintPublicKey(hedgeProgramId: PublicKey): Promise<PublicKey> {
30
+ const [findMintPublicKey] = await PublicKey.findProgramAddress([enc.encode('UshMintV1')], hedgeProgramId)
47
31
  return findMintPublicKey
48
32
  }
49
33
  /**
50
- *
34
+ *
51
35
  * @returns The Vault System State public key
52
36
  */
53
- export async function getVaultSystemStatePublicKey(): Promise<PublicKey> {
54
- const [publicKey] = await PublicKey.findProgramAddress(
55
- [enc.encode('VaultSystemStateV1')],
56
- HEDGE_PROGRAM_PUBLICKEY
57
- )
37
+ export async function getVaultSystemStatePublicKey(hedgeProgramId: PublicKey): Promise<PublicKey> {
38
+ const [publicKey] = await PublicKey.findProgramAddress([enc.encode('VaultSystemStateV1')], hedgeProgramId)
58
39
  return publicKey
59
40
  }
60
41
 
61
42
  /**
62
- *
43
+ *
63
44
  * @returns The Referral State public key
64
45
  */
65
- export async function getReferralStatePublicKey(): Promise<PublicKey> {
66
- const [publicKey] = await PublicKey.findProgramAddress(
67
- [enc.encode('ReferralStateV1')],
68
- HEDGE_PROGRAM_PUBLICKEY
69
- )
46
+ export async function getReferralStatePublicKey(hedgeProgramId: PublicKey): Promise<PublicKey> {
47
+ const [publicKey] = await PublicKey.findProgramAddress([enc.encode('ReferralStateV1')], hedgeProgramId)
70
48
  return publicKey
71
49
  }
72
50
 
73
51
  /**
74
- *
52
+ *
75
53
  * @returns The Referral State public key based off the owner's public key
76
54
  */
77
- export async function getReferralAccountPublicKey(
78
- ownerPublicKey: PublicKey
79
- ): Promise<PublicKey> {
80
- const strToEncode = ownerPublicKey.toBuffer()//.substring(0, 28)
81
- const [publicKey] = await PublicKey.findProgramAddress(
82
- [enc.encode('refer_acct'), strToEncode],
83
- HEDGE_PROGRAM_PUBLICKEY
84
- )
55
+ export async function getReferralAccountPublicKey(hedgeProgramId: PublicKey, ownerPublicKey: PublicKey): Promise<PublicKey> {
56
+ const strToEncode = ownerPublicKey.toBuffer() //.substring(0, 28)
57
+ const [publicKey] = await PublicKey.findProgramAddress([enc.encode('refer_acct'), strToEncode], hedgeProgramId)
85
58
  return publicKey
86
59
  }
87
60
 
88
61
  /**
89
- *
62
+ *
90
63
  * @returns The user referral account public key based off the user's public key
91
64
  */
92
- export async function getUserReferralAccountPublicKey(
65
+ export async function getUserReferralAccountPublicKey(
66
+ hedgeProgramId: PublicKey,
93
67
  ownerPublicKey: PublicKey
94
68
  ): Promise<PublicKey> {
95
- const strToEncode = ownerPublicKey.toBuffer()//.substring(0, 28)
96
- const [publicKey] = await PublicKey.findProgramAddress(
97
- [enc.encode('user_ref'), strToEncode],
98
- HEDGE_PROGRAM_PUBLICKEY
99
- )
69
+ const strToEncode = ownerPublicKey.toBuffer() //.substring(0, 28)
70
+ const [publicKey] = await PublicKey.findProgramAddress([enc.encode('user_ref'), strToEncode], hedgeProgramId)
100
71
  return publicKey
101
72
  }
102
73
 
103
74
  /**
104
- *
75
+ *
105
76
  * @returns The HDG mint public key
106
77
  */
107
- export async function getHedgeMintPublicKey(): Promise<PublicKey> {
108
- const [publicKey] = await PublicKey.findProgramAddress(
109
- [enc.encode('HEDGEMintV1')],
110
- HEDGE_PROGRAM_PUBLICKEY
111
- )
78
+ export async function getHedgeMintPublicKey(hedgeProgramId: PublicKey): Promise<PublicKey> {
79
+ const [publicKey] = await PublicKey.findProgramAddress([enc.encode('HEDGEMintV1')], hedgeProgramId)
112
80
  return publicKey
113
81
  }
114
82
  /**
115
83
  * Get the public key for any staking pool
116
- *
84
+ *
117
85
  * @param mintPublicKey Staked collateral mint public key
118
86
  * @returns the public key for that staking pool
119
87
  */
120
88
  export async function getPoolPublicKeyForMint(
89
+ hedgeProgramId: PublicKey,
121
90
  mintPublicKey: PublicKey
122
91
  ): Promise<[PublicKey, number, string]> {
123
92
  const strToEncode = mintPublicKey.toString().substring(0, 12)
124
- const [publicKey, bump] = await PublicKey.findProgramAddress(
125
- [enc.encode(strToEncode)],
126
- HEDGE_PROGRAM_PUBLICKEY
127
- )
93
+ const [publicKey, bump] = await PublicKey.findProgramAddress([enc.encode(strToEncode)], hedgeProgramId)
128
94
  return [publicKey, bump, strToEncode]
129
95
  }
130
96
 
131
97
  /**
132
- *
98
+ *
133
99
  * @param collateralType String name of the collateral type (must be 16 chars)
134
100
  * @returns The public key for that Vault Type account
135
101
  */
136
- export async function getVaultTypeAccountPublicKey(
137
- collateralType: string
138
- ): Promise<PublicKey> {
102
+ export async function getVaultTypeAccountPublicKey(hedgeProgramId: PublicKey, collateralType: string): Promise<PublicKey> {
139
103
  const [vaultTypeAccount] = await PublicKey.findProgramAddress(
140
104
  [enc.encode(collateralType), enc.encode('State')],
141
- HEDGE_PROGRAM_PUBLICKEY
105
+ hedgeProgramId
142
106
  )
143
107
  return vaultTypeAccount
144
108
  }
145
109
 
146
110
  /**
147
- *
111
+ *
148
112
  * @param collateralType String name of the collateral type (must be 16 chars)
149
113
  * @returns The public key for that Vault Type oracle info
150
114
  */
151
115
  export async function getVaultTypeOracleAccountPublicKey(
116
+ hedgeProgramId: PublicKey,
152
117
  collateralType: string
153
118
  ): Promise<PublicKey> {
154
119
  const [vaultTypeOracleAccount] = await PublicKey.findProgramAddress(
155
120
  [enc.encode(collateralType), enc.encode('Oracle')],
156
- HEDGE_PROGRAM_PUBLICKEY
121
+ hedgeProgramId
157
122
  )
158
123
  return vaultTypeOracleAccount
159
124
  }
160
125
 
161
126
  /**
162
127
  * Vaults are stored in PDA accounts. Use this to get the address from a salt
163
- *
128
+ *
164
129
  * @param vaultSalt String salt for the vault (must be 8 chars)
165
130
  * @returns The public key for that Vault Type oracle info
166
131
  */
167
- export async function findVaultAddress(vaultSalt: string): Promise<PublicKey> {
168
- const [vaultAddress] = await PublicKey.findProgramAddress(
169
- [enc.encode('Vault'), enc.encode(vaultSalt)],
170
- HEDGE_PROGRAM_PUBLICKEY
171
- )
132
+ export async function findVaultAddress(hedgeProgramId: PublicKey, vaultSalt: string): Promise<PublicKey> {
133
+ const [vaultAddress] = await PublicKey.findProgramAddress([enc.encode('Vault'), enc.encode(vaultSalt)], hedgeProgramId)
172
134
  return vaultAddress
173
135
  }
174
136
 
175
137
  /**
176
- *
138
+ *
177
139
  * @param walletAddress Owner public key
178
140
  * @param tokenMintAddress Token mint
179
141
  * @returns The associated token account public key
180
142
  */
181
143
  export async function findAssociatedTokenAddress(
144
+ hedgeProgramId: PublicKey,
182
145
  walletAddress: PublicKey,
183
146
  tokenMintAddress: PublicKey
184
147
  ): Promise<PublicKey> {
185
148
  return (
186
149
  await PublicKey.findProgramAddress(
187
- [
188
- walletAddress.toBuffer(),
189
- TOKEN_PROGRAM_ID.toBuffer(),
190
- tokenMintAddress.toBuffer(),
191
- ],
150
+ [walletAddress.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), tokenMintAddress.toBuffer()],
192
151
  ASSOCIATED_TOKEN_PROGRAM_ID
193
152
  )
194
153
  )[0]