Minor changes to logic, was retrying after queue pop
This commit is contained in:
parent
0a28a20de7
commit
532169d748
2 changed files with 33 additions and 30 deletions
|
|
@ -7,17 +7,19 @@ const queue = new PQueue({ interval: 1000, intervalCap: 10 });
|
||||||
|
|
||||||
export async function fetchRate(pair) {
|
export async function fetchRate(pair) {
|
||||||
const key = pair.toUpperCase();
|
const key = pair.toUpperCase();
|
||||||
|
const max_retries = 3;
|
||||||
|
|
||||||
return queue.add(async () => {
|
for (let attempt = 1; attempt <= max_retries; attempt++) {
|
||||||
for (let attempt = 1; attempt <= 3; attempt++) {
|
|
||||||
let timeout;
|
let timeout;
|
||||||
try {
|
try {
|
||||||
|
const res = await queue.add(async () => {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
timeout = setTimeout(() => controller.abort(), 5000);
|
timeout = setTimeout(() => controller.abort(), 5000);
|
||||||
|
|
||||||
const res = await fetch(`https://api.uphold.com/v0/ticker/${key}`, {
|
return fetch(`https://api.uphold.com/v0/ticker/${key}`, {
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
|
|
||||||
|
|
@ -46,7 +48,6 @@ export async function fetchRate(pair) {
|
||||||
await new Promise((r) => setTimeout(r, 500 * Math.pow(2, attempt - 1)));
|
await new Promise((r) => setTimeout(r, 500 * Math.pow(2, attempt - 1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function prefetchRates(pairs) {
|
export async function prefetchRates(pairs) {
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ export class Bot {
|
||||||
this.lastPrice = price;
|
this.lastPrice = price;
|
||||||
logger.info(`[${this.pair}] Initial price: ${price.toFixed(2)}`);
|
logger.info(`[${this.pair}] Initial price: ${price.toFixed(2)}`);
|
||||||
} else {
|
} else {
|
||||||
|
// Using BigNumber
|
||||||
const change = price
|
const change = price
|
||||||
.minus(this.lastPrice)
|
.minus(this.lastPrice)
|
||||||
.dividedBy(this.lastPrice)
|
.dividedBy(this.lastPrice)
|
||||||
|
|
@ -69,6 +70,7 @@ export class Bot {
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// onAlert must be fast
|
||||||
await this.onAlert(alertData);
|
await this.onAlert(alertData);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue