hedge-web3 0.1.39 → 0.1.42

Sign up to get free protection for your applications and to get access to all the features.
@@ -1560,7 +1560,7 @@ export declare 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 declare 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
  }
@@ -2243,6 +2308,56 @@ export declare 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 declare 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
  };
@@ -16,7 +16,7 @@ export * from './instructions/refreshOraclePrice';
16
16
  export * from './instructions/initHedgeFoundation';
17
17
  export * from './instructions/initHedgeFoundationTokens';
18
18
  export * from './instructions/setHalted';
19
- export * from './instructions/setVaultTypeStatus';
19
+ export * from './instructions/updateVaultType';
20
20
  export * from './HedgeDecimal';
21
21
  export * from './Constants';
22
22
  export * from './state/VaultAccount';
@@ -0,0 +1,15 @@
1
+ /// <reference types="bn.js" />
2
+ import { BN, Program, Provider } from '@project-serum/anchor';
3
+ import { PublicKey, Signer, TransactionInstruction } from '@solana/web3.js';
4
+ import { Vault } from 'idl/vault';
5
+ export interface VaultTypeConfig {
6
+ maxDebtExtended?: BN;
7
+ minDebtPerVault?: BN;
8
+ loanInitFee?: BN;
9
+ oracleChainlink?: PublicKey;
10
+ oraclePyth?: PublicKey;
11
+ oracleSwitchboard?: PublicKey;
12
+ deprecated?: boolean;
13
+ }
14
+ export declare function updateVaultType(program: Program<Vault>, provider: Provider, payer: Signer, vaultTypeAccount: PublicKey, oracleInfoAccount: PublicKey, config: VaultTypeConfig): Promise<PublicKey>;
15
+ export declare function updateVaultTypeStatusInstruction(program: Program<Vault>, vaultSystemStatePublicKey: PublicKey, payerPublicKey: PublicKey, vaultTypeAccount: PublicKey, oracleInfoAccount: PublicKey, vaultTypeConfig: VaultTypeConfig): Promise<TransactionInstruction>;
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import { PublicKey } from '@solana/web3.js';
2
3
  import Decimal from 'decimal.js';
3
4
  /**
@@ -16,8 +17,11 @@ export declare class VaultAccount {
16
17
  denormalizedDebt: number;
17
18
  /** The ordered number of when this vault was created. */
18
19
  vaultNumber: number;
20
+ /** Debt redistribution snapshop */
19
21
  debtProductSnapshotBytes: Decimal;
22
+ /** Collateral redistribution snapshop' */
20
23
  collateralAccumulatorSnapshotBytes: Decimal;
24
+ /** The vault type eg 'SOL-150' */
21
25
  collateralType: string;
22
26
  /** Current State of the vautl ("Open", "Closed", "Liquidated") */
23
27
  vaultStatus: string;
@@ -55,4 +59,23 @@ export declare class VaultAccount {
55
59
  liquidate(): void;
56
60
  updateDebtAndCollateral(vaultTypeAccuntData: any): void;
57
61
  toString(highlight: PublicKey): string;
62
+ /**
63
+ * Creates a VaultAccount from a slice of data
64
+ *
65
+ * It only contains
66
+ * - Denormalized Debt
67
+ * - Deposit
68
+ * - Vault Number
69
+ *
70
+ * This can be used to do off-chain sort with very little data.
71
+ *
72
+ * @returns a new VaultAccount
73
+ */
74
+ static FromMiniSlice(data: Buffer, pubkey: PublicKey): VaultAccount;
75
+ /**
76
+ * Encode a string for doing byte comparison on solana
77
+ *
78
+ * @returns Buffer of the encoded string
79
+ */
80
+ static getBufferForString(string: string): Buffer;
58
81
  }
package/lib/idl/vault.js CHANGED
@@ -1563,7 +1563,7 @@ exports.IDL = {
1563
1563
  ]
1564
1564
  },
