Skip to content

Commit e639e46

Browse files
committed
feat: added logic for fulfill
1 parent e75ee55 commit e639e46

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

src/blockchain/FuelNetwork.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ export class FuelNetwork {
113113
return this.orderbookSdk.fulfillOrderMany(...params);
114114
};
115115

116+
fulfillOrderManyWithDeposit = async (
117+
...params: Parameters<typeof this.orderbookSdk.fulfillOrderManyWithDeposit>
118+
): Promise<WriteTransactionResponse> => {
119+
return this.orderbookSdk.fulfillOrderManyWithDeposit(...params);
120+
};
121+
116122
cancelSpotOrder = async (
117123
...params: Parameters<typeof this.orderbookSdk.cancelOrder>
118124
): Promise<WriteTransactionResponse> => {

src/screens/SpotScreen/RightBlock/CreateOrder/CreateOrderVM.tsx

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { PropsWithChildren, useMemo } from "react";
22
import {
33
AssetType,
44
CreateOrderWithDepositParams,
5-
FulfillOrderManyParams,
5+
FulfillOrderManyWithDepositParams,
66
GetOrdersParams,
77
LimitType,
88
OrderType,
@@ -57,6 +57,14 @@ export enum ORDER_TYPE {
5757
LimitIOC, // TODO: Когда TimeInForce будет переделана в отдельную вкладку оставить только тип limit
5858
}
5959

60+
interface DepositInfo {
61+
amountToSpend: string;
62+
amountFee: string;
63+
depositAssetId: string;
64+
feeAssetId: string;
65+
assetType: AssetType;
66+
}
67+
6068
class CreateOrderVM {
6169
isLoading = false;
6270

@@ -276,15 +284,35 @@ class CreateOrderVM {
276284
const { tradeStore, notificationStore, balanceStore, mixPanelStore, settingsStore } = this.rootStore;
277285

278286
const { market } = tradeStore;
279-
const { orderType, timeInForce } = settingsStore;
287+
const { timeInForce } = settingsStore;
280288
const bcNetwork = FuelNetwork.getInstance();
281289

282290
if (!market) return;
283291
this.isLoading = true;
284292

285293
const isBuy = this.mode === ORDER_MODE.BUY;
286294
const type = isBuy ? OrderType.Buy : OrderType.Sell;
287-
const typeMarket = isBuy ? OrderType.Sell : OrderType.Buy;
295+
296+
const depositAmount = isBuy ? this.inputTotal : this.inputAmount;
297+
const depositAmountWithFee = BN.parseUnits(this.exchangeFee, market.quoteToken.decimals).plus(
298+
BN.parseUnits(tradeStore.matcherFee, market.quoteToken.decimals),
299+
);
300+
301+
const deposit: DepositInfo = {
302+
amountToSpend: depositAmount.toString(),
303+
amountFee: depositAmountWithFee.toString(),
304+
depositAssetId: isBuy ? market.quoteToken.assetId : market.baseToken.assetId,
305+
feeAssetId: market.quoteToken.assetId,
306+
assetType: isBuy ? AssetType.Quote : AssetType.Base,
307+
};
308+
309+
const marketContracts = CONFIG.APP.markets
310+
.filter(
311+
(m) =>
312+
m.baseAssetId.toLowerCase() === deposit.depositAssetId.toLowerCase() ||
313+
m.quoteAssetId.toLowerCase() === deposit.depositAssetId.toLowerCase(),
314+
)
315+
.map((m) => m.contractId);
288316

289317
if (bcNetwork.getIsExternalWallet()) {
290318
notificationStore.info({ text: "Please, confirm operation in your wallet" });
@@ -294,9 +322,9 @@ class CreateOrderVM {
294322
let hash: Undefinable<string> = "";
295323

296324
if (timeInForce === LimitType.GTC) {
297-
hash = await this.createGTCOrder(type, isBuy, market);
325+
hash = await this.createGTCOrder(type, deposit, marketContracts);
298326
} else {
299-
hash = await this.createMarketOrLimitOrder(type, typeMarket, orderType, market);
327+
hash = await this.createMarketOrLimitOrder(type, market, deposit, marketContracts);
300328
}
301329

302330
const token = isBuy ? market.baseToken : market.quoteToken;
@@ -321,46 +349,31 @@ class CreateOrderVM {
321349
};
322350

323351
// Extracted function for creating GTC orders with deposits
324-
private createGTCOrder = async (type: OrderType, isBuy: boolean, market: SpotMarket): Promise<string> => {
352+
private createGTCOrder = async (
353+
type: OrderType,
354+
deposit: DepositInfo,
355+
marketContracts: string[],
356+
): Promise<string> => {
325357
const bcNetwork = FuelNetwork.getInstance();
326358

327-
const depositAmount = isBuy ? this.inputTotal : this.inputAmount;
328-
const depositAmountWithFee = BN.parseUnits(this.exchangeFee, market.quoteToken.decimals).plus(
329-
BN.parseUnits(this.rootStore.tradeStore.matcherFee, market.quoteToken.decimals),
330-
);
331-
332-
const deposit = {
333-
amountToSpend: depositAmount.toString(),
334-
amountFee: depositAmountWithFee.toString(),
335-
depositAssetId: isBuy ? market.quoteToken.assetId : market.baseToken.assetId,
336-
feeAssetId: market.quoteToken.assetId,
337-
assetType: isBuy ? AssetType.Quote : AssetType.Base,
338-
};
339-
340-
const marketContracts = CONFIG.APP.markets
341-
.filter(
342-
(m) =>
343-
m.baseAssetId.toLowerCase() === deposit.depositAssetId.toLowerCase() ||
344-
m.quoteAssetId.toLowerCase() === deposit.depositAssetId.toLowerCase(),
345-
)
346-
.map((m) => m.contractId);
347-
348359
const order: CreateOrderWithDepositParams = {
349360
type,
350361
amount: this.inputAmount.toString(),
351362
price: this.inputPrice.toString(),
352363
...deposit,
353364
};
354365

366+
console.log(order);
367+
355368
const data = await bcNetwork.createSpotOrderWithDeposit(order, marketContracts);
356369
return data.transactionId;
357370
};
358371

359372
private createMarketOrLimitOrder = async (
360373
type: OrderType,
361-
typeMarket: OrderType,
362-
orderType: ORDER_TYPE,
363374
market: SpotMarket,
375+
deposit: DepositInfo,
376+
marketContracts: string[],
364377
): Promise<string> => {
365378
const { settingsStore } = this.rootStore;
366379
const bcNetwork = FuelNetwork.getInstance();
@@ -371,21 +384,27 @@ class CreateOrderVM {
371384
status: ["Active"],
372385
};
373386

374-
const sellOrders = await bcNetwork.fetchSpotOrders({ ...params, orderType: typeMarket });
387+
const oppositeOrderType = type === OrderType.Buy ? OrderType.Sell : OrderType.Buy;
388+
const orders = await bcNetwork.fetchSpotOrders({ ...params, orderType: oppositeOrderType });
389+
390+
const price =
391+
settingsStore.orderType === ORDER_TYPE.Market
392+
? orders[orders.length - 1].price.toString()
393+
: this.inputPrice.toString();
375394

376-
const order: FulfillOrderManyParams = {
395+
const order: FulfillOrderManyWithDepositParams = {
377396
amount: this.inputAmount.toString(),
378397
orderType: type,
379398
limitType: settingsStore.timeInForce,
380-
price:
381-
orderType === ORDER_TYPE.Market
382-
? sellOrders[sellOrders.length - 1].price.toString()
383-
: this.inputPrice.toString(),
384-
orders: sellOrders.map((el) => el.id),
399+
price,
400+
orders: orders.map((el) => el.id),
385401
slippage: "10000",
402+
...deposit,
386403
};
387404

388-
const data = await bcNetwork.swapTokens(order);
405+
console.log(order);
406+
407+
const data = await bcNetwork.fulfillOrderManyWithDeposit(order, marketContracts);
389408
return data.transactionId;
390409
};
391410

0 commit comments

Comments
 (0)