hedge-web3 0.1.25 → 0.1.28
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/Constants.d.ts +3 -3
- package/declarations/idl/vault.d.ts +889 -752
- package/declarations/index.d.ts +2 -0
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
- package/declarations/instructions/createVault.d.ts +1 -1
- package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
- package/declarations/instructions/depositVault.d.ts +1 -1
- package/declarations/instructions/liquidateVault.d.ts +1 -1
- package/declarations/instructions/loanVault.d.ts +1 -1
- package/declarations/instructions/redeemVault.d.ts +1 -1
- package/declarations/instructions/repayVault.d.ts +1 -1
- package/declarations/instructions/setVaultTypeStatus.d.ts +4 -0
- package/declarations/instructions/withdrawVault.d.ts +1 -1
- package/declarations/state/LiquidationPosition.d.ts +2 -2
- package/declarations/state/StakingPool.d.ts +1 -1
- package/declarations/state/StakingPoolPosition.d.ts +2 -2
- package/declarations/state/VaultAccount.d.ts +11 -3
- package/declarations/state/VaultHistoryEvent.d.ts +2 -2
- package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
- package/lib/Constants.js +10 -10
- package/lib/idl/vault.js +922 -785
- package/lib/index.js +2 -0
- package/lib/instructions/closeLiquidationPoolPosition.js +9 -9
- package/lib/instructions/createStakingPool.js +6 -6
- package/lib/instructions/createVault.js +11 -11
- package/lib/instructions/depositLiquidationPool.js +9 -9
- package/lib/instructions/depositVault.js +20 -13
- package/lib/instructions/initHedgeFoundation.js +4 -4
- package/lib/instructions/liquidateVault.js +16 -11
- package/lib/instructions/loanVault.js +16 -11
- package/lib/instructions/redeemVault.js +15 -10
- package/lib/instructions/repayVault.js +17 -12
- package/lib/instructions/setVaultTypeStatus.js +38 -0
- package/lib/instructions/withdrawStakingPool.js +6 -6
- package/lib/instructions/withdrawVault.js +18 -13
- package/lib/state/LiquidationPosition.js +2 -2
- package/lib/state/StakingPool.js +1 -1
- package/lib/state/StakingPoolPosition.js +3 -3
- package/lib/state/VaultAccount.js +56 -3
- package/lib/state/VaultHistoryEvent.js +2 -2
- package/lib/utils/Errors.js +2 -2
- package/lib/utils/getLinkedListAccounts.js +131 -0
- package/package.json +3 -1
- package/src/Constants.ts +73 -31
- package/src/idl/vault.ts +1848 -1574
- package/src/index.ts +3 -0
- package/src/instructions/closeLiquidationPoolPosition.ts +10 -10
- package/src/instructions/createStakingPool.ts +6 -7
- package/src/instructions/createVault.ts +17 -17
- package/src/instructions/depositLiquidationPool.ts +10 -10
- package/src/instructions/depositVault.ts +104 -29
- package/src/instructions/initHedgeFoundation.ts +5 -5
- package/src/instructions/initHedgeFoundationTokens.ts +1 -1
- package/src/instructions/liquidateVault.ts +124 -27
- package/src/instructions/loanVault.ts +97 -25
- package/src/instructions/redeemVault.ts +35 -12
- package/src/instructions/repayVault.ts +91 -26
- package/src/instructions/setHalted.ts +1 -1
- package/src/instructions/setVaultTypeStatus.ts +50 -0
- package/src/instructions/withdrawStakingPool.ts +7 -7
- package/src/instructions/withdrawVault.ts +106 -28
- package/src/state/LiquidationPosition.ts +3 -3
- package/src/state/StakingPool.ts +2 -6
- package/src/state/StakingPoolPosition.ts +4 -4
- package/src/state/VaultAccount.ts +89 -13
- package/src/state/VaultHistoryEvent.ts +4 -4
- package/src/utils/Errors.ts +2 -2
- package/src/utils/getLinkedListAccounts.ts +156 -0
- package/declarations/idl/idl.d.ts +0 -2
- package/lib/idl/idl.js +0 -1475
- package/src/idl/idl.ts +0 -1474
@@ -0,0 +1,38 @@
|
|
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.setVaultTypeStatusInstruction = exports.setVaultTypeStatus = void 0;
|
13
|
+
const web3_js_1 = require("@solana/web3.js");
|
14
|
+
const Constants_1 = require("../Constants");
|
15
|
+
const Errors_1 = require("../utils/Errors");
|
16
|
+
function setVaultTypeStatus(program, provider, payer, vaultTypeAccount, deprecated) {
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
18
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
19
|
+
const transaction = new web3_js_1.Transaction().add(yield setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, deprecated));
|
20
|
+
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer], provider === null || provider === void 0 ? void 0 : provider.opts).catch(Errors_1.parseAnchorErrors);
|
21
|
+
return vaultSystemStatePublicKey;
|
22
|
+
});
|
23
|
+
}
|
24
|
+
exports.setVaultTypeStatus = setVaultTypeStatus;
|
25
|
+
function setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultTypeAccount, deprecated) {
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
27
|
+
const ix = program.instruction.setVaultTypeStatus(deprecated, {
|
28
|
+
accounts: {
|
29
|
+
payer: payerPublicKey,
|
30
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
31
|
+
vaultType: vaultTypeAccount,
|
32
|
+
},
|
33
|
+
signers: []
|
34
|
+
});
|
35
|
+
return ix;
|
36
|
+
});
|
37
|
+
}
|
38
|
+
exports.setVaultTypeStatusInstruction = setVaultTypeStatusInstruction;
|
@@ -27,14 +27,14 @@ exports.withdrawStakingPool = withdrawStakingPool;
|
|
27
27
|
function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
29
29
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
30
|
-
const
|
30
|
+
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
31
31
|
const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
|
32
32
|
const [poolPublickey] = yield (0, Constants_1.getPoolPublicKeyForMint)(stakedTokenMintPublicKey);
|
33
33
|
const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, stakedTokenMintPublicKey);
|
34
|
-
const
|
34
|
+
const poolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, ushMintPublickey);
|
35
35
|
const payerAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, stakedTokenMintPublicKey);
|
36
36
|
const payerAssociatedHedgeAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, hedgeMintPublickey);
|
37
|
-
const
|
37
|
+
const payerAssociatedUshAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, ushMintPublickey);
|
38
38
|
const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultSystemStatePublicKey, hedgeMintPublickey);
|
39
39
|
return program.instruction.withdrawStakingPool(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), // override current time
|
40
40
|
{
|
@@ -44,14 +44,14 @@ function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPub
|
|
44
44
|
pool: poolPublickey,
|
45
45
|
poolPosition: poolPositionPublicKey,
|
46
46
|
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
47
|
-
|
47
|
+
poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
|
48
48
|
payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
|
49
49
|
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
50
|
-
|
50
|
+
payerAssociatedUshAccount: payerAssociatedUshAccount,
|
51
51
|
communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
|
52
52
|
hedgeMint: hedgeMintPublickey,
|
53
53
|
stakedTokenMint: stakedTokenMintPublicKey,
|
54
|
-
|
54
|
+
ushMint: ushMintPublickey,
|
55
55
|
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
56
56
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
57
57
|
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
|
@@ -15,28 +15,30 @@ const spl_token_1 = require("@solana/spl-token");
|
|
15
15
|
const serum_1 = require("@project-serum/serum");
|
16
16
|
const web3_js_1 = require("@solana/web3.js");
|
17
17
|
const Constants_1 = require("../Constants");
|
18
|
+
const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
|
18
19
|
function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount, overrideTime) {
|
19
20
|
return __awaiter(this, void 0, void 0, function* () {
|
20
|
-
const
|
21
|
-
// Prep the user to get
|
22
|
-
yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer,
|
21
|
+
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
22
|
+
// Prep the user to get USH back out at some point
|
23
|
+
yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
23
24
|
const history = web3_js_1.Keypair.generate();
|
24
25
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
25
26
|
const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
|
26
|
-
const
|
27
|
+
const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
|
27
28
|
const vaultAssociatedCollateralAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, serum_1.TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, true);
|
28
|
-
const vaultTypeAccountInfo = yield program.account.vaultType.fetch(
|
29
|
-
const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint,
|
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);
|
30
31
|
const destinationTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
|
31
32
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
|
32
|
-
const
|
33
|
-
const
|
33
|
+
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));
|
34
36
|
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history], provider.opts);
|
35
37
|
return vaultPublicKey;
|
36
38
|
});
|
37
39
|
}
|
38
40
|
exports.withdrawVault = withdrawVault;
|
39
|
-
function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey,
|
41
|
+
function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMint, historyPublicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime) {
|
40
42
|
return __awaiter(this, void 0, void 0, function* () {
|
41
43
|
return program.instruction.withdrawVault(new anchor_1.BN(withdrawAmount), new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
|
42
44
|
{
|
@@ -47,15 +49,18 @@ function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPubli
|
|
47
49
|
vault: vaultPublickey,
|
48
50
|
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
49
51
|
feePool: hedgeStakingPoolPublicKey,
|
50
|
-
|
51
|
-
|
52
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
53
|
+
ushMint: ushMint,
|
52
54
|
history: historyPublicKey,
|
53
55
|
vaultOwner: payerPublicKey,
|
54
56
|
destinationTokenAccount: destinationTokenAccount,
|
57
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
58
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
59
|
+
newLargerVaultInfo: newLargerPublicKey,
|
55
60
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
56
|
-
systemProgram: web3_js_1.SystemProgram.programId
|
61
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
57
62
|
},
|
58
|
-
signers: []
|
63
|
+
signers: [],
|
59
64
|
});
|
60
65
|
});
|
61
66
|
}
|
@@ -12,7 +12,7 @@ class LiquidationPosition {
|
|
12
12
|
this.eraPublicKey = poolPositionInfo.era;
|
13
13
|
this.ownerAccount = poolPositionInfo.ownerAccount;
|
14
14
|
this.deposit = poolPositionInfo.deposit.toNumber();
|
15
|
-
this.
|
15
|
+
this.closedUsh = poolPositionInfo.closedUsh.toNumber();
|
16
16
|
this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
|
17
17
|
this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
|
18
18
|
this.productSnapshotEntry = (0, HedgeDecimal_1.DecimalFromU128)(poolPositionInfo.productSnapshotEntry);
|
@@ -23,7 +23,7 @@ class LiquidationPosition {
|
|
23
23
|
this.open = poolPositionInfo.state === 1;
|
24
24
|
console.log("poolPositionInfo.state, poolPositionInfo.state");
|
25
25
|
}
|
26
|
-
|
26
|
+
getUshAvailable(era) {
|
27
27
|
return (era.product.div(this.productSnapshotEntry)).mul(new decimal_js_1.default(this.deposit));
|
28
28
|
}
|
29
29
|
getTokensAvailable(era, index) {
|
package/lib/state/StakingPool.js
CHANGED
@@ -14,7 +14,7 @@ class StakingPool {
|
|
14
14
|
this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber();
|
15
15
|
this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber();
|
16
16
|
this.hedgeRewardAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.hedgeRewardAccumulator);
|
17
|
-
this.
|
17
|
+
this.ushFeeAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.ushFeeAccumulator);
|
18
18
|
this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum) => { return (0, HedgeDecimal_1.DecimalFromU128)(sum); });
|
19
19
|
// this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
|
20
20
|
}
|
@@ -17,12 +17,12 @@ class StakingPoolPosition {
|
|
17
17
|
this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
|
18
18
|
this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber();
|
19
19
|
this.startHedgeRewardAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolPositionInfo.startHedgeRewardAccumulator);
|
20
|
-
this.
|
20
|
+
this.startUshFeeAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolPositionInfo.startUshFeeAccumulator);
|
21
21
|
this.startSolFeeAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolPositionInfo.startSolFeeAccumulator);
|
22
22
|
this.open = poolPositionInfo.state.open !== undefined;
|
23
23
|
}
|
24
|
-
|
25
|
-
return this.pool.
|
24
|
+
getCurrentUshFeeReward() {
|
25
|
+
return this.pool.ushFeeAccumulator.minus(this.startUshFeeAccumulator).times(new decimal_js_1.default(this.deposited));
|
26
26
|
}
|
27
27
|
}
|
28
28
|
exports.StakingPoolPosition = StakingPoolPosition;
|
@@ -1,7 +1,11 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
6
|
exports.VaultAccount = void 0;
|
4
7
|
const web3_js_1 = require("@solana/web3.js");
|
8
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
5
9
|
const HedgeDecimal_1 = require("../HedgeDecimal");
|
6
10
|
/**
|
7
11
|
* A class that represents an on-chian vault.
|
@@ -10,6 +14,7 @@ class VaultAccount {
|
|
10
14
|
constructor(vault, publicKey) {
|
11
15
|
this.publicKey = publicKey;
|
12
16
|
this.vaultOwner = vault.vaultOwner;
|
17
|
+
this.vaultNumber = vault.vaultNumber.toNumber();
|
13
18
|
this.pdaSalt = vault.pdaSalt;
|
14
19
|
this.deposited = vault.deposited.toNumber();
|
15
20
|
this.denormalizedDebt = vault.denormalizedDebt.toNumber();
|
@@ -36,9 +41,9 @@ class VaultAccount {
|
|
36
41
|
return this.deposited / web3_js_1.LAMPORTS_PER_SOL;
|
37
42
|
}
|
38
43
|
/**
|
39
|
-
* Get the debt value in
|
44
|
+
* Get the debt value in USH
|
40
45
|
*
|
41
|
-
* @returns debt value in
|
46
|
+
* @returns debt value in USH
|
42
47
|
*/
|
43
48
|
inUsd() {
|
44
49
|
return this.denormalizedDebt / web3_js_1.LAMPORTS_PER_SOL;
|
@@ -49,7 +54,55 @@ class VaultAccount {
|
|
49
54
|
* @returns example: `1b6ca...azy71s`
|
50
55
|
*/
|
51
56
|
toDisplayString() {
|
52
|
-
return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey
|
57
|
+
return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey
|
58
|
+
.toString()
|
59
|
+
.substring(this.publicKey.toString().length - 6)}`;
|
60
|
+
}
|
61
|
+
addDebt(newNormalizedDebt, vaultTypeCompoundedInterest) {
|
62
|
+
const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest);
|
63
|
+
this.denormalizedDebt += denormalizedNewDebt.toNumber();
|
64
|
+
}
|
65
|
+
addDeposit(depositAmount) {
|
66
|
+
this.deposited += depositAmount;
|
67
|
+
}
|
68
|
+
redeem() {
|
69
|
+
// TODO - Calculate actual redeem amount and adust correctly
|
70
|
+
this.denormalizedDebt = 0;
|
71
|
+
this.vaultStatus = 'initialized';
|
72
|
+
}
|
73
|
+
liquidate() {
|
74
|
+
// TODO - Calculate actual liquidate amount and adust correctly
|
75
|
+
this.denormalizedDebt = 0;
|
76
|
+
this.vaultStatus = 'liquidated';
|
77
|
+
}
|
78
|
+
redistribution(vaultTypeAccuntData) {
|
79
|
+
const debtProductCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.debtRedistributionProduct);
|
80
|
+
const collateralAccumulatorCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.collateralRedistributionAccumulator);
|
81
|
+
this.denormalizedDebt = debtProductCurrent
|
82
|
+
.div(this.debtProductSnapshotBytes)
|
83
|
+
.mul(new decimal_js_1.default(this.denormalizedDebt))
|
84
|
+
// .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
|
85
|
+
.toNumber();
|
86
|
+
const extraCollateralDeposited = this.denormalizedDebt *
|
87
|
+
collateralAccumulatorCurrent
|
88
|
+
.sub(this.collateralAccumulatorSnapshotBytes)
|
89
|
+
.toNumber();
|
90
|
+
this.deposited += extraCollateralDeposited;
|
91
|
+
this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent;
|
92
|
+
this.debtProductSnapshotBytes = debtProductCurrent;
|
93
|
+
}
|
94
|
+
toString(highlight) {
|
95
|
+
let arrow = '';
|
96
|
+
if (this.publicKey.toString() === highlight.toString()) {
|
97
|
+
arrow = ' <----';
|
98
|
+
}
|
99
|
+
let collateralRatio = 'Infinite';
|
100
|
+
if (this.denormalizedDebt > 0) {
|
101
|
+
collateralRatio = (this.deposited / this.denormalizedDebt).toFixed(8);
|
102
|
+
}
|
103
|
+
return `Vault(${this.vaultNumber}): ${this.publicKey
|
104
|
+
.toString()
|
105
|
+
.substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`;
|
53
106
|
}
|
54
107
|
}
|
55
108
|
exports.VaultAccount = VaultAccount;
|
@@ -8,8 +8,8 @@ class VaultHistoryEvent {
|
|
8
8
|
this.publicKey = publicKey;
|
9
9
|
this.actorAccount = account.actorAccount;
|
10
10
|
this.usdSolPrice = (0, HedgeDecimal_1.DecimalFromU128)(account.usdSolPrice.toString());
|
11
|
-
this.
|
12
|
-
this.
|
11
|
+
this.ushDebtBefore = account.ushDebtBefore.toNumber();
|
12
|
+
this.ushDebtAfter = account.ushDebtAfter.toNumber();
|
13
13
|
this.collateralBalanceBefore = account.collateralBalanceBefore.toNumber();
|
14
14
|
this.collateralBalanceAfter = account.collateralBalanceAfter.toNumber();
|
15
15
|
this.minCollateralRatioBefore = (0, HedgeDecimal_1.DecimalFromU128)(account.minCollateralRatioBefore);
|
package/lib/utils/Errors.js
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.parseAnchorErrors = void 0;
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
5
|
-
const
|
5
|
+
const vault_1 = require("../idl/vault");
|
6
6
|
function parseAnchorErrors(error) {
|
7
|
-
const idlErrors = (0, anchor_1.parseIdlErrors)(
|
7
|
+
const idlErrors = (0, anchor_1.parseIdlErrors)(vault_1.IDL);
|
8
8
|
const parsedError = anchor_1.ProgramError.parse(error, idlErrors);
|
9
9
|
if (parsedError !== null) {
|
10
10
|
throw parsedError;
|
@@ -0,0 +1,131 @@
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.getLinkedListAccounts = void 0;
|
16
|
+
const underscore_1 = __importDefault(require("underscore"));
|
17
|
+
const HedgeDecimal_1 = require("../HedgeDecimal");
|
18
|
+
const VaultAccount_1 = require("../state/VaultAccount");
|
19
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
20
|
+
function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate) {
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
22
|
+
const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
|
23
|
+
// Default for null is the vault itself, so set them all to this vault
|
24
|
+
let oldSmallerPublicKey = vaultPublicKey;
|
25
|
+
let newSmallerPublicKey = vaultPublicKey;
|
26
|
+
let newLargerPublicKey = vaultPublicKey;
|
27
|
+
const thisVaultData = yield program.account.vault.fetch(vaultPublicKey);
|
28
|
+
const thisVault = new VaultAccount_1.VaultAccount(thisVaultData, vaultPublicKey);
|
29
|
+
// Load all the vaults
|
30
|
+
let allVaults = (yield program.account.vault
|
31
|
+
.all([
|
32
|
+
// {
|
33
|
+
// memcmp: { bytes: bs58.encode(inputCollateralType), offset: 8 + 32 + 8 },
|
34
|
+
// },
|
35
|
+
])
|
36
|
+
.catch((error) => {
|
37
|
+
console.log('error', error);
|
38
|
+
})) || [];
|
39
|
+
// Load them into our account objects
|
40
|
+
let vaults = allVaults.map((vault) => {
|
41
|
+
return new VaultAccount_1.VaultAccount(vault.account, vault.publicKey);
|
42
|
+
});
|
43
|
+
// Filter out the account that are not the same vault type
|
44
|
+
vaults = underscore_1.default.filter(vaults, (vault) => {
|
45
|
+
return vault.collateralType === vaultTypeAccount.collateralType;
|
46
|
+
});
|
47
|
+
// Filter out the accounts that are not open
|
48
|
+
vaults = underscore_1.default.filter(vaults, (vault) => {
|
49
|
+
return vault.vaultStatus === 'open';
|
50
|
+
});
|
51
|
+
// Sort them
|
52
|
+
vaults.sort(sortVaults);
|
53
|
+
// Find the location of the vault before the operation
|
54
|
+
let indexBefore = -1;
|
55
|
+
vaults.forEach((vault, index) => {
|
56
|
+
if (vault.publicKey.toString() === vaultPublicKey.toString()) {
|
57
|
+
indexBefore = index;
|
58
|
+
}
|
59
|
+
});
|
60
|
+
// If we found it before, set the old smaller vault the one to the left
|
61
|
+
if (indexBefore > 0) {
|
62
|
+
oldSmallerPublicKey = vaults[indexBefore - 1].publicKey;
|
63
|
+
}
|
64
|
+
// Pretty print all the vaults before the operation
|
65
|
+
// console.log('Sorted open vaults before')
|
66
|
+
// vaults.forEach((vault) => {
|
67
|
+
// console.log(vault.toString(vaultPublicKey))
|
68
|
+
// })
|
69
|
+
// If it wasn't in the list, add it now
|
70
|
+
if (indexBefore < 0) {
|
71
|
+
vaults.push(thisVault);
|
72
|
+
indexBefore = vaults.length - 1;
|
73
|
+
}
|
74
|
+
// Now that we know it's def in the list, iterate the list and update
|
75
|
+
// this vault with the opeation we're going to apply
|
76
|
+
const newNormalizedDebt = new decimal_js_1.default(loanAmount);
|
77
|
+
const vaultTypeCompoundedInterest = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccount.cumulativeRate.toString());
|
78
|
+
vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest);
|
79
|
+
vaults[indexBefore].addDeposit(depositAmount);
|
80
|
+
if (liquidate) {
|
81
|
+
vaults[indexBefore].liquidate();
|
82
|
+
}
|
83
|
+
if (redeem) {
|
84
|
+
vaults[indexBefore].redeem();
|
85
|
+
}
|
86
|
+
vaults[indexBefore].redistribution(vaultTypeAccount);
|
87
|
+
if (vaults[indexBefore].denormalizedDebt === 0) {
|
88
|
+
vaults.splice(indexBefore, 1);
|
89
|
+
}
|
90
|
+
// Sort it again since we've changed one vault
|
91
|
+
vaults = vaults.sort(sortVaults);
|
92
|
+
// Pretty print the list again
|
93
|
+
// console.log('Sorted open vaults with new debt added')
|
94
|
+
// vaults.forEach((vault) => {
|
95
|
+
// console.log(vault.toString(vaultPublicKey))
|
96
|
+
// })
|
97
|
+
// Search for the vaults new position
|
98
|
+
let indexAfter = -1;
|
99
|
+
vaults.forEach((vault, index) => {
|
100
|
+
if (vault.publicKey.toString() === vaultPublicKey.toString()) {
|
101
|
+
indexAfter = index;
|
102
|
+
}
|
103
|
+
});
|
104
|
+
// Print where it moved from / to
|
105
|
+
// console.log('Index Before', indexBefore)
|
106
|
+
// console.log('Index After', indexAfter)
|
107
|
+
// Save references to the new left and right
|
108
|
+
if (indexAfter > 0) {
|
109
|
+
newSmallerPublicKey = vaults[indexAfter - 1].publicKey;
|
110
|
+
}
|
111
|
+
if (indexAfter < vaults.length - 1 && indexAfter >= 0) {
|
112
|
+
newLargerPublicKey = vaults[indexAfter + 1].publicKey;
|
113
|
+
}
|
114
|
+
// Print out the new left/right
|
115
|
+
// console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
|
116
|
+
// console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
|
117
|
+
// console.log('newLargerPublicKey', newLargerPublicKey.toString())
|
118
|
+
return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey];
|
119
|
+
});
|
120
|
+
}
|
121
|
+
exports.getLinkedListAccounts = getLinkedListAccounts;
|
122
|
+
// Sort function we can use to sort the vaults
|
123
|
+
// Sorted by collateral ratio. If two are the same, newer vault first
|
124
|
+
function sortVaults(a, b) {
|
125
|
+
const aRatio = a.deposited / a.denormalizedDebt;
|
126
|
+
const bRatio = b.deposited / b.denormalizedDebt;
|
127
|
+
if (aRatio === bRatio) {
|
128
|
+
return b.vaultNumber - a.vaultNumber;
|
129
|
+
}
|
130
|
+
return aRatio - bRatio;
|
131
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "hedge-web3",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.28",
|
4
4
|
"description": "Hedge Javascript Web3 API",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"types": "declarations/index.d.ts",
|
@@ -21,6 +21,7 @@
|
|
21
21
|
"@solana/spl-token": "^0.2.0",
|
22
22
|
"@solana/web3.js": "^1.37.0",
|
23
23
|
"@types/bn.js": "^5.1.0",
|
24
|
+
"@types/underscore": "^1.11.4",
|
24
25
|
"@types/uuid": "^8.3.4",
|
25
26
|
"bn.js": "^5.2.0",
|
26
27
|
"decimal.js": "^10.3.1",
|
@@ -28,6 +29,7 @@
|
|
28
29
|
"ts-standard": "^11.0.0",
|
29
30
|
"typedoc": "^0.22.10",
|
30
31
|
"typescript": "^4.5.5",
|
32
|
+
"underscore": "^1.13.2",
|
31
33
|
"uuid": "^8.3.2"
|
32
34
|
}
|
33
35
|
}
|
package/src/Constants.ts
CHANGED
@@ -1,64 +1,106 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
3
|
+
TOKEN_PROGRAM_ID,
|
4
|
+
} from '@solana/spl-token'
|
2
5
|
import { PublicKey } from '@solana/web3.js'
|
3
6
|
|
4
|
-
export const HEDGE_PROGRAM_ID = '
|
7
|
+
export const HEDGE_PROGRAM_ID = 'HDG3uyafYaKxSYRW37ZBTdxaUCoyzaqbuirYucAeaPFY'
|
5
8
|
export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
|
6
9
|
|
7
|
-
export const CHAINLINK_SOL_USD_ID =
|
10
|
+
export const CHAINLINK_SOL_USD_ID =
|
11
|
+
'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
|
8
12
|
export const CHAINLINK_SOL_USD_PUBLICKEY = new PublicKey(CHAINLINK_SOL_USD_ID)
|
9
13
|
|
10
14
|
const enc = new TextEncoder()
|
11
15
|
|
12
|
-
export async function getLiquidationPoolStatePublicKey
|
13
|
-
const [poolPublicKey] = await PublicKey.findProgramAddress(
|
16
|
+
export async function getLiquidationPoolStatePublicKey(): Promise<PublicKey> {
|
17
|
+
const [poolPublicKey] = await PublicKey.findProgramAddress(
|
18
|
+
[enc.encode('LiquidationPoolStateV1')],
|
19
|
+
HEDGE_PROGRAM_PUBLICKEY
|
20
|
+
)
|
14
21
|
return poolPublicKey
|
15
22
|
}
|
16
23
|
|
17
|
-
export async function
|
18
|
-
const [poolPublicKey] = await PublicKey.findProgramAddress(
|
24
|
+
export async function getLiquidationPoolUshAccountPublicKey(): Promise<PublicKey> {
|
25
|
+
const [poolPublicKey] = await PublicKey.findProgramAddress(
|
26
|
+
[enc.encode('LiquidationPoolUSHAccountV1')],
|
27
|
+
HEDGE_PROGRAM_PUBLICKEY
|
28
|
+
)
|
19
29
|
return poolPublicKey
|
20
30
|
}
|
21
31
|
|
22
|
-
export async function
|
23
|
-
const [findMintPublicKey] = await PublicKey.findProgramAddress(
|
32
|
+
export async function getUshMintPublicKey(): Promise<PublicKey> {
|
33
|
+
const [findMintPublicKey] = await PublicKey.findProgramAddress(
|
34
|
+
[enc.encode('UshMintV1')],
|
35
|
+
HEDGE_PROGRAM_PUBLICKEY
|
36
|
+
)
|
24
37
|
return findMintPublicKey
|
25
38
|
}
|
26
39
|
|
27
|
-
export async function getVaultSystemStatePublicKey
|
28
|
-
const [publicKey] = await PublicKey.findProgramAddress(
|
40
|
+
export async function getVaultSystemStatePublicKey(): Promise<PublicKey> {
|
41
|
+
const [publicKey] = await PublicKey.findProgramAddress(
|
42
|
+
[enc.encode('VaultSystemStateV1')],
|
43
|
+
HEDGE_PROGRAM_PUBLICKEY
|
44
|
+
)
|
29
45
|
return publicKey
|
30
46
|
}
|
31
47
|
|
32
|
-
export async function getHedgeMintPublicKey
|
33
|
-
const [publicKey] = await PublicKey.findProgramAddress(
|
48
|
+
export async function getHedgeMintPublicKey(): Promise<PublicKey> {
|
49
|
+
const [publicKey] = await PublicKey.findProgramAddress(
|
50
|
+
[enc.encode('HEDGEMintV1')],
|
51
|
+
HEDGE_PROGRAM_PUBLICKEY
|
52
|
+
)
|
34
53
|
return publicKey
|
35
54
|
}
|
36
55
|
|
37
|
-
export async function getPoolPublicKeyForMint
|
38
|
-
|
39
|
-
|
56
|
+
export async function getPoolPublicKeyForMint(
|
57
|
+
mintPublicKey: PublicKey
|
58
|
+
): Promise<[PublicKey, number, string]> {
|
59
|
+
const strToEncode = mintPublicKey.toString().substring(0, 12)
|
60
|
+
const [publicKey, bump] = await PublicKey.findProgramAddress(
|
61
|
+
[enc.encode(strToEncode)],
|
62
|
+
HEDGE_PROGRAM_PUBLICKEY
|
63
|
+
)
|
40
64
|
return [publicKey, bump, strToEncode]
|
41
65
|
}
|
42
|
-
export async function getVaultTypeAccountPublicKey
|
43
|
-
|
66
|
+
export async function getVaultTypeAccountPublicKey(
|
67
|
+
collateralType: string
|
68
|
+
): Promise<PublicKey> {
|
69
|
+
const [vaultTypeAccount] = await PublicKey.findProgramAddress(
|
70
|
+
[enc.encode(collateralType), enc.encode('State')],
|
71
|
+
HEDGE_PROGRAM_PUBLICKEY
|
72
|
+
)
|
44
73
|
return vaultTypeAccount
|
45
74
|
}
|
46
|
-
export async function getVaultTypeOracleAccountPublicKey
|
47
|
-
|
75
|
+
export async function getVaultTypeOracleAccountPublicKey(
|
76
|
+
collateralType: string
|
77
|
+
): Promise<PublicKey> {
|
78
|
+
const [vaultTypeOracleAccount] = await PublicKey.findProgramAddress(
|
79
|
+
[enc.encode(collateralType), enc.encode('Oracle')],
|
80
|
+
HEDGE_PROGRAM_PUBLICKEY
|
81
|
+
)
|
48
82
|
return vaultTypeOracleAccount
|
49
83
|
}
|
50
|
-
export async function findVaultAddress
|
51
|
-
const [vaultAddress] = await PublicKey.findProgramAddress(
|
84
|
+
export async function findVaultAddress(vaultSalt: string): Promise<PublicKey> {
|
85
|
+
const [vaultAddress] = await PublicKey.findProgramAddress(
|
86
|
+
[enc.encode('Vault'), enc.encode(vaultSalt)],
|
87
|
+
HEDGE_PROGRAM_PUBLICKEY
|
88
|
+
)
|
52
89
|
return vaultAddress
|
53
90
|
}
|
54
91
|
|
55
|
-
export async function findAssociatedTokenAddress
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
92
|
+
export async function findAssociatedTokenAddress(
|
93
|
+
walletAddress: PublicKey,
|
94
|
+
tokenMintAddress: PublicKey
|
95
|
+
): Promise<PublicKey> {
|
96
|
+
return (
|
97
|
+
await PublicKey.findProgramAddress(
|
98
|
+
[
|
99
|
+
walletAddress.toBuffer(),
|
100
|
+
TOKEN_PROGRAM_ID.toBuffer(),
|
101
|
+
tokenMintAddress.toBuffer(),
|
102
|
+
],
|
103
|
+
ASSOCIATED_TOKEN_PROGRAM_ID
|
104
|
+
)
|
105
|
+
)[0]
|
64
106
|
}
|