hedge-web3 0.1.43 → 0.1.46

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. package/declarations/idl/vault.d.ts +78 -5
  2. package/declarations/instructions/liquidateVault.d.ts +1 -1
  3. package/declarations/state/VaultAccount.d.ts +8 -6
  4. package/declarations/state/VaultType.d.ts +24 -0
  5. package/declarations/utils/Sender.d.ts +2 -0
  6. package/declarations/utils/sendAndConfirmWithDebug.d.ts +2 -0
  7. package/lib/idl/vault.js +78 -5
  8. package/lib/instructions/createStakingPool.js +1 -0
  9. package/lib/instructions/depositVault.js +7 -7
  10. package/lib/instructions/liquidateVault.js +5 -7
  11. package/lib/instructions/loanVault.js +7 -6
  12. package/lib/instructions/redeemVault.js +4 -5
  13. package/lib/instructions/refreshOraclePrice.js +2 -2
  14. package/lib/instructions/repayVault.js +4 -5
  15. package/lib/instructions/withdrawVault.js +4 -5
  16. package/lib/state/VaultAccount.js +9 -8
  17. package/lib/state/VaultType.js +32 -0
  18. package/lib/utils/Sender.js +32 -0
  19. package/lib/utils/getLinkedListAccounts.js +15 -15
  20. package/lib/utils/sendAndConfirmWithDebug.js +35 -0
  21. package/package.json +1 -1
  22. package/src/idl/vault.ts +156 -10
  23. package/src/instructions/createStakingPool.ts +1 -0
  24. package/src/instructions/depositVault.ts +8 -8
  25. package/src/instructions/liquidateVault.ts +5 -7
  26. package/src/instructions/loanVault.ts +11 -8
  27. package/src/instructions/redeemVault.ts +4 -5
  28. package/src/instructions/refreshOraclePrice.ts +2 -2
  29. package/src/instructions/repayVault.ts +4 -5
  30. package/src/instructions/withdrawVault.ts +4 -5
  31. package/src/state/VaultAccount.ts +14 -10
  32. package/src/utils/getLinkedListAccounts.ts +16 -16
@@ -1649,6 +1649,58 @@ export declare type Vault = {
1649
1649
  };
1650
1650
  }
1651
1651
  ];
1652
+ },
1653
+ {
1654
+ "name": "adminMigrateVault";
1655
+ "accounts": [
1656
+ {
1657
+ "name": "payer";
1658
+ "isMut": true;
1659
+ "isSigner": true;
1660
+ },
1661
+ {
1662
+ "name": "vaultSystemState";
1663
+ "isMut": true;
1664
+ "isSigner": false;
1665
+ },
1666
+ {
1667
+ "name": "vaultAccount";
1668
+ "isMut": true;
1669
+ "isSigner": false;
1670
+ },
1671
+ {
1672
+ "name": "vaultTypeAccount";
1673
+ "isMut": true;
1674
+ "isSigner": false;
1675
+ }
1676
+ ];
1677
+ "args": [];
1678
+ },
1679
+ {
1680
+ "name": "adminMigrateOracle";
1681
+ "accounts": [
1682
+ {
1683
+ "name": "payer";
1684
+ "isMut": true;
1685
+ "isSigner": true;
1686
+ },
1687
+ {
1688
+ "name": "vaultSystemState";
1689
+ "isMut": true;
1690
+ "isSigner": false;
1691
+ },
1692
+ {
1693
+ "name": "oracleInfoAccount";
1694
+ "isMut": true;
1695
+ "isSigner": false;
1696
+ },
1697
+ {
1698
+ "name": "vaultTypeAccount";
1699
+ "isMut": true;
1700
+ "isSigner": false;
1701
+ }
1702
+ ];
1703
+ "args": [];
1652
1704
  }
1653
1705
  ];
