hedge-web3 0.2.27 → 0.2.31

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/declarations/Constants.d.ts +12 -0
  2. package/declarations/idl/vault.d.ts +743 -59
  3. package/declarations/index.d.ts +9 -0
  4. package/declarations/instructions/adminWithdrawCol.d.ts +5 -0
  5. package/declarations/instructions/adminWithdrawUsh.d.ts +5 -0
  6. package/declarations/instructions/createCompoundStakingPool.d.ts +5 -0
  7. package/declarations/instructions/createCompoundStakingPoolPosition.d.ts +6 -0
  8. package/declarations/instructions/createReferralAccount.d.ts +2 -2
  9. package/declarations/instructions/depositCompoundStakingPoolPosition.d.ts +6 -0
  10. package/declarations/instructions/depositRewardsToCompoundPool.d.ts +6 -0
  11. package/declarations/instructions/referralClaimFees.d.ts +2 -2
  12. package/declarations/instructions/setCompoundPoolActive.d.ts +5 -0
  13. package/declarations/instructions/setDelegateWallet.d.ts +5 -0
  14. package/declarations/instructions/withdrawCompoundStakingPoolPosition.d.ts +6 -0
  15. package/declarations/utils/getLinkedListAccounts.d.ts +2 -1
  16. package/lib/Constants.js +28 -1
  17. package/lib/idl/vault.js +741 -57
  18. package/lib/index.js +9 -0
  19. package/lib/instructions/adminWithdrawCol.js +60 -0
  20. package/lib/instructions/adminWithdrawUsh.js +57 -0
  21. package/lib/instructions/createCompoundStakingPool.js +58 -0
  22. package/lib/instructions/createCompoundStakingPoolPosition.js +56 -0
  23. package/lib/instructions/createReferralAccount.js +6 -3
  24. package/lib/instructions/depositCompoundStakingPoolPosition.js +55 -0
  25. package/lib/instructions/depositRewardsToCompoundPool.js +64 -0
  26. package/lib/instructions/liquidateVault.js +7 -1
  27. package/lib/instructions/loanVault.js +7 -1
  28. package/lib/instructions/referralClaimFees.js +6 -3
  29. package/lib/instructions/setCompoundPoolActive.js +43 -0
  30. package/lib/instructions/setDelegateWallet.js +43 -0
  31. package/lib/instructions/withdrawCompoundStakingPoolPosition.js +64 -0
  32. package/lib/utils/getLinkedListAccounts.js +5 -21
  33. package/package.json +5 -2
  34. package/src/Constants.ts +30 -0
  35. package/src/idl/vault.ts +3638 -2270
  36. package/src/index.ts +9 -2
  37. package/src/instructions/adminWithdrawCol.ts +87 -0
  38. package/src/instructions/adminWithdrawUsh.ts +78 -0
  39. package/src/instructions/createCompoundStakingPool.ts +63 -0
  40. package/src/instructions/createCompoundStakingPoolPosition.ts +85 -0
  41. package/src/instructions/createReferralAccount.ts +13 -9
  42. package/src/instructions/createStakingPool.ts +2 -9
  43. package/src/instructions/depositCompoundStakingPoolPosition.ts +78 -0
  44. package/src/instructions/depositRewardsToCompoundPool.ts +110 -0
  45. package/src/instructions/liquidateVault.ts +37 -27
  46. package/src/instructions/loanVault.ts +34 -21
  47. package/src/instructions/referralClaimFees.ts +16 -10
  48. package/src/instructions/setCompoundPoolActive.ts +51 -0
  49. package/src/instructions/setDelegateWallet.ts +51 -0
  50. package/src/instructions/withdrawCompoundStakingPoolPosition.ts +100 -0
  51. package/src/utils/getLinkedListAccounts.ts +6 -20
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.withdrawCompoundStakingPoolPositionInstruction = exports.withdrawCompoundStakingPoolPosition = void 0;
16
+ const anchor_1 = require("@project-serum/anchor");
17
+ const spl_token_1 = require("@solana/spl-token");
18
+ const web3_js_1 = require("@solana/web3.js");
19
+ const Constants_1 = require("../Constants");
20
+ const Errors_1 = require("../utils/Errors");
21
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
22
+ function withdrawCompoundStakingPoolPosition(program, provider, payer, poolPositionPublicKey, stakedTokenMintPublicKey, claimAmount, overrideStartTime) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const [poolPublickey, poolBump] = yield (0, Constants_1.getCompoundPoolPublicKeyForMint)(program.programId, stakedTokenMintPublicKey);
25
+ const poolPosition = yield (0, Constants_1.getCompoundPoolPositionAddress)(program.programId, poolPublickey, payer.publicKey);
26
+ const payerAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, stakedTokenMintPublicKey, payer.publicKey);
27
+ const transaction = new web3_js_1.Transaction().add(yield withdrawCompoundStakingPoolPositionInstruction(program, payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, new anchor_1.BN(claimAmount), overrideStartTime));
28
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
29
+ return payerAssociatedTokenAccount.address;
30
+ });
31
+ }
32
+ exports.withdrawCompoundStakingPoolPosition = withdrawCompoundStakingPoolPosition;
33
+ function withdrawCompoundStakingPoolPositionInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, claimAmount, overrideStartTime) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)(program.programId);
36
+ const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)(program.programId);
37
+ const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)(program.programId);
38
+ const [poolPublickey, poolBump] = yield (0, Constants_1.getCompoundPoolPublicKeyForMint)(program.programId, stakedTokenMintPublicKey);
39
+ const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, poolPublickey, stakedTokenMintPublicKey);
40
+ const poolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, poolPublickey, ushMintPublickey);
41
+ const payerAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, payerPublicKey, stakedTokenMintPublicKey);
42
+ const payerAssociatedHedgeAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, payerPublicKey, hedgeMintPublickey);
43
+ const payerAssociatedUshAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, payerPublicKey, ushMintPublickey);
44
+ const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(program.programId, vaultSystemStatePublicKey, hedgeMintPublickey);
45
+ return yield program.methods
46
+ .withdrawCompoundStakingPoolPosition(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), // override current time
47
+ claimAmount //amount to be claimed
48
+ )
49
+ .accounts({
50
+ payer: payerPublicKey,
51
+ vaultSystemState: vaultSystemStatePublicKey,
52
+ pool: poolPublickey,
53
+ poolPosition: poolPositionPublicKey,
54
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
55
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
56
+ hedgeMint: hedgeMintPublickey,
57
+ stakedTokenMint: stakedTokenMintPublicKey,
58
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
59
+ systemProgram: web3_js_1.SystemProgram.programId,
60
+ })
61
+ .instruction();
62
+ });
63
+ }
64
+ exports.withdrawCompoundStakingPoolPositionInstruction = withdrawCompoundStakingPoolPositionInstruction;
@@ -14,7 +14,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getLinkedListAccounts = void 0;
16
16
  const anchor_1 = require("@project-serum/anchor");
