From 3ff903012a7e05df15c6076359da57bb25e79622 Mon Sep 17 00:00:00 2001 From: JJ-Cro Date: Thu, 20 Feb 2025 11:48:54 +0100 Subject: [PATCH] feat(): added new rest EARN endpoints --- examples/apidoc/V5/Earn/get-product-info.js | 19 +++++ .../V5/Earn/get-stake-redeem-order-history.js | 19 +++++ .../apidoc/V5/Earn/get-staked-position.js | 19 +++++ examples/apidoc/V5/Earn/stake-redeem.js | 24 ++++++ src/rest-client-v5.ts | 79 +++++++++++++++++++ src/types/request/v5-earn.ts | 21 +++++ src/types/response/v5-earn.ts | 30 +++++++ 7 files changed, 211 insertions(+) create mode 100644 examples/apidoc/V5/Earn/get-product-info.js create mode 100644 examples/apidoc/V5/Earn/get-stake-redeem-order-history.js create mode 100644 examples/apidoc/V5/Earn/get-staked-position.js create mode 100644 examples/apidoc/V5/Earn/stake-redeem.js create mode 100644 src/types/request/v5-earn.ts create mode 100644 src/types/response/v5-earn.ts diff --git a/examples/apidoc/V5/Earn/get-product-info.js b/examples/apidoc/V5/Earn/get-product-info.js new file mode 100644 index 0000000..5421ea3 --- /dev/null +++ b/examples/apidoc/V5/Earn/get-product-info.js @@ -0,0 +1,19 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .getEarnProduct({ + category: 'FlexibleSaving', + coin: 'BTC', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Earn/get-stake-redeem-order-history.js b/examples/apidoc/V5/Earn/get-stake-redeem-order-history.js new file mode 100644 index 0000000..5f7200e --- /dev/null +++ b/examples/apidoc/V5/Earn/get-stake-redeem-order-history.js @@ -0,0 +1,19 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .getEarnOrderHistory({ + category: 'FlexibleSaving', + orderId: '0572b030-6a0b-423f-88c4-b6ce31c0c82d', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Earn/get-staked-position.js b/examples/apidoc/V5/Earn/get-staked-position.js new file mode 100644 index 0000000..fd49487 --- /dev/null +++ b/examples/apidoc/V5/Earn/get-staked-position.js @@ -0,0 +1,19 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .getEarnPosition({ + category: 'FlexibleSaving', + coin: 'USDT', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Earn/stake-redeem.js b/examples/apidoc/V5/Earn/stake-redeem.js new file mode 100644 index 0000000..178d970 --- /dev/null +++ b/examples/apidoc/V5/Earn/stake-redeem.js @@ -0,0 +1,24 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .submitStakeRedeem({ + category: 'FlexibleSaving', + orderType: 'Stake', + accountType: 'FUND', + amount: '0.35', + coin: 'BTC', + productId: '430', + orderLinkId: 'btc-earn-001', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/src/rest-client-v5.ts b/src/rest-client-v5.ts index fc48402..7a84e1a 100644 --- a/src/rest-client-v5.ts +++ b/src/rest-client-v5.ts @@ -193,6 +193,16 @@ import { WithdrawableAmountV5, WithdrawalRecordV5, } from './types'; +import { + GetEarnOrderHistoryParams, + GetEarnPositionParams, + SubmitStakeRedeemParams, +} from './types/request/v5-earn'; +import { + EarnOrderHistory, + EarnPosition, + EarnProduct, +} from './types/response/v5-earn'; import { REST_CLIENT_TYPE_ENUM } from './util'; import BaseRestClient from './util/BaseRestClient'; @@ -2546,4 +2556,73 @@ export class RestClientV5 extends BaseRestClient { ): Promise> { return this.postPrivate('/v5/broker/award/distribution-record', params); } + + /** + * + ****** EARN + * + */ + + /** + * Get Product Info for Earn products + * + * INFO: Do not need authentication + */ + getEarnProduct(params: { category: string; coin?: string }): Promise< + APIResponseV3WithTime<{ + list: EarnProduct[]; + }> + > { + return this.get('/v5/earn/product', params); + } + + /** + * Stake or Redeem Earn products + * + * INFO: API key needs "Earn" permission + * + * NOTE: In times of high demand for loans in the market for a specific cryptocurrency, + * the redemption of the principal may encounter delays and is expected to be processed + * within 48 hours. Once the redemption request is initiated, it cannot be canceled, + * and your principal will continue to earn interest until the process is completed. + */ + submitStakeRedeem(params: SubmitStakeRedeemParams): Promise< + APIResponseV3WithTime<{ + orderId: string; + orderLinkId: string; + }> + > { + return this.postPrivate('/v5/earn/place-order', params); + } + + /** + * Get Stake/Redeem Order History + * + * INFO: API key needs "Earn" permission + * + * Note: Either orderId or orderLinkId is required. If both are passed, + * make sure they're matched, otherwise returning empty result + */ + getEarnOrderHistory(params: GetEarnOrderHistoryParams): Promise< + APIResponseV3WithTime<{ + list: EarnOrderHistory[]; + }> + > { + return this.getPrivate('/v5/earn/order', params); + } + + /** + * Get Staked Position + * + * INFO: API key needs "Earn" permission + * + * Note: Fully redeemed position is also returned in the response + */ + getEarnPosition(params: GetEarnPositionParams): Promise< + APIResponseV3WithTime<{ + list: EarnPosition[]; + }> + > { + return this.getPrivate('/v5/earn/position', params); + } } diff --git a/src/types/request/v5-earn.ts b/src/types/request/v5-earn.ts new file mode 100644 index 0000000..b25c354 --- /dev/null +++ b/src/types/request/v5-earn.ts @@ -0,0 +1,21 @@ +export interface SubmitStakeRedeemParams { + category: string; + orderType: 'Stake' | 'Redeem'; + accountType: 'FUND' | 'UNIFIED'; + amount: string; + coin: string; + productId: string; + orderLinkId: string; +} + +export interface GetEarnOrderHistoryParams { + category: string; + orderId?: string; + orderLinkId?: string; +} + +export interface GetEarnPositionParams { + category: string; + productId?: string; + coin?: string; +} diff --git a/src/types/response/v5-earn.ts b/src/types/response/v5-earn.ts new file mode 100644 index 0000000..e25a07a --- /dev/null +++ b/src/types/response/v5-earn.ts @@ -0,0 +1,30 @@ +export interface EarnProduct { + category: string; + estimateApr: string; + coin: string; + minStakeAmount: string; + maxStakeAmount: string; + precision: string; + productId: string; + status: 'Available' | 'NotAvailable'; +} + +export interface EarnOrderHistory { + coin: string; + orderValue: string; + orderType: 'Redeem' | 'Stake'; + orderId: string; + orderLinkId: string; + status: 'Success' | 'Fail' | 'Pending'; + createdAt: string; + productId: string; + updatedAt: string; +} + +export interface EarnPosition { + coin: string; + productId: string; + amount: string; + totalPnl: string; + claimableYield: string; +}