hedge-web3 0.1.50 → 0.2.2

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 (77) hide show
  1. package/declarations/Constants.d.ts +2 -1
  2. package/declarations/idl/vault.d.ts +192 -41
  3. package/declarations/index.d.ts +1 -0
  4. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +1 -1
  5. package/declarations/instructions/claimStakingPoolPosition.d.ts +1 -1
  6. package/declarations/instructions/closeClaimedLiquidationPoolPosition.d.ts +5 -0
  7. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
  8. package/declarations/instructions/createStakingPool.d.ts +1 -1
  9. package/declarations/instructions/createVault.d.ts +1 -1
  10. package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
  11. package/declarations/instructions/depositStakingPool.d.ts +1 -1
  12. package/declarations/instructions/depositVault.d.ts +1 -1
  13. package/declarations/instructions/initHedgeFoundation.d.ts +1 -1
  14. package/declarations/instructions/liquidateVault.d.ts +2 -2
  15. package/declarations/instructions/loanVault.d.ts +1 -1
  16. package/declarations/instructions/redeemVault.d.ts +1 -1
  17. package/declarations/instructions/refreshOraclePrice.d.ts +3 -3
  18. package/declarations/instructions/repayVault.d.ts +1 -1
  19. package/declarations/instructions/setHalted.d.ts +1 -1
  20. package/declarations/instructions/updateVaultType.d.ts +4 -1
  21. package/declarations/instructions/withdrawStakingPool.d.ts +1 -1
  22. package/declarations/instructions/withdrawVault.d.ts +1 -1
  23. package/declarations/state/VaultAccount.d.ts +9 -6
  24. package/declarations/utils/getLinkedListAccounts.d.ts +1 -1
  25. package/lib/Constants.js +3 -2
  26. package/lib/idl/vault.js +192 -41
  27. package/lib/index.js +1 -0
  28. package/lib/instructions/claimLiquidationPoolPosition.js +5 -1
  29. package/lib/instructions/claimStakingPoolPosition.js +5 -1
  30. package/lib/instructions/closeClaimedLiquidationPoolPosition.js +38 -0
  31. package/lib/instructions/closeLiquidationPoolPosition.js +5 -1
  32. package/lib/instructions/createStakingPool.js +5 -1
  33. package/lib/instructions/createVault.js +5 -1
  34. package/lib/instructions/depositLiquidationPool.js +5 -1
  35. package/lib/instructions/depositStakingPool.js +5 -1
  36. package/lib/instructions/depositVault.js +12 -8
  37. package/lib/instructions/initHedgeFoundation.js +5 -1
  38. package/lib/instructions/initHedgeFoundationTokens.js +5 -1
  39. package/lib/instructions/liquidateVault.js +10 -8
  40. package/lib/instructions/loanVault.js +9 -6
  41. package/lib/instructions/redeemVault.js +9 -6
  42. package/lib/instructions/refreshOraclePrice.js +8 -3
  43. package/lib/instructions/repayVault.js +9 -6
  44. package/lib/instructions/setHalted.js +5 -1
  45. package/lib/instructions/updateVaultType.js +9 -2
  46. package/lib/instructions/withdrawStakingPool.js +5 -1
  47. package/lib/instructions/withdrawVault.js +9 -6
  48. package/lib/state/VaultAccount.js +28 -32
  49. package/lib/utils/getLinkedListAccounts.js +54 -57
  50. package/package.json +1 -1
  51. package/src/Constants.ts +2 -1
  52. package/src/idl/vault.ts +384 -82
  53. package/src/index.ts +1 -0
  54. package/src/instructions/claimLiquidationPoolPosition.ts +3 -2
  55. package/src/instructions/claimStakingPoolPosition.ts +3 -2
  56. package/src/instructions/closeClaimedLiquidationPoolPosition.ts +31 -0
  57. package/src/instructions/closeLiquidationPoolPosition.ts +3 -2
  58. package/src/instructions/createStakingPool.ts +3 -2
  59. package/src/instructions/createVault.ts +3 -2
  60. package/src/instructions/depositLiquidationPool.ts +3 -2
  61. package/src/instructions/depositStakingPool.ts +3 -2
  62. package/src/instructions/depositVault.ts +11 -11
  63. package/src/instructions/initHedgeFoundation.ts +3 -2
  64. package/src/instructions/initHedgeFoundationTokens.ts +2 -1
  65. package/src/instructions/liquidateVault.ts +11 -10
  66. package/src/instructions/loanVault.ts +11 -9
  67. package/src/instructions/redeemVault.ts +7 -7
  68. package/src/instructions/refreshOraclePrice.ts +9 -6
  69. package/src/instructions/repayVault.ts +7 -7
  70. package/src/instructions/setHalted.ts +3 -2
  71. package/src/instructions/updateVaultType.ts +10 -2
  72. package/src/instructions/withdrawStakingPool.ts +5 -3
  73. package/src/instructions/withdrawVault.ts +7 -7
  74. package/src/state/VaultAccount.ts +37 -32
  75. package/src/state/VaultType.ts +62 -0
  76. package/src/utils/getLinkedListAccounts.ts +61 -60
  77. package/src/utils/sendAndConfirmWithDebug.ts +27 -0
