hedge-web3 0.1.50 → 0.2.2
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 +2 -1
- package/declarations/idl/vault.d.ts +192 -41
- package/declarations/index.d.ts +1 -0
- package/declarations/instructions/claimLiquidationPoolPosition.d.ts +1 -1
- package/declarations/instructions/claimStakingPoolPosition.d.ts +1 -1
- package/declarations/instructions/closeClaimedLiquidationPoolPosition.d.ts +5 -0
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
- package/declarations/instructions/createStakingPool.d.ts +1 -1
- package/declarations/instructions/createVault.d.ts +1 -1
- package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
- package/declarations/instructions/depositStakingPool.d.ts +1 -1
- package/declarations/instructions/depositVault.d.ts +1 -1
- package/declarations/instructions/initHedgeFoundation.d.ts +1 -1
- package/declarations/instructions/liquidateVault.d.ts +2 -2
- package/declarations/instructions/loanVault.d.ts +1 -1
- package/declarations/instructions/redeemVault.d.ts +1 -1
- package/declarations/instructions/refreshOraclePrice.d.ts +3 -3
- package/declarations/instructions/repayVault.d.ts +1 -1
- package/declarations/instructions/setHalted.d.ts +1 -1
- package/declarations/instructions/updateVaultType.d.ts +4 -1
- package/declarations/instructions/withdrawStakingPool.d.ts +1 -1
- package/declarations/instructions/withdrawVault.d.ts +1 -1
- package/declarations/state/VaultAccount.d.ts +9 -6
- package/declarations/utils/getLinkedListAccounts.d.ts +1 -1
- package/lib/Constants.js +3 -2
- package/lib/idl/vault.js +192 -41
- package/lib/index.js +1 -0
- package/lib/instructions/claimLiquidationPoolPosition.js +5 -1
- package/lib/instructions/claimStakingPoolPosition.js +5 -1
- package/lib/instructions/closeClaimedLiquidationPoolPosition.js +38 -0
- package/lib/instructions/closeLiquidationPoolPosition.js +5 -1
- package/lib/instructions/createStakingPool.js +5 -1
- package/lib/instructions/createVault.js +5 -1
- package/lib/instructions/depositLiquidationPool.js +5 -1
- package/lib/instructions/depositStakingPool.js +5 -1
- package/lib/instructions/depositVault.js +12 -8
- package/lib/instructions/initHedgeFoundation.js +5 -1
- package/lib/instructions/initHedgeFoundationTokens.js +5 -1
- package/lib/instructions/liquidateVault.js +10 -8
- package/lib/instructions/loanVault.js +9 -6
- package/lib/instructions/redeemVault.js +9 -6
- package/lib/instructions/refreshOraclePrice.js +8 -3
- package/lib/instructions/repayVault.js +9 -6
- package/lib/instructions/setHalted.js +5 -1
- package/lib/instructions/updateVaultType.js +9 -2
- package/lib/instructions/withdrawStakingPool.js +5 -1
- package/lib/instructions/withdrawVault.js +9 -6
- package/lib/state/VaultAccount.js +28 -32
- package/lib/utils/getLinkedListAccounts.js +54 -57
- package/package.json +1 -1
- package/src/Constants.ts +2 -1
- package/src/idl/vault.ts +384 -82
- package/src/index.ts +1 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +3 -2
- package/src/instructions/claimStakingPoolPosition.ts +3 -2
- package/src/instructions/closeClaimedLiquidationPoolPosition.ts +31 -0
- package/src/instructions/closeLiquidationPoolPosition.ts +3 -2
- package/src/instructions/createStakingPool.ts +3 -2
- package/src/instructions/createVault.ts +3 -2
- package/src/instructions/depositLiquidationPool.ts +3 -2
- package/src/instructions/depositStakingPool.ts +3 -2
- package/src/instructions/depositVault.ts +11 -11
- package/src/instructions/initHedgeFoundation.ts +3 -2
- package/src/instructions/initHedgeFoundationTokens.ts +2 -1
- package/src/instructions/liquidateVault.ts +11 -10
- package/src/instructions/loanVault.ts +11 -9
- package/src/instructions/redeemVault.ts +7 -7
- package/src/instructions/refreshOraclePrice.ts +9 -6
- package/src/instructions/repayVault.ts +7 -7
- package/src/instructions/setHalted.ts +3 -2
- package/src/instructions/updateVaultType.ts +10 -2
- package/src/instructions/withdrawStakingPool.ts +5 -3
- package/src/instructions/withdrawVault.ts +7 -7
- package/src/state/VaultAccount.ts +37 -32
- package/src/state/VaultType.ts +62 -0
- package/src/utils/getLinkedListAccounts.ts +61 -60
- package/src/utils/sendAndConfirmWithDebug.ts +27 -0
@@ -18,7 +18,8 @@ import {
|
|
18
18
|
getUshMintPublicKey,
|
19
19
|
getVaultSystemStatePublicKey,
|
20
20
|
} from '../Constants'
|
21
|
-
import
|
21
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
22
|
+
import { Vault } from '../idl/vault'
|
22
23
|
|
23
24
|
export async function withdrawStakingPool(
|
24
25
|
program: Program<Vault>,
|
@@ -26,7 +27,7 @@ export async function withdrawStakingPool(
|
|
26
27
|
payer: Signer,
|
27
28
|
poolPositionPublicKey: PublicKey,
|
28
29
|
stakedTokenMintPublicKey: PublicKey,
|
29
|
-
overrideStartTime?: number
|
30
|
+
overrideStartTime?: number,
|
30
31
|
): Promise<PublicKey> {
|
31
32
|
const poolPosition = Keypair.generate()
|
32
33
|
const transaction = new Transaction().add(
|
@@ -38,7 +39,8 @@ export async function withdrawStakingPool(
|
|
38
39
|
overrideStartTime
|
39
40
|
)
|
40
41
|
)
|
41
|
-
|
42
|
+
|
43
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
42
44
|
return poolPosition.publicKey
|
43
45
|
}
|
44
46
|
|
@@ -19,7 +19,8 @@ import {
|
|
19
19
|
getHedgeMintPublicKey,
|
20
20
|
} from '../Constants'
|
21
21
|
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
22
|
-
import
|
22
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
23
|
+
import { Vault } from '../idl/vault'
|
23
24
|
|
24
25
|
export async function withdrawVault(
|
25
26
|
program: Program<Vault>,
|
@@ -37,7 +38,6 @@ export async function withdrawVault(
|
|
37
38
|
const history = Keypair.generate()
|
38
39
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
39
40
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
40
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
41
41
|
const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(
|
42
42
|
provider.connection,
|
43
43
|
payer,
|
@@ -46,12 +46,12 @@ export async function withdrawVault(
|
|
46
46
|
true
|
47
47
|
)
|
48
48
|
|
49
|
-
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
49
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultAccount.vaultType)
|
50
50
|
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
51
51
|
provider.connection,
|
52
52
|
payer,
|
53
53
|
vaultTypeAccountInfo.collateralMint,
|
54
|
-
|
54
|
+
vaultAccount.vaultType,
|
55
55
|
true
|
56
56
|
)
|
57
57
|
|
@@ -70,7 +70,7 @@ export async function withdrawVault(
|
|
70
70
|
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
71
71
|
program,
|
72
72
|
provider,
|
73
|
-
|
73
|
+
vaultAccount.vaultType,
|
74
74
|
vaultPublicKey,
|
75
75
|
withdrawAmount * -1,
|
76
76
|
0,
|
@@ -86,7 +86,7 @@ export async function withdrawVault(
|
|
86
86
|
destinationTokenAccount.address,
|
87
87
|
vaultPublicKey,
|
88
88
|
vaultAssociatedCollateralAccount.address,
|
89
|
-
|
89
|
+
vaultAccount.vaultType,
|
90
90
|
vaultTypeAssociatedTokenAccount.address,
|
91
91
|
hedgeStakingPoolPublicKey,
|
92
92
|
hedgeStakingPoolAssociatedUshTokenAccount,
|
@@ -99,7 +99,7 @@ export async function withdrawVault(
|
|
99
99
|
overrideTime
|
100
100
|
)
|
101
101
|
)
|
102
|
-
await
|
102
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
|
103
103
|
return vaultPublicKey
|
104
104
|
}
|
105
105
|
|
@@ -3,6 +3,8 @@ import Decimal from 'decimal.js'
|
|
3
3
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
4
|
|
5
5
|
import * as borsh from '@project-serum/borsh'
|
6
|
+
import BN from 'bn.js'
|
7
|
+
import VaultType from './VaultType'
|
6
8
|
|
7
9
|
/**
|
8
10
|
* A class that represents an on-chian vault.
|
@@ -18,10 +20,10 @@ export class VaultAccount {
|
|
18
20
|
pdaSalt: string
|
19
21
|
|
20
22
|
/** The deposited collateral of the vault (in SOL Lamports). */
|
21
|
-
deposited
|
23
|
+
deposited: number
|
22
24
|
|
23
25
|
/** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
|
24
|
-
denormalizedDebt
|
26
|
+
denormalizedDebt: number
|
25
27
|
|
26
28
|
/** The ordered number of when this vault was created. */
|
27
29
|
vaultNumber: number
|
@@ -32,8 +34,8 @@ export class VaultAccount {
|
|
32
34
|
/** Collateral redistribution snapshot' */
|
33
35
|
collateralAccumulatorSnapshotBytes = new Decimal(0)
|
34
36
|
|
35
|
-
/** The vault type eg 'SOL-150' */
|
36
|
-
|
37
|
+
/** The vault type eg '2-SOL-150-0-----' */
|
38
|
+
vaultTypeName: string
|
37
39
|
|
38
40
|
/** Current State of the vault ("Open", "Closed", "Liquidated") */
|
39
41
|
vaultStatus = ''
|
@@ -41,29 +43,29 @@ export class VaultAccount {
|
|
41
43
|
/** The public key of the next vault to redeem. */
|
42
44
|
nextVaultToRedeem: PublicKey
|
43
45
|
|
46
|
+
/** The public key of the vault type. */
|
47
|
+
vaultType: PublicKey
|
48
|
+
|
44
49
|
constructor(vault: any, publicKey: PublicKey) {
|
45
50
|
this.publicKey = publicKey
|
46
51
|
this.vaultOwner = vault.vaultOwner
|
47
52
|
this.vaultNumber = vault.vaultNumber?.toNumber()
|
48
53
|
this.pdaSalt = vault.pdaSalt
|
49
|
-
|
50
|
-
|
51
|
-
}
|
52
|
-
if (vault.denormalizedDebt){
|
53
|
-
this.denormalizedDebt = new Decimal(vault.denormalizedDebt.toString())
|
54
|
-
}
|
54
|
+
this.deposited = vault.deposited?.toNumber()
|
55
|
+
this.denormalizedDebt = vault.denormalizedDebt?.toNumber()
|
55
56
|
if (vault.debtProductSnapshotBytes) {
|
56
57
|
this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
|
57
58
|
}
|
58
59
|
if (vault.collateralAccumulatorSnapshotBytes) {
|
59
60
|
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
|
60
61
|
}
|
61
|
-
this.
|
62
|
+
this.vaultTypeName = vault.vaultTypeName
|
62
63
|
this.nextVaultToRedeem = vault.nextVaultToRedeem
|
63
64
|
|
64
65
|
if (vault.vaultStatus) {
|
65
66
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0]
|
66
67
|
}
|
68
|
+
this.vaultType = vault.vaultType
|
67
69
|
}
|
68
70
|
|
69
71
|
/**
|
@@ -82,7 +84,7 @@ export class VaultAccount {
|
|
82
84
|
* @returns collateral value in SOL
|
83
85
|
*/
|
84
86
|
public inSol(): number {
|
85
|
-
return this.deposited
|
87
|
+
return this.deposited / LAMPORTS_PER_SOL
|
86
88
|
}
|
87
89
|
|
88
90
|
/**
|
@@ -91,7 +93,7 @@ export class VaultAccount {
|
|
91
93
|
* @returns debt value in USH
|
92
94
|
*/
|
93
95
|
public inUsd(): number {
|
94
|
-
return this.denormalizedDebt
|
96
|
+
return this.denormalizedDebt / LAMPORTS_PER_SOL
|
95
97
|
}
|
96
98
|
|
97
99
|
/**
|
@@ -105,42 +107,45 @@ export class VaultAccount {
|
|
105
107
|
.substring(this.publicKey.toString().length - 6)}`
|
106
108
|
}
|
107
109
|
|
108
|
-
public addDebt(
|
109
|
-
|
110
|
-
|
111
|
-
|
110
|
+
public addDebt(additionalDebt: Decimal, vaultTypeAccount: VaultType) {
|
111
|
+
let loanFee = new Decimal(0)
|
112
|
+
if (additionalDebt.isPositive()){
|
113
|
+
loanFee = vaultTypeAccount.loanInitFee.mul(additionalDebt)
|
114
|
+
}
|
115
|
+
|
116
|
+
const totalNormalizedLoan = additionalDebt.add(loanFee)
|
117
|
+
const denormalizedNewDebt = totalNormalizedLoan.div(new Decimal(vaultTypeAccount.cumulativeRate.toString()))
|
118
|
+
this.denormalizedDebt = denormalizedNewDebt.add(new Decimal(this.denormalizedDebt)).floor().toNumber()
|
112
119
|
}
|
120
|
+
|
113
121
|
public addDeposit(depositAmount: number) {
|
114
|
-
this.deposited
|
122
|
+
this.deposited += depositAmount
|
115
123
|
}
|
116
124
|
|
117
125
|
public redeem() {
|
118
126
|
// TODO - Calculate actual redeem amount and adust correctly
|
119
|
-
this.denormalizedDebt =
|
127
|
+
this.denormalizedDebt = 0
|
120
128
|
this.vaultStatus = 'initialized'
|
121
129
|
}
|
122
130
|
public liquidate() {
|
123
131
|
// TODO - Calculate actual liquidate amount and adust correctly
|
124
|
-
this.denormalizedDebt =
|
132
|
+
this.denormalizedDebt = 0
|
125
133
|
this.vaultStatus = 'liquidated'
|
126
134
|
}
|
127
135
|
|
128
|
-
public updateDebtAndCollateral(
|
129
|
-
const debtProductCurrent = DecimalFromU128(vaultTypeAccuntData.debtRedistributionProduct)
|
136
|
+
public updateDebtAndCollateral(vaultTypeAccountData: VaultType) {
|
130
137
|
|
131
|
-
|
132
|
-
|
133
|
-
this.denormalizedDebt = debtProductCurrent
|
138
|
+
this.denormalizedDebt = vaultTypeAccountData.debtRedistributionProduct
|
134
139
|
.div(this.debtProductSnapshotBytes)
|
135
140
|
.mul(new Decimal(this.denormalizedDebt))
|
141
|
+
.toNumber()
|
136
142
|
|
137
143
|
const extraCollateralDeposited =
|
138
|
-
this.denormalizedDebt.
|
139
|
-
|
140
|
-
this.deposited = this.deposited.add(extraCollateralDeposited)
|
144
|
+
this.denormalizedDebt * vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
|
145
|
+
this.deposited += extraCollateralDeposited
|
141
146
|
|
142
|
-
this.collateralAccumulatorSnapshotBytes =
|
143
|
-
this.debtProductSnapshotBytes =
|
147
|
+
this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator
|
148
|
+
this.debtProductSnapshotBytes = vaultTypeAccountData.debtRedistributionProduct
|
144
149
|
}
|
145
150
|
|
146
151
|
public toString(highlight: PublicKey): string {
|
@@ -149,7 +154,7 @@ export class VaultAccount {
|
|
149
154
|
arrow = ' <----!!'
|
150
155
|
}
|
151
156
|
let collateralRatio = 'Infinite'
|
152
|
-
if (this.denormalizedDebt
|
157
|
+
if (this.denormalizedDebt > 0) {
|
153
158
|
collateralRatio = new Decimal(this.deposited).div(new Decimal(this.denormalizedDebt)).toString()
|
154
159
|
}
|
155
160
|
|
@@ -160,7 +165,7 @@ export class VaultAccount {
|
|
160
165
|
|
161
166
|
return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${
|
162
167
|
this.denormalizedDebt
|
163
|
-
} Collat: ${this.deposited} Ratio: ${collateralRatio}
|
168
|
+
} Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `
|
164
169
|
}
|
165
170
|
/**
|
166
171
|
* Creates a VaultAccount from a slice of data
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { PublicKey } from '@solana/web3.js'
|
2
|
+
import Decimal from 'decimal.js'
|
3
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
|
+
|
5
|
+
|
6
|
+
export default class VaultType {
|
7
|
+
publicKey?: PublicKey
|
8
|
+
|
9
|
+
// On Chain Data
|
10
|
+
name: string
|
11
|
+
collateralMint: PublicKey
|
12
|
+
collateralIndex: number
|
13
|
+
recentPrice: Decimal
|
14
|
+
priceLastUpdatedTimestamp: Decimal
|
15
|
+
collateralHeld: Decimal
|
16
|
+
denormalizedDebtExtended: Decimal
|
17
|
+
debtRedistributionProduct: Decimal
|
18
|
+
collateralRedistributionAccumulator: Decimal
|
19
|
+
minCollateralRatio: Decimal
|
20
|
+
loanInitFee: Decimal
|
21
|
+
minDebtPerVault: Decimal
|
22
|
+
maxDebtExtended: Decimal
|
23
|
+
canBeRedeemed: boolean
|
24
|
+
cumulativeRate: Decimal
|
25
|
+
cumulativeRateLastUpdated: Decimal
|
26
|
+
interestRatePerSecond: Decimal
|
27
|
+
firstVaultToRedeem: PublicKey
|
28
|
+
|
29
|
+
constructor(config: any, publicKey?: PublicKey) {
|
30
|
+
this.publicKey = publicKey
|
31
|
+
this.name = config.collateralType
|
32
|
+
this.loanInitFee = DecimalFromU128(config.loanInitFee)
|
33
|
+
this.minCollateralRatio = DecimalFromU128(config.minCollateralRatio)
|
34
|
+
this.interestRatePerSecond = DecimalFromU128(config.interestRatePerSecond)
|
35
|
+
this.minDebtPerVault = DecimalFromU128(config.minDebtPerVault)
|
36
|
+
this.maxDebtExtended = DecimalFromU128(config.maxDebtExtended)
|
37
|
+
this.canBeRedeemed = config.canBeRedeemed
|
38
|
+
this.collateralMint = config.collateralMint
|
39
|
+
this.firstVaultToRedeem = config.firstVaultToRedeem
|
40
|
+
|
41
|
+
this.recentPrice = DecimalFromU128(config.recentPrice?.toString())
|
42
|
+
this.priceLastUpdatedTimestamp = new Decimal(
|
43
|
+
config.priceLastUpdatedTimestamp.toString()
|
44
|
+
)
|
45
|
+
this.collateralHeld = new Decimal(config.collateralHeld.toString())
|
46
|
+
this.denormalizedDebtExtended = new Decimal(
|
47
|
+
config.denormalizedDebtExtended.toString()
|
48
|
+
)
|
49
|
+
this.debtRedistributionProduct = DecimalFromU128(
|
50
|
+
config.debtRedistributionProduct.toString()
|
51
|
+
)
|
52
|
+
this.collateralRedistributionAccumulator = DecimalFromU128(
|
53
|
+
config.collateralRedistributionAccumulator.toString()
|
54
|
+
)
|
55
|
+
this.cumulativeRate = DecimalFromU128(config.cumulativeRate.toString())
|
56
|
+
this.cumulativeRateLastUpdated = new Decimal(
|
57
|
+
config.cumulativeRateLastUpdated.toString()
|
58
|
+
)
|
59
|
+
this.collateralIndex = config.collateralIndex.toNumber()
|
60
|
+
|
61
|
+
}
|
62
|
+
}
|
@@ -6,10 +6,11 @@ import { getVaultSystemStatePublicKey, getVaultTypeOracleAccountPublicKey, HEDGE
|
|
6
6
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
7
7
|
import { VaultAccount } from '../state/VaultAccount'
|
8
8
|
import Decimal from 'decimal.js'
|
9
|
-
import { Vault } from 'idl/vault'
|
9
|
+
import { Vault } from '../idl/vault'
|
10
10
|
|
11
11
|
import * as borsh from '@project-serum/borsh'
|
12
12
|
import base58 from 'bs58'
|
13
|
+
import VaultType from '../state/VaultType'
|
13
14
|
|
14
15
|
export async function getLinkedListAccounts(
|
15
16
|
program: Program<Vault>,
|
@@ -22,8 +23,10 @@ export async function getLinkedListAccounts(
|
|
22
23
|
liquidate: boolean,
|
23
24
|
cachedVaults?: VaultAccount[]
|
24
25
|
): Promise<[PublicKey, PublicKey, PublicKey, VaultAccount[]]> {
|
25
|
-
|
26
|
-
const
|
26
|
+
const vaultTypeRaw = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
27
|
+
const vaultType = new VaultType(vaultTypeRaw, vaultTypeAccountPublicKey)
|
28
|
+
|
29
|
+
const DEBUG = false
|
27
30
|
|
28
31
|
// Default for null is the vault itself, so set them all to this vault
|
29
32
|
let oldSmallerPublicKey = vaultPublicKey
|
@@ -31,6 +34,7 @@ export async function getLinkedListAccounts(
|
|
31
34
|
let newLargerPublicKey = vaultPublicKey
|
32
35
|
|
33
36
|
const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
|
37
|
+
// const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
|
34
38
|
const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
|
35
39
|
|
36
40
|
// Load all the vaults
|
@@ -38,37 +42,23 @@ export async function getLinkedListAccounts(
|
|
38
42
|
if (cachedVaults) {
|
39
43
|
vaults = cachedVaults
|
40
44
|
} else {
|
41
|
-
|
42
|
-
// ? cachedVaults
|
43
|
-
// : (await program.account.vault
|
44
|
-
// .all([
|
45
|
-
// {
|
46
|
-
// memcmp: { bytes: vaultTypeAccount.collateralType, offset: 8 + 32 + 8 },
|
47
|
-
// },
|
48
|
-
// ])
|
49
|
-
// .catch((error) => {
|
50
|
-
// console.log('error', error)
|
51
|
-
// })) || []
|
52
|
-
|
53
|
-
// // Load them into our account objects
|
54
|
-
// vaults = allVaults.map((vault) => {
|
55
|
-
// return new VaultAccount(vault.account, vault.publicKey)
|
56
|
-
// })
|
57
|
-
vaults = await getMiniVaults(program, vaultTypeAccount.collateralType)
|
45
|
+
vaults = await getMiniVaults(program, vaultTypeAccountPublicKey)
|
58
46
|
}
|
59
47
|
|
60
|
-
|
61
|
-
|
48
|
+
if (DEBUG) {
|
49
|
+
console.log('Vault count found:', vaults.length)
|
50
|
+
console.log('First Vault', vaults[0])
|
51
|
+
}
|
62
52
|
|
63
53
|
// Filter out the accounts that are not open
|
64
|
-
// TODO filter on vault status. Or we enable people to "close out" empty vaults
|
54
|
+
// TODO filter on vault status. Or we enable people to "close out" empty vaults
|
65
55
|
// vaults = _.filter(vaults, (vault) => {
|
66
56
|
// return vault.vaultStatus === 'open'
|
67
57
|
// })
|
68
58
|
|
69
59
|
// Remove any vaults with no debt or collateral
|
70
60
|
vaults = _.filter(vaults, (vault) => {
|
71
|
-
return vault.denormalizedDebt
|
61
|
+
return vault.denormalizedDebt > 0 && vault.deposited > 0
|
72
62
|
})
|
73
63
|
|
74
64
|
// Sort them
|
@@ -87,6 +77,16 @@ export async function getLinkedListAccounts(
|
|
87
77
|
oldSmallerPublicKey = vaults[indexBefore - 1].publicKey
|
88
78
|
}
|
89
79
|
|
80
|
+
if (DEBUG) {
|
81
|
+
// Pretty print the list again
|
82
|
+
console.log('Sorted open vaults. Index Before: ', indexBefore)
|
83
|
+
console.log(
|
84
|
+
vaults.map((vault) => {
|
85
|
+
return vault.toString(vaultPublicKey)
|
86
|
+
})
|
87
|
+
)
|
88
|
+
}
|
89
|
+
|
90
90
|
// Pretty print all the vaults before the operation
|
91
91
|
// console.log('Sorted open vaults BEFORE at index:', indexBefore)
|
92
92
|
// let correctOrderBefore = true
|
@@ -118,9 +118,8 @@ export async function getLinkedListAccounts(
|
|
118
118
|
// Now that we know it's def in the list, iterate the list and update
|
119
119
|
// this vault with the operation we're going to apply
|
120
120
|
const newNormalizedDebt = new Decimal(loanAmount)
|
121
|
-
|
122
|
-
vaults[indexBefore].
|
123
|
-
vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest)
|
121
|
+
vaults[indexBefore].updateDebtAndCollateral(vaultType)
|
122
|
+
vaults[indexBefore].addDebt(newNormalizedDebt, vaultType)
|
124
123
|
vaults[indexBefore].addDeposit(depositAmount)
|
125
124
|
|
126
125
|
if (liquidate) {
|
@@ -130,19 +129,13 @@ export async function getLinkedListAccounts(
|
|
130
129
|
vaults[indexBefore].redeem()
|
131
130
|
}
|
132
131
|
|
133
|
-
if (vaults[indexBefore].denormalizedDebt
|
132
|
+
if (vaults[indexBefore].denormalizedDebt === 0) {
|
134
133
|
vaults.splice(indexBefore, 1)
|
135
134
|
}
|
136
135
|
|
137
136
|
// Sort it again since we've changed one vault
|
138
137
|
vaults = vaults.sort(sortVaults)
|
139
138
|
|
140
|
-
// Pretty print the list again
|
141
|
-
// console.log('Sorted open vaults with new debt added at index: ', indexAfter)
|
142
|
-
// console.log(vaults.map((vault) => {
|
143
|
-
// return vault.toString(vaultPublicKey)
|
144
|
-
// }))
|
145
|
-
|
146
139
|
// Search for the vaults new position
|
147
140
|
let indexAfter = -1
|
148
141
|
vaults.forEach((vault, index) => {
|
@@ -151,17 +144,20 @@ export async function getLinkedListAccounts(
|
|
151
144
|
}
|
152
145
|
})
|
153
146
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
147
|
+
if (DEBUG) {
|
148
|
+
// New list with vault
|
149
|
+
console.log('New list with vault now at index:', indexAfter)
|
150
|
+
console.log(
|
151
|
+
vaults.map((vault) => {
|
152
|
+
return vault.toString(vaultPublicKey)
|
153
|
+
})
|
154
|
+
)
|
155
|
+
}
|
161
156
|
|
162
|
-
|
163
|
-
|
164
|
-
|
157
|
+
if (DEBUG) {
|
158
|
+
// Print where it moved from / to
|
159
|
+
console.log('Index After', indexAfter)
|
160
|
+
}
|
165
161
|
|
166
162
|
// Save references to the new left and right
|
167
163
|
if (indexAfter > 0) {
|
@@ -171,10 +167,12 @@ export async function getLinkedListAccounts(
|
|
171
167
|
newLargerPublicKey = vaults[indexAfter + 1].publicKey
|
172
168
|
}
|
173
169
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
170
|
+
if (DEBUG) {
|
171
|
+
// Print out the new left/right
|
172
|
+
console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
|
173
|
+
console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
|
174
|
+
console.log('newLargerPublicKey', newLargerPublicKey.toString())
|
175
|
+
}
|
178
176
|
|
179
177
|
return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults]
|
180
178
|
}
|
@@ -182,32 +180,35 @@ export async function getLinkedListAccounts(
|
|
182
180
|
// Sort function we can use to sort the vaults
|
183
181
|
// Sorted by collateral ratio. If two are the same, newer vault first
|
184
182
|
function sortVaults(a: VaultAccount, b: VaultAccount) {
|
185
|
-
const aRatio = a.deposited
|
186
|
-
const bRatio = b.deposited
|
187
|
-
if (aRatio
|
183
|
+
const aRatio = a.deposited / a.denormalizedDebt
|
184
|
+
const bRatio = b.deposited / b.denormalizedDebt
|
185
|
+
if (aRatio === bRatio) {
|
188
186
|
return b.vaultNumber - a.vaultNumber
|
189
187
|
}
|
190
|
-
|
191
|
-
return 1
|
192
|
-
}
|
193
|
-
|
194
|
-
return -1
|
188
|
+
return aRatio - bRatio
|
195
189
|
}
|
196
190
|
|
197
|
-
async function getMiniVaults(program: Program<Vault>,
|
191
|
+
async function getMiniVaults(program: Program<Vault>, vaultTypePublicKey: PublicKey) {
|
198
192
|
const filters = [
|
199
193
|
// Filter for Vault Accounts
|
200
194
|
{
|
201
195
|
// @ts-ignore
|
202
196
|
memcmp: program.account.vault.coder.accounts.memcmp(program.account.vault._idlAccount.name),
|
203
197
|
},
|
204
|
-
// Filter for Vaults
|
198
|
+
// Filter for Vaults that are open
|
205
199
|
{
|
206
200
|
memcmp: {
|
207
|
-
bytes: base58.encode(
|
201
|
+
bytes: base58.encode(Buffer.from([1])),
|
208
202
|
offset: 8 + 32 + 24,
|
209
203
|
},
|
210
204
|
},
|
205
|
+
// Filter for Vaults with this collateral type
|
206
|
+
{
|
207
|
+
memcmp: {
|
208
|
+
bytes: vaultTypePublicKey.toString(),
|
209
|
+
offset: 8 + 32 + 24 + 1,
|
210
|
+
},
|
211
|
+
},
|
211
212
|
]
|
212
213
|
const allAccounts = await program.provider.connection.getProgramAccounts(HEDGE_PROGRAM_PUBLICKEY, {
|
213
214
|
filters: filters,
|
@@ -219,7 +220,7 @@ async function getMiniVaults(program: Program<Vault>, collateralType: string) {
|
|
219
220
|
},
|
220
221
|
})
|
221
222
|
|
222
|
-
return allAccounts.map(vaultData=>{
|
223
|
+
return allAccounts.map((vaultData) => {
|
223
224
|
return VaultAccount.FromMiniSlice(vaultData.account.data, vaultData.pubkey)
|
224
225
|
})
|
225
226
|
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { Connection, Keypair, Signer, Transaction, TransactionSignature } from '@solana/web3.js'
|
2
|
+
|
3
|
+
export default async function sendAndConfirmWithDebug(
|
4
|
+
connection: Connection,
|
5
|
+
transaction: Transaction,
|
6
|
+
signers: Signer[]
|
7
|
+
): Promise<TransactionSignature | void> {
|
8
|
+
return connection
|
9
|
+
.sendTransaction(transaction, signers)
|
10
|
+
.then((signature) => {
|
11
|
+
return connection
|
12
|
+
.confirmTransaction(signature)
|
13
|
+
.then((signatureContext) => {
|
14
|
+
return signature
|
15
|
+
})
|
16
|
+
.catch((error) => {
|
17
|
+
console.log('There was an error confirming the transaction', error)
|
18
|
+
console.trace()
|
19
|
+
throw error
|
20
|
+
})
|
21
|
+
})
|
22
|
+
.catch((error) => {
|
23
|
+
console.log('There was an error sending the transaction', error)
|
24
|
+
console.trace()
|
25
|
+
throw error
|
26
|
+
})
|
27
|
+
}
|