17
- const web3_js_1 = require("@solana/web3.js");
18
17
  const underscore_1 = __importDefault(require("underscore"));
19
18
  const decimal_js_1 = __importDefault(require("decimal.js"));
20
19
  const VaultAccount_1 = require("../state/VaultAccount");
@@ -33,8 +32,9 @@ const VaultType_1 = __importDefault(require("../state/VaultType"));
33
32
  * @param {boolean} redeem - True if vault is going to be redeemed fully
34
33
  * @param {boolean} liquidate - True if vault is going to be liquidated fully
35
34
  * @param {VaultAccount[]} cachedVaults - Optional list of cached vaults. Saves a request to the on-chain data.
35
+ * @param {VaultAccount} preLoadedVault - Optional vault to use as target vault. Overrides the need to load the vault from on-chain data.
36
36
  */
37
- function getLinkedListAccounts(program, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, withdrawAmount, loanAmount, repayAmount, redeem, liquidate, cachedVaults) {
37
+ function getLinkedListAccounts(program, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, withdrawAmount, loanAmount, repayAmount, redeem, liquidate, cachedVaults, preLoadedVault) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
39
  const vaultTypeRaw = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
40
40
  const vaultType = new VaultType_1.default(vaultTypeRaw, vaultTypeAccountPublicKey);
@@ -43,25 +43,9 @@ function getLinkedListAccounts(program, vaultTypeAccountPublicKey, vaultPublicKe
43
43
  let oldSmallerPublicKey = vaultPublicKey;
44
44
  let newSmallerPublicKey = vaultPublicKey;
45
45
  let newLargerPublicKey = vaultPublicKey;
46
- const thisVaultData = yield program.account.vault.fetch(vaultPublicKey)
47
- .catch((e) => {
48
- console.log('Error fetching vault account. Assuming it does not exist yet');
49
- return {
50
- vaultOwner: new web3_js_1.PublicKey('11111111111111111111111111111111'),
51
- pdaSalt: 'foo',
52
- deposited: new anchor_1.BN(0),
53
- denormalizedDebt: new anchor_1.BN(0),
54
- vaultNumber: new anchor_1.BN(0),
55
- debtProductSnapshotBytes: new anchor_1.BN(1),
56
- collateralAccumulatorSnapshotBytes: new anchor_1.BN(1),
57
- vaultTypeName: vaultType.name,
58
- nextVaultToRedeem: new web3_js_1.PublicKey('11111111111111111111111111111111'),
59
- vaultStatus: { open: true },
60
- vaultType: vaultTypeAccountPublicKey
61
- };
62
- });
63
- // const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
64
- const thisVault = new VaultAccount_1.VaultAccount(thisVaultData, vaultPublicKey);
46
+ const thisVault = preLoadedVault
47
+ ? preLoadedVault
48
+ : new VaultAccount_1.VaultAccount(yield program.account.vault.fetch(vaultPublicKey), vaultPublicKey);
65
49
  // Load all the vaults
66
50
  let vaults;
67
51
  if (cachedVaults) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.2.27",
3
+ "version": "0.2.31",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "types": "declarations/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "@rollup/plugin-typescript": "^8.3.0",
20
20
  "@solana/buffer-layout": "^4.0.0",
21
21
  "@solana/spl-token": "^0.2.0",
22
- "@solana/web3.js": "^1.37.0",
22
+ "@solana/web3.js": "^1.63.1",
23
23
  "@types/bn.js": "^5.1.0",
24
24
  "@types/underscore": "^1.11.4",
25
25
  "@types/uuid": "^8.3.4",
@@ -32,5 +32,8 @@
32
32
  "typescript": "^4.5.5",
33
33
  "underscore": "^1.13.2",
34
34
  "uuid": "^8.3.2"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node-fetch": "^2.6.2"
35
38
  }
36
39
  }
