hedge-web3 0.1.46 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/Constants.d.ts +2 -1
- package/declarations/idl/vault.d.ts +109 -47
- 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 -47
- 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 -94
- 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
@@ -16,7 +16,8 @@ import {
|
|
16
16
|
getUshMintPublicKey,
|
17
17
|
getVaultSystemStatePublicKey,
|
18
18
|
} from '../Constants'
|
19
|
-
import
|
19
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
20
|
+
import { Vault } from '../idl/vault'
|
20
21
|
|
21
22
|
export async function createStakingPool(
|
22
23
|
program: Program<Vault>,
|
@@ -29,7 +30,7 @@ export async function createStakingPool(
|
|
29
30
|
const transaction = new Transaction().add(
|
30
31
|
await createStakingPoolInstruction(program, payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime)
|
31
32
|
)
|
32
|
-
await
|
33
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
33
34
|
const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey)
|
34
35
|
return poolPublickey
|
35
36
|
}
|
@@ -41,7 +42,6 @@ export async function createStakingPoolInstruction(
|
|
41
42
|
hedgeTokensToBeMinted: number,
|
42
43
|
overrideStartTime?: number
|
43
44
|
): Promise<TransactionInstruction> {
|
44
|
-
console.log("new createStakingPoolInstruction")
|
45
45
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
46
46
|
const ushMintPublickey = await getUshMintPublicKey()
|
47
47
|
const [poolPublickey, poolBump] = await getPoolPublicKeyForMint(mintPublicKey)
|
@@ -23,7 +23,8 @@ import {
|
|
23
23
|
|
24
24
|
import { v4 as uuidv4 } from 'uuid'
|
25
25
|
import { parseAnchorErrors } from '../utils/Errors'
|
26
|
-
import
|
26
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
27
|
+
import { Vault } from '../idl/vault'
|
27
28
|
|
28
29
|
export async function createVault(
|
29
30
|
program: Program<Vault>,
|
@@ -106,7 +107,7 @@ export async function createVault(
|
|
106
107
|
)
|
107
108
|
}
|
108
109
|
|
109
|
-
await
|
110
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
|
110
111
|
return newVaultPublicKey
|
111
112
|
}
|
112
113
|
|
@@ -17,7 +17,8 @@ import {
|
|
17
17
|
getUshMintPublicKey,
|
18
18
|
getVaultSystemStatePublicKey,
|
19
19
|
} from '../Constants'
|
20
|
-
import
|
20
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
21
|
+
import { Vault } from '../idl/vault'
|
21
22
|
|
22
23
|
export async function depositLiquidationPool(
|
23
24
|
program: Program<Vault>,
|
@@ -45,7 +46,7 @@ export async function depositLiquidationPool(
|
|
45
46
|
overrideStartTime
|
46
47
|
)
|
47
48
|
)
|
48
|
-
await
|
49
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, poolPosition]).catch(parseAnchorErrors)
|
49
50
|
return poolPosition.publicKey
|
50
51
|
}
|
51
52
|
|
@@ -12,7 +12,8 @@ import {
|
|
12
12
|
} from '@solana/web3.js'
|
13
13
|
import { parseAnchorErrors } from '../utils/Errors'
|
14
14
|
import { findAssociatedTokenAddress, getPoolPublicKeyForMint, getVaultSystemStatePublicKey } from '../Constants'
|
15
|
-
import
|
15
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
16
|
+
import { Vault } from '../idl/vault'
|
16
17
|
|
17
18
|
export async function depositStakingPool(
|
18
19
|
program: Program<Vault>,
|
@@ -33,7 +34,7 @@ export async function depositStakingPool(
|
|
33
34
|
overrideStartTime
|
34
35
|
)
|
35
36
|
)
|
36
|
-
await
|
37
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, poolPosition]).catch(parseAnchorErrors)
|
37
38
|
return poolPosition.publicKey
|
38
39
|
}
|
39
40
|
|
@@ -19,7 +19,8 @@ import {
|
|
19
19
|
getPoolPublicKeyForMint,
|
20
20
|
getHedgeMintPublicKey,
|
21
21
|
} from '../Constants'
|
22
|
-
import
|
22
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
23
|
+
import { Vault } from '../idl/vault'
|
23
24
|
import { WRAPPED_SOL_MINT } from '@project-serum/serum/lib/token-instructions'
|
24
25
|
|
25
26
|
export async function depositVault(
|
@@ -64,7 +65,6 @@ export async function depositVault(
|
|
64
65
|
|
65
66
|
const transaction = new Transaction()
|
66
67
|
const signers = [payer, history]
|
67
|
-
|
68
68
|
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
69
69
|
program,
|
70
70
|
provider,
|
@@ -125,7 +125,7 @@ export async function depositVault(
|
|
125
125
|
)
|
126
126
|
}
|
127
127
|
|
128
|
-
await
|
128
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, signers)
|
129
129
|
return vaultPublicKey
|
130
130
|
}
|
131
131
|
|
@@ -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
|
+
}
|