Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ stream.latestTradeDetail$.subscribe((v) => {})
- [ ] Query historical transaction orders
* Listen Key
- [x] Generate Listen Key
- [ ] Extend Listen Key Validity period
- [ ] Delete Listen Key
- [x] Extend Listen Key Validity period
- [x] Delete Listen Key
* Socket API
* Market Data
- [ ] Subscribe Market Depth Data
Expand Down
54 changes: 54 additions & 0 deletions src/bingx-client/services/listen-key.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ApiAccount } from 'bingx-api/bingx/account/api-account';
import { BingxDeleteListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-delete-listen-key-endpoint';
import { BingxExtendListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-extend-listen-key-endpoint';
import { BingxGenerateListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-generate-listen-key-endpoint';
import { ListenKeyService } from 'bingx-api/bingx-client/services/listen-key.service';

describe('ListenKeyService', () => {
const account = new ApiAccount('api-key', 'secret-key');

it('executes generate listen key endpoint', async () => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValue({ listenKey: 'listen-key-123' }),
};
const service = new ListenKeyService(requestExecutorMock);

await expect(service.generateListenKey(account)).resolves.toEqual({
listenKey: 'listen-key-123',
});

expect(requestExecutorMock.execute).toHaveBeenCalledWith(
expect.any(BingxGenerateListenKeyEndpoint),
);
});

it('executes extend listen key endpoint', async () => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValue(undefined),
};
const service = new ListenKeyService(requestExecutorMock);

await expect(
service.extendListenKey('listen-key-123', account),
).resolves.toBeUndefined();

expect(requestExecutorMock.execute).toHaveBeenCalledWith(
expect.any(BingxExtendListenKeyEndpoint),
);
});

it('executes delete listen key endpoint', async () => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValue(undefined),
};
const service = new ListenKeyService(requestExecutorMock);

await expect(
service.deleteListenKey('listen-key-123', account),
).resolves.toBeUndefined();

expect(requestExecutorMock.execute).toHaveBeenCalledWith(
expect.any(BingxDeleteListenKeyEndpoint),
);
});
});
20 changes: 20 additions & 0 deletions src/bingx-client/services/listen-key.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AccountInterface } from 'bingx-api/bingx/account/account.interface';
import { BingxDeleteListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-delete-listen-key-endpoint';
import { BingxExtendListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-extend-listen-key-endpoint';
import { BingxGenerateListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-generate-listen-key-endpoint';
import { BingxGenerateListenKeyResponse } from 'bingx-api/bingx/endpoints/bingx-generate-listen-key-response';
import { RequestExecutorInterface } from 'bingx-api/bingx/request-executor/request-executor.interface';
Expand All @@ -13,4 +15,22 @@ export class ListenKeyService {
new BingxGenerateListenKeyEndpoint(account),
);
}

async extendListenKey(
listenKey: string,
account: AccountInterface,
): Promise<void> {
return this.requestExecutor.execute(
new BingxExtendListenKeyEndpoint(listenKey, account),
);
}

async deleteListenKey(
listenKey: string,
account: AccountInterface,
): Promise<void> {
return this.requestExecutor.execute(
new BingxDeleteListenKeyEndpoint(listenKey, account),
);
}
}
22 changes: 22 additions & 0 deletions src/bingx/endpoints/bingx-delete-listen-key-endpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ApiAccount } from 'bingx-api/bingx/account/api-account';
import { BingxDeleteListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-delete-listen-key-endpoint';

describe('BingxDeleteListenKeyEndpoint', () => {
const endpoint = new BingxDeleteListenKeyEndpoint(
'listen-key-123',
new ApiAccount('api-key', 'secret-key'),
);

it('describes the delete listen key endpoint', () => {
expect(endpoint.method()).toBe('delete');
expect(endpoint.path()).toBe('/openApi/user/auth/userDataStream');
});

it('passes listen key as a signed parameter', () => {
expect(endpoint.parameters().asRecord()).toEqual(
expect.objectContaining({
listenKey: 'listen-key-123',
}),
);
});
});
33 changes: 33 additions & 0 deletions src/bingx/endpoints/bingx-delete-listen-key-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AccountInterface } from 'bingx-api/bingx/account/account.interface';
import { DefaultSignatureParameters } from 'bingx-api/bingx/account/default-signature-parameters';
import { SignatureParametersInterface } from 'bingx-api/bingx/account/signature-parameters.interface';
import { Endpoint } from 'bingx-api/bingx/endpoints/endpoint';
import { EndpointInterface } from 'bingx-api/bingx/endpoints/endpoint.interface';

