hedge-web3 0.1.28 → 0.1.32
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/idl/vault.d.ts +277 -126
- package/declarations/index.d.ts +1 -0
- package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
- package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
- package/declarations/instructions/createStakingPool.d.ts +3 -2
- package/declarations/instructions/createVault.d.ts +6 -5
- package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
- package/declarations/instructions/depositStakingPool.d.ts +3 -2
- package/declarations/instructions/depositVault.d.ts +3 -2
- package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
- package/declarations/instructions/liquidateVault.d.ts +3 -2
- package/declarations/instructions/loanVault.d.ts +3 -2
- package/declarations/instructions/redeemVault.d.ts +3 -2
- package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
- package/declarations/instructions/repayVault.d.ts +3 -2
- package/declarations/instructions/setHalted.d.ts +3 -2
- package/declarations/instructions/setVaultTypeStatus.d.ts +3 -2
- package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
- package/declarations/instructions/withdrawVault.d.ts +3 -2
- package/declarations/state/VaultAccount.d.ts +1 -1
- package/declarations/utils/getLinkedListAccounts.d.ts +3 -1
- package/lib/idl/vault.js +277 -126
- package/lib/index.js +1 -0
- package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
- package/lib/instructions/claimStakingPoolPosition.js +19 -19
- package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
- package/lib/instructions/createStakingPool.js +17 -18
- package/lib/instructions/createVault.js +28 -31
- package/lib/instructions/depositLiquidationPool.js +17 -18
- package/lib/instructions/depositStakingPool.js +16 -18
- package/lib/instructions/depositVault.js +25 -27
- package/lib/instructions/initHedgeFoundation.js +17 -19
- package/lib/instructions/initHedgeFoundationTokens.js +15 -15
- package/lib/instructions/liquidateVault.js +32 -33
- package/lib/instructions/loanVault.js +23 -23
- package/lib/instructions/redeemVault.js +24 -24
- package/lib/instructions/refreshOraclePrice.js +17 -17
- package/lib/instructions/repayVault.js +23 -23
- package/lib/instructions/setHalted.js +8 -9
- package/lib/instructions/setVaultTypeStatus.js +9 -10
- package/lib/instructions/withdrawStakingPool.js +22 -24
- package/lib/instructions/withdrawVault.js +23 -23
- package/lib/state/LiquidationPoolEra.js +3 -1
- package/lib/state/LiquidationPosition.js +0 -7
- package/lib/state/StakingPool.js +3 -4
- package/lib/state/VaultAccount.js +2 -5
- package/lib/utils/getLinkedListAccounts.js +24 -16
- package/package.json +1 -1
- package/src/idl/vault.ts +554 -252
- package/src/index.ts +1 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
- package/src/instructions/claimStakingPoolPosition.ts +45 -25
- package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
- package/src/instructions/createStakingPool.ts +37 -35
- package/src/instructions/createVault.ts +81 -125
- package/src/instructions/depositLiquidationPool.ts +45 -26
- package/src/instructions/depositStakingPool.ts +32 -24
- package/src/instructions/depositVault.ts +57 -86
- package/src/instructions/initHedgeFoundation.ts +42 -43
- package/src/instructions/initHedgeFoundationTokens.ts +38 -39
- package/src/instructions/liquidateVault.ts +42 -65
- package/src/instructions/loanVault.ts +51 -69
- package/src/instructions/redeemVault.ts +83 -47
- package/src/instructions/refreshOraclePrice.ts +41 -32
- package/src/instructions/repayVault.ts +45 -65
- package/src/instructions/setHalted.ts +32 -24
- package/src/instructions/setVaultTypeStatus.ts +32 -24
- package/src/instructions/withdrawStakingPool.ts +44 -30
- package/src/instructions/withdrawVault.ts +58 -82
- package/src/state/LiquidationPoolEra.ts +4 -3
- package/src/state/LiquidationPosition.ts +0 -27
- package/src/state/StakingPool.ts +4 -7
- package/src/state/StakingPoolPosition.ts +2 -3
- package/src/state/VaultAccount.ts +9 -28
- package/src/state/VaultHistoryEvent.ts +1 -2
- package/src/utils/getLinkedListAccounts.ts +31 -30
@@ -1,48 +1,56 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { TokenInstructions } from '@project-serum/serum'
|
3
3
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
4
|
-
import {
|
5
|
-
|
4
|
+
import {
|
5
|
+
Keypair,
|
6
|
+
PublicKey,
|
7
|
+
sendAndConfirmTransaction,
|
8
|
+
Signer,
|
9
|
+
SystemProgram,
|
10
|
+
SYSVAR_RENT_PUBKEY,
|
11
|
+
Transaction,
|
12
|
+
TransactionInstruction,
|
13
|
+
} from '@solana/web3.js'
|
14
|
+
import {
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
findVaultAddress,
|
17
|
+
getVaultTypeAccountPublicKey,
|
18
|
+
getUshMintPublicKey,
|
19
|
+
getVaultSystemStatePublicKey,
|
20
|
+
getPoolPublicKeyForMint,
|
21
|
+
getHedgeMintPublicKey,
|
22
|
+
} from '../Constants'
|
6
23
|
|
7
|
-
import { v4 as uuidv4 } from 'uuid'
|
8
24
|
import { parseAnchorErrors } from '../utils/Errors'
|
25
|
+
import { Vault } from 'idl/vault'
|
9
26
|
|
10
|
-
export async function setHalted
|
11
|
-
program: Program
|
27
|
+
export async function setHalted(
|
28
|
+
program: Program<Vault>,
|
12
29
|
provider: Provider,
|
13
30
|
payer: Signer,
|
14
31
|
halted: boolean
|
15
32
|
): Promise<PublicKey> {
|
16
33
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
17
34
|
|
18
|
-
|
19
35
|
const transaction = new Transaction().add(
|
20
|
-
await setHaltedInstruction(
|
21
|
-
program,
|
22
|
-
vaultSystemStatePublicKey,
|
23
|
-
payer.publicKey,
|
24
|
-
halted
|
25
|
-
)
|
36
|
+
await setHaltedInstruction(program, vaultSystemStatePublicKey, payer.publicKey, halted)
|
26
37
|
)
|
27
38
|
|
28
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
39
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
29
40
|
return vaultSystemStatePublicKey
|
30
41
|
}
|
31
42
|
|
32
|
-
export async function setHaltedInstruction
|
33
|
-
program: Program
|
43
|
+
export async function setHaltedInstruction(
|
44
|
+
program: Program<Vault>,
|
34
45
|
vaultSystemStatePublicKey: PublicKey,
|
35
46
|
payerPublicKey: PublicKey,
|
36
47
|
halted: boolean
|
37
48
|
): Promise<TransactionInstruction> {
|
38
|
-
|
39
|
-
halted
|
40
|
-
{
|
41
|
-
|
42
|
-
|
43
|
-
vaultSystemState: vaultSystemStatePublicKey,
|
44
|
-
},
|
45
|
-
signers: []
|
49
|
+
return await program.methods
|
50
|
+
.setHalted(halted)
|
51
|
+
.accounts({
|
52
|
+
payer: payerPublicKey,
|
53
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
46
54
|
})
|
47
|
-
|
55
|
+
.instruction()
|
48
56
|
}
|
@@ -1,12 +1,30 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
|
2
|
+
import {
|
3
|
+
Keypair,
|
4
|
+
PublicKey,
|
5
|
+
sendAndConfirmTransaction,
|
6
|
+
Signer,
|
7
|
+
SystemProgram,
|
8
|
+
SYSVAR_RENT_PUBKEY,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
12
|
+
import {
|
13
|
+
findAssociatedTokenAddress,
|
14
|
+
findVaultAddress,
|
15
|
+
getVaultTypeAccountPublicKey,
|
16
|
+
getUshMintPublicKey,
|
17
|
+
getVaultSystemStatePublicKey,
|
18
|
+
getPoolPublicKeyForMint,
|
19
|
+
getHedgeMintPublicKey,
|
20
|
+
} from '../Constants'
|
4
21
|
|
5
22
|
import { v4 as uuidv4 } from 'uuid'
|
6
23
|
import { parseAnchorErrors } from '../utils/Errors'
|
24
|
+
import { Vault } from 'idl/vault'
|
7
25
|
|
8
|
-
export async function setVaultTypeStatus
|
9
|
-
program: Program
|
26
|
+
export async function setVaultTypeStatus(
|
27
|
+
program: Program<Vault>,
|
10
28
|
provider: Provider,
|
11
29
|
payer: Signer,
|
12
30
|
vaultTypeAccount: PublicKey,
|
@@ -14,37 +32,27 @@ export async function setVaultTypeStatus (
|
|
14
32
|
): Promise<PublicKey> {
|
15
33
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
16
34
|
|
17
|
-
|
18
35
|
const transaction = new Transaction().add(
|
19
|
-
await setVaultTypeStatusInstruction(
|
20
|
-
program,
|
21
|
-
vaultSystemStatePublicKey,
|
22
|
-
payer.publicKey,
|
23
|
-
vaultTypeAccount,
|
24
|
-
deprecated,
|
25
|
-
)
|
36
|
+
await setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, deprecated)
|
26
37
|
)
|
27
38
|
|
28
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
39
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
29
40
|
return vaultSystemStatePublicKey
|
30
41
|
}
|
31
42
|
|
32
43
|
export async function setVaultTypeStatusInstruction(
|
33
|
-
program: Program
|
44
|
+
program: Program<Vault>,
|
34
45
|
vaultSystemStatePublicKey: PublicKey,
|
35
46
|
payerPublicKey: PublicKey,
|
36
47
|
vaultTypeAccount: PublicKey,
|
37
48
|
deprecated: boolean
|
38
49
|
): Promise<TransactionInstruction> {
|
39
|
-
|
40
|
-
deprecated
|
41
|
-
{
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
vaultType: vaultTypeAccount,
|
46
|
-
},
|
47
|
-
signers: []
|
50
|
+
return await program.methods
|
51
|
+
.setVaultTypeStatus(deprecated)
|
52
|
+
.accounts({
|
53
|
+
payer: payerPublicKey,
|
54
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
55
|
+
vaultType: vaultTypeAccount,
|
48
56
|
})
|
49
|
-
|
57
|
+
.instruction()
|
50
58
|
}
|
@@ -1,11 +1,27 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
3
|
+
import {
|
4
|
+
Keypair,
|
5
|
+
PublicKey,
|
6
|
+
sendAndConfirmTransaction,
|
7
|
+
Signer,
|
8
|
+
SystemProgram,
|
9
|
+
SYSVAR_RENT_PUBKEY,
|
10
|
+
Transaction,
|
11
|
+
TransactionInstruction,
|
12
|
+
} from '@solana/web3.js'
|
4
13
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
14
|
+
import {
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
getHedgeMintPublicKey,
|
17
|
+
getPoolPublicKeyForMint,
|
18
|
+
getUshMintPublicKey,
|
19
|
+
getVaultSystemStatePublicKey,
|
20
|
+
} from '../Constants'
|
21
|
+
import { Vault } from 'idl/vault'
|
6
22
|
|
7
|
-
export async function withdrawStakingPool
|
8
|
-
program: Program
|
23
|
+
export async function withdrawStakingPool(
|
24
|
+
program: Program<Vault>,
|
9
25
|
provider: Provider,
|
10
26
|
payer: Signer,
|
11
27
|
poolPositionPublicKey: PublicKey,
|
@@ -22,12 +38,12 @@ export async function withdrawStakingPool (
|
|
22
38
|
overrideStartTime
|
23
39
|
)
|
24
40
|
)
|
25
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
41
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
26
42
|
return poolPosition.publicKey
|
27
43
|
}
|
28
44
|
|
29
|
-
export async function withdrawStakingPoolInstruction
|
30
|
-
program: Program
|
45
|
+
export async function withdrawStakingPoolInstruction(
|
46
|
+
program: Program<Vault>,
|
31
47
|
payerPublicKey: PublicKey,
|
32
48
|
poolPositionPublicKey: PublicKey,
|
33
49
|
stakedTokenMintPublicKey: PublicKey,
|
@@ -44,28 +60,26 @@ export async function withdrawStakingPoolInstruction (
|
|
44
60
|
const payerAssociatedUshAccount = await findAssociatedTokenAddress(payerPublicKey, ushMintPublickey)
|
45
61
|
const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
|
46
62
|
|
47
|
-
return program.
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
systemProgram: SystemProgram.programId
|
68
|
-
},
|
69
|
-
signers: []
|
63
|
+
return await program.methods
|
64
|
+
.withdrawStakingPool(
|
65
|
+
new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
|
66
|
+
)
|
67
|
+
.accounts({
|
68
|
+
payer: payerPublicKey,
|
69
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
70
|
+
pool: poolPublickey,
|
71
|
+
poolPosition: poolPositionPublicKey,
|
72
|
+
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
73
|
+
poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
|
74
|
+
payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
|
75
|
+
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
76
|
+
payerAssociatedUshAccount: payerAssociatedUshAccount,
|
77
|
+
communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
|
78
|
+
hedgeMint: hedgeMintPublickey,
|
79
|
+
stakedTokenMint: stakedTokenMintPublicKey,
|
80
|
+
ushMint: ushMintPublickey,
|
81
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
82
|
+
systemProgram: SystemProgram.programId,
|
70
83
|
})
|
84
|
+
.instruction()
|
71
85
|
}
|
@@ -1,8 +1,5 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
getOrCreateAssociatedTokenAccount,
|
4
|
-
TOKEN_PROGRAM_ID,
|
5
|
-
} from '@solana/spl-token'
|
2
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
6
3
|
import { TokenInstructions } from '@project-serum/serum'
|
7
4
|
import {
|
8
5
|
Keypair,
|
@@ -22,9 +19,10 @@ import {
|
|
22
19
|
getHedgeMintPublicKey,
|
23
20
|
} from '../Constants'
|
24
21
|
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
22
|
+
import { Vault } from 'idl/vault'
|
25
23
|
|
26
24
|
export async function withdrawVault(
|
27
|
-
program: Program
|
25
|
+
program: Program<Vault>,
|
28
26
|
provider: Provider,
|
29
27
|
payer: Signer,
|
30
28
|
vaultPublicKey: PublicKey,
|
@@ -34,39 +32,28 @@ export async function withdrawVault(
|
|
34
32
|
const ushMintPublickey = await getUshMintPublicKey()
|
35
33
|
|
36
34
|
// Prep the user to get USH back out at some point
|
37
|
-
await getOrCreateAssociatedTokenAccount(
|
38
|
-
provider.connection,
|
39
|
-
payer,
|
40
|
-
ushMintPublickey,
|
41
|
-
payer.publicKey
|
42
|
-
)
|
35
|
+
await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
|
43
36
|
|
44
37
|
const history = Keypair.generate()
|
45
38
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
46
39
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
47
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
48
|
-
|
40
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
41
|
+
const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(
|
42
|
+
provider.connection,
|
43
|
+
payer,
|
44
|
+
TokenInstructions.WRAPPED_SOL_MINT,
|
45
|
+
vaultPublicKey,
|
46
|
+
true
|
49
47
|
)
|
50
|
-
const vaultAssociatedCollateralAccount =
|
51
|
-
await getOrCreateAssociatedTokenAccount(
|
52
|
-
provider.connection,
|
53
|
-
payer,
|
54
|
-
TokenInstructions.WRAPPED_SOL_MINT,
|
55
|
-
vaultPublicKey,
|
56
|
-
true
|
57
|
-
)
|
58
48
|
|
59
|
-
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
60
|
-
|
49
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
50
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
51
|
+
provider.connection,
|
52
|
+
payer,
|
53
|
+
vaultTypeAccountInfo.collateralMint,
|
54
|
+
vaultTypeAccountPublicKey,
|
55
|
+
true
|
61
56
|
)
|
62
|
-
const vaultTypeAssociatedTokenAccount =
|
63
|
-
await getOrCreateAssociatedTokenAccount(
|
64
|
-
provider.connection,
|
65
|
-
payer,
|
66
|
-
vaultTypeAccountInfo.collateralMint,
|
67
|
-
vaultTypeAccountPublicKey,
|
68
|
-
true
|
69
|
-
)
|
70
57
|
|
71
58
|
const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
|
72
59
|
provider.connection,
|
@@ -74,26 +61,22 @@ export async function withdrawVault(
|
|
74
61
|
vaultTypeAccountInfo.collateralMint,
|
75
62
|
payer.publicKey
|
76
63
|
)
|
77
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
78
|
-
|
64
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
65
|
+
const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
|
66
|
+
hedgeStakingPoolPublicKey,
|
67
|
+
ushMintPublickey
|
79
68
|
)
|
80
|
-
const hedgeStakingPoolAssociatedUshTokenAccount =
|
81
|
-
await findAssociatedTokenAddress(
|
82
|
-
hedgeStakingPoolPublicKey,
|
83
|
-
ushMintPublickey
|
84
|
-
)
|
85
69
|
|
86
|
-
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
)
|
70
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
71
|
+
program,
|
72
|
+
provider,
|
73
|
+
vaultTypeAccountPublicKey,
|
74
|
+
vaultPublicKey,
|
75
|
+
withdrawAmount * -1,
|
76
|
+
0,
|
77
|
+
false,
|
78
|
+
false
|
79
|
+
)
|
97
80
|
|
98
81
|
const transaction = new Transaction().add(
|
99
82
|
await withdrawVaultInstruction(
|
@@ -116,17 +99,12 @@ export async function withdrawVault(
|
|
116
99
|
overrideTime
|
117
100
|
)
|
118
101
|
)
|
119
|
-
await sendAndConfirmTransaction(
|
120
|
-
provider.connection,
|
121
|
-
transaction,
|
122
|
-
[payer, history],
|
123
|
-
provider.opts
|
124
|
-
)
|
102
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
|
125
103
|
return vaultPublicKey
|
126
104
|
}
|
127
105
|
|
128
106
|
export async function withdrawVaultInstruction(
|
129
|
-
program: Program
|
107
|
+
program: Program<Vault>,
|
130
108
|
vaultSystemStatePublicKey: PublicKey,
|
131
109
|
payerPublicKey: PublicKey,
|
132
110
|
destinationTokenAccount: PublicKey,
|
@@ -144,30 +122,28 @@ export async function withdrawVaultInstruction(
|
|
144
122
|
withdrawAmount: number,
|
145
123
|
overrideTime?: number
|
146
124
|
): Promise<TransactionInstruction> {
|
147
|
-
return program.
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
}
|
172
|
-
)
|
125
|
+
return await program.methods
|
126
|
+
.withdrawVault(
|
127
|
+
new BN(withdrawAmount),
|
128
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
|
129
|
+
)
|
130
|
+
.accounts({
|
131
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
132
|
+
vaultTypeAccount: vaultTypeAccount,
|
133
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
134
|
+
vault: vaultPublickey,
|
135
|
+
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
136
|
+
feePool: hedgeStakingPoolPublicKey,
|
137
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
138
|
+
ushMint: ushMint,
|
139
|
+
history: historyPublicKey,
|
140
|
+
vaultOwner: payerPublicKey,
|
141
|
+
destinationTokenAccount: destinationTokenAccount,
|
142
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
143
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
144
|
+
newLargerVaultInfo: newLargerPublicKey,
|
145
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
146
|
+
systemProgram: SystemProgram.programId,
|
147
|
+
})
|
148
|
+
.instruction()
|
173
149
|
}
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
import Decimal from 'decimal.js'
|
3
2
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
3
|
|
@@ -13,10 +12,12 @@ export class LiquidationPoolEra {
|
|
13
12
|
public hedgeRewardsAccumulator: Decimal
|
14
13
|
public hedgeRewardsTimestamp: number
|
15
14
|
|
16
|
-
constructor
|
15
|
+
constructor(public liquidyPoolEra: any) {
|
17
16
|
this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
|
18
17
|
this.product = DecimalFromU128(liquidyPoolEra.productBytes)
|
19
|
-
this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => {
|
18
|
+
this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => {
|
19
|
+
return DecimalFromU128(sumBytes)
|
20
|
+
})
|
20
21
|
this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
|
21
22
|
this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
|
22
23
|
}
|
@@ -47,31 +47,4 @@ export class LiquidationPosition {
|
|
47
47
|
public getTokensAvailable (era: LiquidationPoolEra, index: number): Decimal {
|
48
48
|
return era.sum[index].minus(this.sumSnapshotsEntry[index]).div(this.productSnapshotEntry).mul(new Decimal(this.deposit)).floor()
|
49
49
|
}
|
50
|
-
|
51
|
-
// public getHedgeAvailable (): Decimal {
|
52
|
-
// const LiquidationPoolTotalSupply = 2000000.0 * LAMPORTS_PER_SOL
|
53
|
-
// const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(
|
54
|
-
// LiquidationPoolTotalSupply,
|
55
|
-
// this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000,
|
56
|
-
// this.era.hedgeRewardsTimestamp * 1000,
|
57
|
-
// Date.now(),
|
58
|
-
// 365 * 1000)
|
59
|
-
|
60
|
-
// if (this.era.totalDeposits === 0) {
|
61
|
-
// return new Decimal(0)
|
62
|
-
// }
|
63
|
-
|
64
|
-
// const rewardsPerToken = this.era.product.mul(new Decimal(hedgeRewardsSinceLastUpdate / this.era.totalDeposits))
|
65
|
-
// const newAccumulator = rewardsPerToken.add(new Decimal(this.era.hedgeRewardsAccumulator))
|
66
|
-
// const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal(this.deposit)).div(new Decimal(this.productSnapshot))
|
67
|
-
// return hedgeAvailable
|
68
|
-
// }
|
69
50
|
}
|
70
|
-
|
71
|
-
// function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
|
72
|
-
// const timeInOffsetStart = timeIn - birthTime
|
73
|
-
// const timeOutOffsetStart = timeOut - birthTime
|
74
|
-
// const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
|
75
|
-
// const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
|
76
|
-
// return awardedTokens
|
77
|
-
// }
|
package/src/state/StakingPool.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
import { PublicKey } from '@solana/web3.js'
|
3
2
|
import Decimal from 'decimal.js'
|
4
3
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
@@ -15,11 +14,9 @@ export class StakingPool {
|
|
15
14
|
ushFeeAccumulator: Decimal
|
16
15
|
collateralFeeAccumulator: [Decimal]
|
17
16
|
|
18
|
-
constructor
|
17
|
+
constructor(public poolInfo: any, publicKey: PublicKey) {
|
19
18
|
this.publicKey = publicKey
|
20
19
|
this.deposits = poolInfo.deposits.toNumber()
|
21
|
-
// this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
|
22
|
-
// this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
|
23
20
|
this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber()
|
24
21
|
this.startTime = poolInfo.startTime.toNumber()
|
25
22
|
this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber()
|
@@ -27,8 +24,8 @@ export class StakingPool {
|
|
27
24
|
|
28
25
|
this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
|
29
26
|
this.ushFeeAccumulator = DecimalFromU128(poolInfo.ushFeeAccumulator)
|
30
|
-
this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum: any) => {
|
31
|
-
|
27
|
+
this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum: any) => {
|
28
|
+
return DecimalFromU128(sum)
|
29
|
+
})
|
32
30
|
}
|
33
|
-
|
34
31
|
}
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
import { PublicKey } from '@solana/web3.js'
|
3
2
|
import Decimal from 'decimal.js'
|
4
3
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
@@ -17,7 +16,7 @@ export class StakingPoolPosition {
|
|
17
16
|
public startSolFeeAccumulator: Decimal
|
18
17
|
public open: boolean
|
19
18
|
|
20
|
-
constructor
|
19
|
+
constructor(public poolPositionInfo: any, key: PublicKey, stakingPool: StakingPool) {
|
21
20
|
this.publicKey = key
|
22
21
|
this.pool = stakingPool
|
23
22
|
this.owner = poolPositionInfo.owner
|
@@ -33,7 +32,7 @@ export class StakingPoolPosition {
|
|
33
32
|
this.open = poolPositionInfo.state.open !== undefined
|
34
33
|
}
|
35
34
|
|
36
|
-
public getCurrentUshFeeReward
|
35
|
+
public getCurrentUshFeeReward(): Decimal {
|
37
36
|
return this.pool.ushFeeAccumulator.minus(this.startUshFeeAccumulator).times(new Decimal(this.deposited))
|
38
37
|
}
|
39
38
|
}
|
@@ -40,12 +40,8 @@ export class VaultAccount {
|
|
40
40
|
this.pdaSalt = vault.pdaSalt
|
41
41
|
this.deposited = vault.deposited.toNumber()
|
42
42
|
this.denormalizedDebt = vault.denormalizedDebt.toNumber()
|
43
|
-
this.debtProductSnapshotBytes = DecimalFromU128(
|
44
|
-
|
45
|
-
)
|
46
|
-
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(
|
47
|
-
vault.collateralAccumulatorSnapshotBytes.toString()
|
48
|
-
)
|
43
|
+
this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
|
44
|
+
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
|
49
45
|
this.collateralType = vault.collateralType
|
50
46
|
|
51
47
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0]
|
@@ -90,13 +86,8 @@ export class VaultAccount {
|
|
90
86
|
.substring(this.publicKey.toString().length - 6)}`
|
91
87
|
}
|
92
88
|
|
93
|
-
public addDebt(
|
94
|
-
newNormalizedDebt
|
95
|
-
vaultTypeCompoundedInterest: Decimal
|
96
|
-
) {
|
97
|
-
const denormalizedNewDebt = newNormalizedDebt.div(
|
98
|
-
vaultTypeCompoundedInterest
|
99
|
-
)
|
89
|
+
public addDebt(newNormalizedDebt: Decimal, vaultTypeCompoundedInterest: Decimal) {
|
90
|
+
const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest)
|
100
91
|
this.denormalizedDebt += denormalizedNewDebt.toNumber()
|
101
92
|
}
|
102
93
|
public addDeposit(depositAmount: number) {
|
@@ -114,14 +105,10 @@ export class VaultAccount {
|
|
114
105
|
this.vaultStatus = 'liquidated'
|
115
106
|
}
|
116
107
|
|
117
|
-
public
|
118
|
-
const debtProductCurrent = DecimalFromU128(
|
119
|
-
vaultTypeAccuntData.debtRedistributionProduct
|
120
|
-
)
|
108
|
+
public updateDebtAndCollateral(vaultTypeAccuntData: any) {
|
109
|
+
const debtProductCurrent = DecimalFromU128(vaultTypeAccuntData.debtRedistributionProduct)
|
121
110
|
|
122
|
-
const collateralAccumulatorCurrent = DecimalFromU128(
|
123
|
-
vaultTypeAccuntData.collateralRedistributionAccumulator
|
124
|
-
)
|
111
|
+
const collateralAccumulatorCurrent = DecimalFromU128(vaultTypeAccuntData.collateralRedistributionAccumulator)
|
125
112
|
|
126
113
|
this.denormalizedDebt = debtProductCurrent
|
127
114
|
.div(this.debtProductSnapshotBytes)
|
@@ -130,10 +117,7 @@ export class VaultAccount {
|
|
130
117
|
.toNumber()
|
131
118
|
|
132
119
|
const extraCollateralDeposited =
|
133
|
-
this.denormalizedDebt *
|
134
|
-
collateralAccumulatorCurrent
|
135
|
-
.sub(this.collateralAccumulatorSnapshotBytes)
|
136
|
-
.toNumber()
|
120
|
+
this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
|
137
121
|
this.deposited += extraCollateralDeposited
|
138
122
|
|
139
123
|
this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent
|
@@ -151,9 +135,6 @@ export class VaultAccount {
|
|
151
135
|
}
|
152
136
|
return `Vault(${this.vaultNumber}): ${this.publicKey
|
153
137
|
.toString()
|
154
|
-
.substring(
|
155
|
-
0,
|
156
|
-
6
|
157
|
-
)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
|
138
|
+
.substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
|
158
139
|
}
|
159
140
|
}
|
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
import { PublicKey } from '@solana/web3.js'
|
3
2
|
import Decimal from 'decimal.js'
|
4
3
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
@@ -18,7 +17,7 @@ export class VaultHistoryEvent {
|
|
18
17
|
timestamp: PublicKey
|
19
18
|
action: VaultHistoryAction
|
20
19
|
|
21
|
-
constructor
|
20
|
+
constructor(public account: any, publicKey: PublicKey) {
|
22
21
|
this.publicKey = publicKey
|
23
22
|
this.actorAccount = account.actorAccount
|
24
23
|
|