jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 767 forks source link

Get futures balance issue #861

Open cynologic opened 1 year ago

cynologic commented 1 year ago

Hello, I am having an issue with the await. I appreciate if you could help me with this issue

I'm trying this code let balance = await binance.futuresBalance(); console.log(balance)

it's returning an error as follows SyntaxError: await is only valid in async functions and the top level bodies of modules

Same issue for other callback functions such console.info( await binance.futuresAccount() );

Thank you in advance Best regards

cynologic commented 1 year ago

Actually it's relevant for all futures calls. Futures mini ticker is working like a charm undortunately other futures api call seems not working ;(

cynologic commented 1 year ago

I double checked on my Binance account API management both spot and futures trading is market as enabled. Here's the complete return on the terminal

console.info( await binance.futuresPrices() ); ^^^^^

SyntaxError: missing ) after argument list at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1055:15) at Module._compile (node:internal/modules/cjs/loader:1090:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47

It seems something is missing for async / await. But no chance to find out it ;(

Dmitry-Kucher commented 1 year ago

I'm trying this code let balance = await binance.futuresBalance(); console.log(balance)

Try this one instead:

(async () => {
  let balance = await binance.futuresBalance();
  console.log(balance);
})()

Just a friendly advice: learn js before doing something

cynologic commented 1 year ago

Thank you so much Dmitry for your quick comment. I appreciate it. As you advised I need to study ES6 features such async.

cynologic commented 1 year ago

One more quick question I tried it but the error code is changed to ---the tried iife / code part is (async () => { let balance = await binance.futuresBalance(); console.log(balance); })()

[Object: null prototype] { code: -1021, msg: "Timestamp for this request was 1000ms ahead of the server's time." }

Dmitry-Kucher commented 1 year ago

I feel like it is a problem related to troubleshooting section: https://github.com/jaggedsoft/node-binance-api#troubleshooting

try to set userServerTime:

binance.options({
...
useServerTime: true,
...
});

or increase recvWindow value

cynologic commented 1 year ago

Dear Dmitry, Thanks for your answer it's still returning the same error the code as follows. No chance to get the balance and price

const Binance = require('node-binance-api'); const binance = new Binance().options({ APIKEY: '--', APISECRET: '--', useServerTime: true, recvWindow: 30000, });

(async () => { binance.useServerTime() let balance = await binance.futuresBalance(); console.log(balance); })()

cynologic commented 1 year ago

Hello again I fixed it syncing time of my local PC.

cynologic commented 1 year ago

Hello, I'm asking too much sorry for that is there a way assigning to a variable instead of logging to the console

(async () => { let balance = await binance.futuresBalance(); console.log(balance); })()

I want to make this function as follows let bal = (async () => { return await binance.futuresBalance(); })()

and then use the bal variable in other expressions.

Thank you so much in advance.