package/src/Constants.ts CHANGED
@@ -71,6 +71,21 @@ export async function getUserReferralAccountPublicKey(
71
71
  return publicKey
72
72
  }
73
73
 
74
+ /**
75
+ *
76
+ * @returns The compound pool position address for the given user
77
+ */
78
+ export async function getCompoundPoolPositionAddress(
79
+ hedgeProgramId: PublicKey,
80
+ poolPublicKey: PublicKey,
81
+ ownerPublicKey: PublicKey
82
+ ): Promise<PublicKey> {
83
+ const strToEncode = poolPublicKey.toBuffer()
84
+ const strToEncode2 = ownerPublicKey.toBuffer()
85
+ const [compoundPoolPosition] = await PublicKey.findProgramAddress([strToEncode, strToEncode2], hedgeProgramId)
86
+ return compoundPoolPosition
87
+ }
88
+
74
89
  /**
75
90
  *
76
91
  * @returns The HDG mint public key
@@ -94,6 +109,21 @@ export async function getPoolPublicKeyForMint(
94
109
  return [publicKey, bump, strToEncode]
95
110
  }
96
111
 
112
+ /**
113
+ * Get the public key for any compound staking pool
114
+ *
115
+ * @param mintPublicKey Staked collateral mint public key
116
+ * @returns the public key for that compound staking pool
117
+ */
118
+ export async function getCompoundPoolPublicKeyForMint(
119
+ hedgeProgramId: PublicKey,
120
+ mintPublicKey: PublicKey
121
+ ): Promise<[PublicKey, number]> {
122
+ const strToEncode = mintPublicKey.toBuffer()
123
+ const [publicKey, bump] = await PublicKey.findProgramAddress([enc.encode('CompoundPoolV1'), strToEncode], hedgeProgramId)
124
+ return [publicKey, bump]
125
+ }
126
+
97
127
  /**
98
128
  *
99
129
  * @param collateralType String name of the collateral type (must be 16 chars)