tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
728 stars 264 forks source link

v2.11.0: feat(): improved type resolution for order submit/amend methods & add "submitNewOrderList" method and related types #422

Closed ozum closed 1 month ago

ozum commented 1 month ago

Summary

Example

Thanks to the new generic type, the new submitNewOrderList method returns a response with the exact TypeScript type based on the newOrderRespType parameter. If newOrderRespType is not provided, it defaults to the minimum ACK, not to mislead the developer.

// See https://binance-docs.github.io/apidocs/spot/en/#new-order-list-oco-trade

const responseA = await client.submitNewOrderList({ ...args, newOrderRespType: "FULL" });
const executedQtyA = responseA[0].executedQty; // responseA has correct type of "OrderResponseFull"

// TypeScript will warn the wrong type for the below code
const responseB = await client.submitNewOrderList({ ...args, newOrderRespType: "ACK" });
const executedQtyB = responseB[0].executedQty; // TYPING ERROR: responseB is type of "OrderResponseACK"

const responseC = await client.submitNewOrderList({ ...args }); // Defaults to "OrderResponseACK"
const responseA = await client.submitNewOrder({ ...args, type: "MARKET" }); // FULL
const responseB = await client.submitNewOrder({ ...args, type: "MARKET", newOrderRespType: "ACK" }); // ACK

const responseC = await client.submitNewOrder({ ...args, type: "STOP_LOSS" }); // ACK
const responseD = await client.submitNewOrder({ ...args, type: "STOP_LOSS", newOrderRespType: "FULL" }); // FULL

NOTE: I could have divided the features into multiple PRs, but since some of the changes modified the same files, I didn't want to mess with Git conflicts, so I divided them into multiple commits.

ozum commented 1 month ago

@tiagosiebler, I will surely increase the version number.

I will try to find a way to implement the generic type to the sumbitNewOrder(). It is tricky.

MARKET and LIMIT order types default to FULL, all other orders default to ACK.

ozum commented 1 month ago

@tiagosiebler. I also made sumbitNewOrder() and replaceOrder() generic.

const responseA = await client.submitNewOrder({ ...args, type: "MARKET" }); // FULL
const responseB = await client.submitNewOrder({ ...args, type: "MARKET", newOrderRespType: "ACK" }); // ACK

const responseC = await client.submitNewOrder({ ...args, type: "STOP_LOSS" }); // ACK
const responseD = await client.submitNewOrder({ ...args, type: "STOP_LOSS", newOrderRespType: "FULL" }); // FULL
ozum commented 1 month ago

@tiagosiebler, I'm updating the first message in this PR when I update the PR, so you can use it for documentation purposes when you merge it.

ozum commented 1 month ago

@tiagosiebler, could you please review the PR? It's getting bigger. I want to continue adding further fixes in separate PRs, but they may depend on this PR.

tiagosiebler commented 1 month ago

@tiagosiebler, could you please review the PR? It's getting bigger. I want to continue adding further fixes in separate PRs, but they may depend on this PR.

Hey, I will need some time to review (currently travelling). Will review it soon. In the meantime, I suggest making another branch in your fork based on the branch you're currently working on. This way you're building on top of this PR without directly adding more commits to this PR/branch.

ozum commented 1 month ago

Hey, I will need some time to review (currently travelling). Will review it soon.

I wish you a nice trip. FYI, I'm using the version with PR applied in a small personal project, no problem so far.

ozum commented 1 month ago

Amazing work, looks great to me! Thanks for all the time you spent in improving this!

Happy to contribute, and thank you for publishing this great library.