hedge-web3 0.1.40 → 0.1.43

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.
@@ -14,11 +14,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getLinkedListAccounts = void 0;
16
16
  const underscore_1 = __importDefault(require("underscore"));
17
+ const Constants_1 = require("../Constants");
17
18
  const HedgeDecimal_1 = require("../HedgeDecimal");
18
19
  const VaultAccount_1 = require("../state/VaultAccount");
19
20
  const decimal_js_1 = __importDefault(require("decimal.js"));
21
+ const bs58_1 = __importDefault(require("bs58"));
20
22
  function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate, cachedVaults) {
21
23
  return __awaiter(this, void 0, void 0, function* () {
24
+ console.log('Getting getLinkedListAccounts');
22
25
  const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
23
26
  // Default for null is the vault itself, so set them all to this vault
24
27
  let oldSmallerPublicKey = vaultPublicKey;
@@ -32,29 +35,33 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
32
35
  vaults = cachedVaults;
33
36
  }
34
37
  else {
35
- let allVaults = cachedVaults
36
- ? cachedVaults
37
- : (yield program.account.vault
38
- .all([
39
- // {
40
- // memcmp: { bytes: bs58.encode(inputCollateralType), offset: 8 + 32 + 8 },
41
- // },
42
- ])
43
- .catch((error) => {
44
- console.log('error', error);
45
- })) || [];
46
- // Load them into our account objects
47
- vaults = allVaults.map((vault) => {
48
- return new VaultAccount_1.VaultAccount(vault.account, vault.publicKey);
49
- });
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);
50
54
  }
51
- // Filter out the account that are not the same vault type
52
- vaults = underscore_1.default.filter(vaults, (vault) => {
53
- return vault.collateralType === vaultTypeAccount.collateralType;
54
- });
55
+ console.log('Vault count found:', vaults.length);
56
+ console.log('First Vault', vaults[0]);
55
57
  // Filter out the accounts that are not open
58
+ // TODO filter on vault status. Or we enable people to "close out" empty vaults
59
+ // vaults = _.filter(vaults, (vault) => {
60
+ // return vault.vaultStatus === 'open'
61
+ // })
62
+ // Remove any vaults with no debt or collateral
56
63
  vaults = underscore_1.default.filter(vaults, (vault) => {
57
- return vault.vaultStatus === 'open';
64
+ return vault.denormalizedDebt > 0 && vault.deposited > 0;
58
65
  });
59
66
  // Sort them
60
67
  vaults.sort(sortVaults);
@@ -71,28 +78,31 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
71
78
  }
72
79
  // Pretty print all the vaults before the operation
73
80
  // console.log('Sorted open vaults BEFORE at index:', indexBefore)
74
- let correctOrderBefore = true;
75
- for (let i = 0; i < vaults.length; i++) {
76
- if (i < vaults.length - 1) {
77
- // Verify i points to i+1
78
- if (vaults[i].nextVaultToRedeem.toString() !== vaults[i + 1].publicKey.toString()) {
79
- correctOrderBefore = false;
80
- console.log("A vault was found OUT OF ORDER!!", vaults[i]);
81
- }
82
- }
83
- }
84
- if (correctOrderBefore) {
85
- console.log(`Verfied the on-chain order of vault type:`, vaultTypeAccount.collateralType);
86
- }
87
- else {
88
- throw new Error("On-Chian vaults not in order!");
89
- }
81
+ // let correctOrderBefore = true
82
+ // for (let i = 0; i < vaults.length; i++) {
83
+ // if (i < vaults.length - 1) {
84
+ // // Verify i points to i+1
85
+ // if (vaults[i].nextVaultToRedeem.toString() !== vaults[i + 1].publicKey.toString()) {
86
+ // correctOrderBefore = false
87
+ // console.log('A vault was found OUT OF ORDER!!', vaults[i])
88
+ // }
89
+ // }
90
+ // }
91
+ // if (correctOrderBefore) {
92
+ // console.log(`Verfied the on-chain order of vault type:`, vaultTypeAccount.collateralType)
93
+ // } else {
94
+ // throw new Error('On-Chian vaults not in order!')
95
+ // }
90
96
  // If it wasn't in the list, add it now
91
97
  if (indexBefore < 0) {
92
98
  // console.log('Was not in the list before. Adding now.')
93
99
  vaults.push(thisVault);
94
100
  indexBefore = vaults.length - 1;
95
101
  }