@@ -15,19 +15,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getLinkedListAccounts = void 0;
16
16
  const underscore_1 = __importDefault(require("underscore"));
17
17
  const Constants_1 = require("../Constants");
18
- const HedgeDecimal_1 = require("../HedgeDecimal");
19
18
  const VaultAccount_1 = require("../state/VaultAccount");
20
19
  const decimal_js_1 = __importDefault(require("decimal.js"));
21
20
  const bs58_1 = __importDefault(require("bs58"));
21
+ const VaultType_1 = __importDefault(require("../state/VaultType"));
22
22
  function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate, cachedVaults) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
- console.log('Getting getLinkedListAccounts');
25
- const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
24
+ const vaultTypeRaw = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
25
+ const vaultType = new VaultType_1.default(vaultTypeRaw, vaultTypeAccountPublicKey);
26
+ const DEBUG = false;
26
27
  // Default for null is the vault itself, so set them all to this vault
27
28
  let oldSmallerPublicKey = vaultPublicKey;
28
29
  let newSmallerPublicKey = vaultPublicKey;
29
30
  let newLargerPublicKey = vaultPublicKey;
30
31
  const thisVaultData = yield program.account.vault.fetch(vaultPublicKey);
32
+ // const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
31
33
  const thisVault = new VaultAccount_1.VaultAccount(thisVaultData, vaultPublicKey);
32
34
  // Load all the vaults
33
35
  let vaults;
@@ -35,33 +37,20 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
35
37
  vaults = cachedVaults;
36
38
  }
37
39
  else {
38
- // let allVaults = cachedVaults
39
- // ? cachedVaults
40
- // : (await program.account.vault
41
- // .all([
42
- // {
43
- // memcmp: { bytes: vaultTypeAccount.collateralType, offset: 8 + 32 + 8 },
44
- // },
45
- // ])
46
- // .catch((error) => {
47
- // console.log('error', error)
48
- // })) || []
49
- // // Load them into our account objects
50
- // vaults = allVaults.map((vault) => {
51
- // return new VaultAccount(vault.account, vault.publicKey)
52
- // })
53
- vaults = yield getMiniVaults(program, vaultTypeAccount.collateralType);
40
+ vaults = yield getMiniVaults(program, vaultTypeAccountPublicKey);
41
+ }
42
+ if (DEBUG) {
43
+ console.log('Vault count found:', vaults.length);
44
+ console.log('First Vault', vaults[0]);
54
45
  }
55
- console.log('Vault count found:', vaults.length);
56
- console.log('First Vault', vaults[0]);
57
46
  // Filter out the accounts that are not open
58
- // TODO filter on vault status. Or we enable people to "close out" empty vaults
47
+ // TODO filter on vault status. Or we enable people to "close out" empty vaults
59
48
  // vaults = _.filter(vaults, (vault) => {
60
49
  // return vault.vaultStatus === 'open'
61
50
  // })
