hedge-web3 0.1.43 → 0.1.46

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 (32) hide show
  1. package/declarations/idl/vault.d.ts +78 -5
  2. package/declarations/instructions/liquidateVault.d.ts +1 -1
  3. package/declarations/state/VaultAccount.d.ts +8 -6
  4. package/declarations/state/VaultType.d.ts +24 -0
  5. package/declarations/utils/Sender.d.ts +2 -0
  6. package/declarations/utils/sendAndConfirmWithDebug.d.ts +2 -0
  7. package/lib/idl/vault.js +78 -5
  8. package/lib/instructions/createStakingPool.js +1 -0
  9. package/lib/instructions/depositVault.js +7 -7
  10. package/lib/instructions/liquidateVault.js +5 -7
  11. package/lib/instructions/loanVault.js +7 -6
  12. package/lib/instructions/redeemVault.js +4 -5
  13. package/lib/instructions/refreshOraclePrice.js +2 -2
  14. package/lib/instructions/repayVault.js +4 -5
  15. package/lib/instructions/withdrawVault.js +4 -5
  16. package/lib/state/VaultAccount.js +9 -8
  17. package/lib/state/VaultType.js +32 -0
  18. package/lib/utils/Sender.js +32 -0
  19. package/lib/utils/getLinkedListAccounts.js +15 -15
  20. package/lib/utils/sendAndConfirmWithDebug.js +35 -0
  21. package/package.json +1 -1
  22. package/src/idl/vault.ts +156 -10
  23. package/src/instructions/createStakingPool.ts +1 -0
  24. package/src/instructions/depositVault.ts +8 -8
  25. package/src/instructions/liquidateVault.ts +5 -7
  26. package/src/instructions/loanVault.ts +11 -8
  27. package/src/instructions/redeemVault.ts +4 -5
  28. package/src/instructions/refreshOraclePrice.ts +2 -2
  29. package/src/instructions/repayVault.ts +4 -5
  30. package/src/instructions/withdrawVault.ts +4 -5
  31. package/src/state/VaultAccount.ts +14 -10
  32. package/src/utils/getLinkedListAccounts.ts +16 -16
@@ -40,13 +40,12 @@ export async function redeemVault(
40
40
  )
41
41
 
42
42
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
43
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
44
- const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
43
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultAccount.vaultType)
45
44
  const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
46
45
  provider.connection,
47
46
  payer,
48
47
  vaultTypeAccountInfo.collateralMint,
49
- vaultTypeAccountPublicKey,
48
+ vaultAccount.vaultType,
50
49
  true
51
50
  )
52
51
 