102
+ else {
103
+ // Copy in the vault so we have the full data (needed to run updateDebtAndCollateral)
104
+ vaults[indexBefore] = thisVault;
105
+ }
96
106
  // Now that we know it's def in the list, iterate the list and update
97
107
  // this vault with the opeation we're going to apply
98
108
  const newNormalizedDebt = new decimal_js_1.default(loanAmount);
@@ -131,8 +141,8 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
131
141
  // })
132
142
  // )
133
143
  // Print where it moved from / to
134
- // console.log('Index Before', indexBefore)
135
- // console.log('Index After', indexAfter)
144
+ console.log('Index Before', indexBefore);
145
+ console.log('Index After', indexAfter);
136
146
  // Save references to the new left and right
137
147
  if (indexAfter > 0) {
138
148
  newSmallerPublicKey = vaults[indexAfter - 1].publicKey;
@@ -158,3 +168,33 @@ function sortVaults(a, b) {
158
168
  }
159
169
  return aRatio - bRatio;
160
170
  }
171
+ function getMiniVaults(program, collateralType) {
172
+ return __awaiter(this, void 0, void 0, function* () {
173
+ const filters = [
174
+ // Filter for Vault Accounts
175
+ {
176
+ // @ts-ignore
177
+ memcmp: program.account.vault.coder.accounts.memcmp(program.account.vault._idlAccount.name),
178
+ },
179
+ // Filter for Vaults with this collateral type
180
+ {
181
+ memcmp: {
182
+ bytes: bs58_1.default.encode(VaultAccount_1.VaultAccount.getBufferForString(collateralType)),
183
+ offset: 8 + 32 + 24,
184
+ },
185
+ },
186
+ ];
187
+ const allAccounts = yield program.provider.connection.getProgramAccounts(Constants_1.HEDGE_PROGRAM_PUBLICKEY, {
188
+ filters: filters,
189
+ // Slice the data only to grab the 3 u64's of size 8 bytes each
190
+ dataSlice: {
191
+ // See programs/hedge-vault/src/account_data/vault.rs for struct layout
192
+ offset: 8 + 32,
193
+ length: 24,
194
+ },
195
+ });
196
+ return allAccounts.map(vaultData => {
197
+ return VaultAccount_1.VaultAccount.FromMiniSlice(vaultData.account.data, vaultData.pubkey);
198
+ });
199
+ });
200
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.1.40",
3
+ "version": "0.1.43",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "types": "declarations/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "author": "Chris Coudron <coudron@hedge.so>",
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
- "@project-serum/anchor": "^0.24.2",
17
+ "@project-serum/anchor": "0.24.2",
18
18
  "@project-serum/serum": "^0.13.61",
19
19
  "@rollup/plugin-typescript": "^8.3.0",
20
20
  "@solana/buffer-layout": "^4.0.0",
@@ -24,6 +24,7 @@
24
24
  "@types/underscore": "^1.11.4",
25
25
  "@types/uuid": "^8.3.4",
26
26
  "bn.js": "^5.2.0",
27
+ "bs58": "^5.0.0",
27
28
  "decimal.js": "^10.3.1",
28
29
  "rollup": "^2.62.0",
29
30
  "ts-standard": "^11.0.0",
package/src/idl/vault.ts CHANGED
@@ -1560,7 +1560,7 @@ export type Vault = {
1560
1560
  ]
1561
1561
  },
