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..0f679f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zero_one/client", - "version": "0.11.0-beta.14", + "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(".", "")) 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