hedge-web3 0.1.1 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/lib/index.js +2102 -0
- package/lib/index.js.map +1 -0
- package/lib/types/src/Constants.d.ts +13 -0
- package/lib/types/src/Constants.d.ts.map +1 -0
- package/lib/types/src/HedgeDecimal.d.ts +15 -0
- package/lib/types/src/HedgeDecimal.d.ts.map +1 -0
- package/lib/types/src/StakingPools.d.ts +4 -0
- package/lib/types/src/StakingPools.d.ts.map +1 -0
- package/lib/types/src/Vaults.d.ts +9 -0
- package/lib/types/src/Vaults.d.ts.map +1 -0
- package/lib/types/src/idl/idl.d.ts +3 -0
- package/lib/types/src/idl/idl.d.ts.map +1 -0
- package/lib/types/src/index.d.ts +20 -0
- package/lib/types/src/index.d.ts.map +1 -0
- package/lib/types/src/instructions/createStakingPool.d.ts +5 -0
- package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -0
- package/lib/types/src/instructions/createVault.d.ts +5 -0
- package/lib/types/src/instructions/createVault.d.ts.map +1 -0
- package/lib/types/src/instructions/depositStakingPool.d.ts +5 -0
- package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -0
- package/lib/types/src/instructions/depositVault.d.ts +5 -0
- package/lib/types/src/instructions/depositVault.d.ts.map +1 -0
- package/lib/types/src/instructions/liquidateVault.d.ts +5 -0
- package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -0
- package/lib/types/src/instructions/loanVault.d.ts +5 -0
- package/lib/types/src/instructions/loanVault.d.ts.map +1 -0
- package/lib/types/src/instructions/redeemVault.d.ts +5 -0
- package/lib/types/src/instructions/redeemVault.d.ts.map +1 -0
- package/lib/types/src/instructions/repayVault.d.ts +5 -0
- package/lib/types/src/instructions/repayVault.d.ts.map +1 -0
- package/lib/types/src/instructions/withdrawStakingPool.d.ts +5 -0
- package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -0
- package/lib/types/src/instructions/withdrawVault.d.ts +5 -0
- package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -0
- package/lib/types/src/state/LiquidationPoolEra.d.ts +15 -0
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -0
- package/lib/types/src/state/LiquidationPoolState.d.ts +7 -0
- package/lib/types/src/state/LiquidationPoolState.d.ts.map +1 -0
- package/lib/types/src/state/LiquidationPosition.d.ts +25 -0
- package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -0
- package/lib/types/src/state/StakingPool.d.ts +16 -0
- package/lib/types/src/state/StakingPool.d.ts.map +1 -0
- package/lib/types/src/state/StakingPoolPosition.d.ts +20 -0
- package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -0
- package/lib/types/src/state/VaultAccount.d.ts +48 -0
- package/lib/types/src/state/VaultAccount.d.ts.map +1 -0
- package/lib/types/src/state/VaultHistoryEvent.d.ts +40 -0
- package/lib/types/src/state/VaultHistoryEvent.d.ts.map +1 -0
- package/lib/types/src/utils/Errors.d.ts +2 -0
- package/lib/types/src/utils/Errors.d.ts.map +1 -0
- package/lib/types/tsconfig.base.tsbuildinfo +1 -0
- package/package.json +6 -5
- package/rollup.config.js +14 -8
- package/src/Constants.ts +56 -0
- package/src/HedgeDecimal.ts +15 -7
- package/src/StakingPools.ts +3 -0
- package/src/Vaults.ts +8 -0
- package/src/idl/idl.ts +1474 -0
- package/src/index.ts +19 -4
- package/src/instructions/createStakingPool.ts +64 -0
- package/src/instructions/createVault.ts +65 -0
- package/src/instructions/depositStakingPool.ts +60 -0
- package/src/instructions/depositVault.ts +60 -0
- package/src/instructions/liquidateVault.ts +66 -0
- package/src/instructions/loanVault.ts +78 -0
- package/src/instructions/redeemVault.ts +82 -0
- package/src/instructions/repayVault.ts +73 -0
- package/src/instructions/withdrawStakingPool.ts +69 -0
- package/src/instructions/withdrawVault.ts +65 -0
- package/src/state/LiquidationPoolEra.ts +29 -0
- package/src/state/LiquidationPoolState.ts +10 -0
- package/src/state/LiquidationPosition.ts +77 -0
- package/src/state/StakingPool.ts +38 -0
- package/src/state/StakingPoolPosition.ts +39 -0
- package/src/{accounts → state}/VaultAccount.ts +5 -6
- package/src/state/VaultHistoryEvent.ts +61 -0
- package/src/types/buffer-layout/index.d.ts +88 -0
- package/src/utils/Errors.ts +11 -0
- package/tsconfig.base.json +3 -3
- package/tsconfig.d.json +3 -1
- package/tsconfig.json +1 -1
- package/src/accounts/LiquidationPoolEra.ts +0 -16
- package/src/accounts/LiquidationPoolState.ts +0 -22
- package/src/accounts/LiquidationPosition.ts +0 -72
- package/src/accounts/StakingPool.ts +0 -31
- package/src/accounts/StakingPoolPosition.ts +0 -29
- package/src/accounts/VaultHistoryEvent.ts +0 -27
package/src/index.ts
CHANGED
@@ -1,7 +1,22 @@
|
|
1
1
|
|
2
|
-
export
|
3
|
-
|
4
|
-
|
2
|
+
export * from './instructions/createStakingPool'
|
3
|
+
export * from './instructions/depositStakingPool'
|
4
|
+
export * from './instructions/withdrawStakingPool'
|
5
|
+
export * from './instructions/createVault'
|
6
|
+
export * from './instructions/depositVault'
|
7
|
+
export * from './instructions/withdrawVault'
|
8
|
+
export * from './instructions/loanVault'
|
9
|
+
export * from './instructions/repayVault'
|
10
|
+
export * from './instructions/redeemVault'
|
11
|
+
export * from './instructions/liquidateVault'
|
5
12
|
|
6
|
-
export * from './accounts/VaultAccount'
|
7
13
|
export * from './HedgeDecimal'
|
14
|
+
export * from './Constants'
|
15
|
+
|
16
|
+
export * from './state/VaultAccount'
|
17
|
+
export * from './state/VaultHistoryEvent'
|
18
|
+
export * from './state/StakingPool'
|
19
|
+
export * from './state/StakingPoolPosition'
|
20
|
+
export * from './state/LiquidationPoolEra'
|
21
|
+
export * from './state/LiquidationPoolState'
|
22
|
+
export * from './state/LiquidationPosition'
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
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'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { findAssociatedTokenAddress, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
|
+
|
7
|
+
export async function createStakingPool (
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
payer: Signer,
|
11
|
+
mintPublicKey: PublicKey,
|
12
|
+
hedgeTokensToBeMinted: number,
|
13
|
+
overrideStartTime?: number
|
14
|
+
): Promise<PublicKey> {
|
15
|
+
const transaction = new Transaction().add(
|
16
|
+
await createStakingPoolInstruction(
|
17
|
+
program,
|
18
|
+
payer.publicKey,
|
19
|
+
mintPublicKey,
|
20
|
+
hedgeTokensToBeMinted,
|
21
|
+
overrideStartTime
|
22
|
+
)
|
23
|
+
)
|
24
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
|
25
|
+
const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey)
|
26
|
+
return poolPublickey
|
27
|
+
}
|
28
|
+
|
29
|
+
export async function createStakingPoolInstruction (
|
30
|
+
program: Program,
|
31
|
+
payerPublicKey: PublicKey,
|
32
|
+
mintPublicKey: PublicKey,
|
33
|
+
hedgeTokensToBeMinted: number,
|
34
|
+
overrideStartTime?: number
|
35
|
+
): Promise<TransactionInstruction> {
|
36
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
37
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
38
|
+
const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey)
|
39
|
+
|
40
|
+
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
|
41
|
+
const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey)
|
42
|
+
|
43
|
+
return program.instruction.createStakingPool(
|
44
|
+
poolBump,
|
45
|
+
poolSeedPhrase,
|
46
|
+
new BN(hedgeTokensToBeMinted),
|
47
|
+
new BN(overrideStartTime ?? Date.now() / 1000),
|
48
|
+
{
|
49
|
+
accounts: {
|
50
|
+
signer: payerPublicKey,
|
51
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
52
|
+
pool: poolPublickey,
|
53
|
+
stakedTokenMintInfo: mintPublicKey,
|
54
|
+
usdhMint: usdhMintPublickey,
|
55
|
+
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
56
|
+
poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
|
57
|
+
rent: SYSVAR_RENT_PUBKEY,
|
58
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
59
|
+
splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
|
60
|
+
systemProgram: SystemProgram.programId
|
61
|
+
},
|
62
|
+
signers: []
|
63
|
+
})
|
64
|
+
}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
|
+
|
6
|
+
export async function createVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
depositAmount: number,
|
11
|
+
collateralRatio: number
|
12
|
+
): Promise<PublicKey> {
|
13
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
14
|
+
const USDH = new Token(
|
15
|
+
provider.connection,
|
16
|
+
usdhMintPublickey,
|
17
|
+
TOKEN_PROGRAM_ID,
|
18
|
+
payer
|
19
|
+
)
|
20
|
+
|
21
|
+
// Prep the user to get USDH back out at some point
|
22
|
+
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
23
|
+
|
24
|
+
const newVault = Keypair.generate()
|
25
|
+
const history = Keypair.generate()
|
26
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
27
|
+
const transaction = new Transaction().add(
|
28
|
+
createVaultInstruction(
|
29
|
+
program,
|
30
|
+
vaultSystemStatePublicKey,
|
31
|
+
payer.publicKey,
|
32
|
+
newVault.publicKey,
|
33
|
+
history.publicKey,
|
34
|
+
depositAmount,
|
35
|
+
collateralRatio
|
36
|
+
)
|
37
|
+
)
|
38
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, newVault, history], provider?.opts)
|
39
|
+
return newVault.publicKey
|
40
|
+
}
|
41
|
+
|
42
|
+
export function createVaultInstruction (
|
43
|
+
program: Program,
|
44
|
+
vaultSystemStatePublicKey: PublicKey,
|
45
|
+
payerPublicKey: PublicKey,
|
46
|
+
vaultPublicKey: PublicKey,
|
47
|
+
historyPublicKey: PublicKey,
|
48
|
+
depositAmount: number,
|
49
|
+
collateralRatio: number
|
50
|
+
): TransactionInstruction {
|
51
|
+
const ix = program.instruction.createVault(
|
52
|
+
new BN(depositAmount),
|
53
|
+
new BN(collateralRatio)
|
54
|
+
, {
|
55
|
+
accounts: {
|
56
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
57
|
+
vault: vaultPublicKey,
|
58
|
+
history: historyPublicKey,
|
59
|
+
payer: payerPublicKey,
|
60
|
+
systemProgram: SystemProgram.programId
|
61
|
+
},
|
62
|
+
signers: []
|
63
|
+
})
|
64
|
+
return ix
|
65
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { findAssociatedTokenAddress, getPoolPublicKeyForMint } from '../Constants'
|
6
|
+
|
7
|
+
export async function depositStakingPool (
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
payer: Signer,
|
11
|
+
mintPublicKey: PublicKey,
|
12
|
+
depositAmount: number,
|
13
|
+
overrideStartTime?: number
|
14
|
+
): Promise<PublicKey> {
|
15
|
+
const poolPosition = Keypair.generate()
|
16
|
+
const transaction = new Transaction().add(
|
17
|
+
await depositStakingPoolInstruction(
|
18
|
+
program,
|
19
|
+
payer.publicKey,
|
20
|
+
poolPosition.publicKey,
|
21
|
+
mintPublicKey,
|
22
|
+
depositAmount,
|
23
|
+
overrideStartTime
|
24
|
+
)
|
25
|
+
)
|
26
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors)
|
27
|
+
return poolPosition.publicKey
|
28
|
+
}
|
29
|
+
|
30
|
+
export async function depositStakingPoolInstruction (
|
31
|
+
program: Program,
|
32
|
+
payerPublicKey: PublicKey,
|
33
|
+
poolPositionPublicKey: PublicKey,
|
34
|
+
stakedTokenMintPublicKey: PublicKey,
|
35
|
+
depositAmount: number,
|
36
|
+
overrideStartTime?: number
|
37
|
+
): Promise<TransactionInstruction> {
|
38
|
+
const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
|
39
|
+
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
|
40
|
+
const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
|
41
|
+
|
42
|
+
return program.instruction.depositStakingPool(
|
43
|
+
new BN(depositAmount),
|
44
|
+
new BN(overrideStartTime ?? Date.now() / 1000), // override current time
|
45
|
+
{
|
46
|
+
accounts: {
|
47
|
+
payer: payerPublicKey,
|
48
|
+
pool: poolPublickey,
|
49
|
+
poolPosition: poolPositionPublicKey,
|
50
|
+
stakedTokenMintInfo: stakedTokenMintPublicKey,
|
51
|
+
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
52
|
+
payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
|
53
|
+
rent: SYSVAR_RENT_PUBKEY,
|
54
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
55
|
+
splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
|
56
|
+
systemProgram: SystemProgram.programId
|
57
|
+
},
|
58
|
+
signers: []
|
59
|
+
})
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
|
+
|
6
|
+
export async function depositVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
depositAmount: number
|
12
|
+
): Promise<PublicKey> {
|
13
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
14
|
+
const USDH = new Token(
|
15
|
+
provider.connection,
|
16
|
+
usdhMintPublickey,
|
17
|
+
TOKEN_PROGRAM_ID,
|
18
|
+
payer
|
19
|
+
)
|
20
|
+
|
21
|
+
// Prep the user to get USDH back out at some point
|
22
|
+
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
23
|
+
|
24
|
+
const history = Keypair.generate()
|
25
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
26
|
+
const transaction = new Transaction().add(
|
27
|
+
depositVaultInstruction(
|
28
|
+
program,
|
29
|
+
vaultSystemStatePublicKey,
|
30
|
+
payer.publicKey,
|
31
|
+
vaultPublicKey,
|
32
|
+
history.publicKey,
|
33
|
+
depositAmount
|
34
|
+
)
|
35
|
+
)
|
36
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
37
|
+
return vaultPublicKey
|
38
|
+
}
|
39
|
+
|
40
|
+
export function depositVaultInstruction (
|
41
|
+
program: Program,
|
42
|
+
vaultSystemStatePublicKey: PublicKey,
|
43
|
+
payerPublicKey: PublicKey,
|
44
|
+
vaultPublicKey: PublicKey,
|
45
|
+
historyPublicKey: PublicKey,
|
46
|
+
depositAmount: number
|
47
|
+
): TransactionInstruction {
|
48
|
+
return program.instruction.depositVault(
|
49
|
+
new BN(depositAmount)
|
50
|
+
, {
|
51
|
+
accounts: {
|
52
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
53
|
+
vaultAccount: vaultPublicKey,
|
54
|
+
history: historyPublicKey,
|
55
|
+
vaultOwner: payerPublicKey,
|
56
|
+
systemProgram: SystemProgram.programId
|
57
|
+
},
|
58
|
+
signers: []
|
59
|
+
})
|
60
|
+
}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { CHAINLINK_SOL_USD_PUBLICKEY, getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
|
+
|
6
|
+
export async function liquidateVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
chainlinkOverridePrice?: number
|
12
|
+
): Promise<PublicKey> {
|
13
|
+
const history = Keypair.generate()
|
14
|
+
const newEra = Keypair.generate()
|
15
|
+
const transaction = new Transaction().add(
|
16
|
+
await liquidateVaultInstruction(
|
17
|
+
program,
|
18
|
+
payer.publicKey,
|
19
|
+
vaultPublicKey,
|
20
|
+
history.publicKey,
|
21
|
+
newEra.publicKey,
|
22
|
+
chainlinkOverridePrice
|
23
|
+
)
|
24
|
+
)
|
25
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts)
|
26
|
+
return vaultPublicKey
|
27
|
+
}
|
28
|
+
|
29
|
+
export async function liquidateVaultInstruction (
|
30
|
+
program: Program,
|
31
|
+
payerPublicKey: PublicKey,
|
32
|
+
vaultPublickey: PublicKey,
|
33
|
+
historyPublicKey: PublicKey,
|
34
|
+
newEraPublicKey: PublicKey,
|
35
|
+
chainlinkOverridePrice?: number
|
36
|
+
): Promise<TransactionInstruction> {
|
37
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
38
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
39
|
+
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
40
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
41
|
+
const [liquidationPoolStatePublicKey] = await getLiquidationPoolStatePublicKey()
|
42
|
+
const [liquidationPoolUsdhAccountPublickey] = await getLiquidationPoolUsdhAccountPublicKey()
|
43
|
+
|
44
|
+
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
45
|
+
|
46
|
+
return program.instruction.liquidateVault(
|
47
|
+
new BN(chainlinkOverridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
|
48
|
+
{
|
49
|
+
accounts: {
|
50
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
51
|
+
poolEra: poolStateInfo.currentEra,
|
52
|
+
vaultAccount: vaultPublickey,
|
53
|
+
poolState: liquidationPoolStatePublicKey,
|
54
|
+
usdhMint: usdhMintPublickey,
|
55
|
+
chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
|
56
|
+
history: historyPublicKey,
|
57
|
+
payer: payerPublicKey,
|
58
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
59
|
+
systemProgram: SystemProgram.programId,
|
60
|
+
feePool: hedgeStakingPoolPublicKey,
|
61
|
+
poolUsdhAccount: liquidationPoolUsdhAccountPublickey,
|
62
|
+
newEra: newEraPublicKey
|
63
|
+
},
|
64
|
+
signers: []
|
65
|
+
})
|
66
|
+
}
|
@@ -0,0 +1,78 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { CHAINLINK_SOL_USD_PUBLICKEY, findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
|
+
|
6
|
+
export async function loanVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
loanAmount: number,
|
12
|
+
chainlinkOverridePrice?: number
|
13
|
+
): Promise<PublicKey> {
|
14
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
15
|
+
const USDH = new Token(
|
16
|
+
provider.connection,
|
17
|
+
usdhMintPublickey,
|
18
|
+
TOKEN_PROGRAM_ID,
|
19
|
+
payer
|
20
|
+
)
|
21
|
+
|
22
|
+
// Prep the user to get USDH back out at some point
|
23
|
+
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
24
|
+
|
25
|
+
const history = Keypair.generate()
|
26
|
+
const transaction = new Transaction().add(
|
27
|
+
await loanVaultInstruction(
|
28
|
+
program,
|
29
|
+
payer.publicKey,
|
30
|
+
payerUsdhAccount.address,
|
31
|
+
vaultPublicKey,
|
32
|
+
history.publicKey,
|
33
|
+
loanAmount,
|
34
|
+
chainlinkOverridePrice
|
35
|
+
)
|
36
|
+
)
|
37
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
38
|
+
return vaultPublicKey
|
39
|
+
}
|
40
|
+
|
41
|
+
export async function loanVaultInstruction (
|
42
|
+
program: Program,
|
43
|
+
payerPublicKey: PublicKey,
|
44
|
+
ownerUsdhAccount: PublicKey,
|
45
|
+
vaultPublickey: PublicKey,
|
46
|
+
historyPublicKey: PublicKey,
|
47
|
+
loanAmount: number,
|
48
|
+
chainlinkOverridePrice?: number
|
49
|
+
): Promise<TransactionInstruction> {
|
50
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
51
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
52
|
+
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
53
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
54
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
55
|
+
hedgeStakingPoolPublicKey,
|
56
|
+
usdhMintPublickey
|
57
|
+
)
|
58
|
+
|
59
|
+
return program.instruction.loanVault(
|
60
|
+
new BN(loanAmount),
|
61
|
+
new BN(chainlinkOverridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
|
62
|
+
{
|
63
|
+
accounts: {
|
64
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
65
|
+
vaultAccount: vaultPublickey,
|
66
|
+
history: historyPublicKey,
|
67
|
+
feePool: hedgeStakingPoolPublicKey,
|
68
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
69
|
+
usdhMint: usdhMintPublickey,
|
70
|
+
vaultOwner: payerPublicKey,
|
71
|
+
ownerUsdhAccount: ownerUsdhAccount,
|
72
|
+
chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
|
73
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
74
|
+
systemProgram: SystemProgram.programId
|
75
|
+
},
|
76
|
+
signers: []
|
77
|
+
})
|
78
|
+
}
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { CHAINLINK_SOL_USD_PUBLICKEY, findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
|
+
|
6
|
+
export async function redeemVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
repayAmount: number,
|
12
|
+
chainlinkOverridePrice?: number,
|
13
|
+
transactionOverrideTime?: number
|
14
|
+
): Promise<PublicKey> {
|
15
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
16
|
+
const USDH = new Token(
|
17
|
+
provider.connection,
|
18
|
+
usdhMintPublickey,
|
19
|
+
TOKEN_PROGRAM_ID,
|
20
|
+
payer
|
21
|
+
)
|
22
|
+
|
23
|
+
// Prep the user to get USDH back out at some point
|
24
|
+
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
25
|
+
|
26
|
+
const history = Keypair.generate()
|
27
|
+
const transaction = new Transaction().add(
|
28
|
+
await redeemVaultInstruction(
|
29
|
+
program,
|
30
|
+
payer.publicKey,
|
31
|
+
payerUsdhAccount.address,
|
32
|
+
vaultPublicKey,
|
33
|
+
history.publicKey,
|
34
|
+
repayAmount,
|
35
|
+
chainlinkOverridePrice,
|
36
|
+
transactionOverrideTime
|
37
|
+
)
|
38
|
+
)
|
39
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
40
|
+
return vaultPublicKey
|
41
|
+
}
|
42
|
+
|
43
|
+
export async function redeemVaultInstruction (
|
44
|
+
program: Program,
|
45
|
+
payerPublicKey: PublicKey,
|
46
|
+
ownerUsdhAccount: PublicKey,
|
47
|
+
vaultPublickey: PublicKey,
|
48
|
+
historyPublicKey: PublicKey,
|
49
|
+
repayAmount: number,
|
50
|
+
chainlinkOverridePrice?: number,
|
51
|
+
transactionOverrideTime?: number
|
52
|
+
): Promise<TransactionInstruction> {
|
53
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
54
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
55
|
+
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
56
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
57
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
58
|
+
hedgeStakingPoolPublicKey,
|
59
|
+
usdhMintPublickey
|
60
|
+
)
|
61
|
+
|
62
|
+
return program.instruction.redeemVault(
|
63
|
+
new BN(repayAmount),
|
64
|
+
new BN(chainlinkOverridePrice ?? 150 * LAMPORTS_PER_SOL), // override usd/sol price
|
65
|
+
new BN(transactionOverrideTime ?? (Date.now() / 1000)), // override start time
|
66
|
+
{
|
67
|
+
accounts: {
|
68
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
69
|
+
vaultAccount: vaultPublickey,
|
70
|
+
history: historyPublicKey,
|
71
|
+
feePool: hedgeStakingPoolPublicKey,
|
72
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
73
|
+
usdhMint: usdhMintPublickey,
|
74
|
+
payer: payerPublicKey,
|
75
|
+
payerUsdhAccount: ownerUsdhAccount,
|
76
|
+
chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
|
77
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
78
|
+
systemProgram: SystemProgram.programId
|
79
|
+
},
|
80
|
+
signers: []
|
81
|
+
})
|
82
|
+
}
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
|
+
|
6
|
+
export async function repayVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
repayAmount: number
|
12
|
+
): Promise<PublicKey> {
|
13
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
14
|
+
const USDH = new Token(
|
15
|
+
provider.connection,
|
16
|
+
usdhMintPublickey,
|
17
|
+
TOKEN_PROGRAM_ID,
|
18
|
+
payer
|
19
|
+
)
|
20
|
+
|
21
|
+
// Prep the user to get USDH back out at some point
|
22
|
+
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
23
|
+
|
24
|
+
const history = Keypair.generate()
|
25
|
+
const transaction = new Transaction().add(
|
26
|
+
await repayVaultInstruction(
|
27
|
+
program,
|
28
|
+
payer.publicKey,
|
29
|
+
payerUsdhAccount.address,
|
30
|
+
vaultPublicKey,
|
31
|
+
history.publicKey,
|
32
|
+
repayAmount
|
33
|
+
)
|
34
|
+
)
|
35
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
36
|
+
return vaultPublicKey
|
37
|
+
}
|
38
|
+
|
39
|
+
export async function repayVaultInstruction (
|
40
|
+
program: Program,
|
41
|
+
payerPublicKey: PublicKey,
|
42
|
+
ownerUsdhAccount: PublicKey,
|
43
|
+
vaultPublickey: PublicKey,
|
44
|
+
historyPublicKey: PublicKey,
|
45
|
+
repayAmount: number
|
46
|
+
): Promise<TransactionInstruction> {
|
47
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
48
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
49
|
+
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
50
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
51
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
52
|
+
hedgeStakingPoolPublicKey,
|
53
|
+
usdhMintPublickey
|
54
|
+
)
|
55
|
+
|
56
|
+
return program.instruction.repayVault(
|
57
|
+
new BN(repayAmount),
|
58
|
+
{
|
59
|
+
accounts: {
|
60
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
61
|
+
vaultAccount: vaultPublickey,
|
62
|
+
history: historyPublicKey,
|
63
|
+
feePool: hedgeStakingPoolPublicKey,
|
64
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
65
|
+
usdhMint: usdhMintPublickey,
|
66
|
+
vaultOwner: payerPublicKey,
|
67
|
+
ownerUsdhAccount: ownerUsdhAccount,
|
68
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
69
|
+
systemProgram: SystemProgram.programId
|
70
|
+
},
|
71
|
+
signers: []
|
72
|
+
})
|
73
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
|
+
|
7
|
+
export async function withdrawStakingPool (
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
payer: Signer,
|
11
|
+
poolPositionPublicKey: PublicKey,
|
12
|
+
stakedTokenMintPublicKey: PublicKey,
|
13
|
+
overrideStartTime?: number
|
14
|
+
): Promise<PublicKey> {
|
15
|
+
const poolPosition = Keypair.generate()
|
16
|
+
const transaction = new Transaction().add(
|
17
|
+
await withdrawStakingPoolInstruction(
|
18
|
+
program,
|
19
|
+
payer.publicKey,
|
20
|
+
poolPositionPublicKey,
|
21
|
+
stakedTokenMintPublicKey,
|
22
|
+
overrideStartTime
|
23
|
+
)
|
24
|
+
)
|
25
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
|
26
|
+
return poolPosition.publicKey
|
27
|
+
}
|
28
|
+
|
29
|
+
export async function withdrawStakingPoolInstruction (
|
30
|
+
program: Program,
|
31
|
+
payerPublicKey: PublicKey,
|
32
|
+
poolPositionPublicKey: PublicKey,
|
33
|
+
stakedTokenMintPublicKey: PublicKey,
|
34
|
+
overrideStartTime?: number
|
35
|
+
): Promise<TransactionInstruction> {
|
36
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
37
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
38
|
+
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
39
|
+
const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
|
40
|
+
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
|
41
|
+
const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey)
|
42
|
+
const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
|
43
|
+
const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey)
|
44
|
+
const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey)
|
45
|
+
|
46
|
+
return program.instruction.withdrawStakingPool(
|
47
|
+
new BN(overrideStartTime ?? Date.now() / 1000), // override current time
|
48
|
+
{
|
49
|
+
accounts: {
|
50
|
+
payer: payerPublicKey,
|
51
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
52
|
+
pool: poolPublickey,
|
53
|
+
poolPosition: poolPositionPublicKey,
|
54
|
+
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
55
|
+
poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
|
56
|
+
payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
|
57
|
+
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
58
|
+
payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
|
59
|
+
hedgeMint: hedgeMintPublickey,
|
60
|
+
stakedTokenMint: stakedTokenMintPublicKey,
|
61
|
+
usdhMint: usdhMintPublickey,
|
62
|
+
rent: SYSVAR_RENT_PUBKEY,
|
63
|
+
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
64
|
+
splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
|
65
|
+
systemProgram: SystemProgram.programId
|
66
|
+
},
|
67
|
+
signers: []
|
68
|
+
})
|
69
|
+
}
|