1562
1562
  {
1563
- "name": "setVaultTypeStatus",
1563
+ "name": "updateVaultType",
1564
1564
  "accounts": [
1565
1565
  {
1566
1566
  "name": "payer",
@@ -1576,12 +1576,77 @@ export type Vault = {
1576
1576
  "name": "vaultType",
1577
1577
  "isMut": true,
1578
1578
  "isSigner": false
1579
+ },
1580
+ {
1581
+ "name": "oracleInfoAccount",
1582
+ "isMut": true,
1583
+ "isSigner": false
1584
+ }
1585
+ ],
1586
+ "args": [
1587
+ {
1588
+ "name": "config",
1589
+ "type": {
1590
+ "defined": "VaultTypeConfig"
1591
+ }
1592
+ }
1593
+ ]
1594
+ },
1595
+ {
1596
+ "name": "fixVaultRedeemPointer",
1597
+ "accounts": [
1598
+ {
1599
+ "name": "payer",
1600
+ "isMut": true,
1601
+ "isSigner": true
1602
+ },
1603
+ {
1604
+ "name": "vaultSystemState",
1605
+ "isMut": true,
1606
+ "isSigner": false
1607
+ },
1608
+ {
1609
+ "name": "vaultAccount",
1610
+ "isMut": true,
1611
+ "isSigner": false
1612
+ }
1613
+ ],
1614
+ "args": []
1615
+ },
1616
+ {
1617
+ "name": "adminSetRedeemPointer",
1618
+ "accounts": [
1619
+ {
1620
+ "name": "payer",
1621
+ "isMut": true,
1622
+ "isSigner": true
1623
+ },
1624
+ {
1625
+ "name": "vaultSystemState",
1626
+ "isMut": true,
1627
+ "isSigner": false
1628
+ },
1629
+ {
1630
+ "name": "vaultAccount",
1631
+ "isMut": true,
1632
+ "isSigner": false
1633
+ },
1634
+ {
1635
+ "name": "vaultTypeAccount",
1636
+ "isMut": true,
1637
+ "isSigner": false
1579
1638
  }
1580
1639
  ],
1581
1640
  "args": [
1582
1641
  {
1583
- "name": "deprecated",
1642
+ "name": "settingHead",
1584
1643
  "type": "bool"
1644
+ },
1645
+ {
1646
+ "name": "pointerToSet",
1647
+ "type": {
1648
+ "option": "publicKey"
1649
+ }
1585
1650
  }
1586
1651
  ]
1587
1652
  }
@@ -1903,7 +1968,7 @@ export type Vault = {
1903
1968
  "type": {
1904
1969
  "array": [
1905
1970
  "u8",
1906
- 12
1971
+ 16
1907
1972
  ]
1908
1973
  }
1909
1974
  },
@@ -2243,6 +2308,56 @@ export type Vault = {
2243
2308
  ]
2244
2309
  }
2245
2310
  },
2311
+ {
2312
+ "name": "VaultTypeConfig",
2313
+ "type": {
2314
+ "kind": "struct",
2315
+ "fields": [
2316
+ {
2317
+ "name": "maxDebtExtended",
2318
+ "type": {
2319
+ "option": "u64"
2320
+ }
2321
+ },
2322
+ {
2323
+ "name": "minDebtPerVault",
2324
+ "type": {
2325
+ "option": "u64"
2326
+ }
2327
+ },
2328
+ {
2329
+ "name": "loanInitFee",
2330
+ "type": {
2331
+ "option": "u64"
2332
+ }
2333
+ },
2334
+ {
2335
+ "name": "oracleChainlink",
2336
+ "type": {
2337
+ "option": "publicKey"
2338
+ }
2339
+ },
2340
+ {
2341
+ "name": "oraclePyth",
2342
+ "type": {
2343
+ "option": "publicKey"
2344
+ }
2345
+ },
2346
+ {
2347
+ "name": "oracleSwitchboard",
2348
+ "type": {
2349
+ "option": "publicKey"
2350
+ }
2351
+ },
2352
+ {
2353
+ "name": "deprecated",
2354
+ "type": {
2355
+ "option": "bool"
2356
+ }
2357
+ }
2358
+ ]
2359
+ }
2360
+ },
2246
2361
  {
2247
2362
  "name": "PositionState",
2248
2363
  "type": {
@@ -2565,6 +2680,16 @@ export type Vault = {
2565
2680
  "code": 6021,
2566
2681
  "name": "VaultTypeDeprecated",
2567
2682
  "msg": "Vaults of this collateral type are deprecated. No new vaults can be created and no new debt will be issued for this collateral type."
2683
+ },
2684
+ {
2685
+ "code": 6022,
2686
+ "name": "UpdateLinkedListFailed",
2687
+ "msg": "There was an error updating the list of vaults. Please make sure the transaction is fresh."
2688
+ },
2689
+ {
2690
+ "code": 6023,
2691
+ "name": "UpdateVaultTypeBadMaxDebtExtended",
2692
+ "msg": "New Max debt extended value is less than the current debt!"
2568
2693
  }
2569
2694
  ]
2570
2695
  };
@@ -4131,7 +4256,7 @@ export const IDL: Vault = {
4131
4256
  ]
4132
4257
  },
