hedge-web3 0.1.29 → 0.1.31
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/idl/vault.d.ts +99 -99
- package/declarations/index.d.ts +1 -0
- package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
- package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
- package/declarations/instructions/createStakingPool.d.ts +3 -2
- package/declarations/instructions/createVault.d.ts +6 -5
- package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
- package/declarations/instructions/depositStakingPool.d.ts +3 -2
- package/declarations/instructions/depositVault.d.ts +3 -2
- package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
- package/declarations/instructions/liquidateVault.d.ts +3 -2
- package/declarations/instructions/loanVault.d.ts +3 -2
- package/declarations/instructions/redeemVault.d.ts +3 -2
- package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
- package/declarations/instructions/repayVault.d.ts +3 -2
- package/declarations/instructions/setHalted.d.ts +3 -2
- package/declarations/instructions/setVaultTypeStatus.d.ts +3 -2
- package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
- package/declarations/instructions/withdrawVault.d.ts +3 -2
- package/declarations/state/VaultAccount.d.ts +1 -1
- package/declarations/utils/getLinkedListAccounts.d.ts +3 -1
- package/lib/idl/vault.js +99 -99
- package/lib/index.js +1 -0
- package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
- package/lib/instructions/claimStakingPoolPosition.js +19 -19
- package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
- package/lib/instructions/createStakingPool.js +17 -18
- package/lib/instructions/createVault.js +28 -31
- package/lib/instructions/depositLiquidationPool.js +17 -18
- package/lib/instructions/depositStakingPool.js +16 -18
- package/lib/instructions/depositVault.js +25 -27
- package/lib/instructions/initHedgeFoundation.js +17 -19
- package/lib/instructions/initHedgeFoundationTokens.js +15 -15
- package/lib/instructions/liquidateVault.js +32 -33
- package/lib/instructions/loanVault.js +23 -23
- package/lib/instructions/redeemVault.js +24 -24
- package/lib/instructions/refreshOraclePrice.js +17 -17
- package/lib/instructions/repayVault.js +23 -23
- package/lib/instructions/setHalted.js +8 -9
- package/lib/instructions/setVaultTypeStatus.js +9 -10
- package/lib/instructions/withdrawStakingPool.js +22 -24
- package/lib/instructions/withdrawVault.js +23 -23
- package/lib/state/LiquidationPoolEra.js +3 -1
- package/lib/state/LiquidationPosition.js +0 -7
- package/lib/state/StakingPool.js +3 -4
- package/lib/state/VaultAccount.js +2 -5
- package/lib/utils/getLinkedListAccounts.js +24 -16
- package/package.json +2 -2
- package/src/idl/vault.ts +198 -198
- package/src/index.ts +1 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
- package/src/instructions/claimStakingPoolPosition.ts +45 -25
- package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
- package/src/instructions/createStakingPool.ts +37 -35
- package/src/instructions/createVault.ts +81 -125
- package/src/instructions/depositLiquidationPool.ts +45 -26
- package/src/instructions/depositStakingPool.ts +32 -24
- package/src/instructions/depositVault.ts +57 -86
- package/src/instructions/initHedgeFoundation.ts +42 -43
- package/src/instructions/initHedgeFoundationTokens.ts +38 -39
- package/src/instructions/liquidateVault.ts +42 -65
- package/src/instructions/loanVault.ts +51 -69
- package/src/instructions/redeemVault.ts +83 -47
- package/src/instructions/refreshOraclePrice.ts +41 -32
- package/src/instructions/repayVault.ts +45 -65
- package/src/instructions/setHalted.ts +32 -24
- package/src/instructions/setVaultTypeStatus.ts +32 -24
- package/src/instructions/withdrawStakingPool.ts +44 -30
- package/src/instructions/withdrawVault.ts +58 -82
- package/src/state/LiquidationPoolEra.ts +4 -3
- package/src/state/LiquidationPosition.ts +0 -27
- package/src/state/StakingPool.ts +4 -7
- package/src/state/StakingPoolPosition.ts +2 -3
- package/src/state/VaultAccount.ts +9 -28
- package/src/state/VaultHistoryEvent.ts +1 -2
- package/src/utils/getLinkedListAccounts.ts +31 -30
@@ -19,7 +19,7 @@ function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, st
|
|
19
19
|
return __awaiter(this, void 0, void 0, function* () {
|
20
20
|
const poolPosition = web3_js_1.Keypair.generate();
|
21
21
|
const transaction = new web3_js_1.Transaction().add(yield withdrawStakingPoolInstruction(program, payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime));
|
22
|
-
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]
|
22
|
+
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
|
23
23
|
return poolPosition.publicKey;
|
24
24
|
});
|
25
25
|
}
|
@@ -36,29 +36,27 @@ function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPub
|
|
36
36
|
const payerAssociatedHedgeAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, hedgeMintPublickey);
|
37
37
|
const payerAssociatedUshAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, ushMintPublickey);
|
38
38
|
const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultSystemStatePublicKey, hedgeMintPublickey);
|
39
|
-
return program.
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
signers: []
|
61
|
-
});
|
39
|
+
return yield program.methods
|
40
|
+
.withdrawStakingPool(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)) // override current time
|
41
|
+
)
|
42
|
+
.accounts({
|
43
|
+
payer: payerPublicKey,
|
44
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
45
|
+
pool: poolPublickey,
|
46
|
+
poolPosition: poolPositionPublicKey,
|
47
|
+
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
48
|
+
poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
|
49
|
+
payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
|
50
|
+
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
51
|
+
payerAssociatedUshAccount: payerAssociatedUshAccount,
|
52
|
+
communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
|
53
|
+
hedgeMint: hedgeMintPublickey,
|
54
|
+
stakedTokenMint: stakedTokenMintPublicKey,
|
55
|
+
ushMint: ushMintPublickey,
|
56
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
57
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
58
|
+
})
|
59
|
+
.instruction();
|
62
60
|
});
|
63
61
|
}
|
64
62
|
exports.withdrawStakingPoolInstruction = withdrawStakingPoolInstruction;
|
@@ -33,35 +33,35 @@ function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount,
|
|
33
33
|
const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
|
34
34
|
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, withdrawAmount * -1, 0, false, false);
|
35
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));
|
36
|
-
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]
|
36
|
+
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
|
37
37
|
return vaultPublicKey;
|
38
38
|
});
|
39
39
|
}
|
40
40
|
exports.withdrawVault = withdrawVault;
|
41
41
|
function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMint, historyPublicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime) {
|
42
42
|
return __awaiter(this, void 0, void 0, function* () {
|
43
|
-
return program.
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
43
|
+
return yield program.methods
|
44
|
+
.withdrawVault(new anchor_1.BN(withdrawAmount), new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
45
|
+
)
|
46
|
+
.accounts({
|
47
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
48
|
+
vaultTypeAccount: vaultTypeAccount,
|
49
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
50
|
+
vault: vaultPublickey,
|
51
|
+
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
52
|
+
feePool: hedgeStakingPoolPublicKey,
|
53
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
54
|
+
ushMint: ushMint,
|
55
|
+
history: historyPublicKey,
|
56
|
+
vaultOwner: payerPublicKey,
|
57
|
+
destinationTokenAccount: destinationTokenAccount,
|
58
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
59
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
60
|
+
newLargerVaultInfo: newLargerPublicKey,
|
61
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
62
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
63
|
+
})
|
64
|
+
.instruction();
|
65
65
|
});
|
66
66
|
}
|
67
67
|
exports.withdrawVaultInstruction = withdrawVaultInstruction;
|
@@ -11,7 +11,9 @@ class LiquidationPoolEra {
|
|
11
11
|
this.liquidyPoolEra = liquidyPoolEra;
|
12
12
|
this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber();
|
13
13
|
this.product = (0, HedgeDecimal_1.DecimalFromU128)(liquidyPoolEra.productBytes);
|
14
|
-
this.sum = liquidyPoolEra.sumBytes.map((sumBytes) => {
|
14
|
+
this.sum = liquidyPoolEra.sumBytes.map((sumBytes) => {
|
15
|
+
return (0, HedgeDecimal_1.DecimalFromU128)(sumBytes);
|
16
|
+
});
|
15
17
|
this.hedgeRewardsAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(liquidyPoolEra.hedgeRewardsAccumulatorBytes);
|
16
18
|
this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber();
|
17
19
|
}
|
@@ -31,10 +31,3 @@ class LiquidationPosition {
|
|
31
31
|
}
|
32
32
|
}
|
33
33
|
exports.LiquidationPosition = LiquidationPosition;
|
34
|
-
// function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
|
35
|
-
// const timeInOffsetStart = timeIn - birthTime
|
36
|
-
// const timeOutOffsetStart = timeOut - birthTime
|
37
|
-
// const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
|
38
|
-
// const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
|
39
|
-
// return awardedTokens
|
40
|
-
// }
|
package/lib/state/StakingPool.js
CHANGED
@@ -7,16 +7,15 @@ class StakingPool {
|
|
7
7
|
this.poolInfo = poolInfo;
|
8
8
|
this.publicKey = publicKey;
|
9
9
|
this.deposits = poolInfo.deposits.toNumber();
|
10
|
-
// this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
|
11
|
-
// this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
|
12
10
|
this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber();
|
13
11
|
this.startTime = poolInfo.startTime.toNumber();
|
14
12
|
this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber();
|
15
13
|
this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber();
|
16
14
|
this.hedgeRewardAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.hedgeRewardAccumulator);
|
17
15
|
this.ushFeeAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.ushFeeAccumulator);
|
18
|
-
this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum) => {
|
19
|
-
|
16
|
+
this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum) => {
|
17
|
+
return (0, HedgeDecimal_1.DecimalFromU128)(sum);
|
18
|
+
});
|
20
19
|
}
|
21
20
|
}
|
22
21
|
exports.StakingPool = StakingPool;
|
@@ -75,7 +75,7 @@ class VaultAccount {
|
|
75
75
|
this.denormalizedDebt = 0;
|
76
76
|
this.vaultStatus = 'liquidated';
|
77
77
|
}
|
78
|
-
|
78
|
+
updateDebtAndCollateral(vaultTypeAccuntData) {
|
79
79
|
const debtProductCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.debtRedistributionProduct);
|
80
80
|
const collateralAccumulatorCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.collateralRedistributionAccumulator);
|
81
81
|
this.denormalizedDebt = debtProductCurrent
|
@@ -83,10 +83,7 @@ class VaultAccount {
|
|
83
83
|
.mul(new decimal_js_1.default(this.denormalizedDebt))
|
84
84
|
// .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
|
85
85
|
.toNumber();
|
86
|
-
const extraCollateralDeposited = this.denormalizedDebt *
|
87
|
-
collateralAccumulatorCurrent
|
88
|
-
.sub(this.collateralAccumulatorSnapshotBytes)
|
89
|
-
.toNumber();
|
86
|
+
const extraCollateralDeposited = this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber();
|
90
87
|
this.deposited += extraCollateralDeposited;
|
91
88
|
this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent;
|
92
89
|
this.debtProductSnapshotBytes = debtProductCurrent;
|
@@ -17,7 +17,7 @@ const underscore_1 = __importDefault(require("underscore"));
|
|
17
17
|
const HedgeDecimal_1 = require("../HedgeDecimal");
|
18
18
|
const VaultAccount_1 = require("../state/VaultAccount");
|
19
19
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
20
|
-
function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate) {
|
20
|
+
function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate, cachedVaults) {
|
21
21
|
return __awaiter(this, void 0, void 0, function* () {
|
22
22
|
const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
|
23
23
|
// Default for null is the vault itself, so set them all to this vault
|
@@ -27,19 +27,27 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
27
27
|
const thisVaultData = yield program.account.vault.fetch(vaultPublicKey);
|
28
28
|
const thisVault = new VaultAccount_1.VaultAccount(thisVaultData, vaultPublicKey);
|
29
29
|
// Load all the vaults
|
30
|
-
let
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
30
|
+
let vaults;
|
31
|
+
if (cachedVaults) {
|
32
|
+
vaults = cachedVaults;
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
let allVaults = cachedVaults
|
36
|
+
? cachedVaults
|
37
|
+
: (yield program.account.vault
|
38
|
+
.all([
|
39
|
+
// {
|
40
|
+
// memcmp: { bytes: bs58.encode(inputCollateralType), offset: 8 + 32 + 8 },
|
41
|
+
// },
|
42
|
+
])
|
43
|
+
.catch((error) => {
|
44
|
+
console.log('error', error);
|
45
|
+
})) || [];
|
46
|
+
// Load them into our account objects
|
47
|
+
vaults = allVaults.map((vault) => {
|
48
|
+
return new VaultAccount_1.VaultAccount(vault.account, vault.publicKey);
|
49
|
+
});
|
50
|
+
}
|
43
51
|
// Filter out the account that are not the same vault type
|
44
52
|
vaults = underscore_1.default.filter(vaults, (vault) => {
|
45
53
|
return vault.collateralType === vaultTypeAccount.collateralType;
|
@@ -75,6 +83,7 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
75
83
|
// this vault with the opeation we're going to apply
|
76
84
|
const newNormalizedDebt = new decimal_js_1.default(loanAmount);
|
77
85
|
const vaultTypeCompoundedInterest = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccount.cumulativeRate.toString());
|
86
|
+
vaults[indexBefore].updateDebtAndCollateral(vaultTypeAccount);
|
78
87
|
vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest);
|
79
88
|
vaults[indexBefore].addDeposit(depositAmount);
|
80
89
|
if (liquidate) {
|
@@ -83,7 +92,6 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
83
92
|
if (redeem) {
|
84
93
|
vaults[indexBefore].redeem();
|
85
94
|
}
|
86
|
-
vaults[indexBefore].redistribution(vaultTypeAccount);
|
87
95
|
if (vaults[indexBefore].denormalizedDebt === 0) {
|
88
96
|
vaults.splice(indexBefore, 1);
|
89
97
|
}
|
@@ -115,7 +123,7 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
115
123
|
// console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
|
116
124
|
// console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
|
117
125
|
// console.log('newLargerPublicKey', newLargerPublicKey.toString())
|
118
|
-
return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey];
|
126
|
+
return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults];
|
119
127
|
});
|
120
128
|
}
|
121
129
|
exports.getLinkedListAccounts = getLinkedListAccounts;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "hedge-web3",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.31",
|
4
4
|
"description": "Hedge Javascript Web3 API",
|
5
5
|
"main": "lib/index.js",
|
6
6
|
"types": "declarations/index.d.ts",
|
@@ -14,7 +14,7 @@
|
|
14
14
|
"author": "Chris Coudron <coudron@hedge.so>",
|
15
15
|
"license": "ISC",
|
16
16
|
"dependencies": {
|
17
|
-
"@project-serum/anchor": "^0.
|
17
|
+
"@project-serum/anchor": "^0.24.1",
|
18
18
|
"@project-serum/serum": "^0.13.61",
|
19
19
|
"@rollup/plugin-typescript": "^8.3.0",
|
20
20
|
"@solana/buffer-layout": "^4.0.0",
|