62
51
  // Remove any vaults with no debt or collateral
63
52
  vaults = underscore_1.default.filter(vaults, (vault) => {
64
- return vault.denormalizedDebt.greaterThan(0) && vault.deposited.greaterThan(0);
53
+ return vault.denormalizedDebt > 0 && vault.deposited > 0;
65
54
  });
66
55
  // Sort them
67
56
  vaults.sort(sortVaults);
@@ -76,6 +65,13 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
76
65
  if (indexBefore > 0) {
77
66
  oldSmallerPublicKey = vaults[indexBefore - 1].publicKey;
78
67
  }
68
+ if (DEBUG) {
69
+ // Pretty print the list again
70
+ console.log('Sorted open vaults. Index Before: ', indexBefore);
71
+ console.log(vaults.map((vault) => {
72
+ return vault.toString(vaultPublicKey);
73
+ }));
74
+ }
79
75
  // Pretty print all the vaults before the operation
80
76
  // console.log('Sorted open vaults BEFORE at index:', indexBefore)
81
77
  // let correctOrderBefore = true
@@ -106,9 +102,8 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
106
102
  // Now that we know it's def in the list, iterate the list and update
107
103
  // this vault with the operation we're going to apply
108
104
  const newNormalizedDebt = new decimal_js_1.default(loanAmount);
109
- const vaultTypeCompoundedInterest = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccount.cumulativeRate.toString());
110
- vaults[indexBefore].updateDebtAndCollateral(vaultTypeAccount);
111
- vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest);
105
+ vaults[indexBefore].updateDebtAndCollateral(vaultType);
106
+ vaults[indexBefore].addDebt(newNormalizedDebt, vaultType);
112
107
  vaults[indexBefore].addDeposit(depositAmount);
113
108
  if (liquidate) {
114
109
  vaults[indexBefore].liquidate();
@@ -116,16 +111,11 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
116
111
  if (redeem) {
117
112
  vaults[indexBefore].redeem();
118
113
  }
119
- if (vaults[indexBefore].denormalizedDebt.isZero()) {
114
+ if (vaults[indexBefore].denormalizedDebt === 0) {
120
115
  vaults.splice(indexBefore, 1);
121
116
  }
122
117
  // Sort it again since we've changed one vault
123
118
  vaults = vaults.sort(sortVaults);
124
- // Pretty print the list again
125
- // console.log('Sorted open vaults with new debt added at index: ', indexAfter)
126
- // console.log(vaults.map((vault) => {
127
- // return vault.toString(vaultPublicKey)
128
- // }))
129
119
  // Search for the vaults new position
130
120
  let indexAfter = -1;
131
121
  vaults.forEach((vault, index) => {
@@ -133,16 +123,17 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
133
123
  indexAfter = index;
134
124
  }
135
125
  });
136
- // New list with vault
137
- // console.log('New list with vault now at index:', indexAfter)
138
- // console.log(
139
- // vaults.map((vault) => {
140
- // return vault.toString(vaultPublicKey)
141
- // })
142
- // )
143
- // Print where it moved from / to
144
- console.log('Index Before', indexBefore);
145
- console.log('Index After', indexAfter);
126
+ if (DEBUG) {
127
+ // New list with vault
128
+ console.log('New list with vault now at index:', indexAfter);
129
+ console.log(vaults.map((vault) => {
130
+ return vault.toString(vaultPublicKey);
131
+ }));
132
+ }
133
+ if (DEBUG) {
134
+ // Print where it moved from / to
135
+ console.log('Index After', indexAfter);
136
+ }
146
137
  // Save references to the new left and right
147
138
  if (indexAfter > 0) {
148
139
  newSmallerPublicKey = vaults[indexAfter - 1].publicKey;
@@ -150,10 +141,12 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
150
141
  if (indexAfter < vaults.length - 1 && indexAfter >= 0) {
151
142
  newLargerPublicKey = vaults[indexAfter + 1].publicKey;
152
143
  }
153
- // Print out the new left/right
154
- // console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
155
- // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
156
- // console.log('newLargerPublicKey', newLargerPublicKey.toString())
144
+ if (DEBUG) {
145
+ // Print out the new left/right
146
+ console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString());
147
+ console.log('newSmallerPublicKey', newSmallerPublicKey.toString());
148
+ console.log('newLargerPublicKey', newLargerPublicKey.toString());
149
+ }
157
150
  return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults];
