Error: Nonce is too small - Bitfinex api
Error: Nonce is too small - Bitfinex api
我尝试使用 bitfinex 库 (https://www.npmjs.com/package/bitfinex) 从 Bitfinex 调用 rest API。当 运行 同时使用相同的 API 键连接超过 1 个进程时,文档会警告此错误。但是,我相信我一次只 运行 一个进程,即使我生成新的 API 键,错误仍然发生。我正在考虑覆盖随机数或扩展它,但我不知道该怎么做。我已经尝试了互联网上的一些建议,但仍然没有任何效果。
下面是我的代码供参考:
import Bitfinex = require('bitfinex');
import pollingtoevent = require('polling-to-event');
import { Logger, LoggerFactory } from '../../common';
import { AppDataServices } from '../../data';
export class BitfinexPoller {
private static readonly LOGGER: Logger = LoggerFactory.getLogger();
private bitfinex: any = undefined;
private emitter: any = undefined;
private public_key: string = '<my-public-key>';
private secret_key: string = '<my-secret-key>';
private nonce: any = new Date().getTime();
constructor(private appServices: AppDataServices) {
BitfinexPoller.LOGGER.info('Bitfinex poller init');
this.bitfinex = new Bitfinex(this.public_key, this.secret_key, this.nonce);
// Lend book service
this.emitter = pollingtoevent((done: any) => {
this.bitfinex.lendbook('USD', (err: any, res: any, orderId: any) => {
done(err, res);
});
}, { interval: 30000, eventName: 'bitfinex-lending' });
this.emitter.on('bitfinex-lending', (data: any) => {
BitfinexPoller.LOGGER.info(data);
if (data.bids !== undefined) {
for (const row of data.bids) {
appServices.lendbookService.insert(BitfinexPoller.lendingData('BID', row));
}
}
if (data.asks !== undefined) {
for (const row of data.asks) {
appServices.lendbookService.insert(BitfinexPoller.lendingData('ASK', row));
}
}
});
// Order book service
this.emitter = pollingtoevent((done: any) => {
this.bitfinex.orderbook('btcusd', (err: any, res: any, orderId: any) => {
done(err, res);
});
}, { interval: 30000, eventName: 'bitfinex-order' });
this.emitter.on('bitfinex-order', (data: any) => {
BitfinexPoller.LOGGER.info(data);
if (data.bids !== undefined) {
for (const row of data.bids) {
appServices.lendbookService.insert(BitfinexPoller.orderData('BID', row));
}
}
if (data.asks !== undefined) {
for (const row of data.asks) {
appServices.lendbookService.insert(BitfinexPoller.orderData('ASK', row));
}
}
});
}
}
您很可能是连续快速发出两个经过身份验证的呼叫。您发送请求的顺序并不总是与处理它们的顺序相同,因此首先处理具有更高随机数的第二个请求,导致第一个请求失败。
您可以为不同的请求创建和使用多个 API 密钥,或者有一个循环使用的池,这样您就不会快速连续多次使用相同的 api 密钥。
我对 nodejs 的解决方案是通过 promises 延迟调用。
像这样:
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
sleep(500).then(() => {
//do work
});
sleep(600).then(() => {
//do work
});
显然,如果您正在寻找极低的延迟和高速,这不适合您。
我尝试使用 bitfinex 库 (https://www.npmjs.com/package/bitfinex) 从 Bitfinex 调用 rest API。当 运行 同时使用相同的 API 键连接超过 1 个进程时,文档会警告此错误。但是,我相信我一次只 运行 一个进程,即使我生成新的 API 键,错误仍然发生。我正在考虑覆盖随机数或扩展它,但我不知道该怎么做。我已经尝试了互联网上的一些建议,但仍然没有任何效果。
下面是我的代码供参考:
import Bitfinex = require('bitfinex');
import pollingtoevent = require('polling-to-event');
import { Logger, LoggerFactory } from '../../common';
import { AppDataServices } from '../../data';
export class BitfinexPoller {
private static readonly LOGGER: Logger = LoggerFactory.getLogger();
private bitfinex: any = undefined;
private emitter: any = undefined;
private public_key: string = '<my-public-key>';
private secret_key: string = '<my-secret-key>';
private nonce: any = new Date().getTime();
constructor(private appServices: AppDataServices) {
BitfinexPoller.LOGGER.info('Bitfinex poller init');
this.bitfinex = new Bitfinex(this.public_key, this.secret_key, this.nonce);
// Lend book service
this.emitter = pollingtoevent((done: any) => {
this.bitfinex.lendbook('USD', (err: any, res: any, orderId: any) => {
done(err, res);
});
}, { interval: 30000, eventName: 'bitfinex-lending' });
this.emitter.on('bitfinex-lending', (data: any) => {
BitfinexPoller.LOGGER.info(data);
if (data.bids !== undefined) {
for (const row of data.bids) {
appServices.lendbookService.insert(BitfinexPoller.lendingData('BID', row));
}
}
if (data.asks !== undefined) {
for (const row of data.asks) {
appServices.lendbookService.insert(BitfinexPoller.lendingData('ASK', row));
}
}
});
// Order book service
this.emitter = pollingtoevent((done: any) => {
this.bitfinex.orderbook('btcusd', (err: any, res: any, orderId: any) => {
done(err, res);
});
}, { interval: 30000, eventName: 'bitfinex-order' });
this.emitter.on('bitfinex-order', (data: any) => {
BitfinexPoller.LOGGER.info(data);
if (data.bids !== undefined) {
for (const row of data.bids) {
appServices.lendbookService.insert(BitfinexPoller.orderData('BID', row));
}
}
if (data.asks !== undefined) {
for (const row of data.asks) {
appServices.lendbookService.insert(BitfinexPoller.orderData('ASK', row));
}
}
});
}
}
您很可能是连续快速发出两个经过身份验证的呼叫。您发送请求的顺序并不总是与处理它们的顺序相同,因此首先处理具有更高随机数的第二个请求,导致第一个请求失败。
您可以为不同的请求创建和使用多个 API 密钥,或者有一个循环使用的池,这样您就不会快速连续多次使用相同的 api 密钥。
我对 nodejs 的解决方案是通过 promises 延迟调用。
像这样:
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
sleep(500).then(() => {
//do work
});
sleep(600).then(() => {
//do work
});
显然,如果您正在寻找极低的延迟和高速,这不适合您。