hedge-web3 0.1.29 → 0.1.31

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.
Files changed (77) hide show
  1. package/declarations/idl/vault.d.ts +99 -99
  2. package/declarations/index.d.ts +1 -0
  3. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
  4. package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
  5. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
  6. package/declarations/instructions/createStakingPool.d.ts +3 -2
  7. package/declarations/instructions/createVault.d.ts +6 -5
  8. package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
  9. package/declarations/instructions/depositStakingPool.d.ts +3 -2
  10. package/declarations/instructions/depositVault.d.ts +3 -2
  11. package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
  12. package/declarations/instructions/liquidateVault.d.ts +3 -2
  13. package/declarations/instructions/loanVault.d.ts +3 -2
  14. package/declarations/instructions/redeemVault.d.ts +3 -2
  15. package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
  16. package/declarations/instructions/repayVault.d.ts +3 -2
  17. package/declarations/instructions/setHalted.d.ts +3 -2
  18. package/declarations/instructions/setVaultTypeStatus.d.ts +3 -2
  19. package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
  20. package/declarations/instructions/withdrawVault.d.ts +3 -2
  21. package/declarations/state/VaultAccount.d.ts +1 -1
  22. package/declarations/utils/getLinkedListAccounts.d.ts +3 -1
  23. package/lib/idl/vault.js +99 -99
  24. package/lib/index.js +1 -0
  25. package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
  26. package/lib/instructions/claimStakingPoolPosition.js +19 -19
  27. package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
  28. package/lib/instructions/createStakingPool.js +17 -18
  29. package/lib/instructions/createVault.js +28 -31
  30. package/lib/instructions/depositLiquidationPool.js +17 -18
  31. package/lib/instructions/depositStakingPool.js +16 -18
  32. package/lib/instructions/depositVault.js +25 -27
  33. package/lib/instructions/initHedgeFoundation.js +17 -19
  34. package/lib/instructions/initHedgeFoundationTokens.js +15 -15
  35. package/lib/instructions/liquidateVault.js +32 -33
  36. package/lib/instructions/loanVault.js +23 -23
  37. package/lib/instructions/redeemVault.js +24 -24
  38. package/lib/instructions/refreshOraclePrice.js +17 -17
  39. package/lib/instructions/repayVault.js +23 -23
  40. package/lib/instructions/setHalted.js +8 -9
  41. package/lib/instructions/setVaultTypeStatus.js +9 -10
  42. package/lib/instructions/withdrawStakingPool.js +22 -24
  43. package/lib/instructions/withdrawVault.js +23 -23
  44. package/lib/state/LiquidationPoolEra.js +3 -1
  45. package/lib/state/LiquidationPosition.js +0 -7
  46. package/lib/state/StakingPool.js +3 -4
  47. package/lib/state/VaultAccount.js +2 -5
  48. package/lib/utils/getLinkedListAccounts.js +24 -16
  49. package/package.json +2 -2
  50. package/src/idl/vault.ts +198 -198
  51. package/src/index.ts +1 -0
  52. package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
  53. package/src/instructions/claimStakingPoolPosition.ts +45 -25
  54. package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
  55. package/src/instructions/createStakingPool.ts +37 -35
  56. package/src/instructions/createVault.ts +81 -125
  57. package/src/instructions/depositLiquidationPool.ts +45 -26
  58. package/src/instructions/depositStakingPool.ts +32 -24
  59. package/src/instructions/depositVault.ts +57 -86
  60. package/src/instructions/initHedgeFoundation.ts +42 -43
  61. package/src/instructions/initHedgeFoundationTokens.ts +38 -39
  62. package/src/instructions/liquidateVault.ts +42 -65
  63. package/src/instructions/loanVault.ts +51 -69
  64. package/src/instructions/redeemVault.ts +83 -47
  65. package/src/instructions/refreshOraclePrice.ts +41 -32
  66. package/src/instructions/repayVault.ts +45 -65
  67. package/src/instructions/setHalted.ts +32 -24
  68. package/src/instructions/setVaultTypeStatus.ts +32 -24
  69. package/src/instructions/withdrawStakingPool.ts +44 -30
  70. package/src/instructions/withdrawVault.ts +58 -82
  71. package/src/state/LiquidationPoolEra.ts +4 -3
  72. package/src/state/LiquidationPosition.ts +0 -27
  73. package/src/state/StakingPool.ts +4 -7
  74. package/src/state/StakingPoolPosition.ts +2 -3
  75. package/src/state/VaultAccount.ts +9 -28
  76. package/src/state/VaultHistoryEvent.ts +1 -2
  77. 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 { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
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 { getHedgeMintPublicKey, findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getPoolPublicKeyForMint, getVaultSystemStatePublicKey } from '../Constants'
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(provider.connection, payer, collateralMint, feePool, true)
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], provider.opts).catch(parseAnchorErrors)
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.instruction.claimStakingPoolPosition(
58
- {
59
- accounts: {
60
- vaultSystemState: vaultSystemState,
61
- feePool: feePool,
62
- stakedTokenMint: stakedTokenMint,
63
- feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
64
- vaultTypeAccount: vaultTypeAccount,
65
- collateralMint: collateralMint,
66
- poolPosition: poolPosition,
67
- payer: payer,
68
- payerAssociatedTokenAccount: payerAssociatedTokenAccount,
69
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
70
- systemProgram: SystemProgram.programId,
71
- tokenProgram: TOKEN_PROGRAM_ID,
72
- rent: SYSVAR_RENT_PUBKEY
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 { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
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 { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUshAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
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(provider.connection, payer, ushMintPublickey, payer.publicKey)
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(provider.connection, payer, hedgeMint, payer.publicKey)
22
- const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true)
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], provider.opts).catch(parseAnchorErrors)
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.instruction.closeLiquidationPoolPosition(
58
- new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
59
- {
60
- accounts: {
61
- vaultSystemState: vaultSystemStatePublicKey,
62
- poolState: poolState,
63
- poolEra: poolEra,
64
- poolPosition: poolPosition,
65
- poolUshAccount: poolUshAccount,
66
- payer: payerPublicKey,
67
- ownerUshAccount: payerUshAccount,
68
- hedgeMint: hedgeMint,
69
- ushMint: ushMint,
70
- payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
71
- communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
72
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
73
- tokenProgram: TOKEN_PROGRAM_ID,
74
- systemProgram: SystemProgram.programId,
75
- rent: SYSVAR_RENT_PUBKEY
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 { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
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 { findAssociatedTokenAddress, getPoolPublicKeyForMint, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
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], provider.opts).catch(parseAnchorErrors)
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
- return program.instruction.createStakingPool(
45
- poolBump,
46
- new BN(hedgeTokensToBeMinted),
47
- new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)),
48
- {
49
- accounts: {
50
- signer: payerPublicKey,
51
- vaultSystemState: vaultSystemStatePublicKey,
52
- pool: poolPublickey,
53
- stakedTokenMintInfo: mintPublicKey,
54
- ushMint: ushMintPublickey,
55
- poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
56
- poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
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 "@project-serum/anchor";
2
- import { TokenInstructions } from "@project-serum/serum";
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 "@solana/web3.js";
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 "../Constants";
22
+ } from '../Constants'
27
23
 
28
- import { v4 as uuidv4 } from "uuid";
29
- import { parseAnchorErrors } from "../utils/Errors";
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
- collateralType
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
- await getHedgeMintPublicKey()
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
- provider.connection,
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
- console.log("HEDGE SDK: buildCreateVaultTransaction")
148
- const ushMintPublickey = await getUshMintPublicKey();
149
- const payerPublicKey = program.provider.wallet.publicKey;
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
- await getHedgeMintPublicKey()
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("hedgeStakingPoolPublicKey", hedgeStakingPoolPublicKey.toString())
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("transaction", transaction)
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.instruction.createVault(
272
- salt,
273
- new BN(depositAmount),
274
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
275
- {
276
- accounts: {
277
- vaultSystemState: vaultSystemStatePublicKey,
278
- vaultTypeAccount: vaultTypeAccount,
279
- vault: vaultPublicKey,
280
- vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
281
- feePool: feePool,
282
- feePoolAssociatedUshTokenAccount: feePoolAssociatedUshTokenAccount,
283
- history: historyPublicKey,
284
- payer: payerPublicKey,
285
- payerTokenAccount: payerTokenAccountPublicKey,
286
- collateralMint: collateralMint,
287
- ushMint: ushMintPublickey,
288
- systemProgram: SystemProgram.programId,
289
- tokenProgram: TOKEN_PROGRAM_ID,
290
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
291
- rent: SYSVAR_RENT_PUBKEY,
292
- },
293
- signers: [],
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
  }