Skip to content

Commit eb37b72

Browse files
authored
Feat: new market logic (#319)
1 parent d890518 commit eb37b72

File tree

8 files changed

+43
-20
lines changed

8 files changed

+43
-20
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"pnpm": ">=10.4.0"
1818
},
1919
"dependencies": {
20-
"@compolabs/spark-orderbook-ts-sdk": "https://registry.npmjs.org/@compolabs/spark-orderbook-ts-sdk/-/spark-orderbook-ts-sdk-1.16.1.tgz",
20+
"@compolabs/spark-orderbook-ts-sdk": "https://registry.npmjs.org/@compolabs/spark-orderbook-ts-sdk/-/spark-orderbook-ts-sdk-1.16.2.tgz",
2121
"@compolabs/tradingview-chart": "^1.0.21",
2222
"@emotion/react": "^11.11.3",
2323
"@emotion/styled": "^11.11.0",

pnpm-lock.yaml

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.8.0",
2+
"version": "1.8.1",
33
"isMainnet": true,
44
"contractVer": 1539,
55
"tokens": [
@@ -320,4 +320,4 @@
320320
"networkUrl": "https://mainnet.fuel.network/v1/graphql",
321321
"explorerUrl": "https://app.fuel.network",
322322
"sentioUrl": "https://app.sentio.xyz/api/v1/analytics/zhpv96/spark-processor/sql/execute"
323-
}
323+
}

src/entity/SpotMarketOrder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class SpotMarketOrder {
3838

3939
constructor(order: SpotMarketOrderParams) {
4040
const bcNetwork = FuelNetwork.getInstance();
41-
const activeMarket = CONFIG.MARKETS.find((el) => el.contractId === order.market);
41+
const activeMarket = CONFIG.ALL_MARKETS.find((el) => el.contractId === order.market); // TODO: If the market was already hidden and you traded on it, you need to display the history correctly
4242

4343
const baseToken = order.quoteAssetId ? order.asset : (activeMarket?.baseAssetId ?? "");
4444
const quoteToken = order.quoteAssetId ?? activeMarket?.quoteAssetId ?? "";

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { ACTIVE_INPUT, ORDER_MODE, ORDER_TYPE, useCreateOrderVM } from "./Create
3333
import { OrderTypeSheet, OrderTypeTooltip, OrderTypeTooltipIcon } from "./OrderTypeTooltip";
3434

3535
export const ORDER_OPTIONS = [
36-
// { title: "Market", key: ORDER_TYPE.Market, timeInForce: LimitType.FOK },
36+
{ title: "Market", key: ORDER_TYPE.Market, timeInForce: LimitType.MKT },
3737
{ title: "Limit", key: ORDER_TYPE.Limit, timeInForce: LimitType.GTC },
3838
// { title: "Limit (IOC)", key: ORDER_TYPE.LimitIOC, timeInForce: LimitType.IOC },
3939
// { title: "Limit (FOK)", key: ORDER_TYPE.LimitFOK, timeInForce: LimitType.FOK },

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

+29-7
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ class CreateOrderVM {
310310
const fee = getRealFee(market, tradeStore.matcherFee, tradeStore.exchangeFee, !isBuy);
311311

312312
const depositAmount = isBuy ? this.inputTotal : this.inputAmount;
313-
const depositAmountWithFee = fee.exchangeFee.plus(fee.matcherFee);
313+
const feeAmount = fee.exchangeFee.plus(fee.matcherFee);
314314

315315
const deposit: DepositInfo = {
316316
amountToSpend: depositAmount.toString(),
317-
amountFee: depositAmountWithFee.toString(),
317+
amountFee: feeAmount.toString(),
318318
depositAssetId: isBuy ? market.quoteToken.assetId : market.baseToken.assetId,
319319
feeAssetId: market.quoteToken.assetId,
320320
assetType: isBuy ? AssetType.Quote : AssetType.Base,
@@ -349,8 +349,8 @@ class CreateOrderVM {
349349
try {
350350
let hash: Undefinable<string> = "";
351351

352-
if (timeInForce === LimitType.GTC) {
353-
hash = await this.createGTCOrder(type, deposit, compactMarkets);
352+
if (timeInForce === LimitType.GTC || timeInForce === LimitType.MKT) {
353+
hash = await this.createGTCOrder(type, deposit, compactMarkets, timeInForce);
354354
} else {
355355
hash = await this.createMarketOrLimitOrder(type, market, deposit, compactMarkets);
356356
}
@@ -386,22 +386,44 @@ class CreateOrderVM {
386386
this.isLoading = false;
387387
};
388388

389+
applySlippage = (value: BN, slippagePercent: BN, isPositive = true) => {
390+
const factor = new BN(1).plus(isPositive ? slippagePercent.div(100) : slippagePercent.div(100).negated());
391+
392+
return value.multipliedBy(factor).decimalPlaces(0).toString();
393+
};
394+
389395
// Extracted function for creating GTC orders with deposits
390396
private createGTCOrder = async (
391397
type: OrderType,
392398
deposit: DepositInfo,
393399
markets: CompactMarketInfo[],
400+
timeInForce: LimitType,
394401
): Promise<string> => {
395402
const bcNetwork = FuelNetwork.getInstance();
396403

404+
let price = this.inputPrice.toString();
405+
const amount = this.inputAmount.toString();
406+
407+
let amountToSpend = deposit.amountToSpend;
408+
409+
if (timeInForce === LimitType.MKT) {
410+
const isBuy = type === OrderType.Buy;
411+
price = this.applySlippage(this.inputPrice, this.slippage, isBuy);
412+
413+
amountToSpend = isBuy
414+
? this.applySlippage(new BN(amountToSpend), this.slippage.plus(1), isBuy)
415+
: amountToSpend.toString();
416+
}
417+
397418
const order: CreateOrderWithDepositParams = {
398419
type,
399-
amount: this.inputAmount.toString(),
400-
price: this.inputPrice.toString(),
420+
price,
421+
amount,
401422
...deposit,
423+
amountToSpend,
402424
};
403425

404-
const data = await bcNetwork.createSpotOrderWithDeposit(order, markets);
426+
const data = await bcNetwork.createSpotOrderWithDeposit(order, markets, timeInForce);
405427
return data.transactionId;
406428
};
407429

src/stores/SettingsStore.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class SettingsStore {
6363
orderType: ORDER_TYPE = ORDER_TYPE.Limit;
6464
setOrderType = (v: ORDER_TYPE) => (this.orderType = v);
6565

66-
timeInForce: LimitType = LimitType.GTC;
66+
timeInForce: LimitType = LimitType.MKT;
6767
setTimeInForce = (v: LimitType) => (this.timeInForce = v);
6868

6969
serialize = (): ISerializedSettingStore => ({

src/utils/getConfig.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface Market {
2121
}
2222

2323
function createConfig() {
24-
const CURRENT_CONFIG_VER = "1.8.0";
24+
const CURRENT_CONFIG_VER = "1.8.1";
2525
const configJSON = configProdJSON;
2626
assert(configJSON.version === CURRENT_CONFIG_VER, "Version mismatch");
2727

@@ -51,6 +51,7 @@ function createConfig() {
5151
return {
5252
APP: configJSON,
5353
MARKETS: markets,
54+
ALL_MARKETS: configJSON.markets,
5455
TOKENS: tokens,
5556
TOKENS_BY_SYMBOL: tokensBySymbol,
5657
TOKENS_BY_ASSET_ID: tokensByAssetId,

0 commit comments

Comments
 (0)