From d765423b6dd3bc41f202485950c8ee757c7d8064 Mon Sep 17 00:00:00 2001 From: Stratos Date: Tue, 30 Aug 2022 14:35:29 -0700 Subject: [PATCH 1/2] include pnl in margin --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/accounts/margin/Margin.ts | 20 ++++---------------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7538ba0..1295d66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - New `SpecialOrders` classes and methods that enable stop loss and take profit trigger orders. +## [0.11.1] - 2022-08-30 + +- MarginWeb3: Now includes account's unrealized pnl in margin calculations. + ## [0.10.0] - 2022-08-03 - BREAKING: Margin's `withdraw` methods now take and additional `heimdall` account. Previous build's withdraw methods will fail. diff --git a/package.json b/package.json index 62d9c39..e4400d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zero_one/client", - "version": "0.11.0-beta.14", + "version": "0.11.1", "license": "Apache-2.0", "description": "TypeScript Client API", "main": "dist/cjs/index.js", diff --git a/src/accounts/margin/Margin.ts b/src/accounts/margin/Margin.ts index 268c323..4bbff20 100644 --- a/src/accounts/margin/Margin.ts +++ b/src/accounts/margin/Margin.ts @@ -145,10 +145,7 @@ export default abstract class Margin extends MarginWeb3 { if (this.totalOpenPositionNotional.toNumber() == 0) { return new Decimal(1) } - return Decimal.min( - this.weightedAccountValue, - this.weightedCollateralValue, - ).div(this.totalOpenPositionNotional) + return this.weightedAccountValue.div(this.totalOpenPositionNotional) } /** @@ -337,10 +334,7 @@ export default abstract class Margin extends MarginWeb3 { * Collateral of the free value */ get freeCollateralValue() { - const freeCollateral = Decimal.min( - this.weightedAccountValue, - this.weightedCollateralValue, - ).minus(this.tiedCollateral) + const freeCollateral = this.weightedAccountValue.minus(this.tiedCollateral) return Decimal.max(new Decimal(0), freeCollateral) } @@ -526,10 +520,7 @@ export default abstract class Margin extends MarginWeb3 { const imfBase = (1.1 * 1000) / assetBorrowed.weight - 1 const [initialMarginTotalWeighted, _] = this.initialMarginInfo(null) const numerator = initialMarginTotalWeighted.minus( - Decimal.min( - this.weightedAccountValue, - this.weightedCollateralValue, - ), + this.weightedAccountValue, ) const denominator = assetBorrowed.indexPrice.decimal.mul(imfBase).mul( @@ -899,10 +890,7 @@ export default abstract class Margin extends MarginWeb3 { : 1 - TAKER_TRADE_FEE - VALUE_NERF if (this.totalOpenPositionNotional.toNumber() === 0) { - return Decimal.min( - this.weightedAccountValue, - this.weightedCollateralValue, - ) + return this.weightedAccountValue .div(marketInfo.baseImf) .mul( trade.postOrder From 785e4a6a90f791ba75eafbb276568d9734dc9a31 Mon Sep 17 00:00:00 2001 From: Sheheryar Parvaz Date: Mon, 5 Sep 2022 06:36:09 -0400 Subject: [PATCH 2/2] hotfix decimal.js string repr in Num throwing --- package.json | 2 +- src/Num.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e4400d8..0f679f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zero_one/client", - "version": "0.11.1", + "version": "0.11.2-beta.1", "license": "Apache-2.0", "description": "TypeScript Client API", "main": "dist/cjs/index.js", diff --git a/src/Num.ts b/src/Num.ts index 48e1642..bbaa65e 100644 --- a/src/Num.ts +++ b/src/Num.ts @@ -57,6 +57,8 @@ export default class Num { } static fromWI80F48(data: { data: BN }, decimals: number) { + Decimal.set({ toExpPos: 100, toExpNeg: -100 }) + const decimal = loadWI80F48(data) const precisionDecimals = decimal.decimalPlaces() const ogDecimal = new BN(decimal.toString().replace(".", ""))