From a88f59476377c4b2f55153d2a5df739088ddf19c Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Mon, 20 May 2024 18:24:09 +0200 Subject: [PATCH 1/3] chore(): Refactoring types --- examples/rest-private.ts | 26 +- src/RestClient.ts | 1369 +++------------------------- src/types/requests/shared.types.ts | 127 +++ src/types/response/shared.types.ts | 196 ++++ src/types/shared.ts | 333 +++++++ 5 files changed, 794 insertions(+), 1257 deletions(-) diff --git a/examples/rest-private.ts b/examples/rest-private.ts index 34ef8af..7ef20a6 100644 --- a/examples/rest-private.ts +++ b/examples/rest-private.ts @@ -3,8 +3,10 @@ import { RestClient } from '../src'; async function start() { try { const account = { - key: process.env.API_KEY || 'apiKeyHere', - secret: process.env.API_SECRET || 'apiSecretHere', + key: process.env.API_KEY || '8b8eff02d6b6105d195e81684017a43d', + secret: + process.env.API_SECRET || + '545055d0b75c9c2f9234369990bac79e3df4d2c9cda852a8531be65d901f5719', }; console.log('using creds: ', account); @@ -13,13 +15,25 @@ async function start() { apiSecret: account.secret, }); - //const res1 = await rest.getBalances(); + /* const res1 = await rest.submitSpotOrder({ + currency_pair: 'BTC_USDT', + side: 'buy', + type: 'limit', + amount: '10', + time_in_force: 'gtc', + price: '1', + }); */ + + const res1 = await rest.updateAutoRepaymentSetting({ + status: 'on', + }); + /* const res2 = await rest.getIndexConstituents({ settle: 'usdt', index: 'BTC_USDT', }); */ - const res3 = await rest.portfolioMarginCalculator({ + /* const res3 = await rest.portfolioMarginCalculator({ spot_balances: [ { currency: 'BTC', @@ -61,14 +75,14 @@ async function start() { }, ], spot_hedge: false, - }); + }); */ /* const res4 = await rest.getDeliveryContract({ settle: 'usdt', contract: 'BTC_USDT', }); */ // const res1 = await rest.getSystemMaintenanceStatus(); - console.log('res: ', JSON.stringify(res3, null, 2)); + console.log('res: ', JSON.stringify(res1, null, 2)); } catch (e) { console.error(`Error in execution: `, e); } diff --git a/src/RestClient.ts b/src/RestClient.ts index 42100a2..4df9429 100644 --- a/src/RestClient.ts +++ b/src/RestClient.ts @@ -1,3 +1,7 @@ +// double check if schemas are requests +// double check if names are set to what the call represents(get, delete, update etc...) +// check where query params and body is as it should be + import { AxiosRequestConfig } from 'axios'; import { @@ -7,28 +11,52 @@ import { } from './lib/BaseRestClient.js'; import { RestClientOptions } from './lib/requestUtils.js'; import { + DeleteSpotOrderReq, GetMainSubTransfersReq, + GetMarginBalanceHistoryReq, GetSavedAddressReq, GetSmallBalanceHistoryReq, + GetSpotAccountBookReq, + GetSpotAutoOrdersReq, + GetSpotCandlesticksReq, + GetSpotOrderBookReq, + GetSpotOrdersReq, + GetSpotTradesReq, + GetSpotTradingHistoryReq, GetUnifiedInterestRecordsReq, GetUnifiedLoanRecordsReq, GetUnifiedLoansReq, GetWithdrawalDepositRecordsReq, PortfolioMarginCalculatorReq, SetUnifiedAccountModeReq, + SubmitSpotClosePosCrossDisabledReq, + SubmitSpotOrderReq, SubmitUnifiedBorrowOrRepayReq, + UpdateSpotBatchOrdersReq, } from './types/requests/shared.types.js'; import { APIResponse, CreateDepositAddressResp, CreateSubAccountApiKeyResp, + DeleteSpotBatchOrdersResp, GetBalancesResp, GetCurrencyChainsResp, + GetMarginAccountsResp, + GetMarginBalanceHistoryResp, GetSavedAddressResp, GetSmallBalanceHistoryResp, GetSmallBalancesResp, + GetSpotAccountBookResp, + GetSpotAccountsResp, + GetSpotBatchFeeRatesResp, + GetSpotCandlesticksResp, GetSpotCurrenciesResp, + GetSpotFeeRatesResp, + GetSpotOpenOrdersResp, + GetSpotOrderBookResp, GetSpotTickerResp, + GetSpotTradesResp, + GetSpotTradingHistoryResp, GetTradingFeesResp, GetUnifiedAccountInfoResp, GetUnifiedCurrencyDiscountTiersResp, @@ -43,340 +71,22 @@ import { SubAccountMarginBalancesResp, SubAccountResp, SubAccountTransferRecordResp, + SubmitSpotBatchOrdersResp, } from './types/response/shared.types.js'; +import { + CancelBatchOrder, + Contract, + CurrencyPair, + DeliveryContract, + FuturesOrder, + FuturesPriceTriggeredOrder, + Order, + Position, + SpotPriceTriggeredOrder, + SubAccountKey, + Withdraw, +} from './types/shared.js'; -// interfaces - -interface SubAccountKey { - user_id?: string; - mode?: number; - name?: string; - perms?: { - name?: - | 'wallet' - | 'spot' - | 'futures' - | 'delivery' - | 'earn' - | 'options' - | 'account' - | 'unified' - | 'loan'; - read_only?: boolean; - }[]; - ip_whitelist?: string[]; - key?: string; - state?: number; - created_at?: number; - updated_at?: number; - last_access?: number; -} - -interface CurrencyPair { - id?: string; - base?: string; - quote?: string; - fee?: string; - min_base_amount?: string; - min_quote_amount?: string; - max_base_amount?: string; - max_quote_amount?: string; - amount_precision?: number; - precision?: number; - trade_status?: 'untradable' | 'buyable' | 'sellable' | 'tradable'; - sell_start?: number; - buy_start?: number; -} - -interface Order { - id?: string; - text?: string; - amend_text?: string; - create_time?: string; - update_time?: string; - create_time_ms?: number; - update_time_ms?: number; - status?: 'open' | 'closed' | 'cancelled'; - currency_pair: string; - type?: 'limit' | 'market'; - account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; - side: 'buy' | 'sell'; - amount: string; - price?: string; - time_in_force?: 'gtc' | 'ioc' | 'poc' | 'fok'; - iceberg?: string; - auto_borrow?: boolean; - auto_repay?: boolean; - left?: string; - filled_amount?: string; - fill_price?: string; - filled_total?: string; - avg_deal_price?: string; - fee?: string; - fee_currency?: string; - point_fee?: string; - gt_fee?: string; - gt_maker_fee?: string; - gt_taker_fee?: string; - gt_discount?: boolean; - rebated_fee?: string; - rebated_fee_currency?: string; - stp_id?: number; - stp_act?: 'cn' | 'co' | 'cb' | '-'; - finish_as?: 'open' | 'filled' | 'cancelled' | 'ioc' | 'stp'; - action_mode?: 'ACK' | 'RESULT' | 'FULL'; -} - -interface CancelBatchOrder { - currency_pair: string; - id: string; - account?: 'cross_margin'; - action_mode?: 'ACK' | 'RESULT' | 'FULL'; -} - -interface SpotPriceTriggeredOrder { - trigger: { - price: string; - rule: '>=' | '<='; - expiration: number; - }; - put: { - type?: 'limit' | 'market'; - side: 'buy' | 'sell'; - price: string; - amount: string; - account: 'normal' | 'margin' | 'cross_margin'; - time_in_force?: 'gtc' | 'ioc'; - text?: string; - }; - id?: number; - user?: number; - market: string; - ctime?: number; - ftime?: number; - fired_order_id?: number; - status?: 'open' | 'cancelled' | 'finish' | 'failed' | 'expired'; - reason?: string; -} - -interface Contract { - name?: string; - type?: 'inverse' | 'direct'; - quanto_multiplier?: string; - leverage_min?: string; - leverage_max?: string; - maintenance_rate?: string; - mark_type?: 'internal' | 'index'; - mark_price?: string; - index_price?: string; - last_price?: string; - maker_fee_rate?: string; - taker_fee_rate?: string; - order_price_round?: string; - mark_price_round?: string; - funding_rate?: string; - funding_interval?: number; - funding_next_apply?: number; - risk_limit_base?: string; - risk_limit_step?: string; - risk_limit_max?: string; - order_size_min?: number; - order_size_max?: number; - order_price_deviate?: string; - ref_discount_rate?: string; - ref_rebate_rate?: string; - orderbook_id?: number; - trade_id?: number; - trade_size?: number; - position_size?: number; - config_change_time?: number; - in_delisting?: boolean; - orders_limit?: number; - enable_bonus?: boolean; - enable_credit?: boolean; - create_time?: number; - funding_cap_ratio?: string; -} - -interface Position { - user?: number; - contract?: string; - size?: number; - leverage?: string; - risk_limit?: string; - leverage_max?: string; - maintenance_rate?: string; - value?: string; - margin?: string; - entry_price?: string; - liq_price?: string; - mark_price?: string; - initial_margin?: string; - maintenance_margin?: string; - unrealised_pnl?: string; - realised_pnl?: string; - pnl_pnl?: string; - pnl_fund?: string; - pnl_fee?: string; - history_pnl?: string; - last_close_pnl?: string; - realised_point?: string; - history_point?: string; - adl_ranking?: number; - pending_orders?: number; - close_order?: { - id?: number; - price?: string; - is_liq?: boolean; - } | null; - mode?: 'single' | 'dual_long' | 'dual_short'; - cross_leverage_limit?: string; - update_time?: number; - open_time?: number; -} - -interface FuturesOrder { - id?: number; - user?: number; - create_time?: number; - finish_time?: number; - finish_as?: - | 'filled' - | 'cancelled' - | 'liquidated' - | 'ioc' - | 'auto_deleveraged' - | 'reduce_only' - | 'position_closed' - | 'stp'; - status?: 'open' | 'finished'; - contract: string; - size: number; - iceberg?: number; - price?: string; - close?: boolean; - is_close?: boolean; - reduce_only?: boolean; - is_reduce_only?: boolean; - is_liq?: boolean; - tif?: 'gtc' | 'ioc' | 'poc' | 'fok'; - left?: number; - fill_price?: string; - text?: string; - tkfr?: string; - mkfr?: string; - refu?: number; - auto_size?: 'close_long' | 'close_short'; - stp_id?: number; - stp_act?: 'cn' | 'co' | 'cb' | '-'; - amend_text?: string; - biz_info?: string; -} - -interface FuturesPriceTriggeredOrder { - initial: { - contract: string; - size?: number; - price?: string; - close?: boolean; - tif?: 'gtc' | 'ioc'; - text?: string; - reduce_only?: boolean; - auto_size?: string; - is_reduce_only?: boolean; - is_close?: boolean; - }; - trigger: { - strategy_type?: 0 | 1; - price_type?: 0 | 1 | 2; - price?: string; - rule?: 1 | 2; - expiration?: number; - }; - id?: number; - user?: number; - create_time?: number; - finish_time?: number; - trade_id?: number; - status?: 'open' | 'finished' | 'inactive' | 'invalid'; - finish_as?: 'cancelled' | 'succeeded' | 'failed' | 'expired'; - reason?: string; - order_type?: - | 'close-long-order' - | 'close-short-order' - | 'close-long-position' - | 'close-short-position' - | 'plan-close-long-position' - | 'plan-close-short-position'; - me_order_id?: number; -} - -interface DeliveryContract { - name?: string; - underlying?: string; - cycle?: 'WEEKLY' | 'BI-WEEKLY' | 'QUARTERLY' | 'BI-QUARTERLY'; - type?: 'inverse' | 'direct'; - quanto_multiplier?: string; - leverage_min?: string; - leverage_max?: string; - maintenance_rate?: string; - mark_type?: 'internal' | 'index'; - mark_price?: string; - index_price?: string; - last_price?: string; - maker_fee_rate?: string; - taker_fee_rate?: string; - order_price_round?: string; - mark_price_round?: string; - basis_rate?: string; - basis_value?: string; - basis_impact_value?: string; - settle_price?: string; - settle_price_interval?: number; - settle_price_duration?: number; - expire_time?: number; - risk_limit_base?: string; - risk_limit_step?: string; - risk_limit_max?: string; - order_size_min?: number; - order_size_max?: number; - order_price_deviate?: string; - ref_discount_rate?: string; - ref_rebate_rate?: string; - orderbook_id?: number; - trade_id?: number; - trade_size?: number; - position_size?: number; - config_change_time?: number; - in_delisting?: boolean; - orders_limit?: number; -} - -interface Withdraw { - id: string; - txid: string; - withdraw_order_id: string; - timestamp: string; - amount: string; - currency: string; - address: string; - memo?: string; - status: - | 'DONE' - | 'CANCEL' - | 'REQUEST' - | 'MANUAL' - | 'BCODE' - | 'EXTPEND' - | 'FAIL' - | 'INVALID' - | 'VERIFY' - | 'PROCES' - | 'PEND' - | 'DMOVE' - | 'SPLITPEND'; - chain: string; -} /** * Unified REST API client for all of Gate's REST APIs */ @@ -563,7 +273,7 @@ export class RestClient extends BaseRestClient { * @param params Transfer parameters * @returns Promise> */ - getSubToSubTransfer(body: { + submitSubToSubTransfer(body: { currency: string; sub_account_type?: string; sub_account_from: string; @@ -1133,28 +843,11 @@ export class RestClient extends BaseRestClient { * Order book will be sorted by price from high to low on bids; low to high on asks. * * @param params Parameters for retrieving order book - * @returns Promise> + * @returns Promise> */ - getSpotOrderBook(params: { - currency_pair: string; - interval?: string; - limit?: number; - with_id?: boolean; - }): Promise< - APIResponse<{ - id?: number; - current: number; - update: number; - asks: [string, string][]; - bids: [string, string][]; - }> - > { + getSpotOrderBook( + params: GetSpotOrderBookReq, + ): Promise> { return this.get('/spot/order_book', params); } @@ -1165,55 +858,11 @@ export class RestClient extends BaseRestClient { * Scrolling query using last_id is not recommended any more. If last_id is specified, time range query parameters will be ignored. * * @param params Parameters for retrieving market trades - * @returns Promise> + * @returns Promise> */ - getSpotTrades(params: { - currency_pair: string; - limit?: number; - last_id?: string; - reverse?: boolean; - from?: number; - to?: number; - page?: number; - }): Promise< - APIResponse< - { - id: string; - create_time: string; - create_time_ms: string; - currency_pair: string; - side: 'buy' | 'sell'; - role: 'taker' | 'maker'; - amount: string; - price: string; - order_id: string; - fee: string; - fee_currency: string; - point_fee: string; - gt_fee: string; - amend_text: string; - sequence_id: string; - text: string; - }[] - > - > { + getSpotTrades( + params: GetSpotTradesReq, + ): Promise> { return this.get('/spot/trades', params); } @@ -1223,50 +872,11 @@ export class RestClient extends BaseRestClient { * Maximum of 1000 points can be returned in a query. Be sure not to exceed the limit when specifying from, to and interval. * * @param params Parameters for retrieving market candlesticks - * @returns Promise> - */ - getSpotCandlesticks(params: { - currency_pair: string; - limit?: number; - from?: number; - to?: number; - interval?: - | '10s' - | '1m' - | '5m' - | '15m' - | '30m' - | '1h' - | '4h' - | '8h' - | '1d' - | '7d' - | '30d'; - }): Promise< - APIResponse< - [ - [ - string, // Unix timestamp with second precision - string, // Trading volume in quote currency - string, // Closing price - string, // Highest price - string, // Lowest price - string, // Opening price - string, // Trading volume in base currency - boolean, // Whether the window is closed - ], - ] - > - > { + * @returns Promise> + */ + getSpotCandlesticks( + params: GetSpotCandlesticksReq, + ): Promise> { return this.get('/spot/candlesticks', params); } @@ -1276,33 +886,11 @@ export class RestClient extends BaseRestClient { * This API is deprecated in favour of new fee retrieving API /wallet/fee. * * @param params Parameters for querying user trading fee rates - * @returns Promise> + * @returns Promise> */ - getSpotFeeRates(params?: { currency_pair?: string }): Promise< - APIResponse<{ - user_id: number; - taker_fee: string; - maker_fee: string; - gt_discount: boolean; - gt_taker_fee: string; - gt_maker_fee: string; - loan_fee: string; - point_type: string; - currency_pair: string; - debit_fee: number; - }> - > { + getSpotFeeRates(params?: { + currency_pair?: string; + }): Promise> { return this.getPrivate('/spot/fee', params); } @@ -1310,37 +898,11 @@ export class RestClient extends BaseRestClient { * Query a batch of user trading fee rates * * @param params Parameters for querying a batch of user trading fee rates - * @returns Promise> + * @returns Promise> */ - getSpotBatchFeeRates(params: { currency_pairs: string }): Promise< - APIResponse<{ - [key: string]: { - user_id: number; - taker_fee: string; - maker_fee: string; - gt_discount: boolean; - gt_taker_fee: string; - gt_maker_fee: string; - loan_fee: string; - point_type: string; - currency_pair: string; - debit_fee: number; - }; - }> - > { + getSpotBatchFeeRates(params: { + currency_pairs: string; + }): Promise> { return this.getPrivate('/spot/batch_fee', params); } @@ -1348,23 +910,11 @@ export class RestClient extends BaseRestClient { * List spot accounts * * @param params Parameters for listing spot accounts - * @returns Promise> + * @returns Promise> */ - getSpotAccounts(params?: { currency?: string }): Promise< - APIResponse< - { - currency: string; - available: string; - locked: string; - update_id: number; - }[] - > - > { + getSpotAccounts(params?: { + currency?: string; + }): Promise> { return this.getPrivate('/spot/accounts', params); } @@ -1374,36 +924,11 @@ export class RestClient extends BaseRestClient { * Record time range cannot exceed 30 days. * * @param params Parameters for querying account book - * @returns Promise> + * @returns Promise> */ - getSpotAccountBook(params?: { - currency?: string; - from?: number; - to?: number; - page?: number; - limit?: number; - type?: string; - }): Promise< - APIResponse< - { - id: string; - time: number; - currency: string; - change: string; - balance: string; - type: string; - text: string; - }[] - > - > { + getSpotAccountBook( + params?: GetSpotAccountBookReq, + ): Promise> { return this.getPrivate('/spot/account_book', params); } @@ -1416,87 +941,11 @@ export class RestClient extends BaseRestClient { * - No mixture of spot orders and margin orders, i.e. account must be identical for all orders * * @param params Parameters for creating a batch of orders - * @returns Promise> + * @returns Promise> */ - submitSpotBatchOrders(body: Order[]): Promise< - APIResponse< - { - order_id: string; - amend_text: string; - text: string; - succeeded: boolean; - label: string; - message: string; - id: string; - create_time: string; - update_time: string; - create_time_ms: number; - update_time_ms: number; - status: 'open' | 'closed' | 'cancelled'; - currency_pair: string; - type: 'limit' | 'market'; - account: 'spot' | 'margin' | 'cross_margin' | 'unified'; - side: 'buy' | 'sell'; - amount: string; - price: string; - time_in_force: 'gtc' | 'ioc' | 'poc' | 'fok'; - iceberg: string; - auto_repay: boolean; - left: string; - filled_amount: string; - fill_price: string; - filled_total: string; - avg_deal_price: string; - fee: string; - fee_currency: string; - point_fee: string; - gt_fee: string; - gt_discount: boolean; - rebated_fee: string; - rebated_fee_currency: string; - stp_id: number; - stp_act: 'cn' | 'co' | 'cb' | '-'; - finish_as: 'open' | 'filled' | 'cancelled' | 'ioc' | 'stp'; - }[] - > - > { + submitSpotBatchOrders( + body: Order[], + ): Promise> { return this.postPrivate('/spot/batch_orders', body); } @@ -1508,95 +957,13 @@ export class RestClient extends BaseRestClient { * Spot, portfolio, and margin orders are returned by default. To list cross margin orders, account must be set to cross_margin. * * @param params Parameters for listing all open orders - * @returns Promise> + * @returns Promise> */ getSpotOpenOrders(params?: { page?: number; limit?: number; account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; - }): Promise< - APIResponse< - { - currency_pair: string; - total: number; - orders: { - id: string; - text: string; - amend_text: string; - create_time: string; - update_time: string; - create_time_ms: number; - update_time_ms: number; - status: 'open' | 'closed' | 'cancelled'; - currency_pair: string; - type: 'limit' | 'market'; - account: 'spot' | 'margin' | 'cross_margin' | 'unified'; - side: 'buy' | 'sell'; - amount: string; - price: string; - time_in_force: 'gtc' | 'ioc' | 'poc' | 'fok'; - iceberg: string; - auto_repay: boolean; - left: string; - filled_amount: string; - fill_price: string; - filled_total: string; - avg_deal_price: string; - fee: string; - fee_currency: string; - point_fee: string; - gt_fee: string; - gt_maker_fee: string; - gt_taker_fee: string; - gt_discount: boolean; - rebated_fee: string; - rebated_fee_currency: string; - stp_id: number; - stp_act: 'cn' | 'co' | 'cb' | '-'; - finish_as: 'open' | 'filled' | 'cancelled' | 'ioc' | 'stp'; - }[]; - }[] - > - > { + }): Promise> { return this.getPrivate('/spot/open_orders', params); } @@ -1606,22 +973,11 @@ export class RestClient extends BaseRestClient { * Currently, only cross-margin accounts are supported to close position when cross currencies are disabled. Maximum buy quantity = (unpaid principal and interest - currency balance - the amount of the currency in the order book) / 0.998 * * @param params Parameters for closing position when cross-currency is disabled - * @returns Promise> + * @returns Promise> */ - submitSpotClosePosCrossDisabled(body: { - text?: string; - currency_pair: string; - amount: string; - price: string; - action_mode?: 'ACK' | 'RESULT' | 'FULL'; - }): Promise> { + submitSpotClosePosCrossDisabled( + body: SubmitSpotClosePosCrossDisabledReq, + ): Promise> { return this.postPrivate('/spot/cross_liquidate_orders', body); } @@ -1631,24 +987,9 @@ export class RestClient extends BaseRestClient { * You can place orders with spot, portfolio, margin or cross margin account through setting the account field. It defaults to spot, which means spot account is used to place orders. If the user is using unified account, it defaults to the unified account. * * @param params Parameters for creating an order - * @returns Promise> + * @returns Promise> */ - submitSpotOrder(body: Order): Promise> { + submitSpotOrder(body: SubmitSpotOrderReq): Promise> { return this.postPrivate('/spot/orders', body); } @@ -1658,53 +999,9 @@ export class RestClient extends BaseRestClient { * Spot, portfolio and margin orders are returned by default. If cross margin orders are needed, account must be set to cross_margin. * * @param params Parameters for listing orders - * @returns Promise> + * @returns Promise> */ - getSpotOrders(params: { - currency_pair: string; - status: 'open' | 'finished'; - page?: number; - limit?: number; - account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; - from?: number; - to?: number; - side?: 'buy' | 'sell'; - }): Promise> { + getSpotOrders(params: GetSpotOrdersReq): Promise> { return this.getPrivate('/spot/orders', params); } @@ -1715,42 +1012,7 @@ export class RestClient extends BaseRestClient { * You can set account to cancel only orders within the specified account. * * @param params Parameters for cancelling all open orders in specified currency pair - * @returns Promise> + * @returns Promise> */ deleteSpotPairOpenOrders(params: { currency_pair: string; @@ -1767,27 +1029,11 @@ export class RestClient extends BaseRestClient { * Multiple currency pairs can be specified, but maximum 20 orders are allowed per request. * * @param params Parameters for cancelling a batch of orders - * @returns Promise> + * @returns Promise> */ - deleteSpotBatchOrders(body: CancelBatchOrder[]): Promise< - APIResponse< - { - currency_pair: string; - id: string; - succeeded: boolean; - label: string; - message: string; - account: string; - }[] - > - > { + deleteSpotBatchOrders( + body: CancelBatchOrder[], + ): Promise> { return this.postPrivate('/spot/cancel_batch_orders', body); } @@ -1797,42 +1043,7 @@ export class RestClient extends BaseRestClient { * Spot, portfolio and margin orders are queried by default. If cross margin orders are needed or portfolio margin account are used, account must be set to cross_margin. * * @param params Parameters for getting a single order - * @returns Promise> + * @returns Promise> */ getSpotOrder(params: { order_id: string; @@ -1850,42 +1061,7 @@ export class RestClient extends BaseRestClient { * Currently, only supports modification of price or amount fields. * * @param params Parameters for amending an order - * @returns Promise> + * @returns Promise> */ updateSpotOrder( params: { @@ -1909,49 +1085,9 @@ export class RestClient extends BaseRestClient { * Spot, portfolio and margin orders are cancelled by default. If trying to cancel cross margin orders or portfolio margin account are used, account must be set to cross_margin. * * @param params Parameters for cancelling a single order - * @returns Promise> + * @returns Promise> */ - deleteSpotOrder(params: { - order_id: string; - currency_pair: string; - account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; - action_mode?: 'ACK' | 'RESULT' | 'FULL'; - }): Promise> { + deleteSpotOrder(params: DeleteSpotOrderReq): Promise> { return this.deletePrivate(`/spot/orders/${params.order_id}`, params); } @@ -1963,55 +1099,11 @@ export class RestClient extends BaseRestClient { * You can also set from and/or to to query by time range. If you don't specify from and/or to parameters, only the last 7 days of data will be returned. The range of from and to is not allowed to exceed 30 days. Time range parameters are handled as order finish time. * * @param params Parameters for listing personal trading history - * @returns Promise> + * @returns Promise> */ - getSpotTradingHistory(params?: { - currency_pair?: string; - limit?: number; - page?: number; - order_id?: string; - account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; - from?: number; - to?: number; - }): Promise< - APIResponse< - { - id: string; - create_time: string; - create_time_ms: string; - currency_pair: string; - side: 'buy' | 'sell'; - role: 'taker' | 'maker'; - amount: string; - price: string; - order_id: string; - fee: string; - fee_currency: string; - point_fee: string; - gt_fee: string; - amend_text: string; - sequence_id: string; - text: string; - }[] - > - > { + getSpotTradingHistory( + params?: GetSpotTradingHistoryReq, + ): Promise> { return this.getPrivate('/spot/my_trades', params); } @@ -2057,95 +1149,11 @@ export class RestClient extends BaseRestClient { * Default modification of orders for spot, portfolio, and margin accounts. To modify orders for a cross margin account, the account parameter must be specified as cross_margin. For portfolio margin accounts, the account parameter can only be specified as cross_margin. Currently, only modifications to price or quantity (choose one) are supported. * * @param params Parameters for batch modification of orders - * @returns Promise> + * @returns Promise> */ updateSpotBatchOrders( - body: { - order_id?: string; - currency_pair?: string; - amount?: string; - price?: string; - amend_text?: string; - }[], - ): Promise< - APIResponse< - { - order_id: string; - amend_text: string; - text: string; - succeeded: boolean; - label: string; - message: string; - id: string; - create_time: string; - update_time: string; - create_time_ms: number; - update_time_ms: number; - status: 'open' | 'closed' | 'cancelled'; - currency_pair: string; - type: 'limit' | 'market'; - account: 'spot' | 'margin' | 'cross_margin' | 'unified'; - side: 'buy' | 'sell'; - amount: string; - price: string; - time_in_force: 'gtc' | 'ioc' | 'poc' | 'fok'; - iceberg: string; - auto_repay: boolean; - left: string; - filled_amount: string; - fill_price: string; - filled_total: string; - avg_deal_price: string; - fee: string; - fee_currency: string; - point_fee: string; - gt_fee: string; - gt_discount: boolean; - rebated_fee: string; - rebated_fee_currency: string; - stp_id: number; - stp_act: 'cn' | 'co' | 'cb' | '-'; - finish_as: 'open' | 'filled' | 'cancelled' | 'ioc' | 'stp'; - }[] - > - > { + body: UpdateSpotBatchOrdersReq[], + ): Promise> { return this.postPrivate('/spot/amend_batch_orders', body); } @@ -2169,58 +1177,19 @@ export class RestClient extends BaseRestClient { * Retrieve running auto order list * * @param params Parameters for retrieving running auto order list - * @returns Promise=' | '<='; - * expiration: number; - * }; - * put: { - * type: 'limit' | 'market'; - * side: 'buy' | 'sell'; - * price: string; - * amount: string; - * account: 'normal' | 'margin' | 'cross_margin'; - * time_in_force: 'gtc' | 'ioc'; - * text: string; - * }; - * market: string; - * status: 'open' | 'finished'; - * }[]>> + * @returns Promise> */ - getSpotAutoOrders(params: { - status: 'open' | 'finished'; - market?: string; - account?: 'normal' | 'margin' | 'cross_margin'; - limit?: number; - offset?: number; - }): Promise> { + getSpotAutoOrders( + params: GetSpotAutoOrdersReq, + ): Promise> { return this.getPrivate('/spot/price_orders', params); } + /** * Cancel all open orders * * @param params Parameters for cancelling all open orders - * @returns Promise=' | '<='; - * expiration: number; - * }; - * put: { - * type: 'limit' | 'market'; - * side: 'buy' | 'sell'; - * price: string; - * amount: string; - * account: 'normal' | 'margin' | 'cross_margin'; - * time_in_force: 'gtc' | 'ioc'; - * text: string; - * }; - * market: string; - * status: 'open' | 'finished'; - * }[]>> + * @returns Promise> */ deleteSpotAllOpenOrders(params?: { market?: string; @@ -2233,25 +1202,7 @@ export class RestClient extends BaseRestClient { * Get a price-triggered order * * @param params Parameters for getting a price-triggered order - * @returns Promise=' | '<='; - * expiration: number; - * }; - * put: { - * type: 'limit' | 'market'; - * side: 'buy' | 'sell'; - * price: string; - * amount: string; - * account: 'normal' | 'margin' | 'cross_margin'; - * time_in_force: 'gtc' | 'ioc'; - * text: string; - * }; - * market: string; - * status: 'open' | 'finished'; - * }>> + * @returns Promise> */ getPriceTriggeredOrder(params: { order_id: string; @@ -2263,25 +1214,7 @@ export class RestClient extends BaseRestClient { * Cancel a price-triggered order * * @param params Parameters for cancelling a price-triggered order - * @returns Promise=' | '<='; - * expiration: number; - * }; - * put: { - * type: 'limit' | 'market'; - * side: 'buy' | 'sell'; - * price: string; - * amount: string; - * account: 'normal' | 'margin' | 'cross_margin'; - * time_in_force: 'gtc' | 'ioc'; - * text: string; - * }; - * market: string; - * status: 'open' | 'finished'; - * }>> + * @returns Promise> */ deleteSpotPriceTriggeredOrder(params: { order_id: string; @@ -2298,49 +1231,11 @@ export class RestClient extends BaseRestClient { * Margin account list * * @param params Parameters for listing margin accounts - * @returns Promise> + * @returns Promise> */ - getMarginAccounts(params?: { currency_pair?: string }): Promise< - APIResponse< - { - currency_pair: string; - locked: boolean; - risk: string; - base: { - currency: string; - available: string; - locked: string; - borrowed: string; - interest: string; - }; - quote: { - currency: string; - available: string; - locked: string; - borrowed: string; - interest: string; - }; - }[] - > - > { + getMarginAccounts(params?: { + currency_pair?: string; + }): Promise> { return this.getPrivate('/margin/accounts', params); } @@ -2350,39 +1245,11 @@ export class RestClient extends BaseRestClient { * Only transferals from and to margin account are provided for now. Time range allows 30 days at most. * * @param params Parameters for listing margin account balance change history - * @returns Promise> + * @returns Promise> */ - getMarginBalanceHistory(params?: { - currency?: string; - currency_pair?: string; - type?: string; - from?: number; - to?: number; - page?: number; - limit?: number; - }): Promise< - APIResponse< - { - id: string; - time: string; - time_ms: number; - currency: string; - currency_pair: string; - change: string; - balance: string; - type: string; - }[] - > - > { + getMarginBalanceHistory( + params?: GetMarginBalanceHistoryReq, + ): Promise> { return this.getPrivate('/margin/account_book', params); } diff --git a/src/types/requests/shared.types.ts b/src/types/requests/shared.types.ts index e10a0b4..44a8137 100644 --- a/src/types/requests/shared.types.ts +++ b/src/types/requests/shared.types.ts @@ -100,3 +100,130 @@ export interface PortfolioMarginCalculatorReq { * SPOT * ========================================================================================================================== */ + +export interface GetSpotOrderBookReq { + currency_pair: string; + interval?: string; + limit?: number; + with_id?: boolean; +} + +export interface GetSpotTradesReq { + currency_pair: string; + limit?: number; + last_id?: string; + reverse?: boolean; + from?: number; + to?: number; + page?: number; +} + +export interface GetSpotCandlesticksReq { + currency_pair: string; + limit?: number; + from?: number; + to?: number; + interval?: + | '10s' + | '1m' + | '5m' + | '15m' + | '30m' + | '1h' + | '4h' + | '8h' + | '1d' + | '7d' + | '30d'; +} + +export interface GetSpotAccountBookReq { + currency?: string; + from?: number; + to?: number; + page?: number; + limit?: number; + type?: string; +} + +export interface SubmitSpotClosePosCrossDisabledReq { + text?: string; + currency_pair: string; + amount: string; + price: string; + action_mode?: 'ACK' | 'RESULT' | 'FULL'; +} + +export interface GetSpotOrdersReq { + currency_pair: string; + status: 'open' | 'finished'; + page?: number; + limit?: number; + account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; + from?: number; + to?: number; + side?: 'buy' | 'sell'; +} + +export interface DeleteSpotOrderReq { + order_id: string; + currency_pair: string; + account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; + action_mode?: 'ACK' | 'RESULT' | 'FULL'; +} + +export interface GetSpotTradingHistoryReq { + currency_pair?: string; + limit?: number; + page?: number; + order_id?: string; + account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; + from?: number; + to?: number; +} + +export interface UpdateSpotBatchOrdersReq { + order_id?: string; + currency_pair?: string; + amount?: string; + price?: string; + amend_text?: string; +} + +export interface GetSpotAutoOrdersReq { + status: 'open' | 'finished'; + market?: string; + account?: 'normal' | 'margin' | 'cross_margin'; + limit?: number; + offset?: number; +} + +export interface SubmitSpotOrderReq { + side: 'buy' | 'sell'; + amount: string; + text?: string; + currency_pair: string; + type?: 'limit' | 'market'; + account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; + price?: string; + time_in_force?: 'gtc' | 'ioc' | 'poc' | 'fok'; + iceberg?: string; + auto_borrow?: boolean; + auto_repay?: boolean; + stp_act?: string; + action_mode?: string; +} +/**========================================================================================================================== + * MARGIN + * ========================================================================================================================== + */ + +export interface GetMarginBalanceHistoryReq { + currency?: string; + currency_pair?: string; + type?: string; + from?: number; + to?: number; + page?: number; + limit?: number; +} diff --git a/src/types/response/shared.types.ts b/src/types/response/shared.types.ts index 6d01d68..a45d372 100644 --- a/src/types/response/shared.types.ts +++ b/src/types/response/shared.types.ts @@ -1,3 +1,5 @@ +import { Order } from '../shared'; + export interface APIResponse { success: boolean; data: TData; @@ -387,3 +389,197 @@ export interface GetSpotTickerResp { etf_pre_timestamp: number | null; etf_leverage: string | null; } + +export interface GetSpotOrderBookResp { + id?: number; + current: number; + update: number; + asks: [string, string][]; + bids: [string, string][]; +} + +export interface GetSpotTradesResp { + id: string; + create_time: string; + create_time_ms: string; + currency_pair: string; + side: 'buy' | 'sell'; + role: 'taker' | 'maker'; + amount: string; + price: string; + order_id: string; + fee: string; + fee_currency: string; + point_fee: string; + gt_fee: string; + amend_text: string; + sequence_id: string; + text: string; +} + +export type GetSpotCandlesticksResp = [ + [ + string, // Unix timestamp with second precision + string, // Trading volume in quote currency + string, // Closing price + string, // Highest price + string, // Lowest price + string, // Opening price + string, // Trading volume in base currency + boolean, // Whether the window is closed + ], +]; + +export interface GetSpotFeeRatesResp { + user_id: number; + taker_fee: string; + maker_fee: string; + gt_discount: boolean; + gt_taker_fee: string; + gt_maker_fee: string; + loan_fee: string; + point_type: string; + currency_pair: string; + debit_fee: number; +} + +export interface GetSpotBatchFeeRatesResp { + [key: string]: { + user_id: number; + taker_fee: string; + maker_fee: string; + gt_discount: boolean; + gt_taker_fee: string; + gt_maker_fee: string; + loan_fee: string; + point_type: string; + currency_pair: string; + debit_fee: number; + }; +} + +export interface GetSpotAccountsResp { + currency: string; + available: string; + locked: string; + update_id: number; +} + +export interface GetSpotAccountBookResp { + id: string; + time: number; + currency: string; + change: string; + balance: string; + type: string; + text: string; +} + +export interface SubmitSpotBatchOrdersResp { + order_id: string; + amend_text: string; + text: string; + succeeded: boolean; + label: string; + message: string; + id: string; + create_time: string; + update_time: string; + create_time_ms: number; + update_time_ms: number; + status: 'open' | 'closed' | 'cancelled'; + currency_pair: string; + type: 'limit' | 'market'; + account: 'spot' | 'margin' | 'cross_margin' | 'unified'; + side: 'buy' | 'sell'; + amount: string; + price: string; + time_in_force: 'gtc' | 'ioc' | 'poc' | 'fok'; + iceberg: string; + auto_repay: boolean; + left: string; + filled_amount: string; + fill_price: string; + filled_total: string; + avg_deal_price: string; + fee: string; + fee_currency: string; + point_fee: string; + gt_fee: string; + gt_discount: boolean; + rebated_fee: string; + rebated_fee_currency: string; + stp_id: number; + stp_act: 'cn' | 'co' | 'cb' | '-'; + finish_as: 'open' | 'filled' | 'cancelled' | 'ioc' | 'stp'; +} + +export interface GetSpotOpenOrdersResp { + currency_pair: string; + total: number; + orders: Order[]; +} + +export interface DeleteSpotBatchOrdersResp { + currency_pair: string; + id: string; + succeeded: boolean; + label: string; + message: string; + account: string; +} + +export interface GetSpotTradingHistoryResp { + id: string; + create_time: string; + create_time_ms: string; + currency_pair: string; + side: 'buy' | 'sell'; + role: 'taker' | 'maker'; + amount: string; + price: string; + order_id: string; + fee: string; + fee_currency: string; + point_fee: string; + gt_fee: string; + amend_text: string; + sequence_id: string; + text: string; +} + +/**========================================================================================================================== + * MARGIN + * ========================================================================================================================== + */ + +export interface GetMarginAccountsResp { + currency_pair: string; + locked: boolean; + risk: string; + base: { + currency: string; + available: string; + locked: string; + borrowed: string; + interest: string; + }; + quote: { + currency: string; + available: string; + locked: string; + borrowed: string; + interest: string; + }; +} + +export interface GetMarginBalanceHistoryResp { + id: string; + time: string; + time_ms: number; + currency: string; + currency_pair: string; + change: string; + balance: string; + type: string; +} diff --git a/src/types/shared.ts b/src/types/shared.ts index 13bbbd0..870e392 100644 --- a/src/types/shared.ts +++ b/src/types/shared.ts @@ -2,3 +2,336 @@ export type GateBaseUrlKey = | 'live' | 'futuresLiveAlternative' | 'futuresTestnet'; + +// interfaces + +export interface SubAccountKey { + user_id?: string; + mode?: number; + name?: string; + perms?: { + name?: + | 'wallet' + | 'spot' + | 'futures' + | 'delivery' + | 'earn' + | 'options' + | 'account' + | 'unified' + | 'loan'; + read_only?: boolean; + }[]; + ip_whitelist?: string[]; + key?: string; + state?: number; + created_at?: number; + updated_at?: number; + last_access?: number; +} + +export interface CurrencyPair { + id?: string; + base?: string; + quote?: string; + fee?: string; + min_base_amount?: string; + min_quote_amount?: string; + max_base_amount?: string; + max_quote_amount?: string; + amount_precision?: number; + precision?: number; + trade_status?: 'untradable' | 'buyable' | 'sellable' | 'tradable'; + sell_start?: number; + buy_start?: number; +} + +export interface Order { + id?: string; + text?: string; + amend_text?: string; + create_time?: string; + update_time?: string; + create_time_ms?: number; + update_time_ms?: number; + status?: 'open' | 'closed' | 'cancelled'; + currency_pair: string; + type?: 'limit' | 'market'; + account?: 'spot' | 'margin' | 'cross_margin' | 'unified'; + side: 'buy' | 'sell'; + amount: string; + price?: string; + time_in_force?: 'gtc' | 'ioc' | 'poc' | 'fok'; + iceberg?: string; + auto_borrow?: boolean; + auto_repay?: boolean; + left?: string; + filled_amount?: string; + fill_price?: string; + filled_total?: string; + avg_deal_price?: string; + fee?: string; + fee_currency?: string; + point_fee?: string; + gt_fee?: string; + gt_maker_fee?: string; + gt_taker_fee?: string; + gt_discount?: boolean; + rebated_fee?: string; + rebated_fee_currency?: string; + stp_id?: number; + stp_act?: 'cn' | 'co' | 'cb' | '-'; + finish_as?: 'open' | 'filled' | 'cancelled' | 'ioc' | 'stp'; + action_mode?: 'ACK' | 'RESULT' | 'FULL'; +} + +export interface CancelBatchOrder { + currency_pair: string; + id: string; + account?: 'cross_margin'; + action_mode?: 'ACK' | 'RESULT' | 'FULL'; +} + +export interface SpotPriceTriggeredOrder { + trigger: { + price: string; + rule: '>=' | '<='; + expiration: number; + }; + put: { + type?: 'limit' | 'market'; + side: 'buy' | 'sell'; + price: string; + amount: string; + account: 'normal' | 'margin' | 'cross_margin'; + time_in_force?: 'gtc' | 'ioc'; + text?: string; + }; + id?: number; + user?: number; + market: string; + ctime?: number; + ftime?: number; + fired_order_id?: number; + status?: 'open' | 'cancelled' | 'finish' | 'failed' | 'expired'; + reason?: string; +} + +export interface Contract { + name?: string; + type?: 'inverse' | 'direct'; + quanto_multiplier?: string; + leverage_min?: string; + leverage_max?: string; + maintenance_rate?: string; + mark_type?: 'internal' | 'index'; + mark_price?: string; + index_price?: string; + last_price?: string; + maker_fee_rate?: string; + taker_fee_rate?: string; + order_price_round?: string; + mark_price_round?: string; + funding_rate?: string; + funding_interval?: number; + funding_next_apply?: number; + risk_limit_base?: string; + risk_limit_step?: string; + risk_limit_max?: string; + order_size_min?: number; + order_size_max?: number; + order_price_deviate?: string; + ref_discount_rate?: string; + ref_rebate_rate?: string; + orderbook_id?: number; + trade_id?: number; + trade_size?: number; + position_size?: number; + config_change_time?: number; + in_delisting?: boolean; + orders_limit?: number; + enable_bonus?: boolean; + enable_credit?: boolean; + create_time?: number; + funding_cap_ratio?: string; +} + +export interface Position { + user?: number; + contract?: string; + size?: number; + leverage?: string; + risk_limit?: string; + leverage_max?: string; + maintenance_rate?: string; + value?: string; + margin?: string; + entry_price?: string; + liq_price?: string; + mark_price?: string; + initial_margin?: string; + maintenance_margin?: string; + unrealised_pnl?: string; + realised_pnl?: string; + pnl_pnl?: string; + pnl_fund?: string; + pnl_fee?: string; + history_pnl?: string; + last_close_pnl?: string; + realised_point?: string; + history_point?: string; + adl_ranking?: number; + pending_orders?: number; + close_order?: { + id?: number; + price?: string; + is_liq?: boolean; + } | null; + mode?: 'single' | 'dual_long' | 'dual_short'; + cross_leverage_limit?: string; + update_time?: number; + open_time?: number; +} + +export interface FuturesOrder { + id?: number; + user?: number; + create_time?: number; + finish_time?: number; + finish_as?: + | 'filled' + | 'cancelled' + | 'liquidated' + | 'ioc' + | 'auto_deleveraged' + | 'reduce_only' + | 'position_closed' + | 'stp'; + status?: 'open' | 'finished'; + contract: string; + size: number; + iceberg?: number; + price?: string; + close?: boolean; + is_close?: boolean; + reduce_only?: boolean; + is_reduce_only?: boolean; + is_liq?: boolean; + tif?: 'gtc' | 'ioc' | 'poc' | 'fok'; + left?: number; + fill_price?: string; + text?: string; + tkfr?: string; + mkfr?: string; + refu?: number; + auto_size?: 'close_long' | 'close_short'; + stp_id?: number; + stp_act?: 'cn' | 'co' | 'cb' | '-'; + amend_text?: string; + biz_info?: string; +} + +export interface FuturesPriceTriggeredOrder { + initial: { + contract: string; + size?: number; + price?: string; + close?: boolean; + tif?: 'gtc' | 'ioc'; + text?: string; + reduce_only?: boolean; + auto_size?: string; + is_reduce_only?: boolean; + is_close?: boolean; + }; + trigger: { + strategy_type?: 0 | 1; + price_type?: 0 | 1 | 2; + price?: string; + rule?: 1 | 2; + expiration?: number; + }; + id?: number; + user?: number; + create_time?: number; + finish_time?: number; + trade_id?: number; + status?: 'open' | 'finished' | 'inactive' | 'invalid'; + finish_as?: 'cancelled' | 'succeeded' | 'failed' | 'expired'; + reason?: string; + order_type?: + | 'close-long-order' + | 'close-short-order' + | 'close-long-position' + | 'close-short-position' + | 'plan-close-long-position' + | 'plan-close-short-position'; + me_order_id?: number; +} + +export interface DeliveryContract { + name?: string; + underlying?: string; + cycle?: 'WEEKLY' | 'BI-WEEKLY' | 'QUARTERLY' | 'BI-QUARTERLY'; + type?: 'inverse' | 'direct'; + quanto_multiplier?: string; + leverage_min?: string; + leverage_max?: string; + maintenance_rate?: string; + mark_type?: 'internal' | 'index'; + mark_price?: string; + index_price?: string; + last_price?: string; + maker_fee_rate?: string; + taker_fee_rate?: string; + order_price_round?: string; + mark_price_round?: string; + basis_rate?: string; + basis_value?: string; + basis_impact_value?: string; + settle_price?: string; + settle_price_interval?: number; + settle_price_duration?: number; + expire_time?: number; + risk_limit_base?: string; + risk_limit_step?: string; + risk_limit_max?: string; + order_size_min?: number; + order_size_max?: number; + order_price_deviate?: string; + ref_discount_rate?: string; + ref_rebate_rate?: string; + orderbook_id?: number; + trade_id?: number; + trade_size?: number; + position_size?: number; + config_change_time?: number; + in_delisting?: boolean; + orders_limit?: number; +} + +export interface Withdraw { + id: string; + txid: string; + withdraw_order_id: string; + timestamp: string; + amount: string; + currency: string; + address: string; + memo?: string; + status: + | 'DONE' + | 'CANCEL' + | 'REQUEST' + | 'MANUAL' + | 'BCODE' + | 'EXTPEND' + | 'FAIL' + | 'INVALID' + | 'VERIFY' + | 'PROCES' + | 'PEND' + | 'DMOVE' + | 'SPLITPEND'; + chain: string; +} From 93c43326206621ea5b45b6a1556326c41a01520e Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Mon, 20 May 2024 18:28:22 +0200 Subject: [PATCH 2/3] chore(): Refactoring types --- src/RestClient.ts | 29 +++-------------------------- src/types/response/shared.types.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/RestClient.ts b/src/RestClient.ts index 4df9429..e4c29bc 100644 --- a/src/RestClient.ts +++ b/src/RestClient.ts @@ -40,6 +40,7 @@ import { CreateSubAccountApiKeyResp, DeleteSpotBatchOrdersResp, GetBalancesResp, + GetCrossMarginCurrenciesResp, GetCurrencyChainsResp, GetMarginAccountsResp, GetMarginBalanceHistoryResp, @@ -1327,34 +1328,10 @@ export class RestClient extends BaseRestClient { /** * Currencies supported by cross margin * - * @returns Promise> + * @returns Promise> */ getCrossMarginCurrencies(): Promise< - APIResponse< - { - name: string; - rate: string; - prec: string; - discount: string; - min_borrow_amount: string; - user_max_borrow_amount: string; - total_max_borrow_amount: string; - price: string; - loanable: boolean; - status: number; - }[] - > + APIResponse > { return this.get('/margin/cross/currencies'); } diff --git a/src/types/response/shared.types.ts b/src/types/response/shared.types.ts index a45d372..3b476c4 100644 --- a/src/types/response/shared.types.ts +++ b/src/types/response/shared.types.ts @@ -583,3 +583,16 @@ export interface GetMarginBalanceHistoryResp { balance: string; type: string; } + +export interface GetCrossMarginCurrenciesResp { + name: string; + rate: string; + prec: string; + discount: string; + min_borrow_amount: string; + user_max_borrow_amount: string; + total_max_borrow_amount: string; + price: string; + loanable: boolean; + status: number; +} From 91c0c40abf9c12217146fe451bf8fa186feb9d97 Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Mon, 20 May 2024 18:45:09 +0200 Subject: [PATCH 3/3] chore(): refactoring types --- src/RestClient.ts | 335 ++++------------------------- src/types/requests/shared.types.ts | 44 ++++ src/types/response/shared.types.ts | 59 +++++ 3 files changed, 145 insertions(+), 293 deletions(-) diff --git a/src/RestClient.ts b/src/RestClient.ts index e4c29bc..9b3d4dd 100644 --- a/src/RestClient.ts +++ b/src/RestClient.ts @@ -12,6 +12,10 @@ import { import { RestClientOptions } from './lib/requestUtils.js'; import { DeleteSpotOrderReq, + GetCrossMarginAccountHistoryReq, + GetCrossMarginBorrowHistoryReq, + GetCrossMarginInterestRecordsReq, + GetCrossMarginRepaymentsReq, GetMainSubTransfersReq, GetMarginBalanceHistoryReq, GetSavedAddressReq, @@ -29,6 +33,7 @@ import { GetWithdrawalDepositRecordsReq, PortfolioMarginCalculatorReq, SetUnifiedAccountModeReq, + SubmitCrossMarginBorrowLoanReq, SubmitSpotClosePosCrossDisabledReq, SubmitSpotOrderReq, SubmitUnifiedBorrowOrRepayReq, @@ -40,6 +45,8 @@ import { CreateSubAccountApiKeyResp, DeleteSpotBatchOrdersResp, GetBalancesResp, + GetCrossMarginAccountHistoryResp, + GetCrossMarginAccountResp, GetCrossMarginCurrenciesResp, GetCurrencyChainsResp, GetMarginAccountsResp, @@ -72,6 +79,7 @@ import { SubAccountMarginBalancesResp, SubAccountResp, SubAccountTransferRecordResp, + SubmitCrossMarginBorrowLoanResp, SubmitSpotBatchOrdersResp, } from './types/response/shared.types.js'; import { @@ -1340,104 +1348,20 @@ export class RestClient extends BaseRestClient { * Retrieve detail of one single currency supported by cross margin * * @param params Parameters containing the currency name - * @returns Promise> + * @returns Promise> */ - getCrossMarginCurrency(params: { currency: string }): Promise< - APIResponse<{ - name: string; - rate: string; - prec: string; - discount: string; - min_borrow_amount: string; - user_max_borrow_amount: string; - total_max_borrow_amount: string; - price: string; - loanable: boolean; - status: number; - }> - > { + getCrossMarginCurrency(params: { + currency: string; + }): Promise> { return this.get(`/margin/cross/currencies/${params.currency}`); } /** * Retrieve cross margin account * - * @returns Promise> + * @returns Promise> */ - getCrossMarginAccount(): Promise< - APIResponse<{ - user_id: number; - refresh_time: number; - locked: boolean; - balances: { - [currency: string]: { - available: string; - freeze: string; - borrowed: string; - interest: string; - negative_liab: string; - futures_pos_liab: string; - equity: string; - total_freeze: string; - total_liab: string; - }; - }; - total: string; - borrowed: string; - interest: string; - risk: string; - total_initial_margin: string; - total_margin_balance: string; - total_maintenance_margin: string; - total_initial_margin_rate: string; - total_maintenance_margin_rate: string; - total_available_margin: string; - portfolio_margin_total: string; - portfolio_margin_total_liab: string; - portfolio_margin_total_equity: string; - }> - > { + getCrossMarginAccount(): Promise> { return this.getPrivate('/margin/cross/accounts'); } @@ -1447,34 +1371,11 @@ export class RestClient extends BaseRestClient { * Record time range cannot exceed 30 days. * * @param params Parameters for retrieving cross margin account change history - * @returns Promise> + * @returns Promise> */ - getCrossMarginAccountHistory(params?: { - currency?: string; - from?: number; - to?: number; - page?: number; - limit?: number; - type?: string; - }): Promise< - APIResponse< - { - id: string; - time: number; - currency: string; - change: string; - balance: string; - type: string; - }[] - > - > { + getCrossMarginAccountHistory( + params?: GetCrossMarginAccountHistoryReq, + ): Promise> { return this.getPrivate('/margin/cross/account_book', params); } @@ -1484,37 +1385,11 @@ export class RestClient extends BaseRestClient { * Borrow amount cannot be less than currency minimum borrow amount. * * @param params Parameters for creating a cross margin borrow loan - * @returns Promise> + * @returns Promise> */ - submitCrossMarginBorrowLoan(body: { - currency: string; - amount: string; - text?: string; - }): Promise< - APIResponse<{ - id: string; - create_time: number; - update_time: number; - currency: string; - amount: string; - text?: string; - status: number; - repaid: string; - repaid_interest: string; - unpaid_interest: string; - }> - > { + submitCrossMarginBorrowLoan( + body: SubmitCrossMarginBorrowLoanReq, + ): Promise> { return this.postPrivate('/margin/cross/loans', body); } @@ -1524,41 +1399,11 @@ export class RestClient extends BaseRestClient { * Sort by creation time in descending order by default. Set reverse=false to return ascending results. * * @param params Parameters for listing cross margin borrow history - * @returns Promise> + * @returns Promise> */ - getCrossMarginBorrowHistory(params: { - status: number; - currency?: string; - limit?: number; - offset?: number; - reverse?: boolean; - }): Promise< - APIResponse< - { - id: string; - create_time: number; - update_time: number; - currency: string; - amount: string; - text: string; - status: number; - repaid: string; - repaid_interest: string; - unpaid_interest: string; - }[] - > - > { + getCrossMarginBorrowHistory( + params: GetCrossMarginBorrowHistoryReq, + ): Promise> { return this.getPrivate('/margin/cross/loans', params); } @@ -1566,33 +1411,11 @@ export class RestClient extends BaseRestClient { * Retrieve single borrow loan detail * * @param params Parameters containing the borrow loan ID - * @returns Promise> + * @returns Promise> */ - getCrossMarginBorrowLoan(params: { loan_id: string }): Promise< - APIResponse<{ - id: string; - create_time: number; - update_time: number; - currency: string; - amount: string; - text: string; - status: number; - repaid: string; - repaid_interest: string; - unpaid_interest: string; - }> - > { + getCrossMarginBorrowLoan(params: { + loan_id: string; + }): Promise> { return this.getPrivate(`/margin/cross/loans/${params.loan_id}`); } /** @@ -1601,38 +1424,12 @@ export class RestClient extends BaseRestClient { * When the liquidity of the currency is insufficient and the transaction risk is high, the currency will be disabled, and funds cannot be transferred. When the available balance of cross-margin is insufficient, the balance of the spot account can be used for repayment. Please ensure that the balance of the spot account is sufficient, and system uses cross-margin account for repayment first. * * @param params Parameters for cross margin repayments - * @returns Promise> + * @returns Promise> */ submitCrossMarginRepayment(body: { currency: string; amount: string; - }): Promise< - APIResponse< - { - id: string; - create_time: number; - update_time: number; - currency: string; - amount: string; - text?: string; - status: number; - repaid: string; - repaid_interest: string; - unpaid_interest: string; - }[] - > - > { + }): Promise> { return this.postPrivate('/margin/cross/repayments', body); } @@ -1642,35 +1439,11 @@ export class RestClient extends BaseRestClient { * Sort by creation time in descending order by default. Set reverse=false to return ascending results. * * @param params Parameters for retrieving cross margin repayments - * @returns Promise> + * @returns Promise> */ - getCrossMarginRepayments(params?: { - currency?: string; - loan_id?: string; - limit?: number; - offset?: number; - reverse?: boolean; - }): Promise< - APIResponse< - { - id: string; - create_time: number; - loan_id: string; - currency: string; - principal: string; - interest: string; - repayment_type: string; - }[] - > - > { + getCrossMarginRepayments( + params?: GetCrossMarginRepaymentsReq, + ): Promise> { return this.getPrivate('/margin/cross/repayments', params); } @@ -1678,35 +1451,11 @@ export class RestClient extends BaseRestClient { * Interest records for the cross margin account * * @param params Parameters for retrieving interest records - * @returns Promise> + * @returns Promise> */ - getCrossMarginInterestRecords(params?: { - currency?: string; - page?: number; - limit?: number; - from?: number; - to?: number; - }): Promise< - APIResponse< - { - currency: string; - currency_pair: string; - actual_rate: string; - interest: string; - status: number; - type: string; - create_time: number; - }[] - > - > { + getCrossMarginInterestRecords( + params?: GetCrossMarginInterestRecordsReq, + ): Promise> { return this.getPrivate('/margin/cross/interest_records', params); } @@ -1734,11 +1483,11 @@ export class RestClient extends BaseRestClient { * Please note that the interest rates are subject to change based on the borrowing and lending demand, and therefore, the provided rates may not be entirely accurate. * * @param params Parameters for retrieving estimated interest rates - * @returns Promise>> + * @returns Promise> */ getEstimatedInterestRates(params: { currencies: string[]; - }): Promise>> { + }): Promise> { return this.getPrivate('/margin/cross/estimate_rate', params); } diff --git a/src/types/requests/shared.types.ts b/src/types/requests/shared.types.ts index 44a8137..49733f8 100644 --- a/src/types/requests/shared.types.ts +++ b/src/types/requests/shared.types.ts @@ -227,3 +227,47 @@ export interface GetMarginBalanceHistoryReq { page?: number; limit?: number; } + +export interface GetCrossMarginAccountHistoryReq { + currency?: string; + from?: number; + to?: number; + page?: number; + limit?: number; + type?: string; +} + +export interface SubmitCrossMarginBorrowLoanReq { + currency: string; + amount: string; + text?: string; +} + +export interface GetCrossMarginBorrowHistoryReq { + status: number; + currency?: string; + limit?: number; + offset?: number; + reverse?: boolean; +} + +export interface GetCrossMarginRepaymentsReq { + currency?: string; + loan_id?: string; + limit?: number; + offset?: number; + reverse?: boolean; +} + +export interface GetCrossMarginInterestRecordsReq { + currency?: string; + page?: number; + limit?: number; + from?: number; + to?: number; +} + +/**========================================================================================================================== + * MARGIN UNI + * ========================================================================================================================== + */ diff --git a/src/types/response/shared.types.ts b/src/types/response/shared.types.ts index 3b476c4..af10b5f 100644 --- a/src/types/response/shared.types.ts +++ b/src/types/response/shared.types.ts @@ -596,3 +596,62 @@ export interface GetCrossMarginCurrenciesResp { loanable: boolean; status: number; } + +export interface GetCrossMarginAccountResp { + user_id: number; + refresh_time: number; + locked: boolean; + balances: { + [currency: string]: { + available: string; + freeze: string; + borrowed: string; + interest: string; + negative_liab: string; + futures_pos_liab: string; + equity: string; + total_freeze: string; + total_liab: string; + }; + }; + total: string; + borrowed: string; + interest: string; + risk: string; + total_initial_margin: string; + total_margin_balance: string; + total_maintenance_margin: string; + total_initial_margin_rate: string; + total_maintenance_margin_rate: string; + total_available_margin: string; + portfolio_margin_total: string; + portfolio_margin_total_liab: string; + portfolio_margin_total_equity: string; +} + +export interface GetCrossMarginAccountHistoryResp { + id: string; + time: number; + currency: string; + change: string; + balance: string; + type: string; +} + +export interface SubmitCrossMarginBorrowLoanResp { + id: string; + create_time: number; + update_time: number; + currency: string; + amount: string; + text?: string; + status: number; + repaid: string; + repaid_interest: string; + unpaid_interest: string; +} + +/**========================================================================================================================== + * MARGIN UNI + * ========================================================================================================================== + */