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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ stream.latestTradeDetail$.subscribe((v) => {})
- [x] Perpetual Swap Positions
- [ ] Get Account Profit and Loss Fund Flow
- [ ] Export fund flow
- [ ] User fee rate
- [x] User fee rate
* Trade Interface
- [ ] Trade order test
- [x] Trade order
Expand Down
64 changes: 64 additions & 0 deletions src/bingx-client/services/account.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { AccountInterface } from 'bingx-api/bingx/account/account.interface';
import { SignatureInterface } from 'bingx-api/bingx/account/signature.interface';
import { SignatureParametersInterface } from 'bingx-api/bingx/account/signature-parameters.interface';
import { BingxUserFeeRateEndpoint } from 'bingx-api/bingx/endpoints/bingx-user-fee-rate-endpoint';
import { RequestExecutorInterface } from 'bingx-api/bingx/request-executor/request-executor.interface';
import { AccountService } from 'bingx-api/bingx-client/services/account.service';

class TestAccount implements AccountInterface {
getApiKey(): string {
return 'test-api-key';
}

sign(parameters: SignatureParametersInterface): SignatureInterface {
return {
toString: () => JSON.stringify(parameters.asRecord()),
secretKey: () => 'test-secret-key',
};
}
}

describe('AccountService', () => {
let dateNowSpy: jest.SpyInstance<number, []>;

beforeEach(() => {
dateNowSpy = jest.spyOn(Date, 'now').mockReturnValue(1_714_000_000_000);
});

afterEach(() => {
dateNowSpy.mockRestore();
});

it('describes the user fee rate endpoint request shape', () => {
const endpoint = new BingxUserFeeRateEndpoint(new TestAccount(), 5000);

expect(endpoint.method()).toBe('get');
expect(endpoint.path()).toBe('/openApi/swap/v2/user/commissionRate');
expect(endpoint.apiKey().asHeader()).toEqual({
'X-BX-APIKEY': 'test-api-key',
});
expect(endpoint.parameters().asRecord()).toEqual({
recvWindow: '5000',
timestamp: '1714000000000',
});
});

it('executes the user fee rate endpoint through account service', async () => {
const account = new TestAccount();
const requestExecutor: RequestExecutorInterface = {
execute: jest.fn(async (endpoint) => endpoint as never),
};
const service = new AccountService(requestExecutor);

const endpoint = await service.getUserFeeRate(account, 5000);

expect(requestExecutor.execute).toHaveBeenCalledTimes(1);
expect(endpoint).toBeInstanceOf(BingxUserFeeRateEndpoint);
expect(
(endpoint as unknown as BingxUserFeeRateEndpoint).parameters().asRecord(),
).toEqual({
recvWindow: '5000',
timestamp: '1714000000000',
});
});
});
7 changes: 7 additions & 0 deletions src/bingx-client/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RequestExecutorInterface } from 'bingx-api/bingx/request-executor/reque
import { AccountInterface } from 'bingx-api/bingx/account/account.interface';
import { BingxGetPerpetualSwapAccountAssetEndpoint } from 'bingx-api/bingx/endpoints/bingx-get-perpetual-swap-account-asset-endpoint';
import { BingxPerpetualSwapPositionsEndpoint } from 'bingx-api/bingx/endpoints/bingx-perpetual-swap-positions-endpoint';
import { BingxUserFeeRateEndpoint } from 'bingx-api/bingx/endpoints/bingx-user-fee-rate-endpoint';

export class AccountService {
constructor(private readonly requestExecutor: RequestExecutorInterface) {}
Expand All @@ -17,4 +18,10 @@ export class AccountService {
new BingxPerpetualSwapPositionsEndpoint(symbol, account),
);
}

public getUserFeeRate(account: AccountInterface, recvWindow?: number) {
return this.requestExecutor.execute(
new BingxUserFeeRateEndpoint(account, recvWindow),
);
}
}
47 changes: 47 additions & 0 deletions src/bingx/endpoints/bingx-user-fee-rate-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
AccountInterface,
BingxResponse,
DefaultSignatureParameters,
EndpointInterface,
SignatureParametersInterface,
} from 'bingx-api/bingx';
import { Endpoint } from 'bingx-api/bingx/endpoints/endpoint';

export interface BingxUserFeeRateData {
commission: {
takerCommissionRate: number | string;
makerCommissionRate: number | string;
};
}

export class BingxUserFeeRateEndpoint<R = BingxUserFeeRateData>
extends Endpoint<BingxResponse<R>>
implements EndpointInterface<BingxResponse<R>>
{
constructor(
account: AccountInterface,
private readonly recvWindow?: number,
) {
super(account);
}

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

parameters(): SignatureParametersInterface {
return new DefaultSignatureParameters(
this.recvWindow === undefined
? {}
: {
recvWindow: this.recvWindow.toString(10),
},
);
}

path(): string {
return '/openApi/swap/v2/user/commissionRate';
}

readonly t!: BingxResponse<R>;
}
1 change: 1 addition & 0 deletions src/bingx/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './bingx-perpetual-swap-positions-endpoint';
export * from './bingx-request.interface';
export * from './bingx-response.interface';
export * from './bingx-trade-order-endpoint';
export * from './bingx-user-fee-rate-endpoint';
export * from './endpoint.interface';
export * from './endpoint';
export * from './bingx-user-history-orders-endpoint';
Expand Down