From a94f6ecfc79e74ad34b13e08b1e7407e0841fa92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B8b=C4=97rt=C3=B8?= <106074508+EchoDex@users.noreply.github.com> Date: Mon, 2 Sep 2024 13:08:44 +0400 Subject: [PATCH] fix: error with orderbook --- package.json | 2 +- pnpm-lock.yaml | 10 +++---- src/entity/SpotMarketOrder.ts | 27 ++++++++++++++++--- .../BottomTables/SpotTable/SpotTableVM.tsx | 2 +- .../SpotOrderBook/SpotOrderBook.tsx | 4 +-- src/utils/groupOrders.ts | 5 ++-- src/utils/handleWalletErrors.ts | 3 ++- 7 files changed, 36 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 689c0c15..5c2bd27e 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "pnpm": ">=9.7.0" }, "dependencies": { - "@compolabs/spark-orderbook-ts-sdk": "^1.6.5", + "@compolabs/spark-orderbook-ts-sdk": "^1.6.6", "@emotion/react": "^11.11.3", "@emotion/styled": "^11.11.0", "@fuels/connectors": "^0.9.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 27458cc5..f40b2b69 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@compolabs/spark-orderbook-ts-sdk': - specifier: ^1.6.5 - version: 1.6.5(@types/react@18.3.4)(fuels@0.93.0)(graphql@16.9.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + specifier: ^1.6.6 + version: 1.6.6(@types/react@18.3.4)(fuels@0.93.0)(graphql@16.9.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@emotion/react': specifier: ^11.11.3 version: 11.13.3(@types/react@18.3.4)(react@18.3.1) @@ -982,8 +982,8 @@ packages: '@coinbase/wallet-sdk@4.0.4': resolution: {integrity: sha512-74c040CRnGhfRjr3ArnkAgud86erIqdkPHNt5HR1k9u97uTIZCJww9eGYT67Qf7gHPpGS/xW8Be1D4dvRm63FA==} - '@compolabs/spark-orderbook-ts-sdk@1.6.5': - resolution: {integrity: sha512-e7WyyQFGeOENBiCJJaaU8k7VAe7lTBu9czv71MRFBy9BQi8iNc9lLVNITU6K4K/BxyvhXPucsc5eRG1FmbKxjw==} + '@compolabs/spark-orderbook-ts-sdk@1.6.6': + resolution: {integrity: sha512-CeV/TKXDdtvl8c/pVLhEdZ9z0AafWIDGQoXaRkTTO+pTU13BTqQBAC6pbtAGof7TmeAlicXoJs1TWQUSGOPfTw==} engines: {node: '>=18'} peerDependencies: fuels: '>=0.93.0' @@ -8247,7 +8247,7 @@ snapshots: preact: 10.23.2 sha.js: 2.4.11 - '@compolabs/spark-orderbook-ts-sdk@1.6.5(@types/react@18.3.4)(fuels@0.93.0)(graphql@16.9.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + '@compolabs/spark-orderbook-ts-sdk@1.6.6(@types/react@18.3.4)(fuels@0.93.0)(graphql@16.9.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: '@apollo/client': 3.11.5(@types/react@18.3.4)(graphql-ws@5.16.0(graphql@16.9.0))(graphql@16.9.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) bignumber.js: 9.1.2 diff --git a/src/entity/SpotMarketOrder.ts b/src/entity/SpotMarketOrder.ts index 1f480146..9180588f 100644 --- a/src/entity/SpotMarketOrder.ts +++ b/src/entity/SpotMarketOrder.ts @@ -29,6 +29,9 @@ export class SpotMarketOrder { initialQuoteAmount: BN; currentQuoteAmount: BN; + filledAmount: BN; + filledQuoteAmount: BN; + constructor(order: SpotMarketOrderParams) { const bcNetwork = FuelNetwork.getInstance(); @@ -49,6 +52,9 @@ export class SpotMarketOrder { this.currentAmount = new BN(order.amount); this.currentQuoteAmount = this.getQuoteAmount(this.currentAmount, this.price); + this.filledAmount = this.getFilledAmount(this.initialAmount, this.currentAmount); + this.filledQuoteAmount = this.getQuoteAmount(this.filledAmount, this.price); + this.timestamp = dayjs(order.timestamp); } @@ -68,6 +74,10 @@ export class SpotMarketOrder { return BN.formatUnits(this.currentAmount, this.baseToken.decimals); } + get filledAmountUnits(): BN { + return BN.formatUnits(this.filledAmount, this.baseToken.decimals); + } + get initialQuoteAmountUnits(): BN { return BN.formatUnits(this.initialQuoteAmount, this.quoteToken.decimals); } @@ -76,6 +86,10 @@ export class SpotMarketOrder { return BN.formatUnits(this.currentQuoteAmount, this.quoteToken.decimals); } + get filledQuoteAmountUnits(): BN { + return BN.formatUnits(this.filledQuoteAmount, this.quoteToken.decimals); + } + get formatPrice() { return this.priceUnits.toSignificant(2); } @@ -89,20 +103,21 @@ export class SpotMarketOrder { } get formatFilledAmount() { - return this.initialAmount - .minus(this.currentAmount) - .dividedBy(Math.pow(10, this.baseToken.decimals)) - .toSignificant(this.baseToken.decimals - 4); + return this.filledAmountUnits.toSignificant(2); } addInitialAmount = (amount: BN) => { this.initialAmount = this.initialAmount.plus(amount); this.initialQuoteAmount = this.getQuoteAmount(this.initialAmount, this.price); + + this.filledAmount = this.getFilledAmount(this.initialAmount, this.currentAmount); }; addCurrentAmount = (amount: BN) => { this.currentAmount = this.currentAmount.plus(amount); this.currentQuoteAmount = this.getQuoteAmount(this.currentAmount, this.price); + + this.filledAmount = this.getFilledAmount(this.initialAmount, this.currentAmount); }; private getQuoteAmount = (amount: BN, price: BN) => { @@ -117,6 +132,10 @@ export class SpotMarketOrder { return new BN(result); }; + private getFilledAmount = (initialAmount: BN, currentAmount: BN) => { + return initialAmount.minus(currentAmount); + }; + debug = () => { return { initialAmount: this.initialAmount.toString(), diff --git a/src/screens/TradeScreen/BottomTables/SpotTable/SpotTableVM.tsx b/src/screens/TradeScreen/BottomTables/SpotTable/SpotTableVM.tsx index cddeaea7..5cc81ef6 100644 --- a/src/screens/TradeScreen/BottomTables/SpotTable/SpotTableVM.tsx +++ b/src/screens/TradeScreen/BottomTables/SpotTable/SpotTableVM.tsx @@ -43,7 +43,7 @@ class SpotTableVM { constructor(rootStore: RootStore) { makeAutoObservable(this); this.rootStore = rootStore; - const { accountStore, tradeStore, balanceStore } = this.rootStore; + const { accountStore, tradeStore } = this.rootStore; reaction( () => [tradeStore.market, this.rootStore.initialized, accountStore.isConnected], diff --git a/src/screens/TradeScreen/OrderbookAndTradesInterface/SpotOrderBook/SpotOrderBook.tsx b/src/screens/TradeScreen/OrderbookAndTradesInterface/SpotOrderBook/SpotOrderBook.tsx index 4a2994dd..81aa3f53 100644 --- a/src/screens/TradeScreen/OrderbookAndTradesInterface/SpotOrderBook/SpotOrderBook.tsx +++ b/src/screens/TradeScreen/OrderbookAndTradesInterface/SpotOrderBook/SpotOrderBook.tsx @@ -129,9 +129,9 @@ export const SpotOrderBook: React.FC = observer(() => { return orders.map((o, index) => ( orderSpotVm.selectOrderbookOrder(o, orderMode)}> - {o.currentAmountUnits.toFormat(3)} + {o.currentAmountUnits.toFormat(4)} {o.priceUnits.toFormat(vm.decimalGroup)} - {numeral(o.initialQuoteAmountUnits).format(`0.${"0".repeat(vm.decimalGroup)}a`)} + {numeral(o.currentQuoteAmountUnits).format(`0.${"0".repeat(vm.decimalGroup)}a`)} )); }; diff --git a/src/utils/groupOrders.ts b/src/utils/groupOrders.ts index 911e373a..0896b43b 100644 --- a/src/utils/groupOrders.ts +++ b/src/utils/groupOrders.ts @@ -2,7 +2,6 @@ import { DEFAULT_DECIMALS } from "@src/constants"; import { SpotMarketOrder } from "@src/entity"; import BN from "./BN"; -import { CONFIG } from "./getConfig"; const roundPrice = (price: BN, decimals: number): BN => { const factor = new BN(10).pow(decimals); @@ -26,13 +25,13 @@ export const groupOrders = (orders: SpotMarketOrder[], decimals: number): SpotMa initial_amount: BN.ZERO.toString(), order_type: order.orderType, asset: order.baseToken.assetId, - quoteAssetId: CONFIG.TOKENS_BY_SYMBOL.USDC.assetId, + quoteAssetId: order.quoteToken.assetId, timestamp: order.timestamp.toString(), }); } groupedOrders[price].addInitialAmount(order.initialAmount); - groupedOrders[price].addCurrentAmount(order.initialAmount); + groupedOrders[price].addCurrentAmount(order.currentAmount); }); return Object.values(groupedOrders); diff --git a/src/utils/handleWalletErrors.ts b/src/utils/handleWalletErrors.ts index 6fff5bbd..7971562c 100644 --- a/src/utils/handleWalletErrors.ts +++ b/src/utils/handleWalletErrors.ts @@ -26,7 +26,8 @@ export const handleWalletErrors = ( let extendedErrorText; try { - extendedErrorText = getHumanReadableError(error.metadata.logs[0]); + extendedErrorText = getHumanReadableError(error.metadata.logs); + console.error("Detail info: ", error.metadata.logs); } catch (error) { console.error("Failed to parse error: ", error); }