158
151
  });
159
152
  }
@@ -161,17 +154,14 @@ exports.getLinkedListAccounts = getLinkedListAccounts;
161
154
  // Sort function we can use to sort the vaults
162
155
  // Sorted by collateral ratio. If two are the same, newer vault first
163
156
  function sortVaults(a, b) {
164
- const aRatio = a.deposited.floor().div(a.denormalizedDebt.floor());
165
- const bRatio = b.deposited.floor().div(b.denormalizedDebt.floor());
166
- if (aRatio.equals(bRatio)) {
157
+ const aRatio = a.deposited / a.denormalizedDebt;
158
+ const bRatio = b.deposited / b.denormalizedDebt;
159
+ if (aRatio === bRatio) {
167
160
  return b.vaultNumber - a.vaultNumber;
168
161
  }
169
- if (aRatio.greaterThan(bRatio)) {
170
- return 1;
171
- }
172
- return -1;
162
+ return aRatio - bRatio;
173
163
  }
174
- function getMiniVaults(program, collateralType) {
164
+ function getMiniVaults(program, vaultTypePublicKey) {
175
165
  return __awaiter(this, void 0, void 0, function* () {
176
166
  const filters = [
177
167
  // Filter for Vault Accounts
@@ -179,13 +169,20 @@ function getMiniVaults(program, collateralType) {
179
169
  // @ts-ignore
180
170
  memcmp: program.account.vault.coder.accounts.memcmp(program.account.vault._idlAccount.name),
181
171
  },
182
- // Filter for Vaults with this collateral type
172
+ // Filter for Vaults that are open
183
173
  {
184
174
  memcmp: {
185
- bytes: bs58_1.default.encode(VaultAccount_1.VaultAccount.getBufferForString(collateralType)),
175
+ bytes: bs58_1.default.encode(Buffer.from([1])),
186
176
  offset: 8 + 32 + 24,
187
177
  },
188
178
  },
179
+ // Filter for Vaults with this collateral type
180
+ {
181
+ memcmp: {
182
+ bytes: vaultTypePublicKey.toString(),
183
+ offset: 8 + 32 + 24 + 1,
184
+ },
185
+ },
189
186
  ];
190
187
  const allAccounts = yield program.provider.connection.getProgramAccounts(Constants_1.HEDGE_PROGRAM_PUBLICKEY, {
191
188
  filters: filters,
@@ -196,7 +193,7 @@ function getMiniVaults(program, collateralType) {
196
193
  length: 24,
197
194
  },
198
195
  });
199
- return allAccounts.map(vaultData => {
196
+ return allAccounts.map((vaultData) => {
200
197
  return VaultAccount_1.VaultAccount.FromMiniSlice(vaultData.account.data, vaultData.pubkey);
201
198
  });
202
199
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.1.50",
3
+ "version": "0.2.2",
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
@@ -4,12 +4,13 @@ import {
4
4
  } from '@solana/spl-token'
5
5
  import { PublicKey } from '@solana/web3.js'
6
6
 
7
- export const HEDGE_PROGRAM_ID = 'HDG4FDos8fyrB79qMCe98gaFjNEy5kpxGNjMc5V8M6TJ'
7
+ export const HEDGE_PROGRAM_ID = 'HedgeEohwU6RqokrvPU4Hb6XKPub8NuKbnPmY7FoMMtN'
8
8
  export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
9
9
 
10
10
  export const CHAINLINK_SOL_USD_ID =
11
11
  'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
12
12
  export const CHAINLINK_SOL_USD_PUBLICKEY = new PublicKey(CHAINLINK_SOL_USD_ID)
13
+ export const CHAINLINK_PROGRAM_ID = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny";
13
14
 
14
15
  const enc = new TextEncoder()
15
16