@@ -68,7 +67,7 @@ export async function redeemVault(
68
67
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
69
68
  program,
70
69
  provider,
71
- vaultTypeAccountPublicKey,
70
+ vaultAccount.vaultType,
72
71
  vaultPublicKey,
73
72
  0,
74
73
  0,
@@ -86,7 +85,7 @@ export async function redeemVault(
86
85
  vaultPublicKey,
87
86
  vaultAssociatedTokenAccount.address,
88
87
  history.publicKey,
89
- vaultTypeAccountPublicKey,
88
+ vaultAccount.vaultType,
90
89
  vaultTypeAssociatedTokenAccount.address,
91
90
  oldSmallerPublicKey,
92
91
  newSmallerPublicKey,
@@ -51,7 +51,7 @@ export async function refreshOraclePriceInstruction(
51
51
  .accounts({
52
52
  oracleInfoAccount: oracleInfoAccount,
53
53
  vaultTypeAccount: vaultTypeAccount,
54
- oracleChainlink: chainlinkAccunts[network],
54
+ oracleChainlink: chainlinkAccounts[network],
55
55
  oraclePyth: pythAccounts[network],
56
56
  oracleSwitchboard: switchboardAccounts[network],
57
57
  systemProgram: SystemProgram.programId,
@@ -70,7 +70,7 @@ const pythAccounts = {
70
70
  Devnet: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
71
71
  MainnetBeta: new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG'),
72
72
  }
73
- const chainlinkAccunts = {
73
+ const chainlinkAccounts = {
74
74
  Testing: SystemProgram.programId,
75
75
  Devnet: new PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
76
76
  MainnetBeta: SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
@@ -39,10 +39,9 @@ export async function repayVault(
39
39
  )
40
40
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
41
41
 
42
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
43
- const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
42
+ const vaultTypeAccount = await program.account.vaultType.fetch(vaultAccount.vaultType)
44
43
  const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
45
- vaultTypeAccountPublicKey,
44
+ vaultAccount.vaultType,
46
45
  vaultTypeAccount.collateralMint
47
46
  )
48
47
  const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
@@ -50,7 +49,7 @@ export async function repayVault(
50
49
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
51
50
  program,
52
51
  provider,
53
- vaultTypeAccountPublicKey,
52
+ vaultAccount.vaultType,
54
53
  vaultPublicKey,
55
54
  0,
56
55
  repayAmount * -1,
@@ -67,7 +66,7 @@ export async function repayVault(
67
66
  vaultPublicKey,
68
67
  vaultAssociatedTokenAccount,
69
68
  history.publicKey,
70
- vaultTypeAccountPublicKey,
69
+ vaultAccount.vaultType,
71
70
  vaultTypeAssociatedTokenAccount,
72
71
  oldSmallerPublicKey,
73
72
  newSmallerPublicKey,
@@ -37,7 +37,6 @@ export async function withdrawVault(
37
37
  const history = Keypair.generate()
38
38
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
39
39
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
40
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
41
40
  const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(
42
41
  provider.connection,
43
42
  payer,
@@ -46,12 +45,12 @@ export async function withdrawVault(
46
45
  true
47
46
  )
48
47
 
49
- const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
48
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultAccount.vaultType)
50
49
  const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
51
50
  provider.connection,
52
51
  payer,
53
52
  vaultTypeAccountInfo.collateralMint,
54
- vaultTypeAccountPublicKey,
53
+ vaultAccount.vaultType,
55
54
  true
56
55
  )
57
56
 
@@ -70,7 +69,7 @@ export async function withdrawVault(
70
69
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
71
70
  program,
72
71
  provider,
73
- vaultTypeAccountPublicKey,
72
+ vaultAccount.vaultType,
74
73
  vaultPublicKey,
75
74
  withdrawAmount * -1,
76
75
  0,
@@ -86,7 +85,7 @@ export async function withdrawVault(
86
85
  destinationTokenAccount.address,
87
86
  vaultPublicKey,
88
87
  vaultAssociatedCollateralAccount.address,
89
- vaultTypeAccountPublicKey,
88
+ vaultAccount.vaultType,
90
89
  vaultTypeAssociatedTokenAccount.address,
91
90
  hedgeStakingPoolPublicKey,
92
91
  hedgeStakingPoolAssociatedUshTokenAccount,
@@ -26,21 +26,24 @@ export class VaultAccount {
26
26
  /** The ordered number of when this vault was created. */
27
27
  vaultNumber: number
28
28
 
29
- /** Debt redistribution snapshop */
29
+ /** Debt redistribution snapshot */
30
30
  debtProductSnapshotBytes = new Decimal(0)
31
31
 
32
- /** Collateral redistribution snapshop' */
32
+ /** Collateral redistribution snapshot' */
33
33
  collateralAccumulatorSnapshotBytes = new Decimal(0)
34
34
 
35
- /** The vault type eg 'SOL-150' */
36
- collateralType: string
35
+ /** The vault type eg '2-SOL-150-0' */
36
+ vaultTypeName: string
37
37
 
38
- /** Current State of the vautl ("Open", "Closed", "Liquidated") */
38
+ /** Current State of the vault ("Open", "Closed", "Liquidated") */
39
39
  vaultStatus = ''
40
40
 
41
41
  /** The public key of the next vault to redeem. */
42
42
  nextVaultToRedeem: PublicKey
43
43
 
44
+ /** The public key of the vault type. */
45
+ vaultType: PublicKey
46
+
44
47
  constructor(vault: any, publicKey: PublicKey) {
45
48
  this.publicKey = publicKey
46
49
  this.vaultOwner = vault.vaultOwner
@@ -54,12 +57,13 @@ export class VaultAccount {
54
57
  if (vault.collateralAccumulatorSnapshotBytes) {
55
58
  this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
56
59
  }
57
- this.collateralType = vault.collateralType
60
+ this.vaultTypeName = vault.vaultTypeName
58
61
  this.nextVaultToRedeem = vault.nextVaultToRedeem
59
62
 
60
63
  if (vault.vaultStatus) {
61
64
  this.vaultStatus = Object.keys(vault.vaultStatus)[0]
62
65
  }
66
+ this.vaultType = vault.vaultType
63
67
  }
64
68
 
65
69
  /**
@@ -121,10 +125,10 @@ export class VaultAccount {
121
125
  this.vaultStatus = 'liquidated'
122
126
  }
123
127
 
124
- public updateDebtAndCollateral(vaultTypeAccuntData: any) {
125
- const debtProductCurrent = DecimalFromU128(vaultTypeAccuntData.debtRedistributionProduct)
128
+ public updateDebtAndCollateral(vaultTypeAccountData: any) {
129
+ const debtProductCurrent = DecimalFromU128(vaultTypeAccountData.debtRedistributionProduct)
126
130
 
127
- const collateralAccumulatorCurrent = DecimalFromU128(vaultTypeAccuntData.collateralRedistributionAccumulator)
131
+ const collateralAccumulatorCurrent = DecimalFromU128(vaultTypeAccountData.collateralRedistributionAccumulator)
128
132
 
129
133
  this.denormalizedDebt = debtProductCurrent
130
134
  .div(this.debtProductSnapshotBytes)
@@ -156,7 +160,7 @@ export class VaultAccount {
156
160
 
157
161
  return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${
158
162
  this.denormalizedDebt
159
- } Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `
163
+ } Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `
160
164
  }
161
165
  /**
162
166
  * Creates a VaultAccount from a slice of data
@@ -22,7 +22,7 @@ export async function getLinkedListAccounts(
22
22
  liquidate: boolean,
23
23
  cachedVaults?: VaultAccount[]
24
24
  ): Promise<[PublicKey, PublicKey, PublicKey, VaultAccount[]]> {
25
- console.log('Getting getLinkedListAccounts')
25
+ // console.log('Getting getLinkedListAccounts')
26
26
  const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
27
27
 
28
28
  // Default for null is the vault itself, so set them all to this vault
@@ -31,6 +31,7 @@ export async function getLinkedListAccounts(
31
31
  let newLargerPublicKey = vaultPublicKey
32
32
 
33
33
  const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
34
+ const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
34
35
  const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
35
36
 
36
37
  // Load all the vaults
@@ -54,11 +55,11 @@ export async function getLinkedListAccounts(
54
55
  // vaults = allVaults.map((vault) => {
55
56
  // return new VaultAccount(vault.account, vault.publicKey)
56
57
  // })
57
- vaults = await getMiniVaults(program, vaultTypeAccount.collateralType)
58
+ vaults = await getMiniVaults(program, vaultTypeAccount.vaultTypeName)
58
59
  }
59
60
 
60
- console.log('Vault count found:', vaults.length)
61
- console.log('First Vault', vaults[0])
61
+ // console.log('Vault count found:', vaults.length)
62
+ // console.log('First Vault', vaults[0])
62
63
 
63
64
  // Filter out the accounts that are not open
64
65
  // TODO filter on vault status. Or we enable people to "close out" empty vaults
@@ -87,6 +88,12 @@ export async function getLinkedListAccounts(
87
88
  oldSmallerPublicKey = vaults[indexBefore - 1].publicKey
88
89
  }
89
90
 
91
+ // Pretty print the list again
92
+ // console.log('Sorted open vaults. Index Before: ', indexBefore)
93
+ // console.log(vaults.map((vault) => {
94
+ // return vault.toString(vaultPublicKey)
95
+ // }))
96
+
90
97
  // Pretty print all the vaults before the operation
91
98
  // console.log('Sorted open vaults BEFORE at index:', indexBefore)
92
99
  // let correctOrderBefore = true
@@ -100,7 +107,7 @@ export async function getLinkedListAccounts(
100
107
  // }
101
108
  // }
102
109
  // if (correctOrderBefore) {
103
- // console.log(`Verfied the on-chain order of vault type:`, vaultTypeAccount.collateralType)
110
+ // console.log(`Verified the on-chain order of vault type:`, vaultTypeAccount.collateralType)
104
111
  // } else {
105
112
  // throw new Error('On-Chian vaults not in order!')
106
113
  // }
@@ -116,7 +123,7 @@ export async function getLinkedListAccounts(
116
123
  }
117
124
 
118
125
  // Now that we know it's def in the list, iterate the list and update
119
- // this vault with the opeation we're going to apply
126
+ // this vault with the operation we're going to apply
120
127
  const newNormalizedDebt = new Decimal(loanAmount)
121
128
  const vaultTypeCompoundedInterest = DecimalFromU128(vaultTypeAccount.cumulativeRate.toString())
122
129
  vaults[indexBefore].updateDebtAndCollateral(vaultTypeAccount)
@@ -137,12 +144,6 @@ export async function getLinkedListAccounts(
137
144
  // Sort it again since we've changed one vault
138
145
  vaults = vaults.sort(sortVaults)
139
146
 
140
- // Pretty print the list again
141
- // console.log('Sorted open vaults with new debt added at index: ', indexAfter)
142
- // console.log(vaults.map((vault) => {
143
- // return vault.toString(vaultPublicKey)
144
- // }))
145
-
146
147
  // Search for the vaults new position
147
148
  let indexAfter = -1
148
149
  vaults.forEach((vault, index) => {
@@ -160,8 +161,7 @@ export async function getLinkedListAccounts(
160
161
  // )
161
162
 
162
163
  // Print where it moved from / to
163
- console.log('Index Before', indexBefore)
164
- console.log('Index After', indexAfter)
164
+ // console.log('Index After', indexAfter)
165
165
 
166
166
  // Save references to the new left and right
167
167
  if (indexAfter > 0) {
@@ -190,7 +190,7 @@ function sortVaults(a: VaultAccount, b: VaultAccount) {
190
190
  return aRatio - bRatio
191
191
  }
192
192
 
193
- async function getMiniVaults(program: Program<Vault>, collateralType: string) {
193
+ async function getMiniVaults(program: Program<Vault>, vaultTypeName: string ) {
194
194
  const filters = [
195
195
  // Filter for Vault Accounts
196
196
  {
@@ -200,7 +200,7 @@ async function getMiniVaults(program: Program<Vault>, collateralType: string) {
200
200
  // Filter for Vaults with this collateral type
201
201
  {
202
202
  memcmp: {
203
- bytes: base58.encode(VaultAccount.getBufferForString(collateralType)),
203
+ bytes: base58.encode(VaultAccount.getBufferForString(vaultTypeName)),
204
204
  offset: 8 + 32 + 24,
205
205
  },
206
206
  },