| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790 |
- import { FutureConnector } from '../../src/exchanges/binance/FutureConnector'
- const describeBinance = process.env.TEST_BINANCE === '1' ? describe : describe.skip
- import { DerivativesTradingUsdsFuturesRestAPI } from '@binance/derivatives-trading-usds-futures'
- // Mock Binance API client
- jest.mock('@binance/derivatives-trading-usds-futures', () => ({
- DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL: 'https://fapi.binance.com',
- DerivativesTradingUsdsFutures: jest.fn(),
- DerivativesTradingUsdsFuturesRestAPI: {
- NewOrderSideEnum: {
- BUY: 'BUY',
- SELL: 'SELL',
- },
- NewOrderPositionSideEnum: {
- LONG: 'LONG',
- SHORT: 'SHORT',
- BOTH: 'BOTH',
- },
- NewOrderTimeInForceEnum: {
- GTC: 'GTC',
- IOC: 'IOC',
- FOK: 'FOK',
- GTX: 'GTX',
- GTD: 'GTD',
- },
- NewOrderNewOrderRespTypeEnum: {
- ACK: 'ACK',
- RESULT: 'RESULT',
- FULL: 'FULL',
- },
- ChangeMarginTypeMarginTypeEnum: {
- ISOLATED: 'ISOLATED',
- CROSSED: 'CROSSED',
- },
- ModifyOrderSideEnum: {
- BUY: 'BUY',
- SELL: 'SELL',
- },
- },
- }))
- describeBinance('FutureConnector', () => {
- let connector: FutureConnector
- let mockClient: any
- beforeEach(() => {
- // 创建模拟的 API 客户端
- mockClient = {
- restAPI: {
- accountInformationV3: jest.fn(),
- newOrder: jest.fn(),
- modifyOrder: jest.fn(),
- currentAllOpenOrders: jest.fn(),
- cancelOrder: jest.fn(),
- cancelAllOpenOrders: jest.fn(),
- allOrders: jest.fn(),
- accountTradeList: jest.fn(),
- changeInitialLeverage: jest.fn(),
- changeMarginType: jest.fn(),
- },
- }
- // Mock 构造函数
- const { DerivativesTradingUsdsFutures } = require('@binance/derivatives-trading-usds-futures')
- DerivativesTradingUsdsFutures.mockImplementation(() => mockClient)
- connector = new FutureConnector('test-api-key', 'test-api-secret')
- })
- afterEach(() => {
- jest.clearAllMocks()
- })
- describe('Constructor', () => {
- test('should create instance with API credentials', () => {
- expect(connector).toBeInstanceOf(FutureConnector)
- expect(connector.client).toBeDefined()
- })
- test('should initialize with correct configuration', () => {
- const { DerivativesTradingUsdsFutures } = require('@binance/derivatives-trading-usds-futures')
- expect(DerivativesTradingUsdsFutures).toHaveBeenCalledWith({
- configurationRestAPI: {
- apiKey: 'test-api-key',
- apiSecret: 'test-api-secret',
- basePath: 'https://fapi.binance.com',
- },
- })
- })
- })
- describe('getAssetsInfo', () => {
- test('should return assets with positive wallet balance', async () => {
- const mockData = {
- assets: [
- { symbol: 'USDT', walletBalance: '1000.0' },
- { symbol: 'BTC', walletBalance: '0.0' },
- { symbol: 'ETH', walletBalance: '5.0' },
- ],
- }
- mockClient.restAPI.accountInformationV3.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockData),
- })
- const result = await connector.getAssetsInfo()
- expect(result).toEqual([
- { symbol: 'USDT', walletBalance: '1000.0' },
- { symbol: 'ETH', walletBalance: '5.0' },
- ])
- expect(mockClient.restAPI.accountInformationV3).toHaveBeenCalled()
- })
- test('should handle API errors gracefully', async () => {
- mockClient.restAPI.accountInformationV3.mockRejectedValue(new Error('API Error'))
- const result = await connector.getAssetsInfo()
- expect(result).toEqual([])
- })
- })
- describe('getPositonInfo', () => {
- test('should return positions with positive position amount', async () => {
- const mockData = {
- positions: [
- { symbol: 'BTCUSDT', positionAmt: '0.1' },
- { symbol: 'ETHUSDT', positionAmt: '0.0' },
- { symbol: 'ADAUSDT', positionAmt: '100.0' },
- ],
- }
- mockClient.restAPI.accountInformationV3.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockData),
- })
- const result = await connector.getPositonInfo()
- expect(result).toEqual([
- { symbol: 'BTCUSDT', positionAmt: '0.1' },
- { symbol: 'ADAUSDT', positionAmt: '100.0' },
- ])
- })
- test('should handle API errors gracefully', async () => {
- mockClient.restAPI.accountInformationV3.mockRejectedValue(new Error('API Error'))
- const result = await connector.getPositonInfo()
- expect(result).toEqual([])
- })
- })
- describe('getPositionBalanceBySymbol', () => {
- test('should return position for existing symbol', async () => {
- const mockData = {
- positions: [
- { symbol: 'BTCUSDT', positionAmt: '0.1', unrealizedPnl: '10.5' },
- { symbol: 'ETHUSDT', positionAmt: '0.0', unrealizedPnl: '0.0' },
- ],
- }
- mockClient.restAPI.accountInformationV3.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockData),
- })
- const result = await connector.getPositionBalanceBySymbol('BTCUSDT')
- expect(result).toEqual({ symbol: 'BTCUSDT', positionAmt: '0.1', unrealizedPnl: '10.5' })
- })
- test('should return null for non-existing symbol', async () => {
- const mockData = {
- positions: [{ symbol: 'BTCUSDT', positionAmt: '0.1' }],
- }
- mockClient.restAPI.accountInformationV3.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockData),
- })
- const result = await connector.getPositionBalanceBySymbol('ETHUSDT')
- expect(result).toBeNull()
- })
- })
- describe('openPosition', () => {
- test('should place order with basic parameters', async () => {
- const mockOrderResult = {
- orderId: 12345,
- symbol: 'BTCUSDT',
- status: 'NEW',
- }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openPosition(
- 'BTCUSDT',
- DerivativesTradingUsdsFuturesRestAPI.NewOrderSideEnum.BUY,
- 0.001,
- 50000,
- )
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'LIMIT',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 50000,
- })
- })
- test('should place order with all optional parameters', async () => {
- const mockOrderResult = {
- orderId: 12346,
- symbol: 'ETHUSDT',
- status: 'NEW',
- }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const options = {
- type: 'LIMIT',
- timeInForce: DerivativesTradingUsdsFuturesRestAPI.NewOrderTimeInForceEnum.GTC,
- positionSide: DerivativesTradingUsdsFuturesRestAPI.NewOrderPositionSideEnum.LONG,
- reduceOnly: 'false',
- closePosition: 'false',
- activationPrice: 3000,
- callbackRate: 0.1,
- workingType: 'CONTRACT_PRICE',
- priceProtect: 'TRUE',
- newOrderRespType: DerivativesTradingUsdsFuturesRestAPI.NewOrderNewOrderRespTypeEnum.RESULT,
- newClientOrderId: 'test_order_123',
- stopPrice: 2800,
- icebergQty: 0.001,
- orderTag: 'test_tag',
- }
- const result = await connector.openPosition(
- 'ETHUSDT',
- DerivativesTradingUsdsFuturesRestAPI.NewOrderSideEnum.BUY,
- 0.01,
- 3000,
- options,
- )
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'ETHUSDT',
- side: 'BUY',
- type: 'LIMIT',
- timeInForce: 'GTC',
- quantity: 0.01,
- price: 3000,
- positionSide: 'LONG',
- reduceOnly: 'false',
- closePosition: 'false',
- activationPrice: 3000,
- callbackRate: 0.1,
- workingType: 'CONTRACT_PRICE',
- priceProtect: 'TRUE',
- newOrderRespType: 'RESULT',
- newClientOrderId: 'test_order_123',
- stopPrice: 2800,
- icebergQty: 0.001,
- orderTag: 'test_tag',
- })
- })
- test('should handle API errors gracefully', async () => {
- mockClient.restAPI.newOrder.mockRejectedValue(new Error('Order failed'))
- const result = await connector.openPosition(
- 'BTCUSDT',
- DerivativesTradingUsdsFuturesRestAPI.NewOrderSideEnum.BUY,
- 0.001,
- 50000,
- )
- expect(result).toBeNull()
- })
- })
- describe('openLimitOrder', () => {
- test('should place long limit order', async () => {
- const mockOrderResult = { orderId: 12347, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openLimitOrder('BTCUSDT', 'long', 0.001, 50000, 'GTC')
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'LIMIT',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 50000,
- positionSide: 'LONG',
- })
- })
- test('should place short limit order', async () => {
- const mockOrderResult = { orderId: 12348, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openLimitOrder('BTCUSDT', 'short', 0.001, 50000, 'GTC')
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'SELL',
- type: 'LIMIT',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 50000,
- positionSide: 'SHORT',
- })
- })
- })
- describe('openMarketOrder', () => {
- test('should place long market order', async () => {
- const mockOrderResult = { orderId: 12349, status: 'FILLED' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openMarketOrder('BTCUSDT', 'long', 0.001)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- positionSide: 'LONG',
- })
- })
- test('should place short market order', async () => {
- const mockOrderResult = { orderId: 12350, status: 'FILLED' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openMarketOrder('BTCUSDT', 'short', 0.001)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'SELL',
- type: 'MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- positionSide: 'SHORT',
- })
- })
- })
- describe('openStopOrder', () => {
- test('should place stop market order', async () => {
- const mockOrderResult = { orderId: 12351, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openStopOrder('BTCUSDT', 'long', 0.001, 48000)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'STOP_MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- stopPrice: 48000,
- reduceOnly: 'true',
- positionSide: 'LONG',
- })
- })
- test('should place stop limit order', async () => {
- const mockOrderResult = { orderId: 12352, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openStopOrder('BTCUSDT', 'short', 0.001, 52000, 'STOP', 51900, 'GTC')
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'SELL',
- type: 'STOP',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 51900,
- stopPrice: 52000,
- reduceOnly: 'true',
- positionSide: 'SHORT',
- })
- })
- })
- describe('openTakeProfitOrder', () => {
- test('should place take profit market order', async () => {
- const mockOrderResult = { orderId: 12353, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openTakeProfitOrder('BTCUSDT', 'long', 0.001, 52000)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'TAKE_PROFIT_MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- stopPrice: 52000,
- reduceOnly: 'true',
- positionSide: 'LONG',
- })
- })
- })
- describe('openTrailingStopOrder', () => {
- test('should place trailing stop order', async () => {
- const mockOrderResult = { orderId: 12354, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openTrailingStopOrder('BTCUSDT', 'long', 0.001, 50000, 0.1)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'TRAILING_STOP_MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- activationPrice: 50000,
- callbackRate: 0.1,
- reduceOnly: 'true',
- positionSide: 'LONG',
- })
- })
- })
- describe('openIcebergOrder', () => {
- test('should place iceberg order', async () => {
- const mockOrderResult = { orderId: 12355, status: 'NEW' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.openIcebergOrder('BTCUSDT', 'long', 0.01, 50000, 0.001, 'GTC')
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'LIMIT',
- timeInForce: 'GTC',
- quantity: 0.01,
- price: 50000,
- icebergQty: 0.001,
- positionSide: 'LONG',
- })
- })
- })
- describe('closeLongPosition', () => {
- test('should close long position', async () => {
- const mockOrderResult = { orderId: 12356, status: 'FILLED' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.closeLongPosition('BTCUSDT', 0.001)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'SELL',
- type: 'MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- reduceOnly: 'true',
- positionSide: 'LONG',
- })
- })
- })
- describe('closeShortPosition', () => {
- test('should close short position', async () => {
- const mockOrderResult = { orderId: 12357, status: 'FILLED' }
- mockClient.restAPI.newOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrderResult),
- })
- const result = await connector.closeShortPosition('BTCUSDT', 0.001)
- expect(result).toEqual(mockOrderResult)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- type: 'MARKET',
- timeInForce: 'GTC',
- quantity: 0.001,
- price: 0,
- reduceOnly: 'true',
- positionSide: 'SHORT',
- })
- })
- })
- describe('closeAllPositions', () => {
- test('should close all positions', async () => {
- const mockPositions = [
- { symbol: 'BTCUSDT', positionAmt: '0.1' },
- { symbol: 'ETHUSDT', positionAmt: '-0.05' },
- ]
- mockClient.restAPI.accountInformationV3.mockResolvedValue({
- data: jest.fn().mockResolvedValue({ positions: mockPositions }),
- })
- const mockOrderResults = [
- { orderId: 12358, status: 'FILLED' },
- { orderId: 12359, status: 'FILLED' },
- ]
- mockClient.restAPI.newOrder
- .mockResolvedValueOnce({ data: jest.fn().mockResolvedValue(mockOrderResults[0]) })
- .mockResolvedValueOnce({ data: jest.fn().mockResolvedValue(mockOrderResults[1]) })
- const result = await connector.closeAllPositions()
- expect(result).toEqual(mockOrderResults)
- expect(mockClient.restAPI.newOrder).toHaveBeenCalledTimes(2)
- })
- })
- describe('cancelOrder', () => {
- test('should cancel order by orderId', async () => {
- const mockResult = { orderId: 12360, status: 'CANCELED' }
- mockClient.restAPI.cancelOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.cancelOrder('BTCUSDT', 12360)
- expect(mockClient.restAPI.cancelOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- orderId: 12360,
- origClientOrderId: undefined,
- })
- })
- test('should cancel order by clientOrderId', async () => {
- const mockResult = { orderId: 12361, status: 'CANCELED' }
- mockClient.restAPI.cancelOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.cancelOrder('BTCUSDT', undefined, 'client_order_123')
- expect(mockClient.restAPI.cancelOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- orderId: undefined,
- origClientOrderId: 'client_order_123',
- })
- })
- })
- describe('cancelAllOrders', () => {
- test('should cancel all orders for symbol', async () => {
- const mockResult = [{ orderId: 12362, status: 'CANCELED' }]
- mockClient.restAPI.cancelAllOpenOrders.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.cancelAllOrders('BTCUSDT')
- expect(mockClient.restAPI.cancelAllOpenOrders).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- })
- })
- })
- describe('getOrderHistory', () => {
- test('should get order history', async () => {
- const mockOrders = [
- { orderId: 12363, symbol: 'BTCUSDT', status: 'FILLED' },
- { orderId: 12364, symbol: 'BTCUSDT', status: 'CANCELED' },
- ]
- mockClient.restAPI.allOrders.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrders),
- })
- const result = await connector.getOrderHistory('BTCUSDT', 1640995200000, 1641081600000, 100)
- expect(result).toEqual(mockOrders)
- expect(mockClient.restAPI.allOrders).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- startTime: 1640995200000,
- endTime: 1641081600000,
- limit: 100,
- })
- })
- })
- describe('getTradeHistory', () => {
- test('should get trade history', async () => {
- const mockTrades = [
- { id: 12365, symbol: 'BTCUSDT', price: '50000', qty: '0.001' },
- { id: 12366, symbol: 'BTCUSDT', price: '50100', qty: '0.002' },
- ]
- mockClient.restAPI.accountTradeList.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockTrades),
- })
- const result = await connector.getTradeHistory('BTCUSDT', 1640995200000, 1641081600000, 100)
- expect(result).toEqual(mockTrades)
- expect(mockClient.restAPI.accountTradeList).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- startTime: 1640995200000,
- endTime: 1641081600000,
- limit: 100,
- })
- })
- })
- describe('setLeverage', () => {
- test('should set leverage', async () => {
- const mockResult = { leverage: 10, maxNotionalValue: '1000000' }
- mockClient.restAPI.changeInitialLeverage.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.setLeverage('BTCUSDT', 10)
- expect(mockClient.restAPI.changeInitialLeverage).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- leverage: 10,
- })
- })
- })
- describe('setMarginType', () => {
- test('should set margin type to isolated', async () => {
- const mockResult = { code: 200, msg: 'success' }
- mockClient.restAPI.changeMarginType.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.setMarginType(
- 'BTCUSDT',
- DerivativesTradingUsdsFuturesRestAPI.ChangeMarginTypeMarginTypeEnum.ISOLATED,
- )
- expect(mockClient.restAPI.changeMarginType).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- marginType: 'ISOLATED',
- })
- })
- test('should set margin type to crossed', async () => {
- const mockResult = { code: 200, msg: 'success' }
- mockClient.restAPI.changeMarginType.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.setMarginType(
- 'BTCUSDT',
- DerivativesTradingUsdsFuturesRestAPI.ChangeMarginTypeMarginTypeEnum.CROSSED,
- )
- expect(mockClient.restAPI.changeMarginType).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- marginType: 'CROSSED',
- })
- })
- })
- describe('getAllPositions', () => {
- test('should get all positions', async () => {
- const mockPositions = [
- { symbol: 'BTCUSDT', positionAmt: '0.1', unrealizedPnl: '10.5' },
- { symbol: 'ETHUSDT', positionAmt: '0.0', unrealizedPnl: '0.0' },
- { symbol: 'ADAUSDT', positionAmt: '-100.0', unrealizedPnl: '-5.2' },
- ]
- mockClient.restAPI.accountInformationV3.mockResolvedValue({
- data: jest.fn().mockResolvedValue({ positions: mockPositions }),
- })
- const result = await connector.getAllPositions()
- expect(result).toEqual(mockPositions)
- expect(mockClient.restAPI.accountInformationV3).toHaveBeenCalled()
- })
- })
- describe('getCurrentAllOpenPosition', () => {
- test('should get current open orders', async () => {
- const mockOrders = [
- { orderId: 12367, symbol: 'BTCUSDT', status: 'NEW' },
- { orderId: 12368, symbol: 'ETHUSDT', status: 'NEW' },
- ]
- mockClient.restAPI.currentAllOpenOrders.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockOrders),
- })
- await connector.getCurrentAllOpenPosition()
- expect(mockClient.restAPI.currentAllOpenOrders).toHaveBeenCalled()
- })
- test('should handle API errors gracefully', async () => {
- mockClient.restAPI.currentAllOpenOrders.mockRejectedValue(new Error('API Error'))
- await expect(connector.getCurrentAllOpenPosition()).resolves.not.toThrow()
- })
- })
- describe('openModifyPosition', () => {
- test('should modify existing order', async () => {
- const mockResult = { orderId: 12369, status: 'REPLACED' }
- mockClient.restAPI.modifyOrder.mockResolvedValue({
- data: jest.fn().mockResolvedValue(mockResult),
- })
- const result = await connector.openModifyPosition(
- 'BTCUSDT',
- DerivativesTradingUsdsFuturesRestAPI.ModifyOrderSideEnum.BUY,
- 0.002,
- 51000,
- )
- expect(result).toEqual(mockResult)
- expect(mockClient.restAPI.modifyOrder).toHaveBeenCalledWith({
- symbol: 'BTCUSDT',
- side: 'BUY',
- quantity: 0.002,
- price: 51000,
- })
- })
- test('should handle API errors gracefully', async () => {
- mockClient.restAPI.modifyOrder.mockRejectedValue(new Error('Modify failed'))
- const result = await connector.openModifyPosition(
- 'BTCUSDT',
- DerivativesTradingUsdsFuturesRestAPI.ModifyOrderSideEnum.BUY,
- 0.002,
- 51000,
- )
- expect(result).toBeNull()
- })
- })
- })
|