@haven-fi/solauto-sdk 1.0.73 → 1.0.75
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/dist/clients/solautoMarginfiClient.d.ts.map +1 -1
- package/dist/clients/solautoMarginfiClient.js +5 -0
- package/dist/utils/solauto/generalUtils.d.ts +2 -2
- package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
- package/dist/utils/solauto/generalUtils.js +20 -10
- package/package.json +1 -1
- package/src/clients/solautoMarginfiClient.ts +49 -27
- package/src/utils/solauto/generalUtils.ts +23 -18
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAM1D,OAAO,EACL,kBAAkB,EAElB,aAAa,EACb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,CAAC;AAGtB,OAAO,EAGL,eAAe,EAYhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,iB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kBAAkB,GACvB,kBAAkB;IAMrB,OAAO,CAAC,sBAAsB;IA8C9B,OAAO,IAAI,kBAAkB;IAc7B,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB;IAUhE,OAAO,CAAC,6BAA6B;IAmErC,OAAO,CAAC,oCAAoC;IA0D5C,SAAS,CACP,aAAa,EAAE,GAAG,GAAG,GAAG,EACxB,WAAW,EAAE,cAAc,EAC3B,aAAa,EAAE,wBAAwB,EACvC,SAAS,CAAC,EAAE,gBAAgB,EAC5B,2BAA2B,CAAC,EAAE,MAAM,EACpC,WAAW,CAAC,EAAE,MAAM,GACnB,kBAAkB;IA2DrB,WAAW,CACT,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,SAAS,GACjC,kBAAkB;IA2BrB,UAAU,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,kBAAkB;IAoFlE,iCAAiC,IAAI,kBAAkB;IASjD,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CAiBlE"}
|
|
@@ -13,6 +13,7 @@ const marginfi_sdk_1 = require("../marginfi-sdk");
|
|
|
13
13
|
const marginfiUtils_1 = require("../utils/marginfiUtils");
|
|
14
14
|
const numberUtils_1 = require("../utils/numberUtils");
|
|
15
15
|
const constants_1 = require("../constants");
|
|
16
|
+
const utils_1 = require("../utils");
|
|
16
17
|
class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
17
18
|
constructor() {
|
|
18
19
|
super(...arguments);
|
|
@@ -52,6 +53,10 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
|
52
53
|
// this.debtPriceOracle = toWeb3JsPublicKey(debtBank.config.oracleKeys[0]);
|
|
53
54
|
this.supplyPriceOracle = new web3_js_1.PublicKey(this.marginfiSupplyAccounts.priceOracle);
|
|
54
55
|
this.debtPriceOracle = new web3_js_1.PublicKey(this.marginfiDebtAccounts.priceOracle);
|
|
56
|
+
if (!this.solautoPositionState) {
|
|
57
|
+
const [maxLtv, liqThreshold] = await (0, marginfiUtils_1.getMaxLtvAndLiqThreshold)(this.umi, { mint: this.supplyMint }, { mint: this.debtMint });
|
|
58
|
+
this.solautoPositionState = (0, utils_1.createFakePositionState)({ mint: this.supplyMint }, { mint: this.debtMint }, (0, numberUtils_1.toBps)(maxLtv), (0, numberUtils_1.toBps)(liqThreshold));
|
|
59
|
+
}
|
|
55
60
|
if (!this.initialized) {
|
|
56
61
|
await this.setIntermediaryMarginfiDetails();
|
|
57
62
|
}
|
|
@@ -15,8 +15,8 @@ export declare function getAllPositionsByAuthority(umi: Umi, user: PublicKey): P
|
|
|
15
15
|
export declare function positionStateWithLatestPrices(state: PositionState, supplyPrice?: number, debtPrice?: number): Promise<PositionState>;
|
|
16
16
|
interface AssetProps {
|
|
17
17
|
mint: PublicKey;
|
|
18
|
-
price
|
|
19
|
-
amountUsed
|
|
18
|
+
price?: number;
|
|
19
|
+
amountUsed?: number;
|
|
20
20
|
}
|
|
21
21
|
export declare function createFakePositionState(supply: AssetProps, debt: AssetProps, maxLtvBps: number, liqThresholdBps: number): PositionState;
|
|
22
22
|
type PositionAdjustment = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAA+B,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAElB,aAAa,EAEb,yBAAyB,EACzB,gCAAgC,EAIjC,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAA+B,GAAG,EAAE,MAAM,0B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|
|
|
198
198
|
debtMint: x.debtMint,
|
|
199
199
|
})));
|
|
200
200
|
// TODO support other platforms
|
|
201
|
-
return allPositions
|
|
201
|
+
return allPositions.sort((a, b) => {
|
|
202
|
+
if (a.positionId === 0 && b.positionId !== 0) {
|
|
203
|
+
return 1;
|
|
204
|
+
}
|
|
205
|
+
if (b.positionId === 0 && a.positionId !== 0) {
|
|
206
|
+
return -1;
|
|
207
|
+
}
|
|
208
|
+
return a.positionId - b.positionId;
|
|
209
|
+
});
|
|
202
210
|
}
|
|
203
211
|
async function positionStateWithLatestPrices(state, supplyPrice, debtPrice) {
|
|
204
212
|
if (!supplyPrice || !debtPrice) {
|
|
@@ -237,20 +245,20 @@ async function positionStateWithLatestPrices(state, supplyPrice, debtPrice) {
|
|
|
237
245
|
function createFakePositionState(supply, debt, maxLtvBps, liqThresholdBps) {
|
|
238
246
|
const supplyDecimals = constants_1.TOKEN_INFO[supply.mint.toString()].decimals;
|
|
239
247
|
const debtDecimals = constants_1.TOKEN_INFO[debt.mint.toString()].decimals;
|
|
240
|
-
const supplyUsd = supply.amountUsed * supply.price;
|
|
241
|
-
const debtUsd = debt.amountUsed * debt.price;
|
|
248
|
+
const supplyUsd = (supply.amountUsed ?? 0) * (supply.price ?? 0);
|
|
249
|
+
const debtUsd = (debt.amountUsed ?? 0) * (debt.price ?? 0);
|
|
242
250
|
return {
|
|
243
251
|
liqUtilizationRateBps: (0, numberUtils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, liqThresholdBps),
|
|
244
252
|
supply: {
|
|
245
253
|
amountUsed: {
|
|
246
|
-
baseUnit: (0, numberUtils_1.toBaseUnit)(supply.amountUsed, supplyDecimals),
|
|
254
|
+
baseUnit: (0, numberUtils_1.toBaseUnit)(supply.amountUsed ?? 0, supplyDecimals),
|
|
247
255
|
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(supplyUsd, constants_1.USD_DECIMALS),
|
|
248
256
|
},
|
|
249
257
|
amountCanBeUsed: {
|
|
250
258
|
baseUnit: (0, numberUtils_1.toBaseUnit)(1000000, supplyDecimals),
|
|
251
|
-
baseAmountUsdValue: BigInt(Math.round(1000000 * supply.price)),
|
|
259
|
+
baseAmountUsdValue: BigInt(Math.round(1000000 * (supply.price ?? 0))),
|
|
252
260
|
},
|
|
253
|
-
baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(supply.price, constants_1.USD_DECIMALS),
|
|
261
|
+
baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(supply.price ?? 0, constants_1.USD_DECIMALS),
|
|
254
262
|
borrowFeeBps: 0,
|
|
255
263
|
decimals: supplyDecimals,
|
|
256
264
|
flashLoanFeeBps: 0,
|
|
@@ -261,14 +269,14 @@ function createFakePositionState(supply, debt, maxLtvBps, liqThresholdBps) {
|
|
|
261
269
|
},
|
|
262
270
|
debt: {
|
|
263
271
|
amountUsed: {
|
|
264
|
-
baseUnit: (0, numberUtils_1.toBaseUnit)(debt.amountUsed, debtDecimals),
|
|
272
|
+
baseUnit: (0, numberUtils_1.toBaseUnit)(debt.amountUsed ?? 0, debtDecimals),
|
|
265
273
|
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(debtUsd, constants_1.USD_DECIMALS),
|
|
266
274
|
},
|
|
267
275
|
amountCanBeUsed: {
|
|
268
276
|
baseUnit: (0, numberUtils_1.toBaseUnit)(1000000, debtDecimals),
|
|
269
|
-
baseAmountUsdValue: BigInt(Math.round(1000000 * debt.price)),
|
|
277
|
+
baseAmountUsdValue: BigInt(Math.round(1000000 * (debt.price ?? 0))),
|
|
270
278
|
},
|
|
271
|
-
baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(debt.price, constants_1.USD_DECIMALS),
|
|
279
|
+
baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(debt.price ?? 0, constants_1.USD_DECIMALS),
|
|
272
280
|
borrowFeeBps: 0,
|
|
273
281
|
decimals: debtDecimals,
|
|
274
282
|
flashLoanFeeBps: 0,
|
|
@@ -278,7 +286,9 @@ function createFakePositionState(supply, debt, maxLtvBps, liqThresholdBps) {
|
|
|
278
286
|
padding: new Uint8Array([]),
|
|
279
287
|
},
|
|
280
288
|
netWorth: {
|
|
281
|
-
baseUnit:
|
|
289
|
+
baseUnit: supply.price
|
|
290
|
+
? (0, numberUtils_1.toBaseUnit)((supplyUsd - debtUsd) / supply.price, supplyDecimals)
|
|
291
|
+
: BigInt(0),
|
|
282
292
|
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(supplyUsd - debtUsd, constants_1.USD_DECIMALS),
|
|
283
293
|
},
|
|
284
294
|
maxLtvBps,
|
package/package.json
CHANGED
|
@@ -55,9 +55,11 @@ import {
|
|
|
55
55
|
findMarginfiAccounts,
|
|
56
56
|
getAllMarginfiAccountsByAuthority,
|
|
57
57
|
getMarginfiAccountPositionState,
|
|
58
|
+
getMaxLtvAndLiqThreshold,
|
|
58
59
|
} from "../utils/marginfiUtils";
|
|
59
|
-
import { bytesToI80F48 } from "../utils/numberUtils";
|
|
60
|
+
import { bytesToI80F48, toBps } from "../utils/numberUtils";
|
|
60
61
|
import { SOLAUTO_MANAGER } from "../constants";
|
|
62
|
+
import { createFakePositionState } from "../utils";
|
|
61
63
|
|
|
62
64
|
export interface SolautoMarginfiClientArgs extends SolautoClientArgs {
|
|
63
65
|
marginfiAccount?: PublicKey | Signer;
|
|
@@ -127,9 +129,25 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
|
127
129
|
// this.supplyPriceOracle = toWeb3JsPublicKey(supplyBank.config.oracleKeys[0]);
|
|
128
130
|
// this.debtPriceOracle = toWeb3JsPublicKey(debtBank.config.oracleKeys[0]);
|
|
129
131
|
|
|
130
|
-
this.supplyPriceOracle = new PublicKey(
|
|
132
|
+
this.supplyPriceOracle = new PublicKey(
|
|
133
|
+
this.marginfiSupplyAccounts.priceOracle
|
|
134
|
+
);
|
|
131
135
|
this.debtPriceOracle = new PublicKey(this.marginfiDebtAccounts.priceOracle);
|
|
132
136
|
|
|
137
|
+
if (!this.solautoPositionState) {
|
|
138
|
+
const [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(
|
|
139
|
+
this.umi,
|
|
140
|
+
{ mint: this.supplyMint },
|
|
141
|
+
{ mint: this.debtMint }
|
|
142
|
+
);
|
|
143
|
+
this.solautoPositionState = createFakePositionState(
|
|
144
|
+
{ mint: this.supplyMint },
|
|
145
|
+
{ mint: this.debtMint },
|
|
146
|
+
toBps(maxLtv),
|
|
147
|
+
toBps(liqThreshold)
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
133
151
|
if (!this.initialized) {
|
|
134
152
|
await this.setIntermediaryMarginfiDetails();
|
|
135
153
|
}
|
|
@@ -510,33 +528,37 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
|
510
528
|
let includedFlashLoanToken = false;
|
|
511
529
|
|
|
512
530
|
if (this.intermediaryMarginfiAccount) {
|
|
513
|
-
this.intermediaryMarginfiAccount.lendingAccount.balances.forEach(
|
|
514
|
-
|
|
515
|
-
if (x.
|
|
516
|
-
|
|
531
|
+
this.intermediaryMarginfiAccount.lendingAccount.balances.forEach(
|
|
532
|
+
async (x) => {
|
|
533
|
+
if (x.active) {
|
|
534
|
+
if (x.bankPk === accounts.data.bank) {
|
|
535
|
+
includedFlashLoanToken = true;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// TODO: Don't dynamically pull from bank until Marginfi sorts out their price oracle issues.
|
|
539
|
+
// const bankData = await safeFetchBank(this.umi, publicKey(accounts.data.bank));
|
|
540
|
+
// const priceOracle = bankData!.config.oracleKeys[0];
|
|
541
|
+
const priceOracle = publicKey(
|
|
542
|
+
findMarginfiAccounts(toWeb3JsPublicKey(x.bankPk)).priceOracle
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
remainingAccounts.push(
|
|
546
|
+
...[
|
|
547
|
+
{
|
|
548
|
+
pubkey: x.bankPk,
|
|
549
|
+
isSigner: false,
|
|
550
|
+
isWritable: false,
|
|
551
|
+
},
|
|
552
|
+
{
|
|
553
|
+
pubkey: priceOracle,
|
|
554
|
+
isSigner: false,
|
|
555
|
+
isWritable: false,
|
|
556
|
+
},
|
|
557
|
+
]
|
|
558
|
+
);
|
|
517
559
|
}
|
|
518
|
-
|
|
519
|
-
// TODO: Don't dynamically pull from bank until Marginfi sorts out their price oracle issues.
|
|
520
|
-
// const bankData = await safeFetchBank(this.umi, publicKey(accounts.data.bank));
|
|
521
|
-
// const priceOracle = bankData!.config.oracleKeys[0];
|
|
522
|
-
const priceOracle = publicKey(findMarginfiAccounts(toWeb3JsPublicKey(x.bankPk)).priceOracle);
|
|
523
|
-
|
|
524
|
-
remainingAccounts.push(
|
|
525
|
-
...[
|
|
526
|
-
{
|
|
527
|
-
pubkey: x.bankPk,
|
|
528
|
-
isSigner: false,
|
|
529
|
-
isWritable: false,
|
|
530
|
-
},
|
|
531
|
-
{
|
|
532
|
-
pubkey: priceOracle,
|
|
533
|
-
isSigner: false,
|
|
534
|
-
isWritable: false,
|
|
535
|
-
},
|
|
536
|
-
]
|
|
537
|
-
);
|
|
538
560
|
}
|
|
539
|
-
|
|
561
|
+
);
|
|
540
562
|
}
|
|
541
563
|
if (!this.intermediaryMarginfiAccount || !includedFlashLoanToken) {
|
|
542
564
|
remainingAccounts.push(
|
|
@@ -26,9 +26,7 @@ import {
|
|
|
26
26
|
TOKEN_INFO,
|
|
27
27
|
USD_DECIMALS,
|
|
28
28
|
} from "../../constants";
|
|
29
|
-
import {
|
|
30
|
-
getAllMarginfiAccountsByAuthority,
|
|
31
|
-
} from "../marginfiUtils";
|
|
29
|
+
import { getAllMarginfiAccountsByAuthority } from "../marginfiUtils";
|
|
32
30
|
import { RebalanceAction, SolautoPositionDetails } from "../../types/solauto";
|
|
33
31
|
|
|
34
32
|
function newPeriodsPassed(
|
|
@@ -304,7 +302,15 @@ export async function getAllPositionsByAuthority(
|
|
|
304
302
|
|
|
305
303
|
// TODO support other platforms
|
|
306
304
|
|
|
307
|
-
return allPositions
|
|
305
|
+
return allPositions.sort((a, b) => {
|
|
306
|
+
if (a.positionId === 0 && b.positionId !== 0) {
|
|
307
|
+
return 1;
|
|
308
|
+
}
|
|
309
|
+
if (b.positionId === 0 && a.positionId !== 0) {
|
|
310
|
+
return -1;
|
|
311
|
+
}
|
|
312
|
+
return a.positionId - b.positionId;
|
|
313
|
+
});
|
|
308
314
|
}
|
|
309
315
|
|
|
310
316
|
export async function positionStateWithLatestPrices(
|
|
@@ -358,8 +364,8 @@ export async function positionStateWithLatestPrices(
|
|
|
358
364
|
|
|
359
365
|
interface AssetProps {
|
|
360
366
|
mint: PublicKey;
|
|
361
|
-
price
|
|
362
|
-
amountUsed
|
|
367
|
+
price?: number;
|
|
368
|
+
amountUsed?: number;
|
|
363
369
|
}
|
|
364
370
|
|
|
365
371
|
export function createFakePositionState(
|
|
@@ -371,8 +377,8 @@ export function createFakePositionState(
|
|
|
371
377
|
const supplyDecimals = TOKEN_INFO[supply.mint.toString()].decimals;
|
|
372
378
|
const debtDecimals = TOKEN_INFO[debt.mint.toString()].decimals;
|
|
373
379
|
|
|
374
|
-
const supplyUsd = supply.amountUsed * supply.price;
|
|
375
|
-
const debtUsd = debt.amountUsed * debt.price;
|
|
380
|
+
const supplyUsd = (supply.amountUsed ?? 0) * (supply.price ?? 0);
|
|
381
|
+
const debtUsd = (debt.amountUsed ?? 0) * (debt.price ?? 0);
|
|
376
382
|
|
|
377
383
|
return {
|
|
378
384
|
liqUtilizationRateBps: getLiqUtilzationRateBps(
|
|
@@ -382,14 +388,14 @@ export function createFakePositionState(
|
|
|
382
388
|
),
|
|
383
389
|
supply: {
|
|
384
390
|
amountUsed: {
|
|
385
|
-
baseUnit: toBaseUnit(supply.amountUsed, supplyDecimals),
|
|
391
|
+
baseUnit: toBaseUnit(supply.amountUsed ?? 0, supplyDecimals),
|
|
386
392
|
baseAmountUsdValue: toBaseUnit(supplyUsd, USD_DECIMALS),
|
|
387
393
|
},
|
|
388
394
|
amountCanBeUsed: {
|
|
389
395
|
baseUnit: toBaseUnit(1000000, supplyDecimals),
|
|
390
|
-
baseAmountUsdValue: BigInt(Math.round(1000000 * supply.price)),
|
|
396
|
+
baseAmountUsdValue: BigInt(Math.round(1000000 * (supply.price ?? 0))),
|
|
391
397
|
},
|
|
392
|
-
baseAmountMarketPriceUsd: toBaseUnit(supply.price, USD_DECIMALS),
|
|
398
|
+
baseAmountMarketPriceUsd: toBaseUnit(supply.price ?? 0, USD_DECIMALS),
|
|
393
399
|
borrowFeeBps: 0,
|
|
394
400
|
decimals: supplyDecimals,
|
|
395
401
|
flashLoanFeeBps: 0,
|
|
@@ -400,14 +406,14 @@ export function createFakePositionState(
|
|
|
400
406
|
},
|
|
401
407
|
debt: {
|
|
402
408
|
amountUsed: {
|
|
403
|
-
baseUnit: toBaseUnit(debt.amountUsed, debtDecimals),
|
|
409
|
+
baseUnit: toBaseUnit(debt.amountUsed ?? 0, debtDecimals),
|
|
404
410
|
baseAmountUsdValue: toBaseUnit(debtUsd, USD_DECIMALS),
|
|
405
411
|
},
|
|
406
412
|
amountCanBeUsed: {
|
|
407
413
|
baseUnit: toBaseUnit(1000000, debtDecimals),
|
|
408
|
-
baseAmountUsdValue: BigInt(Math.round(1000000 * debt.price)),
|
|
414
|
+
baseAmountUsdValue: BigInt(Math.round(1000000 * (debt.price ?? 0))),
|
|
409
415
|
},
|
|
410
|
-
baseAmountMarketPriceUsd: toBaseUnit(debt.price, USD_DECIMALS),
|
|
416
|
+
baseAmountMarketPriceUsd: toBaseUnit(debt.price ?? 0, USD_DECIMALS),
|
|
411
417
|
borrowFeeBps: 0,
|
|
412
418
|
decimals: debtDecimals,
|
|
413
419
|
flashLoanFeeBps: 0,
|
|
@@ -417,10 +423,9 @@ export function createFakePositionState(
|
|
|
417
423
|
padding: new Uint8Array([]),
|
|
418
424
|
},
|
|
419
425
|
netWorth: {
|
|
420
|
-
baseUnit:
|
|
421
|
-
(supplyUsd - debtUsd) / supply.price,
|
|
422
|
-
|
|
423
|
-
),
|
|
426
|
+
baseUnit: supply.price
|
|
427
|
+
? toBaseUnit((supplyUsd - debtUsd) / supply.price, supplyDecimals)
|
|
428
|
+
: BigInt(0),
|
|
424
429
|
baseAmountUsdValue: toBaseUnit(supplyUsd - debtUsd, USD_DECIMALS),
|
|
425
430
|
},
|
|
426
431
|
maxLtvBps,
|