hedge-web3 0.1.37 → 0.1.40
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/state/VaultAccount.d.ts +2 -0
- package/lib/instructions/claimLiquidationPoolPosition.js +0 -1
- package/lib/state/VaultAccount.js +10 -5
- package/lib/utils/getLinkedListAccounts.js +29 -8
- package/package.json +1 -1
- package/src/instructions/claimLiquidationPoolPosition.ts +0 -1
- package/src/state/VaultAccount.ts +16 -6
- package/src/utils/getLinkedListAccounts.ts +29 -8
@@ -21,6 +21,8 @@ export declare class VaultAccount {
|
|
21
21
|
collateralType: string;
|
22
22
|
/** Current State of the vautl ("Open", "Closed", "Liquidated") */
|
23
23
|
vaultStatus: string;
|
24
|
+
/** The public key of the next vault to redeem. */
|
25
|
+
nextVaultToRedeem: PublicKey;
|
24
26
|
constructor(vault: any, publicKey: PublicKey);
|
25
27
|
/**
|
26
28
|
* Check if some `PublicKey` is the owner
|
@@ -28,7 +28,6 @@ function claimLiquidationPoolPosition(program, provider, poolPosition, payer, co
|
|
28
28
|
exports.claimLiquidationPoolPosition = claimLiquidationPoolPosition;
|
29
29
|
function claimLiquidationPoolPositionInstruction(program, vaultTypeAccount, collateralMint, poolPosition, payer, payerAssociatedTokenAccount, overrideStartTime) {
|
30
30
|
return __awaiter(this, void 0, void 0, function* () {
|
31
|
-
console.log("inside function");
|
32
31
|
const vaultSystemState = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
33
32
|
const poolStatePublicKey = yield (0, Constants_1.getLiquidationPoolStatePublicKey)();
|
34
33
|
const poolAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolStatePublicKey, collateralMint);
|
@@ -21,6 +21,7 @@ class VaultAccount {
|
|
21
21
|
this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
|
22
22
|
this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
|
23
23
|
this.collateralType = vault.collateralType;
|
24
|
+
this.nextVaultToRedeem = vault.nextVaultToRedeem;
|
24
25
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0];
|
25
26
|
}
|
26
27
|
/**
|
@@ -60,7 +61,8 @@ class VaultAccount {
|
|
60
61
|
}
|
61
62
|
addDebt(newNormalizedDebt, vaultTypeCompoundedInterest) {
|
62
63
|
const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest);
|
63
|
-
this.denormalizedDebt
|
64
|
+
this.denormalizedDebt = denormalizedNewDebt.add(new decimal_js_1.default(this.denormalizedDebt)).floor().toNumber();
|
65
|
+
// this.denormalizedDebt = parseFloat(this.denormalizedDebt.toFixed(0))
|
64
66
|
}
|
65
67
|
addDeposit(depositAmount) {
|
66
68
|
this.deposited += depositAmount;
|
@@ -81,7 +83,6 @@ class VaultAccount {
|
|
81
83
|
this.denormalizedDebt = debtProductCurrent
|
82
84
|
.div(this.debtProductSnapshotBytes)
|
83
85
|
.mul(new decimal_js_1.default(this.denormalizedDebt))
|
84
|
-
// .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
|
85
86
|
.toNumber();
|
86
87
|
const extraCollateralDeposited = this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber();
|
87
88
|
this.deposited += extraCollateralDeposited;
|
@@ -91,15 +92,19 @@ class VaultAccount {
|
|
91
92
|
toString(highlight) {
|
92
93
|
let arrow = '';
|
93
94
|
if (this.publicKey.toString() === highlight.toString()) {
|
94
|
-
arrow = '
|
95
|
+
arrow = ' <----!!';
|
95
96
|
}
|
96
97
|
let collateralRatio = 'Infinite';
|
97
98
|
if (this.denormalizedDebt > 0) {
|
98
|
-
collateralRatio = (this.deposited
|
99
|
+
collateralRatio = new decimal_js_1.default(this.deposited).div(new decimal_js_1.default(this.denormalizedDebt)).toString();
|
100
|
+
}
|
101
|
+
let nextVault = 'None';
|
102
|
+
if (this.nextVaultToRedeem) {
|
103
|
+
nextVault = this.nextVaultToRedeem.toString().substring(0, 6);
|
99
104
|
}
|
100
105
|
return `Vault(${this.vaultNumber}): ${this.publicKey
|
101
106
|
.toString()
|
102
|
-
.substring(0, 6)}. Debt: ${this.
|
107
|
+
.substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `;
|
103
108
|
}
|
104
109
|
}
|
105
110
|
exports.VaultAccount = VaultAccount;
|
@@ -70,12 +70,26 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
70
70
|
oldSmallerPublicKey = vaults[indexBefore - 1].publicKey;
|
71
71
|
}
|
72
72
|
// Pretty print all the vaults before the operation
|
73
|
-
// console.log('Sorted open vaults
|
74
|
-
|
75
|
-
|
76
|
-
|
73
|
+
// console.log('Sorted open vaults BEFORE at index:', indexBefore)
|
74
|
+
let correctOrderBefore = true;
|
75
|
+
for (let i = 0; i < vaults.length; i++) {
|
76
|
+
if (i < vaults.length - 1) {
|
77
|
+
// Verify i points to i+1
|
78
|
+
if (vaults[i].nextVaultToRedeem.toString() !== vaults[i + 1].publicKey.toString()) {
|
79
|
+
correctOrderBefore = false;
|
80
|
+
console.log("A vault was found OUT OF ORDER!!", vaults[i]);
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
if (correctOrderBefore) {
|
85
|
+
console.log(`Verfied the on-chain order of vault type:`, vaultTypeAccount.collateralType);
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
throw new Error("On-Chian vaults not in order!");
|
89
|
+
}
|
77
90
|
// If it wasn't in the list, add it now
|
78
91
|
if (indexBefore < 0) {
|
92
|
+
// console.log('Was not in the list before. Adding now.')
|
79
93
|
vaults.push(thisVault);
|
80
94
|
indexBefore = vaults.length - 1;
|
81
95
|
}
|
@@ -98,10 +112,10 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
98
112
|
// Sort it again since we've changed one vault
|
99
113
|
vaults = vaults.sort(sortVaults);
|
100
114
|
// Pretty print the list again
|
101
|
-
// console.log('Sorted open vaults with new debt added')
|
102
|
-
// vaults.
|
103
|
-
//
|
104
|
-
// })
|
115
|
+
// console.log('Sorted open vaults with new debt added at index: ', indexAfter)
|
116
|
+
// console.log(vaults.map((vault) => {
|
117
|
+
// return vault.toString(vaultPublicKey)
|
118
|
+
// }))
|
105
119
|
// Search for the vaults new position
|
106
120
|
let indexAfter = -1;
|
107
121
|
vaults.forEach((vault, index) => {
|
@@ -109,6 +123,13 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
|
|
109
123
|
indexAfter = index;
|
110
124
|
}
|
111
125
|
});
|
126
|
+
// New list with vault
|
127
|
+
// console.log('New list with vault now at index:', indexAfter)
|
128
|
+
// console.log(
|
129
|
+
// vaults.map((vault) => {
|
130
|
+
// return vault.toString(vaultPublicKey)
|
131
|
+
// })
|
132
|
+
// )
|
112
133
|
// Print where it moved from / to
|
113
134
|
// console.log('Index Before', indexBefore)
|
114
135
|
// console.log('Index After', indexAfter)
|
package/package.json
CHANGED
@@ -62,7 +62,6 @@ export async function claimLiquidationPoolPositionInstruction(
|
|
62
62
|
overrideStartTime?: number
|
63
63
|
): Promise<TransactionInstruction> {
|
64
64
|
|
65
|
-
console.log("inside function")
|
66
65
|
const vaultSystemState = await getVaultSystemStatePublicKey()
|
67
66
|
const poolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
68
67
|
const poolAssociatedTokenAccount = await findAssociatedTokenAddress(poolStatePublicKey, collateralMint)
|
@@ -33,6 +33,9 @@ export class VaultAccount {
|
|
33
33
|
/** Current State of the vautl ("Open", "Closed", "Liquidated") */
|
34
34
|
vaultStatus: string
|
35
35
|
|
36
|
+
/** The public key of the next vault to redeem. */
|
37
|
+
nextVaultToRedeem: PublicKey
|
38
|
+
|
36
39
|
constructor(vault: any, publicKey: PublicKey) {
|
37
40
|
this.publicKey = publicKey
|
38
41
|
this.vaultOwner = vault.vaultOwner
|
@@ -43,6 +46,7 @@ export class VaultAccount {
|
|
43
46
|
this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
|
44
47
|
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
|
45
48
|
this.collateralType = vault.collateralType
|
49
|
+
this.nextVaultToRedeem = vault.nextVaultToRedeem
|
46
50
|
|
47
51
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0]
|
48
52
|
}
|
@@ -88,7 +92,8 @@ export class VaultAccount {
|
|
88
92
|
|
89
93
|
public addDebt(newNormalizedDebt: Decimal, vaultTypeCompoundedInterest: Decimal) {
|
90
94
|
const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest)
|
91
|
-
this.denormalizedDebt
|
95
|
+
this.denormalizedDebt = denormalizedNewDebt.add(new Decimal(this.denormalizedDebt)).floor().toNumber()
|
96
|
+
// this.denormalizedDebt = parseFloat(this.denormalizedDebt.toFixed(0))
|
92
97
|
}
|
93
98
|
public addDeposit(depositAmount: number) {
|
94
99
|
this.deposited += depositAmount
|
@@ -113,9 +118,8 @@ export class VaultAccount {
|
|
113
118
|
this.denormalizedDebt = debtProductCurrent
|
114
119
|
.div(this.debtProductSnapshotBytes)
|
115
120
|
.mul(new Decimal(this.denormalizedDebt))
|
116
|
-
// .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
|
117
121
|
.toNumber()
|
118
|
-
|
122
|
+
|
119
123
|
const extraCollateralDeposited =
|
120
124
|
this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
|
121
125
|
this.deposited += extraCollateralDeposited
|
@@ -127,14 +131,20 @@ export class VaultAccount {
|
|
127
131
|
public toString(highlight: PublicKey): string {
|
128
132
|
let arrow = ''
|
129
133
|
if (this.publicKey.toString() === highlight.toString()) {
|
130
|
-
arrow = '
|
134
|
+
arrow = ' <----!!'
|
131
135
|
}
|
132
136
|
let collateralRatio = 'Infinite'
|
133
137
|
if (this.denormalizedDebt > 0) {
|
134
|
-
collateralRatio = (this.deposited
|
138
|
+
collateralRatio = new Decimal(this.deposited).div(new Decimal(this.denormalizedDebt)).toString()
|
135
139
|
}
|
140
|
+
|
141
|
+
let nextVault = 'None'
|
142
|
+
if (this.nextVaultToRedeem) {
|
143
|
+
nextVault = this.nextVaultToRedeem.toString().substring(0, 6)
|
144
|
+
}
|
145
|
+
|
136
146
|
return `Vault(${this.vaultNumber}): ${this.publicKey
|
137
147
|
.toString()
|
138
|
-
.substring(0, 6)}. Debt: ${this.
|
148
|
+
.substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `
|
139
149
|
}
|
140
150
|
}
|
@@ -78,13 +78,26 @@ export async function getLinkedListAccounts(
|
|
78
78
|
}
|
79
79
|
|
80
80
|
// Pretty print all the vaults before the operation
|
81
|
-
// console.log('Sorted open vaults
|
82
|
-
|
83
|
-
|
84
|
-
|
81
|
+
// console.log('Sorted open vaults BEFORE at index:', indexBefore)
|
82
|
+
let correctOrderBefore = true
|
83
|
+
for (let i = 0; i < vaults.length; i++) {
|
84
|
+
if (i < vaults.length - 1) {
|
85
|
+
// Verify i points to i+1
|
86
|
+
if (vaults[i].nextVaultToRedeem.toString() !== vaults[i+1].publicKey.toString()){
|
87
|
+
correctOrderBefore = false
|
88
|
+
console.log("A vault was found OUT OF ORDER!!", vaults[i])
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
if (correctOrderBefore){
|
93
|
+
console.log(`Verfied the on-chain order of vault type:`, vaultTypeAccount.collateralType)
|
94
|
+
} else {
|
95
|
+
throw new Error("On-Chian vaults not in order!");
|
96
|
+
}
|
85
97
|
|
86
98
|
// If it wasn't in the list, add it now
|
87
99
|
if (indexBefore < 0) {
|
100
|
+
// console.log('Was not in the list before. Adding now.')
|
88
101
|
vaults.push(thisVault)
|
89
102
|
indexBefore = vaults.length - 1
|
90
103
|
}
|
@@ -112,10 +125,10 @@ export async function getLinkedListAccounts(
|
|
112
125
|
vaults = vaults.sort(sortVaults)
|
113
126
|
|
114
127
|
// Pretty print the list again
|
115
|
-
// console.log('Sorted open vaults with new debt added')
|
116
|
-
// vaults.
|
117
|
-
//
|
118
|
-
// })
|
128
|
+
// console.log('Sorted open vaults with new debt added at index: ', indexAfter)
|
129
|
+
// console.log(vaults.map((vault) => {
|
130
|
+
// return vault.toString(vaultPublicKey)
|
131
|
+
// }))
|
119
132
|
|
120
133
|
// Search for the vaults new position
|
121
134
|
let indexAfter = -1
|
@@ -125,6 +138,14 @@ export async function getLinkedListAccounts(
|
|
125
138
|
}
|
126
139
|
})
|
127
140
|
|
141
|
+
// New list with vault
|
142
|
+
// console.log('New list with vault now at index:', indexAfter)
|
143
|
+
// console.log(
|
144
|
+
// vaults.map((vault) => {
|
145
|
+
// return vault.toString(vaultPublicKey)
|
146
|
+
// })
|
147
|
+
// )
|
148
|
+
|
128
149
|
// Print where it moved from / to
|
129
150
|
// console.log('Index Before', indexBefore)
|
130
151
|
// console.log('Index After', indexAfter)
|