hedge-web3 0.1.27 → 0.1.28
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 +1 -1
- package/declarations/index.d.ts +2 -0
- 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/VaultAccount.d.ts +8 -0
- package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
- package/lib/Constants.js +1 -1
- package/lib/index.js +2 -0
- package/lib/instructions/createStakingPool.js +2 -2
- package/lib/instructions/depositVault.js +14 -7
- package/lib/instructions/liquidateVault.js +9 -4
- package/lib/instructions/loanVault.js +9 -4
- package/lib/instructions/redeemVault.js +7 -2
- package/lib/instructions/repayVault.js +9 -4
- package/lib/instructions/setVaultTypeStatus.js +38 -0
- package/lib/instructions/withdrawVault.js +12 -7
- package/lib/state/VaultAccount.js +54 -1
- package/lib/utils/getLinkedListAccounts.js +131 -0
- package/package.json +3 -1
- package/src/Constants.ts +1 -1
- package/src/index.ts +3 -0
- package/src/instructions/createStakingPool.ts +1 -2
- package/src/instructions/depositVault.ts +97 -22
- package/src/instructions/liquidateVault.ts +118 -21
- package/src/instructions/loanVault.ts +91 -19
- package/src/instructions/redeemVault.ts +23 -0
- package/src/instructions/repayVault.ts +84 -19
- package/src/instructions/setVaultTypeStatus.ts +50 -0
- package/src/instructions/withdrawVault.ts +99 -21
- package/src/state/VaultAccount.ts +86 -10
- package/src/utils/getLinkedListAccounts.ts +156 -0
@@ -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
@@ -4,7 +4,7 @@ import {
|
|
4
4
|
} from '@solana/spl-token'
|
5
5
|
import { PublicKey } from '@solana/web3.js'
|
6
6
|
|
7
|
-
export const HEDGE_PROGRAM_ID = '
|
7
|
+
export const HEDGE_PROGRAM_ID = 'HDG3uyafYaKxSYRW37ZBTdxaUCoyzaqbuirYucAeaPFY'
|
8
8
|
export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
|
9
9
|
|
10
10
|
export const CHAINLINK_SOL_USD_ID =
|
package/src/index.ts
CHANGED
@@ -17,6 +17,7 @@ export * from './instructions/refreshOraclePrice'
|
|
17
17
|
export * from './instructions/initHedgeFoundation'
|
18
18
|
export * from './instructions/initHedgeFoundationTokens'
|
19
19
|
export * from './instructions/setHalted'
|
20
|
+
export * from './instructions/setVaultTypeStatus'
|
20
21
|
|
21
22
|
export * from './HedgeDecimal'
|
22
23
|
export * from './Constants'
|
@@ -28,3 +29,5 @@ export * from './state/StakingPoolPosition'
|
|
28
29
|
export * from './state/LiquidationPoolEra'
|
29
30
|
export * from './state/LiquidationPoolState'
|
30
31
|
export * from './state/LiquidationPosition'
|
32
|
+
|
33
|
+
export * from './utils/getLinkedListAccounts'
|
@@ -36,14 +36,13 @@ export async function createStakingPoolInstruction (
|
|
36
36
|
console.log("createStakingPoolInstruction program ID", program.programId.toString())
|
37
37
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
38
38
|
const ushMintPublickey = await getUshMintPublicKey()
|
39
|
-
const [poolPublickey, poolBump
|
39
|
+
const [poolPublickey, poolBump] = await getPoolPublicKeyForMint(mintPublicKey)
|
40
40
|
|
41
41
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
|
42
42
|
const poolAssociatedUshTokenAccount = await findAssociatedTokenAddress(poolPublickey, ushMintPublickey)
|
43
43
|
|
44
44
|
return program.instruction.createStakingPool(
|
45
45
|
poolBump,
|
46
|
-
poolSeedPhrase,
|
47
46
|
new BN(hedgeTokensToBeMinted),
|
48
47
|
new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)),
|
49
48
|
{
|
@@ -1,8 +1,27 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { TokenInstructions } from '@project-serum/serum'
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
3
|
+
import {
|
4
|
+
getOrCreateAssociatedTokenAccount,
|
5
|
+
TOKEN_PROGRAM_ID,
|
6
|
+
} from '@solana/spl-token'
|
7
|
+
import {
|
8
|
+
Keypair,
|
9
|
+
PublicKey,
|
10
|
+
sendAndConfirmTransaction,
|
11
|
+
Signer,
|
12
|
+
SystemProgram,
|
13
|
+
Transaction,
|
14
|
+
TransactionInstruction,
|
15
|
+
} from '@solana/web3.js'
|
16
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
17
|
+
import {
|
18
|
+
findAssociatedTokenAddress,
|
19
|
+
getVaultTypeAccountPublicKey,
|
20
|
+
getUshMintPublicKey,
|
21
|
+
getVaultSystemStatePublicKey,
|
22
|
+
getPoolPublicKeyForMint,
|
23
|
+
getHedgeMintPublicKey,
|
24
|
+
} from '../Constants'
|
6
25
|
|
7
26
|
export async function depositVault(
|
8
27
|
program: Program,
|
@@ -15,30 +34,68 @@ export async function depositVault(
|
|
15
34
|
const ushMintPublickey = await getUshMintPublicKey()
|
16
35
|
|
17
36
|
// Prep the user to get USH back out at some point
|
18
|
-
await getOrCreateAssociatedTokenAccount(
|
37
|
+
await getOrCreateAssociatedTokenAccount(
|
38
|
+
provider.connection,
|
39
|
+
payer,
|
40
|
+
ushMintPublickey,
|
41
|
+
payer.publicKey
|
42
|
+
)
|
19
43
|
|
20
44
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
21
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
22
|
-
|
23
|
-
|
45
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
46
|
+
vaultAccount.collateralType
|
47
|
+
)
|
48
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
49
|
+
vaultTypeAccountPublicKey
|
50
|
+
)
|
51
|
+
const vaultTypeAssociatedTokenAccount =
|
52
|
+
await getOrCreateAssociatedTokenAccount(
|
53
|
+
provider.connection,
|
54
|
+
payer,
|
55
|
+
vaultTypeAccountInfo.collateralMint,
|
56
|
+
vaultTypeAccountPublicKey,
|
57
|
+
true
|
58
|
+
)
|
24
59
|
|
25
|
-
const payerTokenAccount = await findAssociatedTokenAddress(
|
26
|
-
|
60
|
+
const payerTokenAccount = await findAssociatedTokenAddress(
|
61
|
+
payer.publicKey,
|
62
|
+
vaultTypeAccountInfo.collateralMint
|
63
|
+
)
|
64
|
+
const vaultAssociatedCollateralAccountPublicKey =
|
65
|
+
await findAssociatedTokenAddress(
|
66
|
+
vaultPublicKey,
|
67
|
+
vaultTypeAccountInfo.collateralMint
|
68
|
+
)
|
27
69
|
|
28
70
|
const history = Keypair.generate()
|
29
71
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
30
72
|
|
31
73
|
const wrappedSolAccount = Keypair.generate()
|
32
74
|
|
33
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
34
|
-
|
35
|
-
hedgeStakingPoolPublicKey,
|
36
|
-
ushMintPublickey
|
75
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
76
|
+
await getHedgeMintPublicKey()
|
37
77
|
)
|
78
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
79
|
+
await findAssociatedTokenAddress(
|
80
|
+
hedgeStakingPoolPublicKey,
|
81
|
+
ushMintPublickey
|
82
|
+
)
|
38
83
|
|
39
84
|
const transaction = new Transaction()
|
40
85
|
const signers = [payer, history]
|
41
86
|
|
87
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
88
|
+
await getLinkedListAccounts(
|
89
|
+
program,
|
90
|
+
provider,
|
91
|
+
vaultTypeAccountPublicKey,
|
92
|
+
vaultPublicKey,
|
93
|
+
depositAmount,
|
94
|
+
0,
|
95
|
+
false,
|
96
|
+
false
|
97
|
+
)
|
98
|
+
|
42
99
|
if (vaultAccount.collateralType === 'SOL') {
|
43
100
|
transaction.add(
|
44
101
|
SystemProgram.createAccount({
|
@@ -46,12 +103,12 @@ export async function depositVault(
|
|
46
103
|
lamports: depositAmount + 2.04e6,
|
47
104
|
newAccountPubkey: wrappedSolAccount.publicKey,
|
48
105
|
programId: TOKEN_PROGRAM_ID,
|
49
|
-
space: 165
|
106
|
+
space: 165,
|
50
107
|
}),
|
51
108
|
TokenInstructions.initializeAccount({
|
52
109
|
account: wrappedSolAccount.publicKey,
|
53
110
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
54
|
-
owner: payer.publicKey
|
111
|
+
owner: payer.publicKey,
|
55
112
|
})
|
56
113
|
)
|
57
114
|
signers.push(wrappedSolAccount)
|
@@ -61,7 +118,9 @@ export async function depositVault(
|
|
61
118
|
program,
|
62
119
|
vaultSystemStatePublicKey,
|
63
120
|
payer.publicKey,
|
64
|
-
vaultAccount.collateralType === 'SOL'
|
121
|
+
vaultAccount.collateralType === 'SOL'
|
122
|
+
? wrappedSolAccount.publicKey
|
123
|
+
: payerTokenAccount,
|
65
124
|
vaultPublicKey,
|
66
125
|
vaultAssociatedCollateralAccountPublicKey,
|
67
126
|
history.publicKey,
|
@@ -71,6 +130,9 @@ export async function depositVault(
|
|
71
130
|
hedgeStakingPoolAssociatedUshTokenAccount,
|
72
131
|
vaultTypeAccountInfo.collateralMint,
|
73
132
|
ushMintPublickey,
|
133
|
+
oldSmallerPublicKey,
|
134
|
+
newSmallerPublicKey,
|
135
|
+
newLargerPublicKey,
|
74
136
|
depositAmount,
|
75
137
|
overrideTime
|
76
138
|
)
|
@@ -80,12 +142,17 @@ export async function depositVault(
|
|
80
142
|
TokenInstructions.closeAccount({
|
81
143
|
source: wrappedSolAccount.publicKey,
|
82
144
|
destination: payer.publicKey,
|
83
|
-
owner: payer.publicKey
|
145
|
+
owner: payer.publicKey,
|
84
146
|
})
|
85
147
|
)
|
86
148
|
}
|
87
149
|
|
88
|
-
await sendAndConfirmTransaction(
|
150
|
+
await sendAndConfirmTransaction(
|
151
|
+
provider.connection,
|
152
|
+
transaction,
|
153
|
+
signers,
|
154
|
+
provider.opts
|
155
|
+
)
|
89
156
|
return vaultPublicKey
|
90
157
|
}
|
91
158
|
|
@@ -103,6 +170,9 @@ export async function depositVaultInstruction(
|
|
103
170
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
104
171
|
collateralMint: PublicKey,
|
105
172
|
ushMintPublickey: PublicKey,
|
173
|
+
oldSmallerPublicKey: PublicKey,
|
174
|
+
newSmallerPublicKey: PublicKey,
|
175
|
+
newLargerPublicKey: PublicKey,
|
106
176
|
depositAmount: number,
|
107
177
|
overrideTime?: number
|
108
178
|
): Promise<TransactionInstruction> {
|
@@ -118,14 +188,19 @@ export async function depositVaultInstruction(
|
|
118
188
|
vault: vaultPublicKey,
|
119
189
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
120
190
|
feePool: hedgeStakingPoolPublicKey,
|
121
|
-
feePoolAssociatedUshTokenAccount:
|
191
|
+
feePoolAssociatedUshTokenAccount:
|
192
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
122
193
|
history: historyPublicKey,
|
123
194
|
vaultOwner: vaultOwner,
|
124
195
|
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
125
196
|
ushMint: ushMintPublickey,
|
197
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
198
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
199
|
+
newLargerVaultInfo: newLargerPublicKey,
|
126
200
|
systemProgram: SystemProgram.programId,
|
127
|
-
tokenProgram: TOKEN_PROGRAM_ID
|
201
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
128
202
|
},
|
129
|
-
signers: []
|
130
|
-
}
|
203
|
+
signers: [],
|
204
|
+
}
|
205
|
+
)
|
131
206
|
}
|
@@ -1,9 +1,31 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
2
|
+
import {
|
3
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
4
|
+
TOKEN_PROGRAM_ID,
|
5
|
+
getOrCreateAssociatedTokenAccount,
|
6
|
+
} from '@solana/spl-token'
|
7
|
+
import {
|
8
|
+
Keypair,
|
9
|
+
PublicKey,
|
10
|
+
sendAndConfirmTransaction,
|
11
|
+
Signer,
|
12
|
+
SystemProgram,
|
13
|
+
SYSVAR_RENT_PUBKEY,
|
14
|
+
Transaction,
|
15
|
+
TransactionInstruction,
|
16
|
+
} from '@solana/web3.js'
|
17
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
18
|
+
import {
|
19
|
+
getHedgeMintPublicKey,
|
20
|
+
getLiquidationPoolStatePublicKey,
|
21
|
+
getLiquidationPoolUshAccountPublicKey,
|
22
|
+
getPoolPublicKeyForMint,
|
23
|
+
getVaultTypeAccountPublicKey,
|
24
|
+
getUshMintPublicKey,
|
25
|
+
getVaultSystemStatePublicKey,
|
26
|
+
} from '../Constants'
|
5
27
|
|
6
|
-
export async function liquidateVault
|
28
|
+
export async function liquidateVault(
|
7
29
|
program: Program,
|
8
30
|
provider: Provider,
|
9
31
|
payer: Signer,
|
@@ -12,22 +34,80 @@ export async function liquidateVault (
|
|
12
34
|
): Promise<PublicKey> {
|
13
35
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
14
36
|
|
15
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
16
|
-
|
37
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
38
|
+
vaultAccount.collateralType
|
39
|
+
)
|
40
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
41
|
+
vaultTypeAccountPublicKey
|
42
|
+
)
|
17
43
|
const collateralMint = vaultTypeAccountInfo.collateralMint
|
18
44
|
|
19
45
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
20
46
|
const ushMintPublickey = await getUshMintPublicKey()
|
21
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
47
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
48
|
+
hedgeMintPublickey
|
49
|
+
)
|
22
50
|
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
23
|
-
const poolStateInfo = await program.account.liquidationPoolState.fetch(
|
51
|
+
const poolStateInfo = await program.account.liquidationPoolState.fetch(
|
52
|
+
liquidationPoolStatePublicKey
|
53
|
+
)
|
54
|
+
|
55
|
+
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
56
|
+
provider.connection,
|
57
|
+
payer,
|
58
|
+
collateralMint,
|
59
|
+
payer.publicKey,
|
60
|
+
true
|
61
|
+
)
|
62
|
+
const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
63
|
+
provider.connection,
|
64
|
+
payer,
|
65
|
+
collateralMint,
|
66
|
+
hedgeStakingPoolPublicKey,
|
67
|
+
true
|
68
|
+
)
|
69
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
70
|
+
provider.connection,
|
71
|
+
payer,
|
72
|
+
collateralMint,
|
73
|
+
vaultPublicKey,
|
74
|
+
true
|
75
|
+
)
|
76
|
+
const poolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
77
|
+
provider.connection,
|
78
|
+
payer,
|
79
|
+
collateralMint,
|
80
|
+
liquidationPoolStatePublicKey,
|
81
|
+
true
|
82
|
+
)
|
83
|
+
const vaultTypeAssociatedTokenAccount =
|
84
|
+
await getOrCreateAssociatedTokenAccount(
|
85
|
+
provider.connection,
|
86
|
+
payer,
|
87
|
+
collateralMint,
|
88
|
+
vaultTypeAccountPublicKey,
|
89
|
+
true
|
90
|
+
)
|
91
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
92
|
+
await getOrCreateAssociatedTokenAccount(
|
93
|
+
provider.connection,
|
94
|
+
payer,
|
95
|
+
ushMintPublickey,
|
96
|
+
hedgeStakingPoolPublicKey,
|
97
|
+
true
|
98
|
+
)
|
24
99
|
|
25
|
-
const
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
100
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
101
|
+
await getLinkedListAccounts(
|
102
|
+
program,
|
103
|
+
provider,
|
104
|
+
vaultTypeAccountPublicKey,
|
105
|
+
vaultPublicKey,
|
106
|
+
0,
|
107
|
+
0,
|
108
|
+
false,
|
109
|
+
true
|
110
|
+
)
|
31
111
|
|
32
112
|
const history = Keypair.generate()
|
33
113
|
const newEra = Keypair.generate()
|
@@ -50,15 +130,23 @@ export async function liquidateVault (
|
|
50
130
|
hedgeStakingPoolAssociatedUshTokenAccount.address,
|
51
131
|
collateralMint,
|
52
132
|
vaultTypeAssociatedTokenAccount.address,
|
133
|
+
oldSmallerPublicKey,
|
134
|
+
newSmallerPublicKey,
|
135
|
+
newLargerPublicKey,
|
53
136
|
vaultAccount.collateralType,
|
54
137
|
overrideTime
|
55
138
|
)
|
56
139
|
)
|
57
|
-
await sendAndConfirmTransaction(
|
140
|
+
await sendAndConfirmTransaction(
|
141
|
+
provider.connection,
|
142
|
+
transaction,
|
143
|
+
[payer, history, newEra],
|
144
|
+
provider.opts
|
145
|
+
)
|
58
146
|
return vaultPublicKey
|
59
147
|
}
|
60
148
|
|
61
|
-
export async function liquidateVaultInstruction
|
149
|
+
export async function liquidateVaultInstruction(
|
62
150
|
program: Program,
|
63
151
|
payerPublicKey: PublicKey,
|
64
152
|
payerAssociatedTokenAccount: PublicKey,
|
@@ -74,12 +162,16 @@ export async function liquidateVaultInstruction (
|
|
74
162
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
75
163
|
collateralMint: PublicKey,
|
76
164
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
165
|
+
oldSmallerPublicKey: PublicKey,
|
166
|
+
newSmallerPublicKey: PublicKey,
|
167
|
+
newLargerPublicKey: PublicKey,
|
77
168
|
collateralType: string,
|
78
169
|
overrideTime?: number
|
79
170
|
): Promise<TransactionInstruction> {
|
80
171
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
81
172
|
const ushMintPublickey = await getUshMintPublicKey()
|
82
|
-
const liquidationPoolUshAccountPublickey =
|
173
|
+
const liquidationPoolUshAccountPublickey =
|
174
|
+
await getLiquidationPoolUshAccountPublicKey()
|
83
175
|
const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
|
84
176
|
|
85
177
|
const payload = {
|
@@ -99,17 +191,22 @@ export async function liquidateVaultInstruction (
|
|
99
191
|
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
100
192
|
feePool: feePool,
|
101
193
|
feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
|
102
|
-
feePoolAssociatedUshTokenAccount:
|
194
|
+
feePoolAssociatedUshTokenAccount:
|
195
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
103
196
|
liquidationPoolUshAccount: liquidationPoolUshAccountPublickey,
|
104
197
|
newEra: newEraPublicKey,
|
198
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
199
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
200
|
+
newLargerVaultInfo: newLargerPublicKey,
|
105
201
|
tokenProgram: TOKEN_PROGRAM_ID,
|
106
202
|
systemProgram: SystemProgram.programId,
|
107
203
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
108
|
-
rent: SYSVAR_RENT_PUBKEY
|
204
|
+
rent: SYSVAR_RENT_PUBKEY,
|
109
205
|
},
|
110
|
-
signers: []
|
206
|
+
signers: [],
|
111
207
|
}
|
112
208
|
return program.instruction.liquidateVault(
|
113
209
|
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
114
|
-
payload
|
210
|
+
payload
|
211
|
+
)
|
115
212
|
}
|