hedge-web3 0.1.46 → 0.2.0
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 +109 -35
- package/declarations/instructions/claimLiquidationPoolPosition.d.ts +1 -1
- package/declarations/instructions/claimStakingPoolPosition.d.ts +1 -1
- 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 +1 -1
- 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 +4 -3
- package/declarations/utils/getLinkedListAccounts.d.ts +1 -1
- package/lib/Constants.js +3 -2
- package/lib/idl/vault.js +109 -35
- package/lib/instructions/claimLiquidationPoolPosition.js +5 -1
- package/lib/instructions/claimStakingPoolPosition.js +5 -1
- package/lib/instructions/closeLiquidationPoolPosition.js +5 -1
- package/lib/instructions/createStakingPool.js +5 -2
- 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 +5 -1
- package/lib/instructions/initHedgeFoundation.js +5 -1
- package/lib/instructions/initHedgeFoundationTokens.js +5 -1
- package/lib/instructions/liquidateVault.js +5 -1
- package/lib/instructions/loanVault.js +5 -3
- package/lib/instructions/redeemVault.js +5 -1
- package/lib/instructions/refreshOraclePrice.js +6 -1
- package/lib/instructions/repayVault.js +5 -1
- 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 +5 -1
- package/lib/state/VaultAccount.js +11 -9
- package/lib/utils/getLinkedListAccounts.js +48 -48
- package/package.json +1 -1
- package/src/Constants.ts +2 -1
- package/src/idl/vault.ts +218 -70
- package/src/instructions/claimLiquidationPoolPosition.ts +3 -2
- package/src/instructions/claimStakingPoolPosition.ts +3 -2
- package/src/instructions/closeLiquidationPoolPosition.ts +3 -2
- package/src/instructions/createStakingPool.ts +3 -3
- 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 +3 -3
- package/src/instructions/initHedgeFoundation.ts +3 -2
- package/src/instructions/initHedgeFoundationTokens.ts +2 -1
- package/src/instructions/liquidateVault.ts +5 -3
- package/src/instructions/loanVault.ts +3 -4
- package/src/instructions/redeemVault.ts +3 -2
- package/src/instructions/refreshOraclePrice.ts +7 -4
- package/src/instructions/repayVault.ts +3 -2
- package/src/instructions/setHalted.ts +3 -2
- package/src/instructions/updateVaultType.ts +10 -2
- package/src/instructions/withdrawStakingPool.ts +3 -2
- package/src/instructions/withdrawVault.ts +3 -2
- package/src/state/VaultAccount.ts +17 -12
- package/src/state/VaultType.ts +62 -0
- package/src/utils/getLinkedListAccounts.ts +54 -49
- package/src/utils/sendAndConfirmWithDebug.ts +27 -0
@@ -19,7 +19,8 @@ import {
|
|
19
19
|
getUshMintPublicKey,
|
20
20
|
getVaultSystemStatePublicKey,
|
21
21
|
} from '../Constants'
|
22
|
-
import
|
22
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
23
|
+
import { Vault } from '../idl/vault'
|
23
24
|
|
24
25
|
export async function initHedgeFoundation(program: Program<Vault>, provider: Provider, payer: Signer): Promise<PublicKey> {
|
25
26
|
const poolEra = Keypair.generate()
|
@@ -27,7 +28,7 @@ export async function initHedgeFoundation(program: Program<Vault>, provider: Pro
|
|
27
28
|
await initHedgeFoundationInstruction(program, poolEra.publicKey, payer.publicKey)
|
28
29
|
)
|
29
30
|
|
30
|
-
await
|
31
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, poolEra]).catch(parseAnchorErrors)
|
31
32
|
return payer.publicKey
|
32
33
|
}
|
33
34
|
|
@@ -19,12 +19,13 @@ import {
|
|
19
19
|
getUshMintPublicKey,
|
20
20
|
getVaultSystemStatePublicKey,
|
21
21
|
} from '../Constants'
|
22
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
22
23
|
|
23
24
|
export async function initHedgeFoundationTokens(program: Program, provider: Provider, payer: Signer): Promise<PublicKey> {
|
24
25
|
const poolEra = Keypair.generate()
|
25
26
|
const transaction = new Transaction().add(await initHedgeFoundationTokensInstruction(program, payer.publicKey))
|
26
27
|
|
27
|
-
await
|
28
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
28
29
|
return payer.publicKey
|
29
30
|
}
|
30
31
|
|
@@ -1,6 +1,7 @@
|
|
1
|
-
import { BN, Program, Provider } from '@project-serum/anchor'
|
1
|
+
import { BN, Program, Provider, translateAddress } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
3
3
|
import {
|
4
|
+
Connection,
|
4
5
|
Keypair,
|
5
6
|
PublicKey,
|
6
7
|
sendAndConfirmTransaction,
|
@@ -20,7 +21,8 @@ import {
|
|
20
21
|
getUshMintPublicKey,
|
21
22
|
getVaultSystemStatePublicKey,
|
22
23
|
} from '../Constants'
|
23
|
-
import
|
24
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
25
|
+
import { Vault } from '../idl/vault'
|
24
26
|
|
25
27
|
export async function liquidateVault(
|
26
28
|
program: Program<Vault>,
|
@@ -122,7 +124,7 @@ export async function liquidateVault(
|
|
122
124
|
overrideTime
|
123
125
|
)
|
124
126
|
)
|
125
|
-
await
|
127
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history, newEra])
|
126
128
|
return vaultPublicKey
|
127
129
|
}
|
128
130
|
|
@@ -19,12 +19,11 @@ import {
|
|
19
19
|
getVaultSystemStatePublicKey,
|
20
20
|
HEDGE_PROGRAM_PUBLICKEY,
|
21
21
|
} from '../Constants'
|
22
|
-
import
|
22
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
23
|
+
import { Vault } from '../idl/vault'
|
23
24
|
import { parseAnchorErrors } from '../utils/Errors'
|
24
25
|
import { VaultAccount } from '../state/VaultAccount'
|
25
26
|
|
26
|
-
const fs = require('fs');
|
27
|
-
|
28
27
|
export async function loanVault(
|
29
28
|
program: Program<Vault>,
|
30
29
|
provider: Provider,
|
@@ -87,7 +86,7 @@ export async function loanVault(
|
|
87
86
|
overrideTime
|
88
87
|
)
|
89
88
|
)
|
90
|
-
await
|
89
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
|
91
90
|
return vaultPublicKey
|
92
91
|
}
|
93
92
|
|
@@ -19,7 +19,8 @@ import {
|
|
19
19
|
getUshMintPublicKey,
|
20
20
|
getVaultSystemStatePublicKey,
|
21
21
|
} from '../Constants'
|
22
|
-
import
|
22
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
23
|
+
import { Vault } from '../idl/vault'
|
23
24
|
|
24
25
|
export async function redeemVault(
|
25
26
|
program: Program<Vault>,
|
@@ -94,7 +95,7 @@ export async function redeemVault(
|
|
94
95
|
transactionOverrideTime
|
95
96
|
)
|
96
97
|
)
|
97
|
-
await
|
98
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
|
98
99
|
return vaultPublicKey
|
99
100
|
}
|
100
101
|
|
@@ -7,9 +7,11 @@ import {
|
|
7
7
|
SystemProgram,
|
8
8
|
Transaction,
|
9
9
|
TransactionInstruction,
|
10
|
+
TransactionSignature,
|
10
11
|
} from '@solana/web3.js'
|
11
|
-
import
|
12
|
-
import {
|
12
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
13
|
+
import { Vault } from '../idl/vault'
|
14
|
+
import { HEDGE_PROGRAM_PUBLICKEY, CHAINLINK_PROGRAM_ID } from '../Constants'
|
13
15
|
|
14
16
|
export async function refreshOraclePrice(
|
15
17
|
program: Program<Vault>,
|
@@ -19,11 +21,11 @@ export async function refreshOraclePrice(
|
|
19
21
|
network: Cluster,
|
20
22
|
overridePrice?: number,
|
21
23
|
overrideTime?: number
|
22
|
-
): Promise<
|
24
|
+
): Promise<TransactionSignature | void> {
|
23
25
|
const transaction = new Transaction().add(
|
24
26
|
await refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime)
|
25
27
|
)
|
26
|
-
return await
|
28
|
+
return await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
|
27
29
|
}
|
28
30
|
|
29
31
|
export async function refreshOraclePriceInstruction(
|
@@ -55,6 +57,7 @@ export async function refreshOraclePriceInstruction(
|
|
55
57
|
oraclePyth: pythAccounts[network],
|
56
58
|
oracleSwitchboard: switchboardAccounts[network],
|
57
59
|
systemProgram: SystemProgram.programId,
|
60
|
+
chainlinkProgram: CHAINLINK_PROGRAM_ID,
|
58
61
|
})
|
59
62
|
.instruction()
|
60
63
|
}
|
@@ -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 repayVault(
|
24
25
|
program: Program<Vault>,
|
@@ -75,7 +76,7 @@ export async function repayVault(
|
|
75
76
|
overrideTime
|
76
77
|
)
|
77
78
|
)
|
78
|
-
await
|
79
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
|
79
80
|
return vaultPublicKey
|
80
81
|
}
|
81
82
|
|
@@ -22,7 +22,8 @@ import {
|
|
22
22
|
} from '../Constants'
|
23
23
|
|
24
24
|
import { parseAnchorErrors } from '../utils/Errors'
|
25
|
-
import
|
25
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
26
|
+
import { Vault } from '../idl/vault'
|
26
27
|
|
27
28
|
export async function setHalted(
|
28
29
|
program: Program<Vault>,
|
@@ -36,7 +37,7 @@ export async function setHalted(
|
|
36
37
|
await setHaltedInstruction(program, vaultSystemStatePublicKey, payer.publicKey, halted)
|
37
38
|
)
|
38
39
|
|
39
|
-
await
|
40
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
40
41
|
return vaultSystemStatePublicKey
|
41
42
|
}
|
42
43
|
|
@@ -21,7 +21,8 @@ import {
|
|
21
21
|
|
22
22
|
import { v4 as uuidv4 } from 'uuid'
|
23
23
|
import { parseAnchorErrors } from '../utils/Errors'
|
24
|
-
import
|
24
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
25
|
+
import { Vault } from '../idl/vault'
|
25
26
|
|
26
27
|
export interface VaultTypeConfig {
|
27
28
|
maxDebtExtended?: BN
|
@@ -32,6 +33,10 @@ export interface VaultTypeConfig {
|
|
32
33
|
oraclePyth?: PublicKey
|
33
34
|
oracleSwitchboard?: PublicKey
|
34
35
|
|
36
|
+
priorityPyth?: number,
|
37
|
+
priorityChainlink?: number,
|
38
|
+
prioritySwitchboard?: number,
|
39
|
+
|
35
40
|
deprecated?: boolean
|
36
41
|
}
|
37
42
|
|
@@ -55,7 +60,7 @@ export async function updateVaultType(
|
|
55
60
|
)
|
56
61
|
)
|
57
62
|
|
58
|
-
await
|
63
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
|
59
64
|
return vaultSystemStatePublicKey
|
60
65
|
}
|
61
66
|
|
@@ -75,6 +80,9 @@ export async function updateVaultTypeStatusInstruction(
|
|
75
80
|
oraclePyth: vaultTypeConfig.oraclePyth ?? null,
|
76
81
|
oracleSwitchboard: vaultTypeConfig.oracleSwitchboard ?? null,
|
77
82
|
deprecated: vaultTypeConfig.deprecated ?? null,
|
83
|
+
priorityPyth: vaultTypeConfig.priorityPyth ?? null,
|
84
|
+
priorityChainlink: vaultTypeConfig.priorityChainlink ?? null,
|
85
|
+
prioritySwitchboard: vaultTypeConfig.prioritySwitchboard ?? null,
|
78
86
|
}
|
79
87
|
|
80
88
|
return await program.methods
|
@@ -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>,
|
@@ -38,7 +39,7 @@ export async function withdrawStakingPool(
|
|
38
39
|
overrideStartTime
|
39
40
|
)
|
40
41
|
)
|
41
|
-
await
|
42
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
42
43
|
return poolPosition.publicKey
|
43
44
|
}
|
44
45
|
|
@@ -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>,
|
@@ -98,7 +99,7 @@ export async function withdrawVault(
|
|
98
99
|
overrideTime
|
99
100
|
)
|
100
101
|
)
|
101
|
-
await
|
102
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
|
102
103
|
return vaultPublicKey
|
103
104
|
}
|
104
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.
|
@@ -32,7 +34,7 @@ export class VaultAccount {
|
|
32
34
|
/** Collateral redistribution snapshot' */
|
33
35
|
collateralAccumulatorSnapshotBytes = new Decimal(0)
|
34
36
|
|
35
|
-
/** The vault type eg '2-SOL-150-0' */
|
37
|
+
/** The vault type eg '2-SOL-150-0-----' */
|
36
38
|
vaultTypeName: string
|
37
39
|
|
38
40
|
/** Current State of the vault ("Open", "Closed", "Liquidated") */
|
@@ -105,11 +107,17 @@ export class VaultAccount {
|
|
105
107
|
.substring(this.publicKey.toString().length - 6)}`
|
106
108
|
}
|
107
109
|
|
108
|
-
public addDebt(
|
109
|
-
|
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()))
|
110
118
|
this.denormalizedDebt = denormalizedNewDebt.add(new Decimal(this.denormalizedDebt)).floor().toNumber()
|
111
|
-
// this.denormalizedDebt = parseFloat(this.denormalizedDebt.toFixed(0))
|
112
119
|
}
|
120
|
+
|
113
121
|
public addDeposit(depositAmount: number) {
|
114
122
|
this.deposited += depositAmount
|
115
123
|
}
|
@@ -125,22 +133,19 @@ export class VaultAccount {
|
|
125
133
|
this.vaultStatus = 'liquidated'
|
126
134
|
}
|
127
135
|
|
128
|
-
public updateDebtAndCollateral(vaultTypeAccountData:
|
129
|
-
const debtProductCurrent = DecimalFromU128(vaultTypeAccountData.debtRedistributionProduct)
|
130
|
-
|
131
|
-
const collateralAccumulatorCurrent = DecimalFromU128(vaultTypeAccountData.collateralRedistributionAccumulator)
|
136
|
+
public updateDebtAndCollateral(vaultTypeAccountData: VaultType) {
|
132
137
|
|
133
|
-
this.denormalizedDebt =
|
138
|
+
this.denormalizedDebt = vaultTypeAccountData.debtRedistributionProduct
|
134
139
|
.div(this.debtProductSnapshotBytes)
|
135
140
|
.mul(new Decimal(this.denormalizedDebt))
|
136
141
|
.toNumber()
|
137
142
|
|
138
143
|
const extraCollateralDeposited =
|
139
|
-
this.denormalizedDebt *
|
144
|
+
this.denormalizedDebt * vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
|
140
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 {
|
@@ -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,7 +34,7 @@ export async function getLinkedListAccounts(
|
|
31
34
|
let newLargerPublicKey = vaultPublicKey
|
32
35
|
|
33
36
|
const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
|
34
|
-
const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
|
37
|
+
// const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
|
35
38
|
const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
|
36
39
|
|
37
40
|
// Load all the vaults
|
@@ -39,30 +42,16 @@ export async function getLinkedListAccounts(
|
|
39
42
|
if (cachedVaults) {
|
40
43
|
vaults = cachedVaults
|
41
44
|
} else {
|
42
|
-
|
43
|
-
// ? cachedVaults
|
44
|
-
// : (await program.account.vault
|
45
|
-
// .all([
|
46
|
-
// {
|
47
|
-
// memcmp: { bytes: vaultTypeAccount.collateralType, offset: 8 + 32 + 8 },
|
48
|
-
// },
|
49
|
-
// ])
|
50
|
-
// .catch((error) => {
|
51
|
-
// console.log('error', error)
|
52
|
-
// })) || []
|
53
|
-
|
54
|
-
// // Load them into our account objects
|
55
|
-
// vaults = allVaults.map((vault) => {
|
56
|
-
// return new VaultAccount(vault.account, vault.publicKey)
|
57
|
-
// })
|
58
|
-
vaults = await getMiniVaults(program, vaultTypeAccount.vaultTypeName)
|
45
|
+
vaults = await getMiniVaults(program, vaultTypeAccountPublicKey)
|
59
46
|
}
|
60
47
|
|
61
|
-
|
62
|
-
|
48
|
+
if (DEBUG) {
|
49
|
+
console.log('Vault count found:', vaults.length)
|
50
|
+
console.log('First Vault', vaults[0])
|
51
|
+
}
|
63
52
|
|
64
53
|
// Filter out the accounts that are not open
|
65
|
-
// 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
|
66
55
|
// vaults = _.filter(vaults, (vault) => {
|
67
56
|
// return vault.vaultStatus === 'open'
|
68
57
|
// })
|
@@ -88,11 +77,15 @@ export async function getLinkedListAccounts(
|
|
88
77
|
oldSmallerPublicKey = vaults[indexBefore - 1].publicKey
|
89
78
|
}
|
90
79
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
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
|
+
}
|
96
89
|
|
97
90
|
// Pretty print all the vaults before the operation
|
98
91
|
// console.log('Sorted open vaults BEFORE at index:', indexBefore)
|
@@ -125,9 +118,8 @@ export async function getLinkedListAccounts(
|
|
125
118
|
// Now that we know it's def in the list, iterate the list and update
|
126
119
|
// this vault with the operation we're going to apply
|
127
120
|
const newNormalizedDebt = new Decimal(loanAmount)
|
128
|
-
|
129
|
-
vaults[indexBefore].
|
130
|
-
vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest)
|
121
|
+
vaults[indexBefore].updateDebtAndCollateral(vaultType)
|
122
|
+
vaults[indexBefore].addDebt(newNormalizedDebt, vaultType)
|
131
123
|
vaults[indexBefore].addDeposit(depositAmount)
|
132
124
|
|
133
125
|
if (liquidate) {
|
@@ -152,16 +144,20 @@ export async function getLinkedListAccounts(
|
|
152
144
|
}
|
153
145
|
})
|
154
146
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
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
|
+
}
|
162
156
|
|
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
|
}
|
@@ -190,20 +188,27 @@ function sortVaults(a: VaultAccount, b: VaultAccount) {
|
|
190
188
|
return aRatio - bRatio
|
191
189
|
}
|
192
190
|
|
193
|
-
async function getMiniVaults(program: Program<Vault>,
|
191
|
+
async function getMiniVaults(program: Program<Vault>, vaultTypePublicKey: PublicKey) {
|
194
192
|
const filters = [
|
195
193
|
// Filter for Vault Accounts
|
196
194
|
{
|
197
195
|
// @ts-ignore
|
198
196
|
memcmp: program.account.vault.coder.accounts.memcmp(program.account.vault._idlAccount.name),
|
199
197
|
},
|
200
|
-
// Filter for Vaults
|
198
|
+
// Filter for Vaults that are open
|
201
199
|
{
|
202
200
|
memcmp: {
|
203
|
-
bytes: base58.encode(
|
201
|
+
bytes: base58.encode(Buffer.from([1])),
|
204
202
|
offset: 8 + 32 + 24,
|
205
203
|
},
|
206
204
|
},
|
205
|
+
// Filter for Vaults with this collateral type
|
206
|
+
{
|
207
|
+
memcmp: {
|
208
|
+
bytes: vaultTypePublicKey.toString(),
|
209
|
+
offset: 8 + 32 + 24 + 1,
|
210
|
+
},
|
211
|
+
},
|
207
212
|
]
|
208
213
|
const allAccounts = await program.provider.connection.getProgramAccounts(HEDGE_PROGRAM_PUBLICKEY, {
|
209
214
|
filters: filters,
|
@@ -215,7 +220,7 @@ async function getMiniVaults(program: Program<Vault>, vaultTypeName: string ) {
|
|
215
220
|
},
|
216
221
|
})
|
217
222
|
|
218
|
-
return allAccounts.map(vaultData=>{
|
223
|
+
return allAccounts.map((vaultData) => {
|
219
224
|
return VaultAccount.FromMiniSlice(vaultData.account.data, vaultData.pubkey)
|
220
225
|
})
|
221
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
|
+
}
|