4133
4258
  {
4134
- "name": "setVaultTypeStatus",
4259
+ "name": "updateVaultType",
4135
4260
  "accounts": [
4136
4261
  {
4137
4262
  "name": "payer",
@@ -4147,12 +4272,77 @@ export const IDL: Vault = {
4147
4272
  "name": "vaultType",
4148
4273
  "isMut": true,
4149
4274
  "isSigner": false
4275
+ },
4276
+ {
4277
+ "name": "oracleInfoAccount",
4278
+ "isMut": true,
4279
+ "isSigner": false
4280
+ }
4281
+ ],
4282
+ "args": [
4283
+ {
4284
+ "name": "config",
4285
+ "type": {
4286
+ "defined": "VaultTypeConfig"
4287
+ }
4288
+ }
4289
+ ]
4290
+ },
4291
+ {
4292
+ "name": "fixVaultRedeemPointer",
4293
+ "accounts": [
4294
+ {
4295
+ "name": "payer",
4296
+ "isMut": true,
4297
+ "isSigner": true
4298
+ },
4299
+ {
4300
+ "name": "vaultSystemState",
4301
+ "isMut": true,
4302
+ "isSigner": false
4303
+ },
4304
+ {
4305
+ "name": "vaultAccount",
4306
+ "isMut": true,
4307
+ "isSigner": false
4308
+ }
4309
+ ],
4310
+ "args": []
4311
+ },
4312
+ {
4313
+ "name": "adminSetRedeemPointer",
4314
+ "accounts": [
4315
+ {
4316
+ "name": "payer",
4317
+ "isMut": true,
4318
+ "isSigner": true
4319
+ },
4320
+ {
4321
+ "name": "vaultSystemState",
4322
+ "isMut": true,
4323
+ "isSigner": false
4324
+ },
4325
+ {
4326
+ "name": "vaultAccount",
4327
+ "isMut": true,
4328
+ "isSigner": false
4329
+ },
4330
+ {
4331
+ "name": "vaultTypeAccount",
4332
+ "isMut": true,
4333
+ "isSigner": false
4150
4334
  }
4151
4335
  ],
4152
4336
  "args": [
4153
4337
  {
4154
- "name": "deprecated",
4338
+ "name": "settingHead",
4155
4339
  "type": "bool"
4340
+ },
4341
+ {
4342
+ "name": "pointerToSet",
4343
+ "type": {
4344
+ "option": "publicKey"
4345
+ }
4156
4346
  }
4157
4347
  ]
4158
4348
  }
@@ -4474,7 +4664,7 @@ export const IDL: Vault = {
4474
4664
  "type": {
4475
4665
  "array": [
4476
4666
  "u8",
4477
- 12
4667
+ 16
4478
4668
  ]
4479
4669
  }
4480
4670
  },
@@ -4814,6 +5004,56 @@ export const IDL: Vault = {
4814
5004
  ]
4815
5005
  }
4816
5006
  },
5007
+ {
5008
+ "name": "VaultTypeConfig",
5009
+ "type": {
5010
+ "kind": "struct",
5011
+ "fields": [
5012
+ {
5013
+ "name": "maxDebtExtended",
5014
+ "type": {
5015
+ "option": "u64"
5016
+ }
5017
+ },
5018
+ {
5019
+ "name": "minDebtPerVault",
5020
+ "type": {
5021
+ "option": "u64"
5022
+ }
5023
+ },
5024
+ {
5025
+ "name": "loanInitFee",
5026
+ "type": {
5027
+ "option": "u64"
5028
+ }
5029
+ },
5030
+ {
5031
+ "name": "oracleChainlink",
5032
+ "type": {
5033
+ "option": "publicKey"
5034
+ }
5035
+ },
5036
+ {
5037
+ "name": "oraclePyth",
5038
+ "type": {
5039
+ "option": "publicKey"
5040
+ }
5041
+ },
5042
+ {
5043
+ "name": "oracleSwitchboard",
5044
+ "type": {
5045
+ "option": "publicKey"
5046
+ }
5047
+ },
5048
+ {
5049
+ "name": "deprecated",
5050
+ "type": {
5051
+ "option": "bool"
5052
+ }
5053
+ }
5054
+ ]
5055
+ }
5056
+ },
4817
5057
  {
4818
5058
  "name": "PositionState",
4819
5059
  "type": {
@@ -5136,6 +5376,16 @@ export const IDL: Vault = {
5136
5376
  "code": 6021,
5137
5377
  "name": "VaultTypeDeprecated",
5138
5378
  "msg": "Vaults of this collateral type are deprecated. No new vaults can be created and no new debt will be issued for this collateral type."
5379
+ },
5380
+ {
5381
+ "code": 6022,
5382
+ "name": "UpdateLinkedListFailed",
5383
+ "msg": "There was an error updating the list of vaults. Please make sure the transaction is fresh."
5384
+ },
5385
+ {
5386
+ "code": 6023,
5387
+ "name": "UpdateVaultTypeBadMaxDebtExtended",
5388
+ "msg": "New Max debt extended value is less than the current debt!"
5139
5389
  }
5140
5390
  ]
5141
5391
  };
