hedge-web3 0.1.14 → 0.1.17
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.
- package/{lib/types/src → declarations}/Constants.d.ts +2 -3
- package/{lib/types/src → declarations}/HedgeDecimal.d.ts +2 -3
- package/{lib/types/src → declarations}/StakingPools.d.ts +0 -1
- package/{lib/types/src → declarations}/Vaults.d.ts +0 -1
- package/{lib/types/src → declarations}/idl/idl.d.ts +0 -1
- package/declarations/idl/vault.d.ts +2283 -0
- package/{lib/types/src → declarations}/index.d.ts +2 -1
- package/{lib/types/src → declarations}/instructions/claimLiquidationPoolPosition.d.ts +1 -2
- package/{lib/types/src → declarations}/instructions/closeLiquidationPoolPosition.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/createStakingPool.d.ts +0 -1
- package/declarations/instructions/createVault.d.ts +5 -0
- package/{lib/types/src → declarations}/instructions/depositLiquidationPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/depositStakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/depositVault.d.ts +2 -3
- package/declarations/instructions/initHedgeFoundation.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/liquidateVault.d.ts +2 -3
- package/{lib/types/src → declarations}/instructions/loanVault.d.ts +2 -3
- package/{lib/types/src → declarations}/instructions/redeemVault.d.ts +1 -2
- package/{lib/types/src → declarations}/instructions/refreshOraclePrice.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/repayVault.d.ts +2 -3
- package/declarations/instructions/setHalted.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/withdrawStakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/withdrawVault.d.ts +2 -3
- package/{lib/types/src → declarations}/state/LiquidationPoolEra.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPoolState.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPosition.d.ts +1 -2
- package/{lib/types/src → declarations}/state/StakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/state/StakingPoolPosition.d.ts +0 -1
- package/{lib/types/src → declarations}/state/VaultAccount.d.ts +7 -5
- package/{lib/types/src → declarations}/state/VaultHistoryEvent.d.ts +0 -1
- package/{lib/types/src → declarations}/utils/Errors.d.ts +0 -1
- package/lib/Constants.js +86 -0
- package/lib/HedgeDecimal.js +24 -0
- package/lib/StakingPools.js +15 -0
- package/lib/Vaults.js +20 -0
- package/lib/idl/idl.js +1475 -0
- package/lib/idl/vault.js +2285 -0
- package/lib/index.js +36 -2382
- package/lib/instructions/claimLiquidationPoolPosition.js +53 -0
- package/lib/instructions/closeLiquidationPoolPosition.js +60 -0
- package/lib/instructions/createStakingPool.js +52 -0
- package/lib/instructions/createVault.js +152 -0
- package/lib/instructions/depositLiquidationPool.js +55 -0
- package/lib/instructions/depositStakingPool.js +52 -0
- package/lib/instructions/depositVault.js +87 -0
- package/lib/instructions/initHedgeFoundation.js +55 -0
- package/lib/instructions/liquidateVault.js +80 -0
- package/lib/instructions/loanVault.js +61 -0
- package/lib/instructions/redeemVault.js +65 -0
- package/lib/instructions/refreshOraclePrice.js +65 -0
- package/lib/instructions/repayVault.js +62 -0
- package/lib/instructions/setHalted.js +37 -0
- package/lib/instructions/withdrawStakingPool.js +64 -0
- package/lib/instructions/withdrawVault.js +62 -0
- package/lib/state/LiquidationPoolEra.js +19 -0
- package/lib/state/LiquidationPoolState.js +12 -0
- package/lib/state/LiquidationPosition.js +39 -0
- package/lib/state/StakingPool.js +22 -0
- package/lib/state/StakingPoolPosition.js +28 -0
- package/lib/state/VaultAccount.js +55 -0
- package/lib/state/VaultHistoryEvent.js +45 -0
- package/lib/utils/Errors.js +14 -0
- package/package.json +9 -7
- package/src/Constants.ts +5 -4
- package/src/HedgeDecimal.ts +3 -3
- package/src/idl/vault.ts +4565 -0
- package/src/index.ts +2 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +19 -11
- package/src/instructions/closeLiquidationPoolPosition.ts +7 -13
- package/src/instructions/createVault.ts +224 -57
- package/src/instructions/depositLiquidationPool.ts +6 -9
- package/src/instructions/depositStakingPool.ts +3 -1
- package/src/instructions/depositVault.ts +41 -29
- package/src/instructions/initHedgeFoundation.ts +64 -0
- package/src/instructions/liquidateVault.ts +30 -33
- package/src/instructions/loanVault.ts +22 -24
- package/src/instructions/redeemVault.ts +15 -28
- package/src/instructions/refreshOraclePrice.ts +2 -2
- package/src/instructions/repayVault.ts +20 -22
- package/src/instructions/setHalted.ts +48 -0
- package/src/instructions/withdrawVault.ts +36 -26
- package/src/state/LiquidationPosition.ts +2 -2
- package/src/state/VaultAccount.ts +20 -9
- package/tsconfig.json +3 -2
- package/.github/workflows/npm-publish.yml +0 -34
- package/README.md +0 -44
- package/jsconfig.json +0 -12
- package/lib/index.js.map +0 -1
- package/lib/types/src/Constants.d.ts.map +0 -1
- package/lib/types/src/HedgeDecimal.d.ts.map +0 -1
- package/lib/types/src/StakingPools.d.ts.map +0 -1
- package/lib/types/src/Vaults.d.ts.map +0 -1
- package/lib/types/src/idl/idl.d.ts.map +0 -1
- package/lib/types/src/index.d.ts.map +0 -1
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +0 -1
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +0 -1
- package/lib/types/src/instructions/createStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/createVault.d.ts +0 -5
- package/lib/types/src/instructions/createVault.d.ts.map +0 -1
- package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositVault.d.ts.map +0 -1
- package/lib/types/src/instructions/liquidateVault.d.ts.map +0 -1
- package/lib/types/src/instructions/loanVault.d.ts.map +0 -1
- package/lib/types/src/instructions/redeemVault.d.ts.map +0 -1
- package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +0 -1
- package/lib/types/src/instructions/repayVault.d.ts.map +0 -1
- package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/withdrawVault.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPoolState.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPosition.d.ts.map +0 -1
- package/lib/types/src/state/StakingPool.d.ts.map +0 -1
- package/lib/types/src/state/StakingPoolPosition.d.ts.map +0 -1
- package/lib/types/src/state/VaultAccount.d.ts.map +0 -1
- package/lib/types/src/state/VaultHistoryEvent.d.ts.map +0 -1
- package/lib/types/src/utils/Errors.d.ts.map +0 -1
- package/lib/types/tsconfig.base.tsbuildinfo +0 -1
- package/rollup.config.js +0 -42
@@ -0,0 +1,55 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.VaultAccount = void 0;
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
5
|
+
const HedgeDecimal_1 = require("../HedgeDecimal");
|
6
|
+
/**
|
7
|
+
* A class that represents an on-chian vault.
|
8
|
+
*/
|
9
|
+
class VaultAccount {
|
10
|
+
constructor(vault, publicKey) {
|
11
|
+
this.publicKey = publicKey;
|
12
|
+
this.vaultOwner = vault.vaultOwner;
|
13
|
+
this.pdaSalt = vault.pdaSalt;
|
14
|
+
this.deposited = vault.deposited.toNumber();
|
15
|
+
this.denormalizedDebt = vault.denormalizedDebt.toNumber();
|
16
|
+
this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
|
17
|
+
this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
|
18
|
+
this.collateralType = vault.collateralType;
|
19
|
+
this.vaultStatus = Object.keys(vault.vaultStatus)[0];
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Check if some `PublicKey` is the owner
|
23
|
+
*
|
24
|
+
* @param publicKey the publicKey to check against the vault owner
|
25
|
+
* @returns true if publicKey matches the owner publicKey
|
26
|
+
*/
|
27
|
+
isOwnedBy(publicKey) {
|
28
|
+
return publicKey && publicKey.toString() === this.vaultOwner.toString();
|
29
|
+
}
|
30
|
+
/**
|
31
|
+
* Get the collateral value in SOL
|
32
|
+
*
|
33
|
+
* @returns collateral value in SOL
|
34
|
+
*/
|
35
|
+
inSol() {
|
36
|
+
return this.deposited / web3_js_1.LAMPORTS_PER_SOL;
|
37
|
+
}
|
38
|
+
/**
|
39
|
+
* Get the debt value in USDH
|
40
|
+
*
|
41
|
+
* @returns debt value in USDH
|
42
|
+
*/
|
43
|
+
inUsd() {
|
44
|
+
return this.denormalizedDebt / web3_js_1.LAMPORTS_PER_SOL;
|
45
|
+
}
|
46
|
+
/**
|
47
|
+
* Pretty print the vault publickey for easy display
|
48
|
+
*
|
49
|
+
* @returns example: `1b6ca...azy71s`
|
50
|
+
*/
|
51
|
+
toDisplayString() {
|
52
|
+
return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
exports.VaultAccount = VaultAccount;
|
@@ -0,0 +1,45 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.VaultHistoryAction = exports.VaultStatus = exports.VaultHistoryEvent = void 0;
|
4
|
+
const HedgeDecimal_1 = require("../HedgeDecimal");
|
5
|
+
class VaultHistoryEvent {
|
6
|
+
constructor(account, publicKey) {
|
7
|
+
this.account = account;
|
8
|
+
this.publicKey = publicKey;
|
9
|
+
this.actorAccount = account.actorAccount;
|
10
|
+
this.usdSolPrice = (0, HedgeDecimal_1.DecimalFromU128)(account.usdSolPrice.toString());
|
11
|
+
this.usdhDebtBefore = account.usdhDebtBefore.toNumber();
|
12
|
+
this.usdhDebtAfter = account.usdhDebtAfter.toNumber();
|
13
|
+
this.solBalanceBefore = account.solBalanceBefore.toNumber();
|
14
|
+
this.solBalanceAfter = account.solBalanceAfter.toNumber();
|
15
|
+
this.minCollateralRatioBefore = (0, HedgeDecimal_1.DecimalFromU128)(account.minCollateralRatioBefore);
|
16
|
+
this.minCollateralRatioAfter = (0, HedgeDecimal_1.DecimalFromU128)(account.minCollateralRatioAfter);
|
17
|
+
this.vaultStateBefore = account.vaultStateBefore;
|
18
|
+
this.vaultStateAfter = account.vaultStateAfter;
|
19
|
+
this.timestamp = account.timestamp.toNumber();
|
20
|
+
this.action = account.action;
|
21
|
+
}
|
22
|
+
}
|
23
|
+
exports.VaultHistoryEvent = VaultHistoryEvent;
|
24
|
+
var VaultStatus;
|
25
|
+
(function (VaultStatus) {
|
26
|
+
VaultStatus[VaultStatus["Open"] = 0] = "Open";
|
27
|
+
VaultStatus[VaultStatus["Closed"] = 1] = "Closed";
|
28
|
+
VaultStatus[VaultStatus["Liquidated"] = 2] = "Liquidated";
|
29
|
+
VaultStatus[VaultStatus["Distributed"] = 3] = "Distributed";
|
30
|
+
VaultStatus[VaultStatus["Redeemed"] = 4] = "Redeemed";
|
31
|
+
})(VaultStatus = exports.VaultStatus || (exports.VaultStatus = {}));
|
32
|
+
var VaultHistoryAction;
|
33
|
+
(function (VaultHistoryAction) {
|
34
|
+
VaultHistoryAction[VaultHistoryAction["Create"] = 0] = "Create";
|
35
|
+
VaultHistoryAction[VaultHistoryAction["Close"] = 1] = "Close";
|
36
|
+
VaultHistoryAction[VaultHistoryAction["Liquidate"] = 2] = "Liquidate";
|
37
|
+
VaultHistoryAction[VaultHistoryAction["PartialLiquidate"] = 3] = "PartialLiquidate";
|
38
|
+
VaultHistoryAction[VaultHistoryAction["Distributed"] = 4] = "Distributed";
|
39
|
+
VaultHistoryAction[VaultHistoryAction["Redeem"] = 5] = "Redeem";
|
40
|
+
VaultHistoryAction[VaultHistoryAction["TransferOwnership"] = 6] = "TransferOwnership";
|
41
|
+
VaultHistoryAction[VaultHistoryAction["Deposit"] = 7] = "Deposit";
|
42
|
+
VaultHistoryAction[VaultHistoryAction["Withdraw"] = 8] = "Withdraw";
|
43
|
+
VaultHistoryAction[VaultHistoryAction["Loan"] = 9] = "Loan";
|
44
|
+
VaultHistoryAction[VaultHistoryAction["RepayCredit"] = 10] = "RepayCredit";
|
45
|
+
})(VaultHistoryAction = exports.VaultHistoryAction || (exports.VaultHistoryAction = {}));
|
@@ -0,0 +1,14 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.parseAnchorErrors = void 0;
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
5
|
+
const idl_1 = require("../idl/idl");
|
6
|
+
function parseAnchorErrors(error) {
|
7
|
+
const idlErrors = (0, anchor_1.parseIdlErrors)(idl_1.vaultIdl);
|
8
|
+
const parsedError = anchor_1.ProgramError.parse(error, idlErrors);
|
9
|
+
if (parsedError !== null) {
|
10
|
+
throw parsedError;
|
11
|
+
}
|
12
|
+
throw error;
|
13
|
+
}
|
14
|
+
exports.parseAnchorErrors = parseAnchorErrors;
|
package/package.json
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
{
|
2
2
|
"name": "hedge-web3",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.17",
|
4
4
|
"description": "Hedge Javascript Web3 API",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"scripts": {
|
7
|
-
"build": "
|
7
|
+
"build": "tsc",
|
8
|
+
"dev": "tsc --watch",
|
8
9
|
"clean": "rimraf ./lib",
|
9
|
-
"dev": "rollup -c -w",
|
10
10
|
"docs": "set -ex; typedoc --includeVersion",
|
11
11
|
"test": "echo \"Error: no test specified\" && exit 1"
|
12
12
|
},
|
13
|
-
"author": "Chris Coudron <coudron@
|
13
|
+
"author": "Chris Coudron <coudron@hedge.so>",
|
14
14
|
"license": "ISC",
|
15
15
|
"devDependencies": {
|
16
16
|
"@rollup/plugin-typescript": "^8.3.0",
|
@@ -19,13 +19,15 @@
|
|
19
19
|
"typedoc": "^0.22.10"
|
20
20
|
},
|
21
21
|
"dependencies": {
|
22
|
-
"@project-serum/anchor": "^0.
|
22
|
+
"@project-serum/anchor": "^0.21.0",
|
23
23
|
"@project-serum/serum": "^0.13.61",
|
24
|
-
"@solana/
|
25
|
-
"@solana/
|
24
|
+
"@solana/buffer-layout": "^4.0.0",
|
25
|
+
"@solana/spl-token": "^0.2.0",
|
26
|
+
"@solana/web3.js": "^1.35.0",
|
26
27
|
"@types/uuid": "^8.3.4",
|
27
28
|
"bn.js": "^5.2.0",
|
28
29
|
"decimal.js": "^10.3.1",
|
30
|
+
"typescript": "^4.5.5",
|
29
31
|
"uuid": "^8.3.2"
|
30
32
|
}
|
31
33
|
}
|
package/src/Constants.ts
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
2
2
|
import { PublicKey } from '@solana/web3.js'
|
3
3
|
|
4
|
-
export const HEDGE_PROGRAM_ID = '
|
4
|
+
export const HEDGE_PROGRAM_ID = 'h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd'
|
5
5
|
export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
|
6
6
|
|
7
7
|
export const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
|
8
8
|
export const CHAINLINK_SOL_USD_PUBLICKEY = new PublicKey(CHAINLINK_SOL_USD_ID)
|
9
|
+
|
9
10
|
const enc = new TextEncoder()
|
10
11
|
|
11
12
|
export async function getLiquidationPoolStatePublicKey (): Promise<PublicKey> {
|
@@ -38,9 +39,9 @@ export async function getPoolPublicKeyForMint (mintPublicKey: PublicKey): Promis
|
|
38
39
|
const [publicKey, bump] = await PublicKey.findProgramAddress([enc.encode(strToEncode)], HEDGE_PROGRAM_PUBLICKEY)
|
39
40
|
return [publicKey, bump, strToEncode]
|
40
41
|
}
|
41
|
-
export async function
|
42
|
-
const [
|
43
|
-
return
|
42
|
+
export async function getVaultTypeAccountPublicKey (collateralType: string): Promise<PublicKey> {
|
43
|
+
const [vaultTypeAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
|
44
|
+
return vaultTypeAccount
|
44
45
|
}
|
45
46
|
export async function findVaultAddress (vaultSalt: string): Promise<PublicKey> {
|
46
47
|
const [vaultAddress] = await PublicKey.findProgramAddress([enc.encode('Vault'), enc.encode(vaultSalt)], HEDGE_PROGRAM_PUBLICKEY)
|
package/src/HedgeDecimal.ts
CHANGED
@@ -5,14 +5,14 @@ import Decimal from 'decimal.js'
|
|
5
5
|
*
|
6
6
|
* On chain, u128s are used to store Decimal numbers. These values
|
7
7
|
* are simply stored as u128 where the actual value is divided by
|
8
|
-
* 1.0e+
|
8
|
+
* 1.0e+18. This gives 24 digits of percision on decimal numbers.
|
9
9
|
*
|
10
|
-
* Example: the number 1.5 is stored as a u128: 1.5 * 10^
|
10
|
+
* Example: the number 1.5 is stored as a u128: 1.5 * 10^18
|
11
11
|
*
|
12
12
|
* @param value the value to be converted to Decimal
|
13
13
|
* @returns the converted value as a decimal.js object
|
14
14
|
*/
|
15
15
|
export function DecimalFromU128 (value: number|string): Decimal {
|
16
16
|
const adjustedValue = new Decimal(value.toString())
|
17
|
-
return adjustedValue.div(new Decimal('1e+
|
17
|
+
return adjustedValue.div(new Decimal('1e+18'))
|
18
18
|
}
|