Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix decimal precision throwing #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/Num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(".", ""))
Expand Down
20 changes: 4 additions & 16 deletions src/accounts/margin/Margin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down