hedge-web3 0.2.7 → 0.2.10
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/declarations/Constants.d.ts +47 -0
- package/declarations/idl/pyth.d.ts +80 -0
- package/declarations/instructions/createVault.d.ts +3 -2
- package/declarations/instructions/depositLiquidationPool.d.ts +4 -3
- package/declarations/instructions/depositStakingPool.d.ts +3 -2
- package/declarations/instructions/depositVault.d.ts +3 -2
- package/declarations/instructions/loanVault.d.ts +3 -2
- package/declarations/instructions/redeemVault.d.ts +3 -2
- package/declarations/instructions/repayVault.d.ts +3 -2
- package/declarations/instructions/withdrawVault.d.ts +3 -2
- package/declarations/state/VaultAccount.d.ts +33 -10
- package/declarations/utils/getLinkedListAccounts.d.ts +17 -2
- package/lib/Constants.js +47 -0
- package/lib/idl/pyth.js +82 -0
- package/lib/instructions/createVault.js +2 -2
- package/lib/instructions/depositLiquidationPool.js +2 -2
- package/lib/instructions/depositStakingPool.js +2 -2
- package/lib/instructions/depositVault.js +3 -3
- package/lib/instructions/liquidateVault.js +1 -1
- package/lib/instructions/loanVault.js +3 -3
- package/lib/instructions/redeemVault.js +3 -3
- package/lib/instructions/refreshOraclePrice.js +6 -5
- package/lib/instructions/repayVault.js +3 -3
- package/lib/instructions/withdrawVault.js +3 -3
- package/lib/state/VaultAccount.js +69 -30
- package/lib/utils/getLinkedListAccounts.js +26 -10
- package/package.json +1 -1
- package/src/Constants.ts +50 -4
- package/src/idl/pyth.ts +159 -0
- package/src/instructions/createVault.ts +3 -3
- package/src/instructions/depositLiquidationPool.ts +4 -4
- package/src/instructions/depositStakingPool.ts +3 -3
- package/src/instructions/depositVault.ts +7 -6
- package/src/instructions/liquidateVault.ts +4 -3
- package/src/instructions/loanVault.ts +7 -6
- package/src/instructions/redeemVault.ts +7 -6
- package/src/instructions/refreshOraclePrice.ts +6 -5
- package/src/instructions/repayVault.ts +7 -6
- package/src/instructions/withdrawVault.ts +7 -6
- package/src/state/VaultAccount.ts +79 -36
- package/src/utils/getLinkedListAccounts.ts +31 -13
@@ -27,9 +27,9 @@ function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrid
|
|
27
27
|
const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
|
28
28
|
const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultAccount.vaultType, true);
|
29
29
|
const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultPublicKey, true);
|
30
|
-
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program,
|
30
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(loanAmount), new anchor_1.BN(0), false, false);
|
31
31
|
const history = web3_js_1.Keypair.generate();
|
32
|
-
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));
|
32
|
+
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, new anchor_1.BN(loanAmount), overrideTime));
|
33
33
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
|
34
34
|
return vaultPublicKey;
|
35
35
|
});
|
@@ -43,7 +43,7 @@ function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPub
|
|
43
43
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
|
44
44
|
const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
|
45
45
|
return yield program.methods
|
46
|
-
.loanVault(
|
46
|
+
.loanVault(loanAmount, new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
47
47
|
)
|
48
48
|
.accounts({
|
49
49
|
vaultSystemState: vaultSystemStatePublicKey,
|
@@ -30,9 +30,9 @@ function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, tra
|
|
30
30
|
const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
|
31
31
|
const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true);
|
32
32
|
const payerTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
|
33
|
-
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program,
|
33
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(0), true, false);
|
34
34
|
const history = web3_js_1.Keypair.generate();
|
35
|
-
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));
|
35
|
+
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, new anchor_1.BN(redeemAmount), transactionOverrideTime));
|
36
36
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
|
37
37
|
return vaultPublicKey;
|
38
38
|
});
|
@@ -46,7 +46,7 @@ function redeemVaultInstruction(program, payerPublicKey, payerUshAccount, destin
|
|
46
46
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
|
47
47
|
const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
|
48
48
|
return yield program.methods
|
49
|
-
.redeemVault(
|
49
|
+
.redeemVault(redeemAmount, new anchor_1.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : Date.now() / 1000) // override start time
|
50
50
|
)
|
51
51
|
.accounts({
|
52
52
|
vaultSystemState: vaultSystemStatePublicKey,
|
@@ -29,6 +29,7 @@ function refreshOraclePriceInstruction(program, collateralType, network, overrid
|
|
29
29
|
const enc = new TextEncoder();
|
30
30
|
const [oracleInfoAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
|
31
31
|
const [vaultTypeAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
|
32
|
+
const oracleInfo = yield program.account.oracleInfoForCollateralType.fetch(oracleInfoAccount);
|
32
33
|
return yield program.methods
|
33
34
|
.refreshOraclePrice(new anchor_1.BN(overridePrice !== null && overridePrice !== void 0 ? overridePrice : web3_js_1.LAMPORTS_PER_SOL * 150), // override usd/sol price
|
34
35
|
new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000) - 1) // override override time
|
@@ -36,9 +37,9 @@ function refreshOraclePriceInstruction(program, collateralType, network, overrid
|
|
36
37
|
.accounts({
|
37
38
|
oracleInfoAccount: oracleInfoAccount,
|
38
39
|
vaultTypeAccount: vaultTypeAccount,
|
39
|
-
oracleChainlink:
|
40
|
-
oraclePyth:
|
41
|
-
oracleSwitchboard:
|
40
|
+
oracleChainlink: oracleInfo.oracleChainlink,
|
41
|
+
oraclePyth: oracleInfo.oraclePyth,
|
42
|
+
oracleSwitchboard: oracleInfo.oracleSwitchboard,
|
42
43
|
systemProgram: web3_js_1.SystemProgram.programId,
|
43
44
|
chainlinkProgram: Constants_1.CHAINLINK_PROGRAM_ID,
|
44
45
|
})
|
@@ -59,12 +60,12 @@ const pythAccounts = {
|
|
59
60
|
};
|
60
61
|
const chainlinkAccounts = {
|
61
62
|
Testing: web3_js_1.SystemProgram.programId,
|
62
|
-
Devnet: new web3_js_1.PublicKey('
|
63
|
+
Devnet: new web3_js_1.PublicKey('HgTtcbcmp5BeThax5AU8vg4VwK79qAvAKKFMs8txMLW6'),
|
63
64
|
MainnetBeta: web3_js_1.SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
|
64
65
|
};
|
65
66
|
const switchboardAccounts = {
|
66
67
|
Testing: web3_js_1.SystemProgram.programId,
|
67
68
|
// Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
|
68
|
-
Devnet: new web3_js_1.PublicKey('
|
69
|
+
Devnet: new web3_js_1.PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
|
69
70
|
MainnetBeta: web3_js_1.SystemProgram.programId, // Switchboard V2 NOT ON MAINNET YET
|
70
71
|
};
|
@@ -28,9 +28,9 @@ function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overr
|
|
28
28
|
const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
|
29
29
|
const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultAccount.vaultType, vaultTypeAccount.collateralMint);
|
30
30
|
const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultPublicKey, vaultTypeAccount.collateralMint);
|
31
|
-
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program,
|
31
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(Math.abs(repayAmount)), false, false);
|
32
32
|
const history = web3_js_1.Keypair.generate();
|
33
|
-
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));
|
33
|
+
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, new anchor_1.BN(repayAmount), overrideTime));
|
34
34
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
|
35
35
|
return vaultPublicKey;
|
36
36
|
});
|
@@ -44,7 +44,7 @@ function repayVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPu
|
|
44
44
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
|
45
45
|
const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
|
46
46
|
return yield program.methods
|
47
|
-
.repayVault(
|
47
|
+
.repayVault(repayAmount, new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
48
48
|
)
|
49
49
|
.accounts({
|
50
50
|
vaultSystemState: vaultSystemStatePublicKey,
|
@@ -34,8 +34,8 @@ function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount,
|
|
34
34
|
const destinationTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
|
35
35
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
|
36
36
|
const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
|
37
|
-
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program,
|
38
|
-
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));
|
37
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(Math.abs(withdrawAmount)), new anchor_1.BN(0), new anchor_1.BN(0), false, false);
|
38
|
+
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, new anchor_1.BN(withdrawAmount), overrideTime));
|
39
39
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
|
40
40
|
return vaultPublicKey;
|
41
41
|
});
|
@@ -44,7 +44,7 @@ exports.withdrawVault = withdrawVault;
|
|
44
44
|
function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMint, historyPublicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime) {
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
46
46
|
return yield program.methods
|
47
|
-
.withdrawVault(
|
47
|
+
.withdrawVault(withdrawAmount, new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
48
48
|
)
|
49
49
|
.accounts({
|
50
50
|
vaultSystemState: vaultSystemStatePublicKey,
|
@@ -27,12 +27,16 @@ const web3_js_1 = require("@solana/web3.js");
|
|
27
27
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
28
28
|
const HedgeDecimal_1 = require("../HedgeDecimal");
|
29
29
|
const borsh = __importStar(require("@project-serum/borsh"));
|
30
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
30
31
|
/**
|
31
32
|
* A class that represents an on-chian vault.
|
32
33
|
*/
|
33
34
|
class VaultAccount {
|
34
35
|
constructor(vault, publicKey) {
|
35
|
-
|
36
|
+
/** The deposited collateral of the vault (in SOL Lamports). */
|
37
|
+
this.deposited = new bn_js_1.default(0);
|
38
|
+
/** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
|
39
|
+
this.denormalizedDebt = new bn_js_1.default(0);
|
36
40
|
/** The ordered number of when this vault was created. */
|
37
41
|
this.vaultNumber = 0;
|
38
42
|
/** Debt redistribution snapshot */
|
@@ -44,8 +48,8 @@ class VaultAccount {
|
|
44
48
|
this.publicKey = publicKey;
|
45
49
|
this.vaultOwner = vault.vaultOwner;
|
46
50
|
this.pdaSalt = vault.pdaSalt;
|
47
|
-
this.deposited =
|
48
|
-
this.denormalizedDebt =
|
51
|
+
this.deposited = vault.deposited;
|
52
|
+
this.denormalizedDebt = vault.denormalizedDebt;
|
49
53
|
if (vault.vaultNumber) {
|
50
54
|
this.vaultNumber = vault.vaultNumber.toNumber();
|
51
55
|
}
|
@@ -71,21 +75,22 @@ class VaultAccount {
|
|
71
75
|
isOwnedBy(publicKey) {
|
72
76
|
return publicKey && publicKey.toString() === this.vaultOwner.toString();
|
73
77
|
}
|
74
|
-
/**
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
inSol() {
|
80
|
-
|
81
|
-
|
78
|
+
// /**
|
79
|
+
// * Get the collateral value in SOL
|
80
|
+
// *
|
81
|
+
// * @returns collateral value in SOL
|
82
|
+
// */
|
83
|
+
// public inSol(): number {
|
84
|
+
// This should not be LAMPORTS_PER_SOL. Should be collateral units like Ray
|
85
|
+
// return new Decimal(this.deposited.toString()).div(LAMPORTS_PER_SOL).toNumber()
|
86
|
+
// }
|
82
87
|
/**
|
83
88
|
* Get the debt value in USH
|
84
89
|
*
|
85
90
|
* @returns debt value in USH
|
86
91
|
*/
|
87
92
|
inUsd() {
|
88
|
-
return this.denormalizedDebt
|
93
|
+
return new decimal_js_1.default(this.denormalizedDebt.toString()).div(web3_js_1.LAMPORTS_PER_SOL).toNumber();
|
89
94
|
}
|
90
95
|
/**
|
91
96
|
* Pretty print the vault publickey for easy display
|
@@ -97,35 +102,69 @@ class VaultAccount {
|
|
97
102
|
.toString()
|
98
103
|
.substring(this.publicKey.toString().length - 6)}`;
|
99
104
|
}
|
105
|
+
/**
|
106
|
+
* Add additional debt to the vault.
|
107
|
+
*
|
108
|
+
* @param {BN} additionalDebt - Additional normalized debt to add in (USH) lamports.
|
109
|
+
* @param {VaultType} vaultTypeAccount - Vault's vaultType
|
110
|
+
*
|
111
|
+
*/
|
100
112
|
addDebt(additionalDebt, vaultTypeAccount) {
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
const totalNormalizedLoan =
|
106
|
-
const denormalizedNewDebt =
|
107
|
-
this.denormalizedDebt =
|
113
|
+
const additionalDebtAsDecimal = new decimal_js_1.default(additionalDebt.toString());
|
114
|
+
// Calculate the fee on the loan
|
115
|
+
const loanFee = vaultTypeAccount.loanInitFee.mul(additionalDebtAsDecimal);
|
116
|
+
// TODO: There's a chance this needs to be .Floor()
|
117
|
+
const totalNormalizedLoan = additionalDebtAsDecimal.add(loanFee);
|
118
|
+
const denormalizedNewDebt = new bn_js_1.default(totalNormalizedLoan.div(vaultTypeAccount.cumulativeRate).floor().toString());
|
119
|
+
this.denormalizedDebt = this.denormalizedDebt.add(denormalizedNewDebt);
|
108
120
|
}
|
109
|
-
|
110
|
-
|
121
|
+
/**
|
122
|
+
* Repay debt on a vault
|
123
|
+
*
|
124
|
+
* @param {BN} repayAmount - Normalized debt to repay in (USH) lamports.
|
125
|
+
* @param {VaultType} vaultTypeAccount - Vault's vaultType
|
126
|
+
*
|
127
|
+
*/
|
128
|
+
repayDebt(repayAmount, vaultTypeAccount) {
|
129
|
+
const denormalizedRepayment = new decimal_js_1.default(repayAmount.toString()).div(vaultTypeAccount.cumulativeRate).floor();
|
130
|
+
this.denormalizedDebt = this.denormalizedDebt.sub(new bn_js_1.default(denormalizedRepayment.toString()));
|
131
|
+
}
|
132
|
+
/**
|
133
|
+
* Deposit Collateral
|
134
|
+
*
|
135
|
+
* @param {BN} depositAmount - Amount to deposit in (CollateralMint) lamports
|
136
|
+
*
|
137
|
+
*/
|
138
|
+
depositCollateral(depositAmount) {
|
139
|
+
this.deposited = this.deposited.add(depositAmount);
|
140
|
+
}
|
141
|
+
/**
|
142
|
+
* Withdraw Collateral
|
143
|
+
*
|
144
|
+
* @param {BN} withdrawAmount - Amount to withdraw in (CollateralMint) lamports
|
145
|
+
*
|
146
|
+
*/
|
147
|
+
withdrawCollateral(withdrawAmount) {
|
148
|
+
this.deposited = this.deposited.sub(withdrawAmount);
|
111
149
|
}
|
112
150
|
redeem() {
|
113
151
|
// TODO - Calculate actual redeem amount and adust correctly
|
114
|
-
this.denormalizedDebt = 0;
|
152
|
+
this.denormalizedDebt = new bn_js_1.default(0);
|
115
153
|
this.vaultStatus = 'initialized';
|
116
154
|
}
|
117
155
|
liquidate() {
|
118
156
|
// TODO - Calculate actual liquidate amount and adust correctly
|
119
|
-
this.denormalizedDebt = 0;
|
157
|
+
this.denormalizedDebt = new bn_js_1.default(0);
|
120
158
|
this.vaultStatus = 'liquidated';
|
121
159
|
}
|
122
160
|
updateDebtAndCollateral(vaultTypeAccountData) {
|
123
|
-
this.denormalizedDebt = vaultTypeAccountData.debtRedistributionProduct
|
161
|
+
this.denormalizedDebt = new bn_js_1.default(vaultTypeAccountData.debtRedistributionProduct
|
124
162
|
.div(this.debtProductSnapshotBytes)
|
125
|
-
.mul(new decimal_js_1.default(this.denormalizedDebt))
|
126
|
-
.
|
127
|
-
|
128
|
-
this.
|
163
|
+
.mul(new decimal_js_1.default(this.denormalizedDebt.toString()))
|
164
|
+
.floor()
|
165
|
+
.toString());
|
166
|
+
const extraCollateralDeposited = new decimal_js_1.default(this.denormalizedDebt.toString()).mul(vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber()).floor();
|
167
|
+
this.deposited = this.deposited.add(new bn_js_1.default(extraCollateralDeposited.toString()));
|
129
168
|
this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator;
|
130
169
|
this.debtProductSnapshotBytes = vaultTypeAccountData.debtRedistributionProduct;
|
131
170
|
}
|
@@ -135,8 +174,8 @@ class VaultAccount {
|
|
135
174
|
arrow = ' <----!!';
|
136
175
|
}
|
137
176
|
let collateralRatio = 'Infinite';
|
138
|
-
if (this.denormalizedDebt
|
139
|
-
collateralRatio = new decimal_js_1.default(this.deposited).div(new decimal_js_1.default(this.denormalizedDebt)).toString();
|
177
|
+
if (this.denormalizedDebt.gt(new bn_js_1.default(0))) {
|
178
|
+
collateralRatio = new decimal_js_1.default(this.deposited.toString()).div(new decimal_js_1.default(this.denormalizedDebt.toString())).toString();
|
140
179
|
}
|
141
180
|
let nextVault = 'None';
|
142
181
|
if (this.nextVaultToRedeem) {
|
@@ -13,13 +13,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
13
|
};
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.getLinkedListAccounts = void 0;
|
16
|
+
const anchor_1 = require("@project-serum/anchor");
|
16
17
|
const underscore_1 = __importDefault(require("underscore"));
|
17
18
|
const Constants_1 = require("../Constants");
|
18
19
|
const VaultAccount_1 = require("../state/VaultAccount");
|
19
20
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
20
21
|
const bs58_1 = __importDefault(require("bs58"));
|
21
22
|
const VaultType_1 = __importDefault(require("../state/VaultType"));
|
22
|
-
|
23
|
+
/**
|
24
|
+
* Get the accounts the left and right for re-inserting in the linked list
|
25
|
+
*
|
26
|
+
* @param {Program<Vault>} program - Anchor program <Vault ILD>
|
27
|
+
* @param {PublicKey} vaultTypeAccountPublicKey - Vault Type Account PublicKey
|
28
|
+
* @param {PublicKey} vaultPublicKey - Vault Account PublicKey
|
29
|
+
* @param {BN} depositAmount - Amount that will be deposited into vault with instruction
|
30
|
+
* @param {BN} withdrawAmount - Amount that will be withdrawn from vault with instruction
|
31
|
+
* @param {BN} loanAmount - Amount that will be deposited into vault with transaction (sans fees)
|
32
|
+
* @param {BN} repayAmount - Amount that will be repaid into vault with transaction
|
33
|
+
* @param {boolean} redeem - True if vault is going to be redeemed fully
|
34
|
+
* @param {boolean} liquidate - True if vault is going to be liquidated fully
|
35
|
+
* @param {VaultAccount[]} cachedVaults - Optional list of cached vaults. Saves a request to the on-chain data.
|
36
|
+
*/
|
37
|
+
function getLinkedListAccounts(program, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, withdrawAmount, loanAmount, repayAmount, redeem, liquidate, cachedVaults) {
|
23
38
|
return __awaiter(this, void 0, void 0, function* () {
|
24
39
|
const vaultTypeRaw = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
|
25
40
|
const vaultType = new VaultType_1.default(vaultTypeRaw, vaultTypeAccountPublicKey);
|
@@ -50,7 +65,7 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
50
65
|
// })
|
51
66
|
// Remove any vaults with no debt or collateral
|
52
67
|
vaults = underscore_1.default.filter(vaults, (vault) => {
|
53
|
-
return vault.denormalizedDebt
|
68
|
+
return vault.denormalizedDebt.gt(new anchor_1.BN(0)) && vault.deposited.gt(new anchor_1.BN(0));
|
54
69
|
});
|
55
70
|
// Sort them
|
56
71
|
vaults.sort(sortVaults);
|
@@ -101,17 +116,18 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
101
116
|
}
|
102
117
|
// Now that we know it's def in the list, iterate the list and update
|
103
118
|
// this vault with the operation we're going to apply
|
104
|
-
const newNormalizedDebt = new decimal_js_1.default(loanAmount);
|
105
119
|
vaults[indexBefore].updateDebtAndCollateral(vaultType);
|
106
|
-
vaults[indexBefore].addDebt(
|
107
|
-
vaults[indexBefore].
|
120
|
+
vaults[indexBefore].addDebt(loanAmount, vaultType);
|
121
|
+
vaults[indexBefore].repayDebt(repayAmount, vaultType);
|
122
|
+
vaults[indexBefore].depositCollateral(depositAmount);
|
123
|
+
vaults[indexBefore].withdrawCollateral(withdrawAmount);
|
108
124
|
if (liquidate) {
|
109
125
|
vaults[indexBefore].liquidate();
|
110
126
|
}
|
111
127
|
if (redeem) {
|
112
128
|
vaults[indexBefore].redeem();
|
113
129
|
}
|
114
|
-
if (vaults[indexBefore].denormalizedDebt
|
130
|
+
if (vaults[indexBefore].denormalizedDebt.isZero()) {
|
115
131
|
vaults.splice(indexBefore, 1);
|
116
132
|
}
|
117
133
|
// Sort it again since we've changed one vault
|
@@ -154,12 +170,12 @@ exports.getLinkedListAccounts = getLinkedListAccounts;
|
|
154
170
|
// Sort function we can use to sort the vaults
|
155
171
|
// Sorted by collateral ratio. If two are the same, newer vault first
|
156
172
|
function sortVaults(a, b) {
|
157
|
-
const aRatio = a.deposited
|
158
|
-
const bRatio = b.deposited
|
159
|
-
if (aRatio
|
173
|
+
const aRatio = new decimal_js_1.default(a.deposited.toString()).div(new decimal_js_1.default(a.denormalizedDebt.toString()));
|
174
|
+
const bRatio = new decimal_js_1.default(b.deposited.toString()).div(new decimal_js_1.default(b.denormalizedDebt.toString()));
|
175
|
+
if (aRatio.equals(bRatio)) {
|
160
176
|
return a.publicKey.toString() > b.publicKey.toString() ? 1 : -1;
|
161
177
|
}
|
162
|
-
return aRatio -
|
178
|
+
return aRatio.greaterThan(bRatio) ? 1 : -1;
|
163
179
|
}
|
164
180
|
function getMiniVaults(program, vaultTypePublicKey) {
|
165
181
|
return __awaiter(this, void 0, void 0, function* () {
|
package/package.json
CHANGED
package/src/Constants.ts
CHANGED
@@ -14,6 +14,9 @@ export const CHAINLINK_PROGRAM_ID = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHn
|
|
14
14
|
|
15
15
|
const enc = new TextEncoder()
|
16
16
|
|
17
|
+
/**
|
18
|
+
* @returns The Liquidation pool public key
|
19
|
+
*/
|
17
20
|
export async function getLiquidationPoolStatePublicKey(): Promise<PublicKey> {
|
18
21
|
const [poolPublicKey] = await PublicKey.findProgramAddress(
|
19
22
|
[enc.encode('LiquidationPoolStateV1')],
|
@@ -21,7 +24,10 @@ export async function getLiquidationPoolStatePublicKey(): Promise<PublicKey> {
|
|
21
24
|
)
|
22
25
|
return poolPublicKey
|
23
26
|
}
|
24
|
-
|
27
|
+
/**
|
28
|
+
*
|
29
|
+
* @returns The liquidation pool ush account public key
|
30
|
+
*/
|
25
31
|
export async function getLiquidationPoolUshAccountPublicKey(): Promise<PublicKey> {
|
26
32
|
const [poolPublicKey] = await PublicKey.findProgramAddress(
|
27
33
|
[enc.encode('LiquidationPoolUSHAccountV1')],
|
@@ -29,7 +35,10 @@ export async function getLiquidationPoolUshAccountPublicKey(): Promise<PublicKey
|
|
29
35
|
)
|
30
36
|
return poolPublicKey
|
31
37
|
}
|
32
|
-
|
38
|
+
/**
|
39
|
+
*
|
40
|
+
* @returns The USH mint public key
|
41
|
+
*/
|
33
42
|
export async function getUshMintPublicKey(): Promise<PublicKey> {
|
34
43
|
const [findMintPublicKey] = await PublicKey.findProgramAddress(
|
35
44
|
[enc.encode('UshMintV1')],
|
@@ -37,7 +46,10 @@ export async function getUshMintPublicKey(): Promise<PublicKey> {
|
|
37
46
|
)
|
38
47
|
return findMintPublicKey
|
39
48
|
}
|
40
|
-
|
49
|
+
/**
|
50
|
+
*
|
51
|
+
* @returns The Vault System State public key
|
52
|
+
*/
|
41
53
|
export async function getVaultSystemStatePublicKey(): Promise<PublicKey> {
|
42
54
|
const [publicKey] = await PublicKey.findProgramAddress(
|
43
55
|
[enc.encode('VaultSystemStateV1')],
|
@@ -46,6 +58,10 @@ export async function getVaultSystemStatePublicKey(): Promise<PublicKey> {
|
|
46
58
|
return publicKey
|
47
59
|
}
|
48
60
|
|
61
|
+
/**
|
62
|
+
*
|
63
|
+
* @returns The HDG mint public key
|
64
|
+
*/
|
49
65
|
export async function getHedgeMintPublicKey(): Promise<PublicKey> {
|
50
66
|
const [publicKey] = await PublicKey.findProgramAddress(
|
51
67
|
[enc.encode('HEDGEMintV1')],
|
@@ -53,7 +69,12 @@ export async function getHedgeMintPublicKey(): Promise<PublicKey> {
|
|
53
69
|
)
|
54
70
|
return publicKey
|
55
71
|
}
|
56
|
-
|
72
|
+
/**
|
73
|
+
* Get the public key for any staking pool
|
74
|
+
*
|
75
|
+
* @param mintPublicKey Staked collateral mint public key
|
76
|
+
* @returns the public key for that staking pool
|
77
|
+
*/
|
57
78
|
export async function getPoolPublicKeyForMint(
|
58
79
|
mintPublicKey: PublicKey
|
59
80
|
): Promise<[PublicKey, number, string]> {
|
@@ -64,6 +85,12 @@ export async function getPoolPublicKeyForMint(
|
|
64
85
|
)
|
65
86
|
return [publicKey, bump, strToEncode]
|
66
87
|
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
*
|
91
|
+
* @param collateralType String name of the collateral type (must be 16 chars)
|
92
|
+
* @returns The public key for that Vault Type account
|
93
|
+
*/
|
67
94
|
export async function getVaultTypeAccountPublicKey(
|
68
95
|
collateralType: string
|
69
96
|
): Promise<PublicKey> {
|
@@ -73,6 +100,12 @@ export async function getVaultTypeAccountPublicKey(
|
|
73
100
|
)
|
74
101
|
return vaultTypeAccount
|
75
102
|
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
*
|
106
|
+
* @param collateralType String name of the collateral type (must be 16 chars)
|
107
|
+
* @returns The public key for that Vault Type oracle info
|
108
|
+
*/
|
76
109
|
export async function getVaultTypeOracleAccountPublicKey(
|
77
110
|
collateralType: string
|
78
111
|
): Promise<PublicKey> {
|
@@ -82,6 +115,13 @@ export async function getVaultTypeOracleAccountPublicKey(
|
|
82
115
|
)
|
83
116
|
return vaultTypeOracleAccount
|
84
117
|
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Vaults are stored in PDA accounts. Use this to get the address from a salt
|
121
|
+
*
|
122
|
+
* @param vaultSalt String salt for the vault (must be 8 chars)
|
123
|
+
* @returns The public key for that Vault Type oracle info
|
124
|
+
*/
|
85
125
|
export async function findVaultAddress(vaultSalt: string): Promise<PublicKey> {
|
86
126
|
const [vaultAddress] = await PublicKey.findProgramAddress(
|
87
127
|
[enc.encode('Vault'), enc.encode(vaultSalt)],
|
@@ -90,6 +130,12 @@ export async function findVaultAddress(vaultSalt: string): Promise<PublicKey> {
|
|
90
130
|
return vaultAddress
|
91
131
|
}
|
92
132
|
|
133
|
+
/**
|
134
|
+
*
|
135
|
+
* @param walletAddress Owner public key
|
136
|
+
* @param tokenMintAddress Token mint
|
137
|
+
* @returns
|
138
|
+
*/
|
93
139
|
export async function findAssociatedTokenAddress(
|
94
140
|
walletAddress: PublicKey,
|
95
141
|
tokenMintAddress: PublicKey
|
package/src/idl/pyth.ts
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
export type Pyth = {
|
2
|
+
"version": "0.1.0",
|
3
|
+
"name": "pyth",
|
4
|
+
"instructions": [
|
5
|
+
{
|
6
|
+
"name": "initialize",
|
7
|
+
"accounts": [
|
8
|
+
{
|
9
|
+
"name": "payer",
|
10
|
+
"isMut": true,
|
11
|
+
"isSigner": true
|
12
|
+
},
|
13
|
+
{
|
14
|
+
"name": "price",
|
15
|
+
"isMut": true,
|
16
|
+
"isSigner": true
|
17
|
+
},
|
18
|
+
{
|
19
|
+
"name": "systemProgram",
|
20
|
+
"isMut": false,
|
21
|
+
"isSigner": false
|
22
|
+
}
|
23
|
+
],
|
24
|
+
"args": [
|
25
|
+
{
|
26
|
+
"name": "price",
|
27
|
+
"type": "i64"
|
28
|
+
},
|
29
|
+
{
|
30
|
+
"name": "expo",
|
31
|
+
"type": "i32"
|
32
|
+
},
|
33
|
+
{
|
34
|
+
"name": "conf",
|
35
|
+
"type": "u64"
|
36
|
+
}
|
37
|
+
]
|
38
|
+
},
|
39
|
+
{
|
40
|
+
"name": "setPrice",
|
41
|
+
"accounts": [
|
42
|
+
{
|
43
|
+
"name": "price",
|
44
|
+
"isMut": true,
|
45
|
+
"isSigner": false
|
46
|
+
}
|
47
|
+
],
|
48
|
+
"args": [
|
49
|
+
{
|
50
|
+
"name": "price",
|
51
|
+
"type": "i64"
|
52
|
+
}
|
53
|
+
]
|
54
|
+
}
|
55
|
+
],
|
56
|
+
"accounts": [
|
57
|
+
{
|
58
|
+
"name": "priceWrapper",
|
59
|
+
"type": {
|
60
|
+
"kind": "struct",
|
61
|
+
"fields": [
|
62
|
+
{
|
63
|
+
"name": "price",
|
64
|
+
"type": {
|
65
|
+
"defined": "Price"
|
66
|
+
}
|
67
|
+
}
|
68
|
+
]
|
69
|
+
}
|
70
|
+
}
|
71
|
+
],
|
72
|
+
"errors": [
|
73
|
+
{
|
74
|
+
"code": 6000,
|
75
|
+
"name": "InvalidTradingStatus",
|
76
|
+
"msg": "invalid trading status"
|
77
|
+
}
|
78
|
+
]
|
79
|
+
};
|
80
|
+
|
81
|
+
export const IDL: Pyth = {
|
82
|
+
"version": "0.1.0",
|
83
|
+
"name": "pyth",
|
84
|
+
"instructions": [
|
85
|
+
{
|
86
|
+
"name": "initialize",
|
87
|
+
"accounts": [
|
88
|
+
{
|
89
|
+
"name": "payer",
|
90
|
+
"isMut": true,
|
91
|
+
"isSigner": true
|
92
|
+
},
|
93
|
+
{
|
94
|
+
"name": "price",
|
95
|
+
"isMut": true,
|
96
|
+
"isSigner": true
|
97
|
+
},
|
98
|
+
{
|
99
|
+
"name": "systemProgram",
|
100
|
+
"isMut": false,
|
101
|
+
"isSigner": false
|
102
|
+
}
|
103
|
+
],
|
104
|
+
"args": [
|
105
|
+
{
|
106
|
+
"name": "price",
|
107
|
+
"type": "i64"
|
108
|
+
},
|
109
|
+
{
|
110
|
+
"name": "expo",
|
111
|
+
"type": "i32"
|
112
|
+
},
|
113
|
+
{
|
114
|
+
"name": "conf",
|
115
|
+
"type": "u64"
|
116
|
+
}
|
117
|
+
]
|
118
|
+
},
|
119
|
+
{
|
120
|
+
"name": "setPrice",
|
121
|
+
"accounts": [
|
122
|
+
{
|
123
|
+
"name": "price",
|
124
|
+
"isMut": true,
|
125
|
+
"isSigner": false
|
126
|
+
}
|
127
|
+
],
|
128
|
+
"args": [
|
129
|
+
{
|
130
|
+
"name": "price",
|
131
|
+
"type": "i64"
|
132
|
+
}
|
133
|
+
]
|
134
|
+
}
|
135
|
+
],
|
136
|
+
"accounts": [
|
137
|
+
{
|
138
|
+
"name": "priceWrapper",
|
139
|
+
"type": {
|
140
|
+
"kind": "struct",
|
141
|
+
"fields": [
|
142
|
+
{
|
143
|
+
"name": "price",
|
144
|
+
"type": {
|
145
|
+
"defined": "Price"
|
146
|
+
}
|
147
|
+
}
|
148
|
+
]
|
149
|
+
}
|
150
|
+
}
|
151
|
+
],
|
152
|
+
"errors": [
|
153
|
+
{
|
154
|
+
"code": 6000,
|
155
|
+
"name": "InvalidTradingStatus",
|
156
|
+
"msg": "invalid trading status"
|
157
|
+
}
|
158
|
+
]
|
159
|
+
};
|