export class BingxDeleteListenKeyEndpoint<R = void>
extends Endpoint
implements EndpointInterface<R>
{
constructor(
private readonly listenKey: string,
account: AccountInterface,
) {
super(account);
}

method(): 'get' | 'post' | 'put' | 'patch' | 'delete' {
return 'delete';
}

parameters(): SignatureParametersInterface {
return new DefaultSignatureParameters({
listenKey: this.listenKey,
});
}

path(): string {
return '/openApi/user/auth/userDataStream';
}

readonly t!: R;
}
22 changes: 22 additions & 0 deletions src/bingx/endpoints/bingx-extend-listen-key-endpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ApiAccount } from 'bingx-api/bingx/account/api-account';
import { BingxExtendListenKeyEndpoint } from 'bingx-api/bingx/endpoints/bingx-extend-listen-key-endpoint';

describe('BingxExtendListenKeyEndpoint', () => {
const endpoint = new BingxExtendListenKeyEndpoint(
'listen-key-123',
new ApiAccount('api-key', 'secret-key'),
);

it('describes the extend listen key endpoint', () => {
expect(endpoint.method()).toBe('put');
expect(endpoint.path()).toBe('/openApi/user/auth/userDataStream');
});

it('passes listen key as a signed parameter', () => {
expect(endpoint.parameters().asRecord()).toEqual(
expect.objectContaining({
listenKey: 'listen-key-123',
}),
);
});
});
12 changes: 5 additions & 7 deletions src/bingx/endpoints/bingx-extend-listen-key-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
AccountInterface,
DefaultSignatureParameters,
Endpoint,
EndpointInterface,
SignatureParametersInterface,
} from 'bingx-api/bingx';
import { AccountInterface } from 'bingx-api/bingx/account/account.interface';
import { DefaultSignatureParameters } from 'bingx-api/bingx/account/default-signature-parameters';
import { SignatureParametersInterface } from 'bingx-api/bingx/account/signature-parameters.interface';
import { Endpoint } from 'bingx-api/bingx/endpoints/endpoint';
import { EndpointInterface } from 'bingx-api/bingx/endpoints/endpoint.interface';

export class BingxExtendListenKeyEndpoint<R = void>
extends Endpoint
Expand Down
2 changes: 2 additions & 0 deletions src/bingx/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export * from './bingx-cancel-all-orders-endpoint';
export * from './bingx-close-all-positions-endpoint';
export * from './bingx-generate-listen-key-endpoint';
export * from './bingx-generate-listen-key-response';
export * from './bingx-extend-listen-key-endpoint';
export * from './bingx-delete-listen-key-endpoint';
export * from './bingx-get-perpetual-swap-account-asset-endpoint';
export * from './bingx-get-server-time-endpoint';
export * from './bingx-perpetual-swap-positions-endpoint';
Expand Down
1 change: 0 additions & 1 deletion src/bingx/interfaces/websocket-event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { OrderTypeEnum } from 'bingx-api/bingx/enums/order-type.enum';
import { OrderSideEnum } from 'bingx-api/bingx/enums/order-side.enum';
import { OrderStatusEnum } from 'bingx-api/bingx/enums/order-status.enum';
import { OrderPositionSideEnum } from 'bingx-api/bingx/enums/order-position-side.enum';
Expand Down