package/src/index.ts CHANGED
@@ -17,7 +17,7 @@ export * from './instructions/refreshOraclePrice'
17
17
  export * from './instructions/initHedgeFoundation'
18
18
  export * from './instructions/initHedgeFoundationTokens'
19
19
  export * from './instructions/setHalted'
20
- export * from './instructions/setVaultTypeStatus'
20
+ export * from './instructions/updateVaultType'
21
21
 
22
22
  export * from './HedgeDecimal'
23
23
  export * from './Constants'
@@ -23,36 +23,67 @@ import { v4 as uuidv4 } from 'uuid'
23
23
  import { parseAnchorErrors } from '../utils/Errors'
24
24
  import { Vault } from 'idl/vault'
25
25
 
26
- export async function setVaultTypeStatus(
26
+ export interface VaultTypeConfig {
27
+ maxDebtExtended?: BN
28
+ minDebtPerVault?: BN
29
+ loanInitFee?: BN
30
+
31
+ oracleChainlink?: PublicKey
32
+ oraclePyth?: PublicKey
33
+ oracleSwitchboard?: PublicKey
34
+
35
+ deprecated?: boolean
36
+ }
37
+
38
+ export async function updateVaultType(
27
39
  program: Program<Vault>,
28
40
  provider: Provider,
29
41
  payer: Signer,
30
42
  vaultTypeAccount: PublicKey,
31
- deprecated: boolean
43
+ oracleInfoAccount: PublicKey,
44
+ config: VaultTypeConfig
32
45
  ): Promise<PublicKey> {
33
46
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
34
-
35
47
  const transaction = new Transaction().add(
36
- await setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, deprecated)
48
+ await updateVaultTypeStatusInstruction(
49
+ program,
50
+ vaultSystemStatePublicKey,
51
+ payer.publicKey,
52
+ vaultTypeAccount,
53
+ oracleInfoAccount,
54
+ config
55
+ )
37
56
  )
38
57
 
39
- await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
58
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer])
40
59
  return vaultSystemStatePublicKey
41
60
  }
42
61
 
43
- export async function setVaultTypeStatusInstruction(
62
+ export async function updateVaultTypeStatusInstruction(
44
63
  program: Program<Vault>,
45
64
  vaultSystemStatePublicKey: PublicKey,
46
65
  payerPublicKey: PublicKey,
47
66
  vaultTypeAccount: PublicKey,
48
- deprecated: boolean
67
+ oracleInfoAccount: PublicKey,
68
+ vaultTypeConfig: VaultTypeConfig
49
69
  ): Promise<TransactionInstruction> {
70
+ const config = {
71
+ maxDebtExtended: vaultTypeConfig.maxDebtExtended ?? null,
72
+ minDebtPerVault: vaultTypeConfig.minDebtPerVault ?? null,
73
+ loanInitFee: vaultTypeConfig.loanInitFee ?? null,
74
+ oracleChainlink: vaultTypeConfig.oracleChainlink ?? null,
75
+ oraclePyth: vaultTypeConfig.oraclePyth ?? null,
76
+ oracleSwitchboard: vaultTypeConfig.oracleSwitchboard ?? null,
77
+ deprecated: vaultTypeConfig.deprecated ?? null,
78
+ }
79
+
50
80
  return await program.methods
51
- .setVaultTypeStatus(deprecated)
81
+ .updateVaultType(config)
52
82
  .accounts({
53
83
  payer: payerPublicKey,
54
84
  vaultSystemState: vaultSystemStatePublicKey,
55
85
  vaultType: vaultTypeAccount,
86
+ oracleInfoAccount: oracleInfoAccount,
56
87
  })
57
88
  .instruction()
58
89
  }