hedge-web3 0.1.29 → 0.1.31
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/idl/vault.d.ts +99 -99
- 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 +99 -99
- 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 +2 -2
- package/src/idl/vault.ts +198 -198
- 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,22 +1,43 @@
|
|
1
1
|
import { Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
3
|
+
import {
|
4
|
+
PublicKey,
|
5
|
+
sendAndConfirmTransaction,
|
6
|
+
Signer,
|
7
|
+
SystemProgram,
|
8
|
+
SYSVAR_RENT_PUBKEY,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
4
12
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
13
|
+
import {
|
14
|
+
getHedgeMintPublicKey,
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
getVaultTypeAccountPublicKey,
|
17
|
+
getPoolPublicKeyForMint,
|
18
|
+
getVaultSystemStatePublicKey,
|
19
|
+
} from '../Constants'
|
20
|
+
import { Vault } from 'idl/vault'
|
6
21
|
|
7
22
|
export async function claimStakingPoolPosition(
|
8
|
-
program: Program
|
23
|
+
program: Program<Vault>,
|
9
24
|
provider: Provider,
|
10
25
|
poolPosition: PublicKey,
|
11
26
|
payer: Signer,
|
12
|
-
collateralType: string
|
27
|
+
collateralType: string
|
13
28
|
): Promise<PublicKey> {
|
14
29
|
const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
|
15
30
|
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccount)
|
16
31
|
const collateralMint = vaultTypeAccountInfo.collateralMint
|
17
32
|
const stakedTokenMint = await getHedgeMintPublicKey()
|
18
33
|
const [feePool] = await getPoolPublicKeyForMint(stakedTokenMint)
|
19
|
-
const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
34
|
+
const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
35
|
+
provider.connection,
|
36
|
+
payer,
|
37
|
+
collateralMint,
|
38
|
+
feePool,
|
39
|
+
true
|
40
|
+
)
|
20
41
|
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
21
42
|
provider.connection,
|
22
43
|
payer,
|
@@ -37,12 +58,12 @@ export async function claimStakingPoolPosition(
|
|
37
58
|
payerAssociatedTokenAccount.address
|
38
59
|
)
|
39
60
|
)
|
40
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
61
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
41
62
|
return payerAssociatedTokenAccount.address
|
42
63
|
}
|
43
64
|
|
44
65
|
export async function claimStakingPoolPositionInstruction(
|
45
|
-
program: Program
|
66
|
+
program: Program<Vault>,
|
46
67
|
feePool: PublicKey,
|
47
68
|
feePoolAssociatedTokenAccount: PublicKey,
|
48
69
|
stakedTokenMint: PublicKey,
|
@@ -54,23 +75,22 @@ export async function claimStakingPoolPositionInstruction(
|
|
54
75
|
): Promise<TransactionInstruction> {
|
55
76
|
const vaultSystemState = await getVaultSystemStatePublicKey()
|
56
77
|
|
57
|
-
return program.
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
},
|
74
|
-
signers: []
|
78
|
+
return await program.methods
|
79
|
+
.claimStakingPoolPosition()
|
80
|
+
.accounts({
|
81
|
+
vaultSystemState: vaultSystemState,
|
82
|
+
feePool: feePool,
|
83
|
+
stakedTokenMint: stakedTokenMint,
|
84
|
+
feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
|
85
|
+
vaultTypeAccount: vaultTypeAccount,
|
86
|
+
collateralMint: collateralMint,
|
87
|
+
poolPosition: poolPosition,
|
88
|
+
payer: payer,
|
89
|
+
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
90
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
91
|
+
systemProgram: SystemProgram.programId,
|
92
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
93
|
+
rent: SYSVAR_RENT_PUBKEY,
|
75
94
|
})
|
95
|
+
.instruction()
|
76
96
|
}
|
@@ -1,25 +1,56 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
3
|
+
import {
|
4
|
+
PublicKey,
|
5
|
+
sendAndConfirmTransaction,
|
6
|
+
Signer,
|
7
|
+
SystemProgram,
|
8
|
+
SYSVAR_RENT_PUBKEY,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
4
12
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
13
|
+
import {
|
14
|
+
getHedgeMintPublicKey,
|
15
|
+
getLiquidationPoolStatePublicKey,
|
16
|
+
getLiquidationPoolUshAccountPublicKey,
|
17
|
+
getUshMintPublicKey,
|
18
|
+
getVaultSystemStatePublicKey,
|
19
|
+
} from '../Constants'
|
20
|
+
import { Vault } from 'idl/vault'
|
6
21
|
|
7
|
-
export async function closeLiquidationPoolPosition
|
8
|
-
program: Program
|
22
|
+
export async function closeLiquidationPoolPosition(
|
23
|
+
program: Program<Vault>,
|
9
24
|
provider: Provider,
|
10
25
|
poolPosition: PublicKey,
|
11
26
|
payer: Signer,
|
12
27
|
overrideStartTime?: number
|
13
28
|
): Promise<PublicKey> {
|
14
29
|
const ushMintPublickey = await getUshMintPublicKey()
|
15
|
-
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
30
|
+
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
31
|
+
provider.connection,
|
32
|
+
payer,
|
33
|
+
ushMintPublickey,
|
34
|
+
payer.publicKey
|
35
|
+
)
|
16
36
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
17
37
|
const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
|
18
38
|
const poolEra = liquidationPositionAccount.era
|
19
39
|
|
20
40
|
const hedgeMint = await getHedgeMintPublicKey()
|
21
|
-
const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(
|
22
|
-
|
41
|
+
const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(
|
42
|
+
provider.connection,
|
43
|
+
payer,
|
44
|
+
hedgeMint,
|
45
|
+
payer.publicKey
|
46
|
+
)
|
47
|
+
const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(
|
48
|
+
provider.connection,
|
49
|
+
payer,
|
50
|
+
hedgeMint,
|
51
|
+
vaultSystemStatePublicKey,
|
52
|
+
true
|
53
|
+
)
|
23
54
|
|
24
55
|
const transaction = new Transaction().add(
|
25
56
|
await closeLiquidationPoolPositionInstruction(
|
@@ -33,12 +64,12 @@ export async function closeLiquidationPoolPosition (
|
|
33
64
|
overrideStartTime
|
34
65
|
)
|
35
66
|
)
|
36
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
67
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
37
68
|
return poolPosition
|
38
69
|
}
|
39
70
|
|
40
|
-
export async function closeLiquidationPoolPositionInstruction
|
41
|
-
program: Program
|
71
|
+
export async function closeLiquidationPoolPositionInstruction(
|
72
|
+
program: Program<Vault>,
|
42
73
|
poolEra: PublicKey,
|
43
74
|
poolPosition: PublicKey,
|
44
75
|
payerPublicKey: PublicKey,
|
@@ -47,33 +78,32 @@ export async function closeLiquidationPoolPositionInstruction (
|
|
47
78
|
communityAssociatedHedgeTokenAccount: PublicKey,
|
48
79
|
overrideStartTime?: number
|
49
80
|
): Promise<TransactionInstruction> {
|
50
|
-
|
51
81
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
52
82
|
const ushMint = await getUshMintPublicKey()
|
53
83
|
const hedgeMint = await getHedgeMintPublicKey()
|
54
84
|
const poolUshAccount = await getLiquidationPoolUshAccountPublicKey()
|
55
85
|
const poolState = await getLiquidationPoolStatePublicKey()
|
56
86
|
|
57
|
-
return program.
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
signers: []
|
87
|
+
return await program.methods
|
88
|
+
.closeLiquidationPoolPosition(
|
89
|
+
new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
|
90
|
+
)
|
91
|
+
.accounts({
|
92
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
93
|
+
poolState: poolState,
|
94
|
+
poolEra: poolEra,
|
95
|
+
poolPosition: poolPosition,
|
96
|
+
poolUshAccount: poolUshAccount,
|
97
|
+
payer: payerPublicKey,
|
98
|
+
ownerUshAccount: payerUshAccount,
|
99
|
+
hedgeMint: hedgeMint,
|
100
|
+
ushMint: ushMint,
|
101
|
+
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
102
|
+
communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
|
103
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
104
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
105
|
+
systemProgram: SystemProgram.programId,
|
106
|
+
rent: SYSVAR_RENT_PUBKEY,
|
78
107
|
})
|
108
|
+
.instruction()
|
79
109
|
}
|
@@ -1,11 +1,25 @@
|
|
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
|
+
PublicKey,
|
5
|
+
sendAndConfirmTransaction,
|
6
|
+
Signer,
|
7
|
+
SystemProgram,
|
8
|
+
SYSVAR_RENT_PUBKEY,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
4
12
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
13
|
+
import {
|
14
|
+
findAssociatedTokenAddress,
|
15
|
+
getPoolPublicKeyForMint,
|
16
|
+
getUshMintPublicKey,
|
17
|
+
getVaultSystemStatePublicKey,
|
18
|
+
} from '../Constants'
|
19
|
+
import { Vault } from 'idl/vault'
|
6
20
|
|
7
|
-
export async function createStakingPool
|
8
|
-
program: Program
|
21
|
+
export async function createStakingPool(
|
22
|
+
program: Program<Vault>,
|
9
23
|
provider: Provider,
|
10
24
|
payer: Signer,
|
11
25
|
mintPublicKey: PublicKey,
|
@@ -13,52 +27,40 @@ export async function createStakingPool (
|
|
13
27
|
overrideStartTime?: number
|
14
28
|
): Promise<PublicKey> {
|
15
29
|
const transaction = new Transaction().add(
|
16
|
-
await createStakingPoolInstruction(
|
17
|
-
program,
|
18
|
-
payer.publicKey,
|
19
|
-
mintPublicKey,
|
20
|
-
hedgeTokensToBeMinted,
|
21
|
-
overrideStartTime
|
22
|
-
)
|
30
|
+
await createStakingPoolInstruction(program, payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime)
|
23
31
|
)
|
24
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
32
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
25
33
|
const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey)
|
26
34
|
return poolPublickey
|
27
35
|
}
|
28
36
|
|
29
|
-
export async function createStakingPoolInstruction
|
30
|
-
program: Program
|
37
|
+
export async function createStakingPoolInstruction(
|
38
|
+
program: Program<Vault>,
|
31
39
|
payerPublicKey: PublicKey,
|
32
40
|
mintPublicKey: PublicKey,
|
33
41
|
hedgeTokensToBeMinted: number,
|
34
42
|
overrideStartTime?: number
|
35
43
|
): Promise<TransactionInstruction> {
|
36
|
-
console.log("createStakingPoolInstruction program ID", program.programId.toString())
|
37
44
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
38
45
|
const ushMintPublickey = await getUshMintPublicKey()
|
39
46
|
const [poolPublickey, poolBump] = await getPoolPublicKeyForMint(mintPublicKey)
|
40
47
|
|
41
48
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
|
42
49
|
const poolAssociatedUshTokenAccount = await findAssociatedTokenAddress(poolPublickey, ushMintPublickey)
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
rent: SYSVAR_RENT_PUBKEY,
|
58
|
-
tokenProgram: TOKEN_PROGRAM_ID,
|
59
|
-
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
60
|
-
systemProgram: SystemProgram.programId
|
61
|
-
},
|
62
|
-
signers: []
|
50
|
+
return await program.methods
|
51
|
+
.createStakingPool(poolBump, new BN(hedgeTokensToBeMinted), new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)))
|
52
|
+
.accounts({
|
53
|
+
signer: payerPublicKey,
|
54
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
55
|
+
pool: poolPublickey,
|
56
|
+
stakedTokenMintInfo: mintPublicKey,
|
57
|
+
ushMint: ushMintPublickey,
|
58
|
+
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
59
|
+
poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
|
60
|
+
rent: SYSVAR_RENT_PUBKEY,
|
61
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
62
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
63
|
+
systemProgram: SystemProgram.programId,
|
63
64
|
})
|
65
|
+
.instruction()
|
64
66
|
}
|
@@ -1,10 +1,6 @@
|
|
1
|
-
import { BN, Idl, Program, Provider } from
|
2
|
-
import { TokenInstructions } from
|
3
|
-
import {
|
4
|
-
ASSOCIATED_TOKEN_PROGRAM_ID,
|
5
|
-
TOKEN_PROGRAM_ID,
|
6
|
-
getOrCreateAssociatedTokenAccount,
|
7
|
-
} from "@solana/spl-token";
|
1
|
+
import { BN, Idl, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { TokenInstructions } from '@project-serum/serum'
|
3
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
8
4
|
import {
|
9
5
|
Keypair,
|
10
6
|
PublicKey,
|
@@ -14,7 +10,7 @@ import {
|
|
14
10
|
SYSVAR_RENT_PUBKEY,
|
15
11
|
Transaction,
|
16
12
|
TransactionInstruction,
|
17
|
-
} from
|
13
|
+
} from '@solana/web3.js'
|
18
14
|
import {
|
19
15
|
findAssociatedTokenAddress,
|
20
16
|
findVaultAddress,
|
@@ -23,65 +19,47 @@ import {
|
|
23
19
|
getVaultSystemStatePublicKey,
|
24
20
|
getPoolPublicKeyForMint,
|
25
21
|
getHedgeMintPublicKey,
|
26
|
-
} from
|
22
|
+
} from '../Constants'
|
27
23
|
|
28
|
-
import { v4 as uuidv4 } from
|
29
|
-
import { parseAnchorErrors } from
|
24
|
+
import { v4 as uuidv4 } from 'uuid'
|
25
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
26
|
+
import { Vault } from 'idl/vault'
|
30
27
|
|
31
28
|
export async function createVault(
|
32
|
-
program: Program
|
29
|
+
program: Program<Vault>,
|
33
30
|
provider: Provider,
|
34
31
|
payer: Signer,
|
35
32
|
collateralType: string,
|
36
33
|
depositAmount: number,
|
37
34
|
overrideTime?: number
|
38
35
|
): Promise<PublicKey> {
|
39
|
-
const ushMintPublickey = await getUshMintPublicKey()
|
36
|
+
const ushMintPublickey = await getUshMintPublicKey()
|
40
37
|
|
41
|
-
const salt = uuidv4().substring(0, 8)
|
42
|
-
const newVaultPublicKey = await findVaultAddress(salt)
|
43
|
-
const history = Keypair.generate()
|
38
|
+
const salt = uuidv4().substring(0, 8)
|
39
|
+
const newVaultPublicKey = await findVaultAddress(salt)
|
40
|
+
const history = Keypair.generate()
|
44
41
|
|
45
42
|
// Prep the user to get USH back out at some point
|
46
|
-
await getOrCreateAssociatedTokenAccount(
|
47
|
-
provider.connection,
|
48
|
-
payer,
|
49
|
-
ushMintPublickey,
|
50
|
-
payer.publicKey
|
51
|
-
);
|
43
|
+
await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
|
52
44
|
|
53
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
54
|
-
|
55
|
-
);
|
56
|
-
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
57
|
-
vaultTypeAccountPublicKey
|
58
|
-
);
|
45
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
|
46
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
59
47
|
|
60
|
-
const payerTokenAccount = await findAssociatedTokenAddress(
|
61
|
-
payer.publicKey,
|
62
|
-
vaultTypeAccountInfo.collateralMint
|
63
|
-
);
|
48
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
|
64
49
|
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
65
50
|
newVaultPublicKey,
|
66
51
|
vaultTypeAccountInfo.collateralMint
|
67
|
-
)
|
52
|
+
)
|
68
53
|
|
69
|
-
const wrappedSolAccount = Keypair.generate()
|
54
|
+
const wrappedSolAccount = Keypair.generate()
|
70
55
|
|
71
|
-
const transaction = new Transaction()
|
72
|
-
const signers = [payer, history]
|
56
|
+
const transaction = new Transaction()
|
57
|
+
const signers = [payer, history]
|
73
58
|
|
74
|
-
const isWrappedSol =
|
75
|
-
vaultTypeAccountInfo.collateralMint.toString() ===
|
76
|
-
TokenInstructions.WRAPPED_SOL_MINT.toString();
|
59
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() === TokenInstructions.WRAPPED_SOL_MINT.toString()
|
77
60
|
|
78
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
79
|
-
|
80
|
-
);
|
81
|
-
const feePoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
|
82
|
-
hedgeStakingPoolPublicKey,
|
83
|
-
ushMintPublickey
|
84
|
-
);
|
61
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
62
|
+
const feePoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, ushMintPublickey)
|
85
63
|
|
86
64
|
if (isWrappedSol) {
|
87
65
|
transaction.add(
|
@@ -97,8 +75,8 @@ export async function createVault(
|
|
97
75
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
98
76
|
owner: payer.publicKey,
|
99
77
|
})
|
100
|
-
)
|
101
|
-
signers.push(wrappedSolAccount)
|
78
|
+
)
|
79
|
+
signers.push(wrappedSolAccount)
|
102
80
|
}
|
103
81
|
transaction.add(
|
104
82
|
await createVaultInstruction(
|
@@ -117,7 +95,7 @@ export async function createVault(
|
|
117
95
|
depositAmount,
|
118
96
|
overrideTime
|
119
97
|
)
|
120
|
-
)
|
98
|
+
)
|
121
99
|
if (isWrappedSol) {
|
122
100
|
transaction.add(
|
123
101
|
TokenInstructions.closeAccount({
|
@@ -125,74 +103,53 @@ export async function createVault(
|
|
125
103
|
destination: payer.publicKey,
|
126
104
|
owner: payer.publicKey,
|
127
105
|
})
|
128
|
-
)
|
106
|
+
)
|
129
107
|
}
|
130
108
|
|
131
|
-
await sendAndConfirmTransaction(
|
132
|
-
|
133
|
-
transaction,
|
134
|
-
signers,
|
135
|
-
provider?.opts
|
136
|
-
).catch(parseAnchorErrors);
|
137
|
-
return newVaultPublicKey;
|
109
|
+
await sendAndConfirmTransaction(provider.connection, transaction, signers).catch(parseAnchorErrors)
|
110
|
+
return newVaultPublicKey
|
138
111
|
}
|
139
112
|
|
140
113
|
export async function buildCreateVaultTransaction(
|
141
|
-
program: Program
|
114
|
+
program: Program<Vault>,
|
115
|
+
payerPublicKey: PublicKey,
|
142
116
|
collateralType: string,
|
143
117
|
depositAmount: number,
|
144
118
|
overrideTime?: number
|
145
119
|
): Promise<[Transaction, Signer[], PublicKey]> {
|
146
|
-
|
147
|
-
|
148
|
-
const
|
149
|
-
const
|
150
|
-
|
151
|
-
const salt = uuidv4().substring(0, 8);
|
152
|
-
const newVaultPublicKey = await findVaultAddress(salt);
|
153
|
-
const history = Keypair.generate() as Signer;
|
120
|
+
console.log('HEDGE SDK: buildCreateVaultTransaction')
|
121
|
+
const ushMintPublickey = await getUshMintPublicKey()
|
122
|
+
const salt = uuidv4().substring(0, 8)
|
123
|
+
const newVaultPublicKey = await findVaultAddress(salt)
|
124
|
+
const history = Keypair.generate() as Signer
|
154
125
|
const { blockhash } = await program.provider.connection.getLatestBlockhash()
|
155
126
|
const transaction = new Transaction({
|
156
127
|
feePayer: payerPublicKey,
|
157
128
|
recentBlockhash: blockhash,
|
158
129
|
})
|
159
|
-
const signers = [history]
|
160
|
-
const wrappedSolAccount = Keypair.generate()
|
130
|
+
const signers = [history]
|
131
|
+
const wrappedSolAccount = Keypair.generate()
|
161
132
|
|
162
133
|
console.log('Lookup getVaultTypeAccountPublicKey', collateralType)
|
163
134
|
|
164
135
|
// Lookup the vault type info
|
165
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
166
|
-
collateralType
|
167
|
-
);
|
136
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
|
168
137
|
|
169
138
|
console.log('Lookup vaultTypeAccountPublicKey', vaultTypeAccountPublicKey.toString())
|
170
139
|
|
171
|
-
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
172
|
-
vaultTypeAccountPublicKey
|
173
|
-
);
|
140
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
174
141
|
console.log('Lookup vaultTypeAccountInfo', vaultTypeAccountInfo)
|
175
142
|
|
176
|
-
const isWrappedSol =
|
177
|
-
vaultTypeAccountInfo.collateralMint.toString() ===
|
178
|
-
TokenInstructions.WRAPPED_SOL_MINT.toString();
|
143
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() === TokenInstructions.WRAPPED_SOL_MINT.toString()
|
179
144
|
|
180
|
-
const payerTokenAccount = await findAssociatedTokenAddress(
|
181
|
-
payerPublicKey,
|
182
|
-
vaultTypeAccountInfo.collateralMint
|
183
|
-
);
|
145
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payerPublicKey, vaultTypeAccountInfo.collateralMint)
|
184
146
|
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
185
147
|
newVaultPublicKey,
|
186
148
|
vaultTypeAccountInfo.collateralMint
|
187
|
-
)
|
149
|
+
)
|
188
150
|
|
189
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
190
|
-
|
191
|
-
);
|
192
|
-
const feePoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
|
193
|
-
hedgeStakingPoolPublicKey,
|
194
|
-
ushMintPublickey
|
195
|
-
);
|
151
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
152
|
+
const feePoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, ushMintPublickey)
|
196
153
|
|
197
154
|
console.log('about to start building transaction')
|
198
155
|
|
@@ -211,10 +168,10 @@ export async function buildCreateVaultTransaction(
|
|
211
168
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
212
169
|
owner: payerPublicKey,
|
213
170
|
})
|
214
|
-
)
|
215
|
-
signers.push(wrappedSolAccount)
|
171
|
+
)
|
172
|
+
signers.push(wrappedSolAccount)
|
216
173
|
}
|
217
|
-
console.log(
|
174
|
+
console.log('hedgeStakingPoolPublicKey', hedgeStakingPoolPublicKey.toString())
|
218
175
|
transaction.add(
|
219
176
|
await createVaultInstruction(
|
220
177
|
program,
|
@@ -232,7 +189,7 @@ export async function buildCreateVaultTransaction(
|
|
232
189
|
depositAmount,
|
233
190
|
overrideTime
|
234
191
|
)
|
235
|
-
)
|
192
|
+
)
|
236
193
|
if (isWrappedSol) {
|
237
194
|
transaction.add(
|
238
195
|
TokenInstructions.closeAccount({
|
@@ -240,18 +197,18 @@ export async function buildCreateVaultTransaction(
|
|
240
197
|
destination: payerPublicKey,
|
241
198
|
owner: payerPublicKey,
|
242
199
|
})
|
243
|
-
)
|
200
|
+
)
|
244
201
|
transaction.partialSign(wrappedSolAccount)
|
245
202
|
}
|
246
|
-
|
203
|
+
|
247
204
|
transaction.partialSign(history)
|
248
|
-
console.log(
|
205
|
+
console.log('transaction', transaction)
|
249
206
|
|
250
|
-
return [transaction, signers, newVaultPublicKey]
|
207
|
+
return [transaction, signers, newVaultPublicKey]
|
251
208
|
}
|
252
209
|
|
253
210
|
export async function createVaultInstruction(
|
254
|
-
program: Program
|
211
|
+
program: Program<Vault>,
|
255
212
|
salt: string,
|
256
213
|
payerPublicKey: PublicKey,
|
257
214
|
payerTokenAccountPublicKey: PublicKey,
|
@@ -266,32 +223,31 @@ export async function createVaultInstruction(
|
|
266
223
|
depositAmount: number,
|
267
224
|
overrideTime?: number
|
268
225
|
): Promise<TransactionInstruction> {
|
269
|
-
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
226
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
270
227
|
|
271
|
-
const ix = program.
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
return ix;
|
228
|
+
const ix = program.methods
|
229
|
+
.createVault(
|
230
|
+
salt,
|
231
|
+
new BN(depositAmount),
|
232
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
|
233
|
+
)
|
234
|
+
.accounts({
|
235
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
236
|
+
vaultTypeAccount: vaultTypeAccount,
|
237
|
+
vault: vaultPublicKey,
|
238
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
239
|
+
feePool: feePool,
|
240
|
+
feePoolAssociatedUshTokenAccount: feePoolAssociatedUshTokenAccount,
|
241
|
+
history: historyPublicKey,
|
242
|
+
payer: payerPublicKey,
|
243
|
+
payerTokenAccount: payerTokenAccountPublicKey,
|
244
|
+
collateralMint: collateralMint,
|
245
|
+
ushMint: ushMintPublickey,
|
246
|
+
systemProgram: SystemProgram.programId,
|
247
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
248
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
249
|
+
rent: SYSVAR_RENT_PUBKEY,
|
250
|
+
})
|
251
|
+
.instruction()
|
252
|
+
return ix
|
297
253
|
}
|