hedge-web3 0.1.25 → 0.1.28
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/Constants.d.ts +3 -3
- package/declarations/idl/vault.d.ts +889 -752
- package/declarations/index.d.ts +2 -0
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
- package/declarations/instructions/createVault.d.ts +1 -1
- package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
- package/declarations/instructions/depositVault.d.ts +1 -1
- package/declarations/instructions/liquidateVault.d.ts +1 -1
- package/declarations/instructions/loanVault.d.ts +1 -1
- package/declarations/instructions/redeemVault.d.ts +1 -1
- package/declarations/instructions/repayVault.d.ts +1 -1
- package/declarations/instructions/setVaultTypeStatus.d.ts +4 -0
- package/declarations/instructions/withdrawVault.d.ts +1 -1
- package/declarations/state/LiquidationPosition.d.ts +2 -2
- package/declarations/state/StakingPool.d.ts +1 -1
- package/declarations/state/StakingPoolPosition.d.ts +2 -2
- package/declarations/state/VaultAccount.d.ts +11 -3
- package/declarations/state/VaultHistoryEvent.d.ts +2 -2
- package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
- package/lib/Constants.js +10 -10
- package/lib/idl/vault.js +922 -785
- package/lib/index.js +2 -0
- package/lib/instructions/closeLiquidationPoolPosition.js +9 -9
- package/lib/instructions/createStakingPool.js +6 -6
- package/lib/instructions/createVault.js +11 -11
- package/lib/instructions/depositLiquidationPool.js +9 -9
- package/lib/instructions/depositVault.js +20 -13
- package/lib/instructions/initHedgeFoundation.js +4 -4
- package/lib/instructions/liquidateVault.js +16 -11
- package/lib/instructions/loanVault.js +16 -11
- package/lib/instructions/redeemVault.js +15 -10
- package/lib/instructions/repayVault.js +17 -12
- package/lib/instructions/setVaultTypeStatus.js +38 -0
- package/lib/instructions/withdrawStakingPool.js +6 -6
- package/lib/instructions/withdrawVault.js +18 -13
- package/lib/state/LiquidationPosition.js +2 -2
- package/lib/state/StakingPool.js +1 -1
- package/lib/state/StakingPoolPosition.js +3 -3
- package/lib/state/VaultAccount.js +56 -3
- package/lib/state/VaultHistoryEvent.js +2 -2
- package/lib/utils/Errors.js +2 -2
- package/lib/utils/getLinkedListAccounts.js +131 -0
- package/package.json +3 -1
- package/src/Constants.ts +73 -31
- package/src/idl/vault.ts +1848 -1574
- package/src/index.ts +3 -0
- package/src/instructions/closeLiquidationPoolPosition.ts +10 -10
- package/src/instructions/createStakingPool.ts +6 -7
- package/src/instructions/createVault.ts +17 -17
- package/src/instructions/depositLiquidationPool.ts +10 -10
- package/src/instructions/depositVault.ts +104 -29
- package/src/instructions/initHedgeFoundation.ts +5 -5
- package/src/instructions/initHedgeFoundationTokens.ts +1 -1
- package/src/instructions/liquidateVault.ts +124 -27
- package/src/instructions/loanVault.ts +97 -25
- package/src/instructions/redeemVault.ts +35 -12
- package/src/instructions/repayVault.ts +91 -26
- package/src/instructions/setHalted.ts +1 -1
- package/src/instructions/setVaultTypeStatus.ts +50 -0
- package/src/instructions/withdrawStakingPool.ts +7 -7
- package/src/instructions/withdrawVault.ts +106 -28
- package/src/state/LiquidationPosition.ts +3 -3
- package/src/state/StakingPool.ts +2 -6
- package/src/state/StakingPoolPosition.ts +4 -4
- package/src/state/VaultAccount.ts +89 -13
- package/src/state/VaultHistoryEvent.ts +4 -4
- package/src/utils/Errors.ts +2 -2
- package/src/utils/getLinkedListAccounts.ts +156 -0
- package/declarations/idl/idl.d.ts +0 -2
- package/lib/idl/idl.js +0 -1475
- package/src/idl/idl.ts +0 -1474
@@ -0,0 +1,156 @@
|
|
1
|
+
import { Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { PublicKey, Signer } from '@solana/web3.js'
|
3
|
+
import _ from 'underscore'
|
4
|
+
import {
|
5
|
+
getVaultSystemStatePublicKey,
|
6
|
+
getVaultTypeOracleAccountPublicKey,
|
7
|
+
} from '../Constants'
|
8
|
+
|
9
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
10
|
+
import { VaultAccount } from '../state/VaultAccount'
|
11
|
+
import Decimal from 'decimal.js'
|
12
|
+
export async function getLinkedListAccounts(
|
13
|
+
program: Program,
|
14
|
+
provider: Provider,
|
15
|
+
vaultTypeAccountPublicKey: PublicKey,
|
16
|
+
vaultPublicKey: PublicKey,
|
17
|
+
depositAmount: number,
|
18
|
+
loanAmount: number,
|
19
|
+
redeem: boolean,
|
20
|
+
liquidate: boolean
|
21
|
+
): Promise<[PublicKey, PublicKey, PublicKey]> {
|
22
|
+
const vaultTypeAccount = await program.account.vaultType.fetch(
|
23
|
+
vaultTypeAccountPublicKey
|
24
|
+
)
|
25
|
+
|
26
|
+
// Default for null is the vault itself, so set them all to this vault
|
27
|
+
let oldSmallerPublicKey = vaultPublicKey
|
28
|
+
let newSmallerPublicKey = vaultPublicKey
|
29
|
+
let newLargerPublicKey = vaultPublicKey
|
30
|
+
|
31
|
+
const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
|
32
|
+
const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
|
33
|
+
|
34
|
+
// Load all the vaults
|
35
|
+
let allVaults =
|
36
|
+
(await program.account.vault
|
37
|
+
.all([
|
38
|
+
// {
|
39
|
+
// memcmp: { bytes: bs58.encode(inputCollateralType), offset: 8 + 32 + 8 },
|
40
|
+
// },
|
41
|
+
])
|
42
|
+
.catch((error) => {
|
43
|
+
console.log('error', error)
|
44
|
+
})) || []
|
45
|
+
|
46
|
+
// Load them into our account objects
|
47
|
+
let vaults = allVaults.map((vault) => {
|
48
|
+
return new VaultAccount(vault.account, vault.publicKey)
|
49
|
+
})
|
50
|
+
|
51
|
+
// Filter out the account that are not the same vault type
|
52
|
+
vaults = _.filter(vaults, (vault) => {
|
53
|
+
return vault.collateralType === vaultTypeAccount.collateralType
|
54
|
+
})
|
55
|
+
|
56
|
+
// Filter out the accounts that are not open
|
57
|
+
vaults = _.filter(vaults, (vault) => {
|
58
|
+
return vault.vaultStatus === 'open'
|
59
|
+
})
|
60
|
+
|
61
|
+
// Sort them
|
62
|
+
vaults.sort(sortVaults)
|
63
|
+
|
64
|
+
// Find the location of the vault before the operation
|
65
|
+
let indexBefore = -1
|
66
|
+
vaults.forEach((vault, index) => {
|
67
|
+
if (vault.publicKey.toString() === vaultPublicKey.toString()) {
|
68
|
+
indexBefore = index
|
69
|
+
}
|
70
|
+
})
|
71
|
+
|
72
|
+
// If we found it before, set the old smaller vault the one to the left
|
73
|
+
if (indexBefore > 0) {
|
74
|
+
oldSmallerPublicKey = vaults[indexBefore - 1].publicKey
|
75
|
+
}
|
76
|
+
|
77
|
+
// Pretty print all the vaults before the operation
|
78
|
+
// console.log('Sorted open vaults before')
|
79
|
+
// vaults.forEach((vault) => {
|
80
|
+
// console.log(vault.toString(vaultPublicKey))
|
81
|
+
// })
|
82
|
+
|
83
|
+
// If it wasn't in the list, add it now
|
84
|
+
if (indexBefore < 0) {
|
85
|
+
vaults.push(thisVault)
|
86
|
+
indexBefore = vaults.length - 1
|
87
|
+
}
|
88
|
+
|
89
|
+
// Now that we know it's def in the list, iterate the list and update
|
90
|
+
// this vault with the opeation we're going to apply
|
91
|
+
const newNormalizedDebt = new Decimal(loanAmount)
|
92
|
+
const vaultTypeCompoundedInterest = DecimalFromU128(
|
93
|
+
vaultTypeAccount.cumulativeRate.toString()
|
94
|
+
)
|
95
|
+
vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest)
|
96
|
+
vaults[indexBefore].addDeposit(depositAmount)
|
97
|
+
|
98
|
+
if (liquidate) {
|
99
|
+
vaults[indexBefore].liquidate()
|
100
|
+
}
|
101
|
+
if (redeem) {
|
102
|
+
vaults[indexBefore].redeem()
|
103
|
+
}
|
104
|
+
vaults[indexBefore].redistribution(vaultTypeAccount)
|
105
|
+
|
106
|
+
if (vaults[indexBefore].denormalizedDebt === 0) {
|
107
|
+
vaults.splice(indexBefore, 1)
|
108
|
+
}
|
109
|
+
|
110
|
+
// Sort it again since we've changed one vault
|
111
|
+
vaults = vaults.sort(sortVaults)
|
112
|
+
|
113
|
+
// Pretty print the list again
|
114
|
+
// console.log('Sorted open vaults with new debt added')
|
115
|
+
// vaults.forEach((vault) => {
|
116
|
+
// console.log(vault.toString(vaultPublicKey))
|
117
|
+
// })
|
118
|
+
|
119
|
+
// Search for the vaults new position
|
120
|
+
let indexAfter = -1
|
121
|
+
vaults.forEach((vault, index) => {
|
122
|
+
if (vault.publicKey.toString() === vaultPublicKey.toString()) {
|
123
|
+
indexAfter = index
|
124
|
+
}
|
125
|
+
})
|
126
|
+
|
127
|
+
// Print where it moved from / to
|
128
|
+
// console.log('Index Before', indexBefore)
|
129
|
+
// console.log('Index After', indexAfter)
|
130
|
+
|
131
|
+
// Save references to the new left and right
|
132
|
+
if (indexAfter > 0) {
|
133
|
+
newSmallerPublicKey = vaults[indexAfter - 1].publicKey
|
134
|
+
}
|
135
|
+
if (indexAfter < vaults.length - 1 && indexAfter >= 0) {
|
136
|
+
newLargerPublicKey = vaults[indexAfter + 1].publicKey
|
137
|
+
}
|
138
|
+
|
139
|
+
// Print out the new left/right
|
140
|
+
// console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
|
141
|
+
// console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
|
142
|
+
// console.log('newLargerPublicKey', newLargerPublicKey.toString())
|
143
|
+
|
144
|
+
return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey]
|
145
|
+
}
|
146
|
+
|
147
|
+
// Sort function we can use to sort the vaults
|
148
|
+
// Sorted by collateral ratio. If two are the same, newer vault first
|
149
|
+
function sortVaults(a: VaultAccount, b: VaultAccount) {
|
150
|
+
const aRatio = a.deposited / a.denormalizedDebt
|
151
|
+
const bRatio = b.deposited / b.denormalizedDebt
|
152
|
+
if (aRatio === bRatio) {
|
153
|
+
return b.vaultNumber - a.vaultNumber
|
154
|
+
}
|
155
|
+
return aRatio - bRatio
|
156
|
+
}
|