1565
1565
  {
1566
- "name": "setVaultTypeStatus",
1566
+ "name": "updateVaultType",
1567
1567
  "accounts": [
1568
1568
  {
1569
1569
  "name": "payer",
@@ -1579,12 +1579,77 @@ exports.IDL = {
1579
1579
  "name": "vaultType",
1580
1580
  "isMut": true,
1581
1581
  "isSigner": false
1582
+ },
1583
+ {
1584
+ "name": "oracleInfoAccount",
1585
+ "isMut": true,
1586
+ "isSigner": false
1587
+ }
1588
+ ],
1589
+ "args": [
1590
+ {
1591
+ "name": "config",
1592
+ "type": {
1593
+ "defined": "VaultTypeConfig"
1594
+ }
1595
+ }
1596
+ ]
1597
+ },
1598
+ {
1599
+ "name": "fixVaultRedeemPointer",
1600
+ "accounts": [
1601
+ {
1602
+ "name": "payer",
1603
+ "isMut": true,
1604
+ "isSigner": true
1605
+ },
1606
+ {
1607
+ "name": "vaultSystemState",
1608
+ "isMut": true,
1609
+ "isSigner": false
1610
+ },
1611
+ {
1612
+ "name": "vaultAccount",
1613
+ "isMut": true,
1614
+ "isSigner": false
1615
+ }
1616
+ ],
1617
+ "args": []
1618
+ },
1619
+ {
1620
+ "name": "adminSetRedeemPointer",
1621
+ "accounts": [
1622
+ {
1623
+ "name": "payer",
1624
+ "isMut": true,
1625
+ "isSigner": true
1626
+ },
1627
+ {
1628
+ "name": "vaultSystemState",
1629
+ "isMut": true,
1630
+ "isSigner": false
1631
+ },
1632
+ {
1633
+ "name": "vaultAccount",
1634
+ "isMut": true,
1635
+ "isSigner": false
1636
+ },
1637
+ {
1638
+ "name": "vaultTypeAccount",
1639
+ "isMut": true,
1640
+ "isSigner": false
1582
1641
  }
1583
1642
  ],
1584
1643
  "args": [
1585
1644
  {
1586
- "name": "deprecated",
1645
+ "name": "settingHead",
1587
1646
  "type": "bool"
1647
+ },
1648
+ {
1649
+ "name": "pointerToSet",
1650
+ "type": {
1651
+ "option": "publicKey"
1652
+ }
1588
1653
  }
1589
1654
  ]
1590
1655
  }
@@ -2246,6 +2311,56 @@ exports.IDL = {
2246
2311
  ]
2247
2312
  }
2248
2313
  },
2314
+ {
2315
+ "name": "VaultTypeConfig",
2316
+ "type": {
2317
+ "kind": "struct",
2318
+ "fields": [
2319
+ {
2320
+ "name": "maxDebtExtended",
2321
+ "type": {
2322
+ "option": "u64"
2323
+ }
2324
+ },
2325
+ {
2326
+ "name": "minDebtPerVault",
2327
+ "type": {
2328
+ "option": "u64"
2329
+ }
2330
+ },
2331
+ {
2332
+ "name": "loanInitFee",
2333
+ "type": {
2334
+ "option": "u64"
2335
+ }
2336
+ },
2337
+ {
2338
+ "name": "oracleChainlink",
2339
+ "type": {
2340
+ "option": "publicKey"
2341
+ }
2342
+ },
2343
+ {
2344
+ "name": "oraclePyth",
2345
+ "type": {
2346
+ "option": "publicKey"
2347
+ }
2348
+ },
2349
+ {
2350
+ "name": "oracleSwitchboard",
2351
+ "type": {
2352
+ "option": "publicKey"
2353
+ }
2354
+ },
2355
+ {
2356
+ "name": "deprecated",
2357
+ "type": {
2358
+ "option": "bool"
2359
+ }
2360
+ }
2361
+ ]
2362
+ }
2363
+ },
2249
2364
  {
2250
2365
  "name": "PositionState",
2251
2366
  "type": {
@@ -2568,6 +2683,16 @@ exports.IDL = {
2568
2683
  "code": 6021,
2569
2684
  "name": "VaultTypeDeprecated",
2570
2685
  "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."
2686
+ },
2687
+ {
2688
+ "code": 6022,
2689
+ "name": "UpdateLinkedListFailed",
2690
+ "msg": "There was an error updating the list of vaults. Please make sure the transaction is fresh."
2691
+ },
2692
+ {
2693
+ "code": 6023,
2694
+ "name": "UpdateVaultTypeBadMaxDebtExtended",
2695
+ "msg": "New Max debt extended value is less than the current debt!"
2571
2696
  }
2572
2697
  ]
2573
2698
  };
