hedge-web3 0.1.38 → 0.1.39

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,6 +21,8 @@ export declare class VaultAccount {
21
21
  collateralType: string;
22
22
  /** Current State of the vautl ("Open", "Closed", "Liquidated") */
23
23
  vaultStatus: string;
24
+ /** The public key of the next vault to redeem. */
25
+ nextVaultToRedeem: PublicKey;
24
26
  constructor(vault: any, publicKey: PublicKey);
25
27
  /**
26
28
  * Check if some `PublicKey` is the owner
@@ -21,6 +21,7 @@ class VaultAccount {
21
21
  this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
22
22
  this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
23
23
  this.collateralType = vault.collateralType;
24
+ this.nextVaultToRedeem = vault.nextVaultToRedeem;
24
25
  this.vaultStatus = Object.keys(vault.vaultStatus)[0];
25
26
  }
26
27
  /**
@@ -60,7 +61,8 @@ class VaultAccount {
60
61
  }
61
62
  addDebt(newNormalizedDebt, vaultTypeCompoundedInterest) {
62
63
  const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest);
63
- this.denormalizedDebt += denormalizedNewDebt.toNumber();
64
+ this.denormalizedDebt = denormalizedNewDebt.add(new decimal_js_1.default(this.denormalizedDebt)).floor().toNumber();
65
+ // this.denormalizedDebt = parseFloat(this.denormalizedDebt.toFixed(0))
64
66
  }
65
67
  addDeposit(depositAmount) {
66
68
  this.deposited += depositAmount;
@@ -81,7 +83,6 @@ class VaultAccount {
81
83
  this.denormalizedDebt = debtProductCurrent
82
84
  .div(this.debtProductSnapshotBytes)
83
85
  .mul(new decimal_js_1.default(this.denormalizedDebt))
84
- // .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
85
86
  .toNumber();
86
87
  const extraCollateralDeposited = this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber();
87
88
  this.deposited += extraCollateralDeposited;
@@ -91,15 +92,19 @@ class VaultAccount {
91
92
  toString(highlight) {
92
93
  let arrow = '';
93
94
  if (this.publicKey.toString() === highlight.toString()) {
94
- arrow = ' <----';
95
+ arrow = ' <----!!';
95
96
  }
96
97
  let collateralRatio = 'Infinite';
97
98
  if (this.denormalizedDebt > 0) {
98
- collateralRatio = (this.deposited / this.denormalizedDebt).toFixed(8);
99
+ collateralRatio = new decimal_js_1.default(this.deposited).div(new decimal_js_1.default(this.denormalizedDebt)).toString();
100
+ }
101
+ let nextVault = 'None';
102
+ if (this.nextVaultToRedeem) {
103
+ nextVault = this.nextVaultToRedeem.toString().substring(0, 6);
99
104
  }
100
105
  return `Vault(${this.vaultNumber}): ${this.publicKey
101
106
  .toString()
102
- .substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`;
107
+ .substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `;
103
108
  }
104
109
  }
105
110
  exports.VaultAccount = VaultAccount;
@@ -70,12 +70,26 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
70
70
  oldSmallerPublicKey = vaults[indexBefore - 1].publicKey;
71
71
  }
72
72
  // Pretty print all the vaults before the operation
73
- console.log('Sorted open vaults before');
74
- vaults.forEach((vault) => {
75
- console.log(vault.toString(vaultPublicKey));
76
- });
73
+ console.log('Sorted open vaults BEFORE at index:', indexBefore);
74
+ let correctOrderBefore = true;
75
+ for (let i = 0; i < vaults.length; i++) {
76
+ if (i < vaults.length - 1) {
77
+ // Verify i points to i+1
78
+ if (vaults[i].nextVaultToRedeem.toString() !== vaults[i + 1].publicKey.toString()) {
79
+ correctOrderBefore = false;
80
+ console.log("A vault was found OUT OF ORDER!!", vaults[i]);
81
+ }
82
+ }
83
+ }
84
+ if (correctOrderBefore) {
85
+ console.log(`Verfied the on-chain order of `, correctOrderBefore);
86
+ }
87
+ else {
88
+ throw new Error("On-Chian vaults not in order!");
89
+ }
77
90
  // If it wasn't in the list, add it now
78
91
  if (indexBefore < 0) {
92
+ // console.log('Was not in the list before. Adding now.')
79
93
  vaults.push(thisVault);
80
94
  indexBefore = vaults.length - 1;
81
95
  }
@@ -98,10 +112,10 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
98
112
  // Sort it again since we've changed one vault
99
113
  vaults = vaults.sort(sortVaults);
100
114
  // Pretty print the list again
101
- console.log('Sorted open vaults with new debt added');
102
- vaults.forEach((vault) => {
103
- console.log(vault.toString(vaultPublicKey));
104
- });
115
+ // console.log('Sorted open vaults with new debt added at index: ', indexAfter)
116
+ // console.log(vaults.map((vault) => {
117
+ // return vault.toString(vaultPublicKey)
118
+ // }))
105
119
  // Search for the vaults new position
106
120
  let indexAfter = -1;
107
121
  vaults.forEach((vault, index) => {
@@ -109,9 +123,16 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
109
123
  indexAfter = index;
110
124
  }
111
125
  });
126
+ // New list with vault
127
+ // console.log('New list with vault now at index:', indexAfter)
128
+ // console.log(
129
+ // vaults.map((vault) => {
130
+ // return vault.toString(vaultPublicKey)
131
+ // })
132
+ // )
112
133
  // Print where it moved from / to
113
- console.log('Index Before', indexBefore);
114
- console.log('Index After', indexAfter);
134
+ // console.log('Index Before', indexBefore)
135
+ // console.log('Index After', indexAfter)
115
136
  // Save references to the new left and right
116
137
  if (indexAfter > 0) {
117
138
  newSmallerPublicKey = vaults[indexAfter - 1].publicKey;
@@ -120,9 +141,9 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
120
141
  newLargerPublicKey = vaults[indexAfter + 1].publicKey;
121
142
  }
122
143
  // Print out the new left/right
123
- console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString());
124
- console.log('newSmallerPublicKey', newSmallerPublicKey.toString());
125
- console.log('newLargerPublicKey', newLargerPublicKey.toString());
144
+ // console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
145
+ // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
146
+ // console.log('newLargerPublicKey', newLargerPublicKey.toString())
126
147
  return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults];
127
148
  });
128
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.1.38",
3
+ "version": "0.1.39",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "types": "declarations/index.d.ts",
@@ -33,6 +33,9 @@ export class VaultAccount {
33
33
  /** Current State of the vautl ("Open", "Closed", "Liquidated") */
34
34
  vaultStatus: string
35
35
 
36
+ /** The public key of the next vault to redeem. */
37
+ nextVaultToRedeem: PublicKey
38
+
36
39
  constructor(vault: any, publicKey: PublicKey) {
37
40
  this.publicKey = publicKey
38
41
  this.vaultOwner = vault.vaultOwner
@@ -43,6 +46,7 @@ export class VaultAccount {
43
46
  this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
44
47
  this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
45
48
  this.collateralType = vault.collateralType
49
+ this.nextVaultToRedeem = vault.nextVaultToRedeem
46
50
 
47
51
  this.vaultStatus = Object.keys(vault.vaultStatus)[0]
48
52
  }
@@ -88,7 +92,8 @@ export class VaultAccount {
88
92
 
89
93
  public addDebt(newNormalizedDebt: Decimal, vaultTypeCompoundedInterest: Decimal) {
90
94
  const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest)
91
- this.denormalizedDebt += denormalizedNewDebt.toNumber()
95
+ this.denormalizedDebt = denormalizedNewDebt.add(new Decimal(this.denormalizedDebt)).floor().toNumber()
96
+ // this.denormalizedDebt = parseFloat(this.denormalizedDebt.toFixed(0))
92
97
  }
93
98
  public addDeposit(depositAmount: number) {
94
99
  this.deposited += depositAmount
@@ -113,9 +118,8 @@ export class VaultAccount {
113
118
  this.denormalizedDebt = debtProductCurrent
114
119
  .div(this.debtProductSnapshotBytes)
115
120
  .mul(new Decimal(this.denormalizedDebt))
116
- // .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
117
121
  .toNumber()
118
-
122
+
119
123
  const extraCollateralDeposited =
120
124
  this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
121
125
  this.deposited += extraCollateralDeposited
@@ -127,14 +131,20 @@ export class VaultAccount {
127
131
  public toString(highlight: PublicKey): string {
128
132
  let arrow = ''
129
133
  if (this.publicKey.toString() === highlight.toString()) {
130
- arrow = ' <----'
134
+ arrow = ' <----!!'
131
135
  }
132
136
  let collateralRatio = 'Infinite'
133
137
  if (this.denormalizedDebt > 0) {
134
- collateralRatio = (this.deposited / this.denormalizedDebt).toFixed(8)
138
+ collateralRatio = new Decimal(this.deposited).div(new Decimal(this.denormalizedDebt)).toString()
135
139
  }
140
+
141
+ let nextVault = 'None'
142
+ if (this.nextVaultToRedeem) {
143
+ nextVault = this.nextVaultToRedeem.toString().substring(0, 6)
144
+ }
145
+
136
146
  return `Vault(${this.vaultNumber}): ${this.publicKey
137
147
  .toString()
138
- .substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
148
+ .substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `
139
149
  }
140
150
  }
@@ -78,13 +78,26 @@ export async function getLinkedListAccounts(
78
78
  }
79
79
 
80
80
  // Pretty print all the vaults before the operation
81
- console.log('Sorted open vaults before')
82
- vaults.forEach((vault) => {
83
- console.log(vault.toString(vaultPublicKey))
84
- })
81
+ console.log('Sorted open vaults BEFORE at index:', indexBefore)
82
+ let correctOrderBefore = true
83
+ for (let i = 0; i < vaults.length; i++) {
84
+ if (i < vaults.length - 1) {
85
+ // Verify i points to i+1
86
+ if (vaults[i].nextVaultToRedeem.toString() !== vaults[i+1].publicKey.toString()){
87
+ correctOrderBefore = false
88
+ console.log("A vault was found OUT OF ORDER!!", vaults[i])
89
+ }
90
+ }
91
+ }
92
+ if (correctOrderBefore){
93
+ console.log(`Verfied the on-chain order of `, correctOrderBefore)
94
+ } else {
95
+ throw new Error("On-Chian vaults not in order!");
96
+ }
85
97
 
86
98
  // If it wasn't in the list, add it now
87
99
  if (indexBefore < 0) {
100
+ // console.log('Was not in the list before. Adding now.')
88
101
  vaults.push(thisVault)
89
102
  indexBefore = vaults.length - 1
90
103
  }
@@ -112,10 +125,10 @@ export async function getLinkedListAccounts(
112
125
  vaults = vaults.sort(sortVaults)
113
126
 
114
127
  // Pretty print the list again
115
- console.log('Sorted open vaults with new debt added')
116
- vaults.forEach((vault) => {
117
- console.log(vault.toString(vaultPublicKey))
118
- })
128
+ // console.log('Sorted open vaults with new debt added at index: ', indexAfter)
129
+ // console.log(vaults.map((vault) => {
130
+ // return vault.toString(vaultPublicKey)
131
+ // }))
119
132
 
120
133
  // Search for the vaults new position
121
134
  let indexAfter = -1
@@ -125,9 +138,17 @@ export async function getLinkedListAccounts(
125
138
  }
126
139
  })
127
140
 
141
+ // New list with vault
142
+ // console.log('New list with vault now at index:', indexAfter)
143
+ // console.log(
144
+ // vaults.map((vault) => {
145
+ // return vault.toString(vaultPublicKey)
146
+ // })
147
+ // )
148
+
128
149
  // Print where it moved from / to
129
- console.log('Index Before', indexBefore)
130
- console.log('Index After', indexAfter)
150
+ // console.log('Index Before', indexBefore)
151
+ // console.log('Index After', indexAfter)
131
152
 
132
153
  // Save references to the new left and right
133
154
  if (indexAfter > 0) {
@@ -138,9 +159,9 @@ export async function getLinkedListAccounts(
138
159
  }
139
160
 
140
161
  // Print out the new left/right
141
- console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
142
- console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
143
- console.log('newLargerPublicKey', newLargerPublicKey.toString())
162
+ // console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
163
+ // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
164
+ // console.log('newLargerPublicKey', newLargerPublicKey.toString())
144
165
 
145
166
  return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults]
146
167
  }