gpt4 book ai didi

javascript - BINANCE API - 如何使用用户数据流获取账户信息

转载 作者:搜寻专家 更新时间:2023-10-31 23:26:15 29 4
gpt4 key购买 nike

我正在使用 Node 和 ws npm 包来处理 WebSockets。获得了文档(如下)中所述的 listenKey,但我无法使用用户数据流获取我的帐户信息。我更喜欢使用流来读取我最新的帐户信息(余额等),因为每次使用 Rest API 都会产生惩罚(权重:5)。

我试过执行 ws.send('outboundAccountInfo') 但没有成功。

文档:https://github.com/binance-exchange/binance-official-api-docs/blob/master/user-data-stream.md

完整代码示例 - 不返回任何数据:

import request from 'request'
import WebSocket from 'ws'

import { API_KEY } from '../../assets/secrets'


const DATA_STREAM_ENDPOINT = 'wss://stream.binance.com:9443/ws'
const BINANCE_API_ROOT = 'https://api.binance.com'
const LISTEN_KEY_ENDPOINT = `${BINANCE_API_ROOT}/api/v1/userDataStream`

const fetchAccountWebsocketData = async() => {
const listenKey = await fetchListenKey()

console.log('-> ', listenKey) // valid key is returned

let ws

try {
ws = await openWebSocket(`${DATA_STREAM_ENDPOINT}/${listenKey}`)
} catch (err) {
throw(`ERROR - fetchAccountWebsocketData: ${err}`)
}

// Nothing returns from either
ws.on('message', data => console.log(data))
ws.on('outboundAccountInfo', accountData => console.log(accountData))
}

const openWebSocket = endpoint => {
const p = new Promise((resolve, reject) => {
const ws = new WebSocket(endpoint)

console.log('\n-->> New Account Websocket')

ws.on('open', () => {
console.log('\n-->> Websocket Account open...')
resolve(ws)
}, err => {
console.log('fetchAccountWebsocketData error:', err)
reject(err)
})
})

p.catch(err => console.log(`ERROR - fetchAccountWebsocketData: ${err}`))
return p
}

const fetchListenKey = () => {
const p = new Promise((resolve, reject) => {
const options = {
url: LISTEN_KEY_ENDPOINT,
headers: {'X-MBX-APIKEY': API_KEY}
}

request.post(options, (err, httpResponse, body) => {
if (err)
return reject(err)

resolve(JSON.parse(body).listenKey)
})
})

p.catch(err => console.log(`ERROR - fetchListenKey: ${err}`))
return p
}

export default fetchAccountWebsocketData

最佳答案

努力....几个小时!!!

https://www.reddit.com/r/BinanceExchange/comments/a902cq/user_data_streams_has_anyone_used_it_successfully/

The binance user data stream doesn't return anything when you connect to it, only when something changes in your account. Try running your code, then go to binance and place an order in the book, you should see some data show up*

关于javascript - BINANCE API - 如何使用用户数据流获取账户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49101671/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com