hedge-web3 0.1.46 → 0.2.1

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 (71) hide show
  1. package/declarations/Constants.d.ts +2 -1
  2. package/declarations/idl/vault.d.ts +109 -47
  3. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +1 -1
  4. package/declarations/instructions/claimStakingPoolPosition.d.ts +1 -1
  5. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
  6. package/declarations/instructions/createStakingPool.d.ts +1 -1
  7. package/declarations/instructions/createVault.d.ts +1 -1
  8. package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
  9. package/declarations/instructions/depositStakingPool.d.ts +1 -1
  10. package/declarations/instructions/depositVault.d.ts +1 -1
  11. package/declarations/instructions/initHedgeFoundation.d.ts +1 -1
  12. package/declarations/instructions/liquidateVault.d.ts +1 -1
  13. package/declarations/instructions/loanVault.d.ts +1 -1
  14. package/declarations/instructions/redeemVault.d.ts +1 -1
  15. package/declarations/instructions/refreshOraclePrice.d.ts +3 -3
  16. package/declarations/instructions/repayVault.d.ts +1 -1
  17. package/declarations/instructions/setHalted.d.ts +1 -1
  18. package/declarations/instructions/updateVaultType.d.ts +4 -1
  19. package/declarations/instructions/withdrawStakingPool.d.ts +1 -1
  20. package/declarations/instructions/withdrawVault.d.ts +1 -1
  21. package/declarations/state/VaultAccount.d.ts +4 -3
  22. package/declarations/utils/getLinkedListAccounts.d.ts +1 -1
  23. package/lib/Constants.js +3 -2
  24. package/lib/idl/vault.js +109 -47
  25. package/lib/instructions/claimLiquidationPoolPosition.js +5 -1
  26. package/lib/instructions/claimStakingPoolPosition.js +5 -1
  27. package/lib/instructions/closeLiquidationPoolPosition.js +5 -1
  28. package/lib/instructions/createStakingPool.js +5 -2
  29. package/lib/instructions/createVault.js +5 -1
  30. package/lib/instructions/depositLiquidationPool.js +5 -1
  31. package/lib/instructions/depositStakingPool.js +5 -1
  32. package/lib/instructions/depositVault.js +5 -1
  33. package/lib/instructions/initHedgeFoundation.js +5 -1
  34. package/lib/instructions/initHedgeFoundationTokens.js +5 -1
  35. package/lib/instructions/liquidateVault.js +5 -1
  36. package/lib/instructions/loanVault.js +5 -3
  37. package/lib/instructions/redeemVault.js +5 -1
  38. package/lib/instructions/refreshOraclePrice.js +6 -1
  39. package/lib/instructions/repayVault.js +5 -1
  40. package/lib/instructions/setHalted.js +5 -1
  41. package/lib/instructions/updateVaultType.js +9 -2
  42. package/lib/instructions/withdrawStakingPool.js +5 -1
  43. package/lib/instructions/withdrawVault.js +5 -1
  44. package/lib/state/VaultAccount.js +11 -9
  45. package/lib/utils/getLinkedListAccounts.js +48 -48
  46. package/package.json +1 -1
  47. package/src/Constants.ts +2 -1
  48. package/src/idl/vault.ts +218 -94
  49. package/src/instructions/claimLiquidationPoolPosition.ts +3 -2
  50. package/src/instructions/claimStakingPoolPosition.ts +3 -2
  51. package/src/instructions/closeLiquidationPoolPosition.ts +3 -2
  52. package/src/instructions/createStakingPool.ts +3 -3
  53. package/src/instructions/createVault.ts +3 -2
  54. package/src/instructions/depositLiquidationPool.ts +3 -2
  55. package/src/instructions/depositStakingPool.ts +3 -2
  56. package/src/instructions/depositVault.ts +3 -3
  57. package/src/instructions/initHedgeFoundation.ts +3 -2
  58. package/src/instructions/initHedgeFoundationTokens.ts +2 -1
  59. package/src/instructions/liquidateVault.ts +5 -3
  60. package/src/instructions/loanVault.ts +3 -4
  61. package/src/instructions/redeemVault.ts +3 -2
  62. package/src/instructions/refreshOraclePrice.ts +7 -4
  63. package/src/instructions/repayVault.ts +3 -2
  64. package/src/instructions/setHalted.ts +3 -2
  65. package/src/instructions/updateVaultType.ts +10 -2
  66. package/src/instructions/withdrawStakingPool.ts +3 -2
  67. package/src/instructions/withdrawVault.ts +3 -2
  68. package/src/state/VaultAccount.ts +17 -12
  69. package/src/state/VaultType.ts +62 -0
  70. package/src/utils/getLinkedListAccounts.ts +54 -49
  71. package/src/utils/sendAndConfirmWithDebug.ts +27 -0
