Skip to content

Commit

Permalink
add symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ishuen committed Oct 11, 2022
1 parent 1e62838 commit 31a1b18
Show file tree
Hide file tree
Showing 5 changed files with 581 additions and 1,350 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,10 @@ Latest price for a symbol, not providing the symbol will return prices for all s
console.log(await client.prices())
```

| Param | Type | Required |
| ------ | ------ | -------- |
| symbol | String | false |
| Param | Type | Required |
| ------- | -------- | -------- |
| symbol | String | false |
| symbols | String[] | false |

<details>
<summary>Output</summary>
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ declare module 'binance-api-node' {
orderTest(options: NewOrderSpot): Promise<Order>
orderOco(options: NewOcoOrder): Promise<OcoOrder>
ping(): Promise<boolean>
prices(options?: { symbol?: string }): Promise<{ [index: string]: string }>
prices(options?: { symbol?: string, symbols?: string[] }): Promise<{ [index: string]: string }>
avgPrice(options?: { symbol: string }): Promise<AvgPriceResult | AvgPriceResult[]>
time(): Promise<number>
trades(options: { symbol: string; limit?: number }): Promise<TradeResult[]>
Expand Down
11 changes: 9 additions & 2 deletions src/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ const aggTrades = (pubCall, payload, endpoint = '/api/v3/aggTrades') =>
}),
)

// Convert the array to string to prevent unacceptable encoding
const processSymbols = payload => {
if (payload?.symbols) {
payload.symbols = `["${payload.symbols.join('"')}"]`
}
}

export default opts => {
const endpoints = {
base: (opts && opts.httpBase) || BASE,
Expand Down Expand Up @@ -355,8 +362,8 @@ export default opts => {
kCall('/api/v3/historicalTrades', payload),

dailyStats: payload => pubCall('/api/v3/ticker/24hr', payload),
prices: payload =>
pubCall('/api/v3/ticker/price', payload).then(r =>
prices: payload =>
pubCall('/api/v3/ticker/price', processSymbols(payload)).then(r =>
(Array.isArray(r) ? r : [r]).reduce((out, cur) => ((out[cur.symbol] = cur.price), out), {}),
),

Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ test('[REST] individual price', async t => {
t.truthy(prices.ETHUSDT)
})

test('[REST] multiple prices', async t => {
const prices = await client.prices({ symbols: ["ETHUSDT","ETHBUSD"] })
t.truthy(prices)
t.truthy(prices.ETHUSDT)
t.truthy(prices.ETHBUSD)
})

test('[REST] avgPrice', async t => {
const res = await client.avgPrice({ symbol: 'ETHBTC' })
t.truthy(res)
Expand Down
Loading

0 comments on commit 31a1b18

Please sign in to comment.