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