package/lib/index.js CHANGED
@@ -28,7 +28,7 @@ __exportStar(require("./instructions/refreshOraclePrice"), exports);
28
28
  __exportStar(require("./instructions/initHedgeFoundation"), exports);
29
29
  __exportStar(require("./instructions/initHedgeFoundationTokens"), exports);
30
30
  __exportStar(require("./instructions/setHalted"), exports);
31
- __exportStar(require("./instructions/setVaultTypeStatus"), exports);
31
+ __exportStar(require("./instructions/updateVaultType"), exports);
32
32
  __exportStar(require("./HedgeDecimal"), exports);
33
33
  __exportStar(require("./Constants"), exports);
34
34
  __exportStar(require("./state/VaultAccount"), exports);
@@ -0,0 +1,47 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.updateVaultTypeStatusInstruction = exports.updateVaultType = void 0;
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const Constants_1 = require("../Constants");
15
+ function updateVaultType(program, provider, payer, vaultTypeAccount, oracleInfoAccount, config) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
18
+ const transaction = new web3_js_1.Transaction().add(yield updateVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, oracleInfoAccount, config));
19
+ yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]);
20
+ return vaultSystemStatePublicKey;
21
+ });
22
+ }
23
+ exports.updateVaultType = updateVaultType;
24
+ function updateVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultTypeAccount, oracleInfoAccount, vaultTypeConfig) {
25
+ var _a, _b, _c, _d, _e, _f, _g;
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ const config = {
28
+ maxDebtExtended: (_a = vaultTypeConfig.maxDebtExtended) !== null && _a !== void 0 ? _a : null,
29
+ minDebtPerVault: (_b = vaultTypeConfig.minDebtPerVault) !== null && _b !== void 0 ? _b : null,
30
+ loanInitFee: (_c = vaultTypeConfig.loanInitFee) !== null && _c !== void 0 ? _c : null,
31
+ oracleChainlink: (_d = vaultTypeConfig.oracleChainlink) !== null && _d !== void 0 ? _d : null,
32
+ oraclePyth: (_e = vaultTypeConfig.oraclePyth) !== null && _e !== void 0 ? _e : null,
33
+ oracleSwitchboard: (_f = vaultTypeConfig.oracleSwitchboard) !== null && _f !== void 0 ? _f : null,
34
+ deprecated: (_g = vaultTypeConfig.deprecated) !== null && _g !== void 0 ? _g : null,
35
+ };
36
+ return yield program.methods
37
+ .updateVaultType(config)
38
+ .accounts({
39
+ payer: payerPublicKey,
40
+ vaultSystemState: vaultSystemStatePublicKey,
41
+ vaultType: vaultTypeAccount,
42
+ oracleInfoAccount: oracleInfoAccount,
43
+ })
44
+ .instruction();
45
+ });
46
+ }
47
+ exports.updateVaultTypeStatusInstruction = updateVaultTypeStatusInstruction;
@@ -1,4 +1,23 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
2
21
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
23
  };
@@ -7,22 +26,36 @@ exports.VaultAccount = void 0;
7
26
  const web3_js_1 = require("@solana/web3.js");
8
27
  const decimal_js_1 = __importDefault(require("decimal.js"));
9
28
  const HedgeDecimal_1 = require("../HedgeDecimal");
29
+ const borsh = __importStar(require("@project-serum/borsh"));
10
30
  /**
11
31
  * A class that represents an on-chian vault.
12
32
  */
