@binance/margin-trading 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE +21 -0
- package/README.md +201 -0
- package/dist/index.d.mts +10691 -0
- package/dist/index.d.ts +10691 -0
- package/dist/index.js +5602 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5602 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Binance
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Binance JavaScript Margin Trading Connector
|
|
2
|
+
|
|
3
|
+
[](https://github.com/binance/binance-connector-js/issues)
|
|
4
|
+
[](https://prettier.io/)
|
|
5
|
+
[](https://badge.fury.io/js/@binance%2Fmargin-trading)
|
|
6
|
+
[](https://www.npmjs.com/package/@binance/margin-trading)
|
|
7
|
+

|
|
8
|
+
[](https://snyk.io/test/github/binance/binance-connector-js)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
|
|
11
|
+
This is a client library for the Binance Margin Trading API, enabling developers to interact programmatically with Binance's Margin Trading trading platform. The library provides tools to use funds provided by a third party to conduct asset transactions through the REST API:
|
|
12
|
+
|
|
13
|
+
- [REST API](./src/rest-api/rest-api.ts)
|
|
14
|
+
|
|
15
|
+
## Table of Contents
|
|
16
|
+
|
|
17
|
+
- [Supported Features](#supported-features)
|
|
18
|
+
- [Installation](#installation)
|
|
19
|
+
- [Documentation](#documentation)
|
|
20
|
+
- [REST APIs](#rest-apis)
|
|
21
|
+
- [Testing](#testing)
|
|
22
|
+
- [Migration Guide](#migration-guide)
|
|
23
|
+
- [Contributing](#contributing)
|
|
24
|
+
- [Licence](#licence)
|
|
25
|
+
|
|
26
|
+
## Supported Features
|
|
27
|
+
|
|
28
|
+
- REST API Endpoints:
|
|
29
|
+
- `/sapi/v1/margin/*`
|
|
30
|
+
- `/sapi/v1/bnbBurn/*`
|
|
31
|
+
- `/sapi/v1/userDataStream/*`
|
|
32
|
+
- Inclusion of test cases and examples for quick onboarding.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
To use this library, ensure your environment is running Node.js version **22.12.0** or later. If you're using `nvm` (Node Version Manager), you can set the correct version as follows:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
nvm install 22.12.0
|
|
40
|
+
nvm use 22.12.0
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then install the library using `npm`:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @binance/margin-trading
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Documentation
|
|
50
|
+
|
|
51
|
+
For detailed information, refer to the [Binance API Documentation](https://developers.binance.com/docs/margin_trading).
|
|
52
|
+
|
|
53
|
+
### REST APIs
|
|
54
|
+
|
|
55
|
+
All REST API endpoints are available through the [`rest-api`](./src/rest-api/rest-api.ts) module. Note that some endpoints require authentication using your Binance API credentials.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { MarginTrading, MarginTradingRestAPI } from '@binance/margin-trading';
|
|
59
|
+
|
|
60
|
+
const configurationRestAPI = {
|
|
61
|
+
apiKey: 'your-api-key',
|
|
62
|
+
apiSecret: 'your-api-secret',
|
|
63
|
+
};
|
|
64
|
+
const client = new MarginTrading({ configurationRestAPI });
|
|
65
|
+
|
|
66
|
+
client.restAPI
|
|
67
|
+
.getSummaryOfMarginAccount()
|
|
68
|
+
.then((res) => res.data())
|
|
69
|
+
.then((data: MarginTradingRestAPI.GetSummaryOfMarginAccountResponse) => console.log(data))
|
|
70
|
+
.catch((err) => console.error(err));
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
More examples can be found in the [`examples/rest-api`](./examples/rest-api/) folder.
|
|
74
|
+
|
|
75
|
+
#### Configuration Options
|
|
76
|
+
|
|
77
|
+
The REST API supports the following advanced configuration options:
|
|
78
|
+
|
|
79
|
+
- `timeout`: Timeout for requests in milliseconds (default: 1000 ms).
|
|
80
|
+
- `proxy`: Proxy configuration:
|
|
81
|
+
- `host`: Proxy server hostname.
|
|
82
|
+
- `port`: Proxy server port.
|
|
83
|
+
- `protocol`: Proxy protocol (http or https).
|
|
84
|
+
- `auth`: Proxy authentication credentials:
|
|
85
|
+
- `username`: Proxy username.
|
|
86
|
+
- `password`: Proxy password.
|
|
87
|
+
- `keepAlive`: Enable HTTP keep-alive (default: true).
|
|
88
|
+
- `compression`: Enable response compression (default: true).
|
|
89
|
+
- `retries`: Number of retry attempts for failed requests (default: 3).
|
|
90
|
+
- `backoff`: Delay in milliseconds between retries (default: 1000 ms).
|
|
91
|
+
- `httpsAgent`: Custom HTTPS agent for advanced TLS configuration.
|
|
92
|
+
- `privateKey`: RSA or ED25519 private key for authentication.
|
|
93
|
+
- `privateKeyPassphrase`: Passphrase for the private key, if encrypted.
|
|
94
|
+
|
|
95
|
+
##### Timeout
|
|
96
|
+
|
|
97
|
+
You can configure a timeout for requests in milliseconds. If the request exceeds the specified timeout, it will be aborted. See the [Timeout example](./docs/rest-api/timeout.md) for detailed usage.
|
|
98
|
+
|
|
99
|
+
##### Proxy
|
|
100
|
+
|
|
101
|
+
The REST API supports HTTP/HTTPS proxy configurations. See the [Proxy example](./docs/rest-api/proxy.md) for detailed usage.
|
|
102
|
+
|
|
103
|
+
##### Keep-Alive
|
|
104
|
+
|
|
105
|
+
Enable HTTP keep-alive for persistent connections. See the [Keep-Alive example](./docs/rest-api/keepAlive.md) for detailed usage.
|
|
106
|
+
|
|
107
|
+
##### Compression
|
|
108
|
+
|
|
109
|
+
Enable or disable response compression. See the [Compression example](./docs/rest-api/compression.md) for detailed usage.
|
|
110
|
+
|
|
111
|
+
##### Retries
|
|
112
|
+
|
|
113
|
+
Configure the number of retry attempts and delay in milliseconds between retries for failed requests. See the [Retries example](./docs/rest-api/retries.md) for detailed usage.
|
|
114
|
+
|
|
115
|
+
##### HTTPS Agent
|
|
116
|
+
|
|
117
|
+
Customize the HTTPS agent for advanced TLS configurations. See the [HTTPS Agent example](./docs/rest-api/httpsAgent.md) for detailed usage.
|
|
118
|
+
|
|
119
|
+
##### Key Pair Based Authentication
|
|
120
|
+
|
|
121
|
+
The REST API supports key pair-based authentication for secure communication. You can use `RSA` or `ED25519` keys for signing requests. See the [Key Pair Based Authentication example](./docs/rest-api/key-pair-authentication.md) for detailed usage.
|
|
122
|
+
|
|
123
|
+
##### Certificate Pinning
|
|
124
|
+
|
|
125
|
+
To enhance security, you can use certificate pinning with the `httpsAgent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/rest-api/certificate-pinning.md) for detailed usage.
|
|
126
|
+
|
|
127
|
+
#### Error Handling
|
|
128
|
+
|
|
129
|
+
The REST API provides detailed error types to help you handle issues effectively:
|
|
130
|
+
|
|
131
|
+
- `ConnectorClientError`: General client error.
|
|
132
|
+
- `RequiredError`: Thrown when a required parameter is missing.
|
|
133
|
+
- `UnauthorizedError`: Indicates missing or invalid authentication credentials.
|
|
134
|
+
- `ForbiddenError`: Access to the requested resource is forbidden.
|
|
135
|
+
- `TooManyRequestsError`: Rate limit exceeded.
|
|
136
|
+
- `RateLimitBanError`: IP address banned for exceeding rate limits.
|
|
137
|
+
- `ServerError`: Internal server error.
|
|
138
|
+
- `NetworkError`: Issues with network connectivity.
|
|
139
|
+
- `NotFoundError`: Resource not found.
|
|
140
|
+
- `BadRequestError`: Invalid request.
|
|
141
|
+
|
|
142
|
+
See the [Error Handling example](./docs/rest-api/error-handling.md) for detailed usage.
|
|
143
|
+
|
|
144
|
+
#### Testnet
|
|
145
|
+
|
|
146
|
+
For testing purposes, `/sapi/v1/margin/*`, `/sapi/v1/bnbBurn/*` and `/sapi/v1/userDataStream/*` endpoints can be used in the [Testnet](https://testnet.binance.vision/). Update the `basePath` in your configuration:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import {
|
|
150
|
+
MarginTrading,
|
|
151
|
+
MarginTradingRestAPI,
|
|
152
|
+
MARGIN_TRADING_REST_API_TESTNET_URL,
|
|
153
|
+
} from '@binance/margin-trading';
|
|
154
|
+
|
|
155
|
+
const configurationRestAPI = {
|
|
156
|
+
apiKey: 'your-api-key',
|
|
157
|
+
apiSecret: 'your-api-secret',
|
|
158
|
+
basePath: MARGIN_TRADING_REST_API_TESTNET_URL,
|
|
159
|
+
};
|
|
160
|
+
const client = new MarginTrading({ configurationRestAPI });
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
If `basePath` is not provided, it defaults to `https://api.binance.com`.
|
|
164
|
+
|
|
165
|
+
## Testing
|
|
166
|
+
|
|
167
|
+
To run the tests:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
npm install
|
|
171
|
+
|
|
172
|
+
npm run test
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The tests cover:
|
|
176
|
+
|
|
177
|
+
- REST API endpoints
|
|
178
|
+
- Error handling and edge cases
|
|
179
|
+
|
|
180
|
+
## Migration Guide
|
|
181
|
+
|
|
182
|
+
If you are upgrading to the new modularized structure, refer to the [Migration Guide](./docs/migration_guide_margin_trading_connector.md) for detailed steps.
|
|
183
|
+
|
|
184
|
+
## Contributing
|
|
185
|
+
|
|
186
|
+
Contributions are welcome!
|
|
187
|
+
|
|
188
|
+
Since this repository contains auto-generated code, we encourage you to start by opening a GitHub issue to discuss your ideas or suggest improvements. This helps ensure that changes align with the project's goals and auto-generation processes.
|
|
189
|
+
|
|
190
|
+
To contribute:
|
|
191
|
+
|
|
192
|
+
1. Open a GitHub issue describing your suggestion or the bug you've identified.
|
|
193
|
+
2. If it's determined that changes are necessary, the maintainers will merge the changes into the main branch.
|
|
194
|
+
|
|
195
|
+
Please ensure that all tests pass if you're making a direct contribution. Submit a pull request only after discussing and confirming the change.
|
|
196
|
+
|
|
197
|
+
Thank you for your contributions!
|
|
198
|
+
|
|
199
|
+
## Licence
|
|
200
|
+
|
|
201
|
+
This project is licensed under the MIT License. See the [LICENCE](./LICENCE) file for details.
|