Skip to content

Commit 97fda32

Browse files
authored
Merge pull request #131 from compolabs/feat/1578-orderbook
[1578] Bug with orderbook
2 parents 384f968 + a94f6ec commit 97fda32

File tree

7 files changed

+36
-17
lines changed

7 files changed

+36
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"pnpm": ">=9.7.0"
1616
},
1717
"dependencies": {
18-
"@compolabs/spark-orderbook-ts-sdk": "^1.6.5",
18+
"@compolabs/spark-orderbook-ts-sdk": "^1.6.6",
1919
"@emotion/react": "^11.11.3",
2020
"@emotion/styled": "^11.11.0",
2121
"@fuels/connectors": "^0.9.1",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/entity/SpotMarketOrder.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class SpotMarketOrder {
2929
initialQuoteAmount: BN;
3030
currentQuoteAmount: BN;
3131

32+
filledAmount: BN;
33+
filledQuoteAmount: BN;
34+
3235
constructor(order: SpotMarketOrderParams) {
3336
const bcNetwork = FuelNetwork.getInstance();
3437

@@ -49,6 +52,9 @@ export class SpotMarketOrder {
4952
this.currentAmount = new BN(order.amount);
5053
this.currentQuoteAmount = this.getQuoteAmount(this.currentAmount, this.price);
5154

55+
this.filledAmount = this.getFilledAmount(this.initialAmount, this.currentAmount);
56+
this.filledQuoteAmount = this.getQuoteAmount(this.filledAmount, this.price);
57+
5258
this.timestamp = dayjs(order.timestamp);
5359
}
5460

@@ -68,6 +74,10 @@ export class SpotMarketOrder {
6874
return BN.formatUnits(this.currentAmount, this.baseToken.decimals);
6975
}
7076

77+
get filledAmountUnits(): BN {
78+
return BN.formatUnits(this.filledAmount, this.baseToken.decimals);
79+
}
80+
7181
get initialQuoteAmountUnits(): BN {
7282
return BN.formatUnits(this.initialQuoteAmount, this.quoteToken.decimals);
7383
}
@@ -76,6 +86,10 @@ export class SpotMarketOrder {
7686
return BN.formatUnits(this.currentQuoteAmount, this.quoteToken.decimals);
7787
}
7888

89+
get filledQuoteAmountUnits(): BN {
90+
return BN.formatUnits(this.filledQuoteAmount, this.quoteToken.decimals);
91+
}
92+
7993
get formatPrice() {
8094
return this.priceUnits.toSignificant(2);
8195
}
@@ -89,20 +103,21 @@ export class SpotMarketOrder {
89103
}
90104

91105
get formatFilledAmount() {
92-
return this.initialAmount
93-
.minus(this.currentAmount)
94-
.dividedBy(Math.pow(10, this.baseToken.decimals))
95-
.toSignificant(this.baseToken.decimals - 4);
106+
return this.filledAmountUnits.toSignificant(2);
96107
}
97108

98109
addInitialAmount = (amount: BN) => {
99110
this.initialAmount = this.initialAmount.plus(amount);
100111
this.initialQuoteAmount = this.getQuoteAmount(this.initialAmount, this.price);
112+
113+
this.filledAmount = this.getFilledAmount(this.initialAmount, this.currentAmount);
101114
};
102115

103116
addCurrentAmount = (amount: BN) => {
104117
this.currentAmount = this.currentAmount.plus(amount);
105118
this.currentQuoteAmount = this.getQuoteAmount(this.currentAmount, this.price);
119+
120+
this.filledAmount = this.getFilledAmount(this.initialAmount, this.currentAmount);
106121
};
107122

108123
private getQuoteAmount = (amount: BN, price: BN) => {
@@ -117,6 +132,10 @@ export class SpotMarketOrder {
117132
return new BN(result);
118133
};
119134

135+
private getFilledAmount = (initialAmount: BN, currentAmount: BN) => {
136+
return initialAmount.minus(currentAmount);
137+
};
138+
120139
debug = () => {
121140
return {
122141
initialAmount: this.initialAmount.toString(),

src/screens/TradeScreen/BottomTables/SpotTable/SpotTableVM.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SpotTableVM {
4343
constructor(rootStore: RootStore) {
4444
makeAutoObservable(this);
4545
this.rootStore = rootStore;
46-
const { accountStore, tradeStore, balanceStore } = this.rootStore;
46+
const { accountStore, tradeStore } = this.rootStore;
4747

4848
reaction(
4949
() => [tradeStore.market, this.rootStore.initialized, accountStore.isConnected],

src/screens/TradeScreen/OrderbookAndTradesInterface/SpotOrderBook/SpotOrderBook.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ export const SpotOrderBook: React.FC<IProps> = observer(() => {
129129
return orders.map((o, index) => (
130130
<OrderRow key={index + "order"} type={type} onClick={() => orderSpotVm.selectOrderbookOrder(o, orderMode)}>
131131
<VolumeBar type={type} volumePercent={volumePercent(o).times(100).toNumber()} />
132-
<Text primary>{o.currentAmountUnits.toFormat(3)}</Text>
132+
<Text primary>{o.currentAmountUnits.toFormat(4)}</Text>
133133
<TextOverflow color={color}>{o.priceUnits.toFormat(vm.decimalGroup)}</TextOverflow>
134-
<Text primary>{numeral(o.initialQuoteAmountUnits).format(`0.${"0".repeat(vm.decimalGroup)}a`)}</Text>
134+
<Text primary>{numeral(o.currentQuoteAmountUnits).format(`0.${"0".repeat(vm.decimalGroup)}a`)}</Text>
135135
</OrderRow>
136136
));
137137
};

src/utils/groupOrders.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { DEFAULT_DECIMALS } from "@src/constants";
22
import { SpotMarketOrder } from "@src/entity";
33

44
import BN from "./BN";
5-
import { CONFIG } from "./getConfig";
65

76
const roundPrice = (price: BN, decimals: number): BN => {
87
const factor = new BN(10).pow(decimals);
@@ -26,13 +25,13 @@ export const groupOrders = (orders: SpotMarketOrder[], decimals: number): SpotMa
2625
initial_amount: BN.ZERO.toString(),
2726
order_type: order.orderType,
2827
asset: order.baseToken.assetId,
29-
quoteAssetId: CONFIG.TOKENS_BY_SYMBOL.USDC.assetId,
28+
quoteAssetId: order.quoteToken.assetId,
3029
timestamp: order.timestamp.toString(),
3130
});
3231
}
3332

3433
groupedOrders[price].addInitialAmount(order.initialAmount);
35-
groupedOrders[price].addCurrentAmount(order.initialAmount);
34+
groupedOrders[price].addCurrentAmount(order.currentAmount);
3635
});
3736

3837
return Object.values(groupedOrders);

src/utils/handleWalletErrors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export const handleWalletErrors = (
2626
let extendedErrorText;
2727

2828
try {
29-
extendedErrorText = getHumanReadableError(error.metadata.logs[0]);
29+
extendedErrorText = getHumanReadableError(error.metadata.logs);
30+
console.error("Detail info: ", error.metadata.logs);
3031
} catch (error) {
3132
console.error("Failed to parse error: ", error);
3233
}

0 commit comments

Comments
 (0)