13
33
  class VaultAccount {
14
34
  constructor(vault, publicKey) {
35
+ var _a, _b, _c;
36
+ /** Debt redistribution snapshop */
37
+ this.debtProductSnapshotBytes = new decimal_js_1.default(0);
38
+ /** Collateral redistribution snapshop' */
39
+ this.collateralAccumulatorSnapshotBytes = new decimal_js_1.default(0);
40
+ /** Current State of the vautl ("Open", "Closed", "Liquidated") */
41
+ this.vaultStatus = '';
15
42
  this.publicKey = publicKey;
16
43
  this.vaultOwner = vault.vaultOwner;
17
- this.vaultNumber = vault.vaultNumber.toNumber();
44
+ this.vaultNumber = (_a = vault.vaultNumber) === null || _a === void 0 ? void 0 : _a.toNumber();
18
45
  this.pdaSalt = vault.pdaSalt;
19
- this.deposited = vault.deposited.toNumber();
20
- this.denormalizedDebt = vault.denormalizedDebt.toNumber();
21
- this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
22
- this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
46
+ this.deposited = (_b = vault.deposited) === null || _b === void 0 ? void 0 : _b.toNumber();
47
+ this.denormalizedDebt = (_c = vault.denormalizedDebt) === null || _c === void 0 ? void 0 : _c.toNumber();
48
+ if (vault.debtProductSnapshotBytes) {
49
+ this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
50
+ }
51
+ if (vault.collateralAccumulatorSnapshotBytes) {
52
+ this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
53
+ }
23
54
  this.collateralType = vault.collateralType;
24
55
  this.nextVaultToRedeem = vault.nextVaultToRedeem;
25
- this.vaultStatus = Object.keys(vault.vaultStatus)[0];
56
+ if (vault.vaultStatus) {
57
+ this.vaultStatus = Object.keys(vault.vaultStatus)[0];
58
+ }
26
59
  }
27
60
  /**
28
61
  * Check if some `PublicKey` is the owner
@@ -102,9 +135,40 @@ class VaultAccount {
102
135
  if (this.nextVaultToRedeem) {
103
136
  nextVault = this.nextVaultToRedeem.toString().substring(0, 6);
104
137
  }
105
- return `Vault(${this.vaultNumber}): ${this.publicKey
106
- .toString()
107
- .substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `;
138
+ return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `;
139
+ }
140
+ /**
141
+ * Creates a VaultAccount from a slice of data
142
+ *
143
+ * It only contains
144
+ * - Denormalized Debt
145
+ * - Deposit
146
+ * - Vault Number
147
+ *
148
+ * This can be used to do off-chain sort with very little data.
149
+ *
150
+ * @returns a new VaultAccount
151
+ */
152
+ static FromMiniSlice(data, pubkey) {
153
+ const props = [borsh.u64('vaultNumber'), borsh.u64('deposited'), borsh.u64('denormalizedDebt')];
154
+ const miniVaultLayout = borsh.struct(props, 'minVaultLayout');
155
+ const decodedData = miniVaultLayout.decode(data);
156
+ return new VaultAccount(decodedData, pubkey);
157
+ }
158
+ /**
159
+ * Encode a string for doing byte comparison on solana
160
+ *
161
+ * @returns Buffer of the encoded string
162
+ */
163
+ static getBufferForString(string) {
164
+ const stringLayout = borsh.struct([borsh.str('name')], 'justStringLayout');
165
+ let buffer = Buffer.alloc(50);
166
+ stringLayout.encode({ name: string }, buffer);
167
+ // Trim empty space at end of buffer
168
+ while (buffer.at(buffer.length - 1) == 0) {
169
+ buffer = buffer.slice(0, buffer.length - 1);
170
+ }
171
+ return buffer;
108
172
  }
109
173
  }
110
174
  exports.VaultAccount = VaultAccount;
@@ -5,6 +5,7 @@ const anchor_1 = require("@project-serum/anchor");
5
5
  const vault_1 = require("../idl/vault");
6
6
  function parseAnchorErrors(error) {
7
7
  const idlErrors = (0, anchor_1.parseIdlErrors)(vault_1.IDL);
8
+ console.log(error);
8
9
  const parsedError = anchor_1.ProgramError.parse(error, idlErrors);
9
10
  if (parsedError !== null) {
10
11
  throw parsedError;