@@ -6,10 +6,11 @@ import { getVaultSystemStatePublicKey, getVaultTypeOracleAccountPublicKey, HEDGE
6
6
  import { DecimalFromU128 } from '../HedgeDecimal'
7
7
  import { VaultAccount } from '../state/VaultAccount'
8
8
  import Decimal from 'decimal.js'
9
- import { Vault } from 'idl/vault'
9
+ import { Vault } from '../idl/vault'
10
10
 
11
11
  import * as borsh from '@project-serum/borsh'
12
12
  import base58 from 'bs58'
13
+ import VaultType from '../state/VaultType'
13
14
 
14
15
  export async function getLinkedListAccounts(
15
16
  program: Program<Vault>,
@@ -22,8 +23,10 @@ export async function getLinkedListAccounts(
22
23
  liquidate: boolean,
23
24
  cachedVaults?: VaultAccount[]
24
25
  ): Promise<[PublicKey, PublicKey, PublicKey, VaultAccount[]]> {
25
- // console.log('Getting getLinkedListAccounts')
26
- const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
26
+ const vaultTypeRaw = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
27
+ const vaultType = new VaultType(vaultTypeRaw, vaultTypeAccountPublicKey)
28
+
29
+ const DEBUG = false
27
30
 
28
31
  // Default for null is the vault itself, so set them all to this vault
29
32
  let oldSmallerPublicKey = vaultPublicKey
@@ -31,7 +34,7 @@ export async function getLinkedListAccounts(
31
34
  let newLargerPublicKey = vaultPublicKey
32
35
 
33
36
  const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
34
- const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
37
+ // const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
35
38
  const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
36
39
 
37
40
  // Load all the vaults
@@ -39,30 +42,16 @@ export async function getLinkedListAccounts(
39
42
  if (cachedVaults) {
40
43
  vaults = cachedVaults
41
44
  } else {
42
- // let allVaults = cachedVaults
43
- // ? cachedVaults
44
- // : (await program.account.vault
45
- // .all([
46
- // {
47
- // memcmp: { bytes: vaultTypeAccount.collateralType, offset: 8 + 32 + 8 },
48
- // },
49
- // ])
50
- // .catch((error) => {
51
- // console.log('error', error)
52
- // })) || []
53
-
54
- // // Load them into our account objects
55
- // vaults = allVaults.map((vault) => {
56
- // return new VaultAccount(vault.account, vault.publicKey)
57
- // })
58
- vaults = await getMiniVaults(program, vaultTypeAccount.vaultTypeName)
45
+ vaults = await getMiniVaults(program, vaultTypeAccountPublicKey)
59
46
  }
60
47
 
61
- // console.log('Vault count found:', vaults.length)
62
- // console.log('First Vault', vaults[0])
48
+ if (DEBUG) {
49
+ console.log('Vault count found:', vaults.length)
50
+ console.log('First Vault', vaults[0])
51
+ }
63
52
 
64
53
  // Filter out the accounts that are not open
65
- // TODO filter on vault status. Or we enable people to "close out" empty vaults
54
+ // TODO filter on vault status. Or we enable people to "close out" empty vaults
66
55
  // vaults = _.filter(vaults, (vault) => {
67
56
  // return vault.vaultStatus === 'open'
68
57
  // })
@@ -88,11 +77,15 @@ export async function getLinkedListAccounts(
88
77
  oldSmallerPublicKey = vaults[indexBefore - 1].publicKey
89
78
  }
90
79
 
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
- // }))
80
+ if (DEBUG) {
81
+ // Pretty print the list again
82
+ console.log('Sorted open vaults. Index Before: ', indexBefore)
83
+ console.log(
84
+ vaults.map((vault) => {
85
+ return vault.toString(vaultPublicKey)
86
+ })
87
+ )
88
+ }
96
89
 
97
90
  // Pretty print all the vaults before the operation
98
91
  // console.log('Sorted open vaults BEFORE at index:', indexBefore)
@@ -125,9 +118,8 @@ export async function getLinkedListAccounts(
125
118
  // Now that we know it's def in the list, iterate the list and update
126
119
  // this vault with the operation we're going to apply
127
120
  const newNormalizedDebt = new Decimal(loanAmount)
128
- const vaultTypeCompoundedInterest = DecimalFromU128(vaultTypeAccount.cumulativeRate.toString())
129
- vaults[indexBefore].updateDebtAndCollateral(vaultTypeAccount)
130
- vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest)
121
+ vaults[indexBefore].updateDebtAndCollateral(vaultType)
122
+ vaults[indexBefore].addDebt(newNormalizedDebt, vaultType)
131
123
  vaults[indexBefore].addDeposit(depositAmount)
132
124
 
133
125
  if (liquidate) {
@@ -152,16 +144,20 @@ export async function getLinkedListAccounts(
152
144
  }
153
145
  })
154
146
 
155
- // New list with vault
156
- // console.log('New list with vault now at index:', indexAfter)
157
- // console.log(
158
- // vaults.map((vault) => {
159
- // return vault.toString(vaultPublicKey)
160
- // })
161
- // )
147
+ if (DEBUG) {
148
+ // New list with vault
149
+ console.log('New list with vault now at index:', indexAfter)
150
+ console.log(
151
+ vaults.map((vault) => {
152
+ return vault.toString(vaultPublicKey)
153
+ })
154
+ )
155
+ }
162
156
 
163
- // Print where it moved from / to
164
- // console.log('Index After', indexAfter)
157
+ if (DEBUG) {
158
+ // Print where it moved from / to
159
+ console.log('Index After', indexAfter)
160
+ }
165
161
 
166
162
  // Save references to the new left and right
167
163
  if (indexAfter > 0) {
@@ -171,10 +167,12 @@ export async function getLinkedListAccounts(
171
167
  newLargerPublicKey = vaults[indexAfter + 1].publicKey
172
168
  }
173
169
 
174
- // Print out the new left/right
175
- // console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
176
- // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
177
- // console.log('newLargerPublicKey', newLargerPublicKey.toString())
170
+ if (DEBUG) {
171
+ // Print out the new left/right
172
+ console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
173
+ console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
174
+ console.log('newLargerPublicKey', newLargerPublicKey.toString())
175
+ }
178
176
 
179
177
  return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults]
180
178
  }
@@ -190,20 +188,27 @@ function sortVaults(a: VaultAccount, b: VaultAccount) {
190
188
  return aRatio - bRatio
191
189
  }
192
190
 
193
- async function getMiniVaults(program: Program<Vault>, vaultTypeName: string ) {
191
+ async function getMiniVaults(program: Program<Vault>, vaultTypePublicKey: PublicKey) {
194
192
  const filters = [
195
193
  // Filter for Vault Accounts
196
194
  {
197
195
  // @ts-ignore
198
196
  memcmp: program.account.vault.coder.accounts.memcmp(program.account.vault._idlAccount.name),
199
197
  },
200
- // Filter for Vaults with this collateral type
198
+ // Filter for Vaults that are open
201
199
  {
202
200
  memcmp: {
203
- bytes: base58.encode(VaultAccount.getBufferForString(vaultTypeName)),
201
+ bytes: base58.encode(Buffer.from([1])),
204
202
  offset: 8 + 32 + 24,
205
203
  },
206
204
  },
205
+ // Filter for Vaults with this collateral type
206
+ {
207
+ memcmp: {
208
+ bytes: vaultTypePublicKey.toString(),
209
+ offset: 8 + 32 + 24 + 1,
210
+ },
211
+ },
207
212
  ]
208
213
  const allAccounts = await program.provider.connection.getProgramAccounts(HEDGE_PROGRAM_PUBLICKEY, {
209
214
  filters: filters,
@@ -215,7 +220,7 @@ async function getMiniVaults(program: Program<Vault>, vaultTypeName: string ) {
215
220
  },
216
221
  })
217
222
 
218
- return allAccounts.map(vaultData=>{
223
+ return allAccounts.map((vaultData) => {
219
224
  return VaultAccount.FromMiniSlice(vaultData.account.data, vaultData.pubkey)
220
225
  })
221
226
  }
@@ -0,0 +1,27 @@
1
+ import { Connection, Keypair, Signer, Transaction, TransactionSignature } from '@solana/web3.js'
2
+
3
+ export default async function sendAndConfirmWithDebug(
4
+ connection: Connection,
5
+ transaction: Transaction,
6
+ signers: Signer[]
7
+ ): Promise<TransactionSignature | void> {
8
+ return connection
9
+ .sendTransaction(transaction, signers)
10
+ .then((signature) => {
11
+ return connection
12
+ .confirmTransaction(signature)
13
+ .then((signatureContext) => {
14
+ return signature
15
+ })
16
+ .catch((error) => {
17
+ console.log('There was an error confirming the transaction', error)
18
+ console.trace()
19
+ throw error
20
+ })
21
+ })
22
+ .catch((error) => {
23
+ console.log('There was an error sending the transaction', error)
24
+ console.trace()
25
+ throw error
26
+ })
27
+ }