1654
1706
  "accounts": [
@@ -1844,7 +1896,7 @@ export declare type Vault = {
1844
1896
  "kind": "struct";
1845
1897
  "fields": [
1846
1898
  {
1847
- "name": "collateralType";
1899
+ "name": "vaultTypeName";
1848
1900
  "type": "string";
1849
1901
  },
1850
1902
  {
@@ -1858,6 +1910,10 @@ export declare type Vault = {
1858
1910
  {
1859
1911
  "name": "oracleSwitchboard";
1860
1912
  "type": "publicKey";
1913
+ },
1914
+ {
1915
+ "name": "vaultType";
1916
+ "type": "publicKey";
1861
1917
  }
1862
1918
  ];
1863
1919
  };
@@ -2102,7 +2158,7 @@ export declare type Vault = {
2102
2158
  "kind": "struct";
2103
2159
  "fields": [
2104
2160
  {
2105
- "name": "collateralType";
2161
+ "name": "vaultTypeName";
2106
2162
  "type": "string";
2107
2163
  },
2108
2164
  {
@@ -2186,6 +2242,14 @@ export declare type Vault = {
2186
2242
  {
2187
2243
  "name": "deprecated";
2188
2244
  "type": "bool";
2245
+ },
2246
+ {
2247
+ "name": "totalFeesAccumulatedUsh";
2248
+ "type": "u128";
2249
+ },
2250
+ {
2251
+ "name": "totalFeesAccumulatedCollateral";
2252
+ "type": "u128";
2189
2253
  }
2190
2254
  ];
2191
2255
  };
@@ -2212,7 +2276,7 @@ export declare type Vault = {
2212
2276
  "type": "u64";
2213
2277
  },
2214
2278
  {
2215
- "name": "collateralType";
2279
+ "name": "vaultTypeName";
2216
2280
  "type": "string";
2217
2281
  },
2218
2282
  {
@@ -2260,6 +2324,10 @@ export declare type Vault = {
2260
2324
  "type": {
2261
2325
  "option": "publicKey";
2262
2326
  };
2327
+ },
2328
+ {
2329
+ "name": "vaultType";
2330
+ "type": "publicKey";
2263
2331
  }
2264
2332
  ];
2265
2333
  };
@@ -2673,8 +2741,8 @@ export declare type Vault = {
2673
2741
  },
2674
2742
  {
2675
2743
  "code": 6020;
2676
- "name": "RedeemingNonHighestCollateralRatioVault";
2677
- "msg": "Can only redeem highest collateral ratio vault in system.";
2744
+ "name": "NotRedeemingLowestCollateralRatioVault";
2745
+ "msg": "Can only redeem lowest collateral ratio vault in system.";
2678
2746
  },
2679
2747
  {
2680
2748
  "code": 6021;
@@ -2688,6 +2756,11 @@ export declare type Vault = {
2688
2756
  },
2689
2757
  {
2690
2758
  "code": 6023;
2759
+ "name": "InvalidSaltLength";
2760
+ "msg": "Salt for new vaults must be of length 8";
2761
+ },
2762
+ {
2763
+ "code": 6024;
2691
2764
  "name": "UpdateVaultTypeBadMaxDebtExtended";
2692
2765
  "msg": "New Max debt extended value is less than the current debt!";
2693
2766
  }
@@ -2,4 +2,4 @@ import { Program, Provider } from '@project-serum/anchor';
2
2
  import { PublicKey, Signer, TransactionInstruction } from '@solana/web3.js';
3
3
  import { Vault } from 'idl/vault';
4
4
  export declare function liquidateVault(program: Program<Vault>, provider: Provider, payer: Signer, vaultPublicKey: PublicKey, overrideTime?: number): Promise<PublicKey>;
5
- export declare function liquidateVaultInstruction(program: Program<Vault>, payerPublicKey: PublicKey, payerAssociatedTokenAccount: PublicKey, vaultPublickey: PublicKey, vaultAssociatedTokenAccount: PublicKey, poolState: PublicKey, poolEra: PublicKey, poolAssociatedTokenAccount: PublicKey, historyPublicKey: PublicKey, newEraPublicKey: PublicKey, feePool: PublicKey, feePoolAssociatedTokenAccount: PublicKey, hedgeStakingPoolAssociatedUshTokenAccount: PublicKey, collateralMint: PublicKey, vaultTypeAssociatedTokenAccount: PublicKey, oldSmallerPublicKey: PublicKey, newSmallerPublicKey: PublicKey, newLargerPublicKey: PublicKey, collateralType: string, overrideTime?: number): Promise<TransactionInstruction>;
5
+ export declare function liquidateVaultInstruction(program: Program<Vault>, payerPublicKey: PublicKey, payerAssociatedTokenAccount: PublicKey, vaultPublickey: PublicKey, vaultAssociatedTokenAccount: PublicKey, poolState: PublicKey, poolEra: PublicKey, poolAssociatedTokenAccount: PublicKey, historyPublicKey: PublicKey, newEraPublicKey: PublicKey, feePool: PublicKey, feePoolAssociatedTokenAccount: PublicKey, hedgeStakingPoolAssociatedUshTokenAccount: PublicKey, collateralMint: PublicKey, vaultTypeAssociatedTokenAccount: PublicKey, oldSmallerPublicKey: PublicKey, newSmallerPublicKey: PublicKey, newLargerPublicKey: PublicKey, vaultTypeAccount: PublicKey, overrideTime?: number): Promise<TransactionInstruction>;
@@ -17,16 +17,18 @@ export declare class VaultAccount {
17
17
  denormalizedDebt: number;
18
18
  /** The ordered number of when this vault was created. */
19
19
  vaultNumber: number;
20
- /** Debt redistribution snapshop */
20
+ /** Debt redistribution snapshot */
21
21
  debtProductSnapshotBytes: Decimal;
22
- /** Collateral redistribution snapshop' */
22
+ /** Collateral redistribution snapshot' */
23
23
  collateralAccumulatorSnapshotBytes: Decimal;
24
- /** The vault type eg 'SOL-150' */
25
- collateralType: string;
26
- /** Current State of the vautl ("Open", "Closed", "Liquidated") */
24
+ /** The vault type eg '2-SOL-150-0' */
25
+ vaultTypeName: string;
26
+ /** Current State of the vault ("Open", "Closed", "Liquidated") */
27
27
  vaultStatus: string;
28
28
  /** The public key of the next vault to redeem. */
29
29
  nextVaultToRedeem: PublicKey;
30
+ /** The public key of the vault type. */
31
+ vaultType: PublicKey;
30
32
  constructor(vault: any, publicKey: PublicKey);
31
33
  /**
32
34
  * Check if some `PublicKey` is the owner
@@ -57,7 +59,7 @@ export declare class VaultAccount {
57
59
  addDeposit(depositAmount: number): void;
58
60
  redeem(): void;
59
61
  liquidate(): void;
60
- updateDebtAndCollateral(vaultTypeAccuntData: any): void;
62
+ updateDebtAndCollateral(vaultTypeAccountData: any): void;
61
63
  toString(highlight: PublicKey): string;
62
64
  /**
63
65
  * Creates a VaultAccount from a slice of data
@@ -0,0 +1,24 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import Decimal from 'decimal.js';
3
+ export default class VaultType {
4
+ publicKey?: PublicKey;
5
+ name: string;
6
+ collateralMint: PublicKey;
7
+ collateralIndex: number;
8
+ recentPrice: Decimal;
9
+ priceLastUpdatedTimestamp: Decimal;
10
+ collateralHeld: Decimal;
11
+ denormalizedDebtExtended: Decimal;
12
+ debtRedistributionProduct: Decimal;
13
+ collateralRedistributionAccumulator: Decimal;
14
+ minCollateralRatio: Decimal;
15
+ loanInitFee: Decimal;
16
+ minDebtPerVault: Decimal;
17
+ maxDebtExtended: Decimal;
18
+ canBeRedeemed: boolean;
19
+ cumulativeRate: Decimal;
20
+ cumulativeRateLastUpdated: Decimal;
21
+ interestRatePerSecond: Decimal;
22
+ firstVaultToRedeem: PublicKey;
23
+ constructor(config: any, publicKey?: PublicKey);
24
+ }
@@ -0,0 +1,2 @@
1
+ import { Connection, Signer, Transaction, TransactionSignature } from '@solana/web3.js';
2
+ export declare function sendAndConfirmWithDebug(connection: Connection, transaction: Transaction, signers: Signer[]): Promise<TransactionSignature | void>;
@@ -0,0 +1,2 @@
1
+ import { Connection, Signer, Transaction, TransactionSignature } from '@solana/web3.js';
2
+ export default function sendAndConfirmWithDebug(connection: Connection, transaction: Transaction, signers: Signer[]): Promise<TransactionSignature | void>;
package/lib/idl/vault.js CHANGED
@@ -1652,6 +1652,58 @@ exports.IDL = {
1652
1652
  }
1653
1653
  }
1654
1654
  ]
1655
+ },
1656
+ {
1657
+ "name": "adminMigrateVault",
1658
+ "accounts": [
1659
+ {
1660
+ "name": "payer",
1661
+ "isMut": true,
1662
+ "isSigner": true
1663
+ },
1664
+ {
1665
+ "name": "vaultSystemState",
1666
+ "isMut": true,
1667
+ "isSigner": false
1668
+ },
1669
+ {
1670
+ "name": "vaultAccount",
1671
+ "isMut": true,
1672
+ "isSigner": false
1673
+ },
1674
+ {
1675
+ "name": "vaultTypeAccount",
1676
+ "isMut": true,
1677
+ "isSigner": false
1678
+ }
1679
+ ],
1680
+ "args": []
1681
+ },
1682
+ {
1683
+ "name": "adminMigrateOracle",
1684
+ "accounts": [
1685
+ {
1686
+ "name": "payer",
1687
+ "isMut": true,
1688
+ "isSigner": true
1689
+ },
1690
+ {
1691
+ "name": "vaultSystemState",
1692
+ "isMut": true,
1693
+ "isSigner": false
1694
+ },
1695
+ {
1696
+ "name": "oracleInfoAccount",
1697
+ "isMut": true,
1698
+ "isSigner": false
1699
+ },
1700
+ {
1701
+ "name": "vaultTypeAccount",
1702
+ "isMut": true,
1703
+ "isSigner": false
1704
+ }
1705
+ ],
1706
+ "args": []
1655
1707
  }
1656
1708
  ],
1657
1709
  "accounts": [
@@ -1847,7 +1899,7 @@ exports.IDL = {
1847
1899
  "kind": "struct",
1848
1900
  "fields": [
1849
1901
  {
1850
- "name": "collateralType",
1902
+ "name": "vaultTypeName",
1851
1903
  "type": "string"
1852
1904
  },
1853
1905
  {
@@ -1861,6 +1913,10 @@ exports.IDL = {
1861
1913
  {
1862
1914
  "name": "oracleSwitchboard",
1863
1915
  "type": "publicKey"
1916
+ },
1917
+ {
1918
+ "name": "vaultType",
1919
+ "type": "publicKey"
1864
1920
  }
1865
1921
  ]
1866
1922
  }
@@ -2105,7 +2161,7 @@ exports.IDL = {
2105
2161
  "kind": "struct",
2106
2162
  "fields": [
2107
2163
  {
2108
- "name": "collateralType",
2164
+ "name": "vaultTypeName",
2109
2165
  "type": "string"
2110
2166
  },
2111
2167
  {
@@ -2189,6 +2245,14 @@ exports.IDL = {
2189
2245
  {
2190
2246
  "name": "deprecated",
2191
2247
  "type": "bool"
2248
+ },
2249
+ {
2250
+ "name": "totalFeesAccumulatedUsh",
2251
+ "type": "u128"
2252
+ },
2253
+ {
2254
+ "name": "totalFeesAccumulatedCollateral",
2255
+ "type": "u128"
2192
2256
  }
2193
2257
  ]
2194
2258
  }
@@ -2215,7 +2279,7 @@ exports.IDL = {
2215
2279
  "type": "u64"
2216
2280
  },
2217
2281
  {
2218
- "name": "collateralType",
2282
+ "name": "vaultTypeName",
2219
2283
  "type": "string"
2220
2284
  },
2221
2285
  {
@@ -2263,6 +2327,10 @@ exports.IDL = {
2263
2327
  "type": {
2264
2328
  "option": "publicKey"
2265
2329
  }
2330
+ },
2331
+ {
2332
+ "name": "vaultType",
2333
+ "type": "publicKey"
2266
2334
  }
2267
2335
  ]
2268
2336
  }
@@ -2676,8 +2744,8 @@ exports.IDL = {
2676
2744
  },
2677
2745
  {
2678
2746
  "code": 6020,
2679
- "name": "RedeemingNonHighestCollateralRatioVault",
2680
- "msg": "Can only redeem highest collateral ratio vault in system."
2747
+ "name": "NotRedeemingLowestCollateralRatioVault",
2748
+ "msg": "Can only redeem lowest collateral ratio vault in system."
2681
2749
  },
2682
2750
  {
2683
2751
  "code": 6021,
@@ -2691,6 +2759,11 @@ exports.IDL = {
2691
2759
  },
2692
2760
  {
2693
2761
  "code": 6023,
2762
+ "name": "InvalidSaltLength",
2763
+ "msg": "Salt for new vaults must be of length 8"
2764
+ },
2765
+ {
2766
+ "code": 6024,
2694
2767
  "name": "UpdateVaultTypeBadMaxDebtExtended",
2695
2768
  "msg": "New Max debt extended value is less than the current debt!"
2696
2769
  }
@@ -26,6 +26,7 @@ function createStakingPool(program, provider, payer, mintPublicKey, hedgeTokensT
26
26
  exports.createStakingPool = createStakingPool;
27
27
  function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
28
28
  return __awaiter(this, void 0, void 0, function* () {
29
+ console.log("new createStakingPoolInstruction");
29
30
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
30
31
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
31
32
  const [poolPublickey, poolBump] = yield (0, Constants_1.getPoolPublicKeyForMint)(mintPublicKey);
@@ -16,15 +16,15 @@ const spl_token_1 = require("@solana/spl-token");
16
16
  const web3_js_1 = require("@solana/web3.js");
17
17
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
18
18
  const Constants_1 = require("../Constants");
19
+ const token_instructions_1 = require("@project-serum/serum/lib/token-instructions");
19
20
  function depositVault(program, provider, payer, vaultPublicKey, depositAmount, overrideTime) {
20
21
  return __awaiter(this, void 0, void 0, function* () {
21
22
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
22
23
  // Prep the user to get USH back out at some point
23
24
  yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
24
25
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
25
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
26
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
27
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
26
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
27
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
28
28
  const payerTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payer.publicKey, vaultTypeAccountInfo.collateralMint);
29
29
  const vaultAssociatedCollateralAccountPublicKey = yield (0, Constants_1.findAssociatedTokenAddress)(vaultPublicKey, vaultTypeAccountInfo.collateralMint);
30
30
  const history = web3_js_1.Keypair.generate();
@@ -34,8 +34,8 @@ function depositVault(program, provider, payer, vaultPublicKey, depositAmount, o
34
34
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
35
35
  const transaction = new web3_js_1.Transaction();
36
36
  const signers = [payer, history];
37
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, 0, false, false);
38
- if (vaultAccount.collateralType === 'SOL') {
37
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, depositAmount, 0, false, false);
38
+ if (vaultTypeAccountInfo.collateralMint.toString() === token_instructions_1.WRAPPED_SOL_MINT.toString()) {
39
39
  transaction.add(web3_js_1.SystemProgram.createAccount({
40
40
  fromPubkey: payer.publicKey,
41
41
  lamports: depositAmount + 2.04e6,
@@ -49,8 +49,8 @@ function depositVault(program, provider, payer, vaultPublicKey, depositAmount, o
49
49
  }));
50
50
  signers.push(wrappedSolAccount);
51
51
  }
52
- transaction.add(yield depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultAccount.collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount, vaultPublicKey, vaultAssociatedCollateralAccountPublicKey, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, vaultTypeAccountInfo.collateralMint, ushMintPublickey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, depositAmount, overrideTime));
53
- if (vaultAccount.collateralType === 'SOL') {
52
+ transaction.add(yield depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccountInfo.collateralMint.toString() === token_instructions_1.WRAPPED_SOL_MINT.toString() ? wrappedSolAccount.publicKey : payerTokenAccount, vaultPublicKey, vaultAssociatedCollateralAccountPublicKey, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, vaultTypeAccountInfo.collateralMint, ushMintPublickey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, depositAmount, overrideTime));
53
+ if (vaultTypeAccountInfo.collateralMint.toString() === token_instructions_1.WRAPPED_SOL_MINT.toString()) {
54
54
  transaction.add(serum_1.TokenInstructions.closeAccount({
55
55
  source: wrappedSolAccount.publicKey,
56
56
  destination: payer.publicKey,
@@ -18,8 +18,7 @@ const Constants_1 = require("../Constants");
18
18
  function liquidateVault(program, provider, payer, vaultPublicKey, overrideTime) {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
20
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
21
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
22
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
21
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
23
22
  const collateralMint = vaultTypeAccountInfo.collateralMint;
24
23
  const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
25
24
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -30,24 +29,23 @@ function liquidateVault(program, provider, payer, vaultPublicKey, overrideTime)
30
29
  const feePoolAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, hedgeStakingPoolPublicKey, true);
31
30
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultPublicKey, true);
32
31
  const poolAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, liquidationPoolStatePublicKey, true);
33
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultTypeAccountPublicKey, true);
32
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultAccount.vaultType, true);
34
33
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, hedgeStakingPoolPublicKey, true);
35
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, 0, false, true);
34
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, 0, false, true);
36
35
  const history = web3_js_1.Keypair.generate();
37
36
  const newEra = web3_js_1.Keypair.generate();
38
37
  const transaction = new web3_js_1.Transaction();
39
- transaction.add(yield liquidateVaultInstruction(program, payer.publicKey, payerAssociatedTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, liquidationPoolStatePublicKey, poolStateInfo.currentEra, poolAssociatedTokenAccount.address, history.publicKey, newEra.publicKey, hedgeStakingPoolPublicKey, feePoolAssociatedTokenAccount.address, hedgeStakingPoolAssociatedUshTokenAccount.address, collateralMint, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaultAccount.collateralType, overrideTime));
38
+ transaction.add(yield liquidateVaultInstruction(program, payer.publicKey, payerAssociatedTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, liquidationPoolStatePublicKey, poolStateInfo.currentEra, poolAssociatedTokenAccount.address, history.publicKey, newEra.publicKey, hedgeStakingPoolPublicKey, feePoolAssociatedTokenAccount.address, hedgeStakingPoolAssociatedUshTokenAccount.address, collateralMint, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaultAccount.vaultType, overrideTime));
40
39
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history, newEra]);
41
40
  return vaultPublicKey;
42
41
  });
43
42
  }
44
43
  exports.liquidateVault = liquidateVault;
45
- function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, collateralType, overrideTime) {
44
+ function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaultTypeAccount, overrideTime) {
46
45
  return __awaiter(this, void 0, void 0, function* () {
47
46
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
48
47
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
49
48
  const liquidationPoolUshAccountPublickey = yield (0, Constants_1.getLiquidationPoolUshAccountPublicKey)();
50
- const vaultTypeAccount = yield (0, Constants_1.getVaultTypeAccountPublicKey)(collateralType);
51
49
  return yield program.methods
52
50
  .liquidateVault(new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
53
51
  )
@@ -15,19 +15,20 @@ const spl_token_1 = require("@solana/spl-token");
15
15
  const web3_js_1 = require("@solana/web3.js");
16
16
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
17
17
  const Constants_1 = require("../Constants");
18
+ const Errors_1 = require("../utils/Errors");
19
+ const fs = require('fs');
18
20
  function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrideTime) {
19
21
  return __awaiter(this, void 0, void 0, function* () {
20
22
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
21
23
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
22
24
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
23
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
24
- const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
25
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultTypeAccountPublicKey, true);
25
+ const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
26
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultAccount.vaultType, true);
26
27
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultPublicKey, true);
27
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, loanAmount, false, false);
28
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, loanAmount, false, false);
28
29
  const history = web3_js_1.Keypair.generate();
29
- const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, overrideTime));
30
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
30
+ const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, overrideTime));
31
+ yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]).catch(Errors_1.parseAnchorErrors);
31
32
  return vaultPublicKey;
32
33
  });
33
34
  }
@@ -22,14 +22,13 @@ function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, tra
22
22
  // Prep the user to get USH back out at some point
23
23
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
24
24
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
25
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
26
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
27
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
25
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
26
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
28
27
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true);
29
28
  const payerTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
30
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, 0, true, false);
29
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, 0, true, false);
31
30
  const history = web3_js_1.Keypair.generate();
32
- const transaction = new web3_js_1.Transaction().add(yield redeemVaultInstruction(program, payer.publicKey, payerUshAccount.address, payerTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, redeemAmount, transactionOverrideTime));
31
+ const transaction = new web3_js_1.Transaction().add(yield redeemVaultInstruction(program, payer.publicKey, payerUshAccount.address, payerTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, redeemAmount, transactionOverrideTime));
33
32
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
34
33
  return vaultPublicKey;
35
34
  });
@@ -32,7 +32,7 @@ function refreshOraclePriceInstruction(program, collateralType, network, overrid
32
32
  .accounts({
33
33
  oracleInfoAccount: oracleInfoAccount,
34
34
  vaultTypeAccount: vaultTypeAccount,
35
- oracleChainlink: chainlinkAccunts[network],
35
+ oracleChainlink: chainlinkAccounts[network],
36
36
  oraclePyth: pythAccounts[network],
37
37
  oracleSwitchboard: switchboardAccounts[network],
38
38
  systemProgram: web3_js_1.SystemProgram.programId,
@@ -52,7 +52,7 @@ const pythAccounts = {
52
52
  Devnet: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
53
53
  MainnetBeta: new web3_js_1.PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG'),
54
54
  };
55
- const chainlinkAccunts = {
55
+ const chainlinkAccounts = {
56
56
  Testing: web3_js_1.SystemProgram.programId,
57
57
  Devnet: new web3_js_1.PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
58
58
  MainnetBeta: web3_js_1.SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
@@ -21,13 +21,12 @@ function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overr
21
21
  // Prep the user to get USH back out at some point
22
22
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
23
23
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
24
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
25
- const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
26
- const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint);
24
+ const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
25
+ const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultAccount.vaultType, vaultTypeAccount.collateralMint);
27
26
  const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultPublicKey, vaultTypeAccount.collateralMint);
28
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, repayAmount * -1, false, false);
27
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, repayAmount * -1, false, false);
29
28
  const history = web3_js_1.Keypair.generate();
30
- const transaction = new web3_js_1.Transaction().add(yield repayVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, repayAmount, overrideTime));
29
+ 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, repayAmount, overrideTime));
31
30
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
32
31
  return vaultPublicKey;
33
32
  });
@@ -24,15 +24,14 @@ function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount,
24
24
  const history = web3_js_1.Keypair.generate();
25
25
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
26
26
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
27
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
28
27
  const vaultAssociatedCollateralAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, serum_1.TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, true);
29
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
30
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
28
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
29
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
31
30
  const destinationTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
32
31
  const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
33
32
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
34
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, withdrawAmount * -1, 0, false, false);
35
- const transaction = new web3_js_1.Transaction().add(yield withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, destinationTokenAccount.address, vaultPublicKey, vaultAssociatedCollateralAccount.address, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMintPublickey, history.publicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime));
33
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, withdrawAmount * -1, 0, false, false);
34
+ 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, withdrawAmount, overrideTime));
36
35
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
37
36
  return vaultPublicKey;
38
37
  });
@@ -33,11 +33,11 @@ const borsh = __importStar(require("@project-serum/borsh"));
33
33
  class VaultAccount {
34
34
  constructor(vault, publicKey) {
35
35
  var _a, _b, _c;
36
- /** Debt redistribution snapshop */
36
+ /** Debt redistribution snapshot */
37
37
  this.debtProductSnapshotBytes = new decimal_js_1.default(0);
38
- /** Collateral redistribution snapshop' */
38
+ /** Collateral redistribution snapshot' */
39
39
  this.collateralAccumulatorSnapshotBytes = new decimal_js_1.default(0);
40
- /** Current State of the vautl ("Open", "Closed", "Liquidated") */
40
+ /** Current State of the vault ("Open", "Closed", "Liquidated") */
41
41
  this.vaultStatus = '';
42
42
  this.publicKey = publicKey;
43
43
  this.vaultOwner = vault.vaultOwner;
@@ -51,11 +51,12 @@ class VaultAccount {
51
51
  if (vault.collateralAccumulatorSnapshotBytes) {
52
52
  this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
53
53
  }
54
- this.collateralType = vault.collateralType;
54
+ this.vaultTypeName = vault.vaultTypeName;
55
55
  this.nextVaultToRedeem = vault.nextVaultToRedeem;
56
56
  if (vault.vaultStatus) {
57
57
  this.vaultStatus = Object.keys(vault.vaultStatus)[0];
58
58
  }
59
+ this.vaultType = vault.vaultType;
59
60
  }
60
61
  /**
61
62
  * Check if some `PublicKey` is the owner
@@ -110,9 +111,9 @@ class VaultAccount {
110
111
  this.denormalizedDebt = 0;
111
112
  this.vaultStatus = 'liquidated';
112
113
  }
113
- updateDebtAndCollateral(vaultTypeAccuntData) {
114
- const debtProductCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.debtRedistributionProduct);
115
- const collateralAccumulatorCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.collateralRedistributionAccumulator);
114
+ updateDebtAndCollateral(vaultTypeAccountData) {
115
+ const debtProductCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccountData.debtRedistributionProduct);
116
+ const collateralAccumulatorCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccountData.collateralRedistributionAccumulator);
116
117
  this.denormalizedDebt = debtProductCurrent
117
118
  .div(this.debtProductSnapshotBytes)
118
119
  .mul(new decimal_js_1.default(this.denormalizedDebt))
@@ -135,7 +136,7 @@ class VaultAccount {
135
136
  if (this.nextVaultToRedeem) {
136
137
  nextVault = this.nextVaultToRedeem.toString().substring(0, 6);
137
138
  }
138
- return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `;
139
+ return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `;
139
140
  }
140
141
  /**
141
142
  * Creates a VaultAccount from a slice of data