[javascript] qwe
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- const request = require('node-fetch')
- const BigNumber = require('bignumber.js')
- class ETHPrice {
- getKlines({ startTime, endTime, interval = '1m', limit }) {
- const params = {
- symbol: 'ETHUSDT',
- startTime,
- interval
- }
- if (limit) params.limit = limit
- if (endTime) params.endTime = endTime
- const queryString = new URLSearchParams(params)
- return request(`https://www.binance.com/api/v1/klines?${queryString}`).then(res => res.json())
- }
- async getPriceAt(time, { includePriceTime = false } = {}) {
- const startTime = new Date(time).getTime()
- const endTime = startTime + 15 * 60e3
- const klines = await this.getKlines({ startTime, endTime, limit: 5 })
- if (!klines || !klines.length) throw new Error('Unable to fetch klines')
- const [priceTime, open] = klines[0]
- const price = new BigNumber(open)
- if (price.isNaN()) throw new Error(`Price ${open} is not a number`)
- if (!includePriceTime) return price.toNumber()
- return {
- time: new Date(priceTime),
- price: price.toNumber()
- }
- }
- async getPrices({ startTime, endTime, interval, limit }) {
- const klines = await this.getKlines({ startTime, endTime, interval, limit })
- if (!klines || !klines.length) throw new Error('Unable to fetch klines')
- return klines.map(k => {
- const [time, open] = k
- const price = new BigNumber(open)
- if (price.isNaN()) throw new Error(`Price ${open} is not a number`)
- return {
- time: new Date(time),
- price: price.toNumber()
- }
- })
- }
- }
- const ethPrice = new ETHPrice()
- module.exports = ethPrice
Editor
You can edit this paste and save as new: