Skip to content

Commit

Permalink
Fix anchor.bn truncation, improve invalid asset behaviour and improve…
Browse files Browse the repository at this point in the history
… market refresh robustness (#307)
  • Loading branch information
filipzeta authored Nov 23, 2023
1 parent c4e5ae4 commit 1e4b636
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Version changes are pinned to SDK releases.

## Unreleased

## [1.13.3]

- Fix anchor.BN truncation in risk calcs. ([#307](https://github.com/zetamarkets/sdk/pull/307))
- Replace throw with an undefined return for invalid assets. ([#307](https://github.com/zetamarkets/sdk/pull/307))
- Add check for Exchange.isInitialized when refreshing markets. ([#307](https://github.com/zetamarkets/sdk/pull/307))

## [1.13.2]

- Add pyth asset. ([#305](https://github.com/zetamarkets/sdk/pull/305))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zetamarkets/sdk",
"repository": "https://github.com/zetamarkets/sdk/",
"version": "1.13.2",
"version": "1.13.3",
"description": "Zeta SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 6 additions & 8 deletions src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ export function assetToName(asset: Asset): string | null {
if (asset == Asset.ARB) return "ARB";
if (asset == Asset.BNB) return "BNB";
if (asset == Asset.PYTH) return "PYTH";
if (asset == Asset.UNDEFINED) return "UNDEFINED";
if (asset == null) return null; // Some things, like clock callbacks, are for all assets and return asset=null
throw Error("Invalid asset");
return "UNDEFINED";
}

export function nameToAsset(name: string): Asset {
Expand All @@ -53,8 +52,7 @@ export function nameToAsset(name: string): Asset {
if (name == "ARB") return Asset.ARB;
if (name == "BNB") return Asset.BNB;
if (name == "PYTH") return Asset.PYTH;
if (name == "UNDEFINED") return Asset.UNDEFINED;
throw Error("Invalid asset");
return Asset.UNDEFINED;
}

export function getAssetMint(asset: Asset): PublicKey {
Expand All @@ -69,7 +67,7 @@ export function toProgramAsset(asset: Asset): any {
if (asset == Asset.ARB) return { arb: {} };
if (asset == Asset.BNB) return { bnb: {} };
if (asset == Asset.PYTH) return { pyth: {} };
throw Error("Invalid asset");
return { undefined: {} };
}

export function fromProgramAsset(asset: any): Asset {
Expand All @@ -94,7 +92,7 @@ export function fromProgramAsset(asset: any): Asset {
if (objectEquals(asset, { pyth: {} })) {
return Asset.PYTH;
}
throw Error("Invalid asset");
return Asset.UNDEFINED;
}

export function assetToIndex(asset: Asset): number {
Expand All @@ -121,7 +119,7 @@ export function assetToIndex(asset: Asset): number {
return 6;
}
}
throw new Error("Invalid asset");
return 255; // Undefined is 255 onchain
}

export function indexToAsset(index: number): Asset {
Expand All @@ -148,5 +146,5 @@ export function indexToAsset(index: number): Asset {
return Asset.PYTH;
}
}
throw new Error("Invalid index");
return Asset.UNDEFINED;
}
5 changes: 4 additions & 1 deletion src/risk-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ export function addFakeTradeToAccount(
let priceDiff = entryPrice.sub(
new anchor.BN(convertDecimalToNativeInteger(price, 1))
);

let sizeMul = side == types.Side.BID ? size : -1 * size;

marginAccount.balance = marginAccount.balance.add(
new anchor.BN(side == types.Side.BID ? size : -size).mul(priceDiff)
new anchor.BN(sizeMul * priceDiff.toNumber())
);

editedPosition.costOfTrades = editedPosition.costOfTrades.sub(
Expand Down
4 changes: 4 additions & 0 deletions src/subexchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ export class SubExchange {
* Checks only if the perp serum markets are stale and refreshes it if so
*/
public async updatePerpSerumMarketIfNeeded(epochDelay: number) {
if (!Exchange.isInitialized) {
return;
}

if (Exchange.isHalted(this._asset)) {
return;
}
Expand Down

0 comments on commit 1e4b636

Please sign in to comment.