-
Notifications
You must be signed in to change notification settings - Fork 14
/
bot.js
75 lines (69 loc) · 2.5 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const near = require("./near");
const config = require("./config");
const { IsDifferentEnough } = require("./functions");
const pjson = require('./package.json');
module.exports = {
updatePrices: async function (relativeDiffs, old_prices, new_prices, state, liveAssets) {
const current_time = new Date().getTime();
let prices_to_update = [];
const all_prices_updates = [];
Object.entries(relativeDiffs).map(([ticker, relativeDiff]) => {
const old_price = old_prices[ticker];
const new_price = new_prices[ticker] || { multiplier: 0, decimals: 0 };
console.log(
`Compare ${ticker}: ${old_price.multiplier.toString()} and ${new_price.multiplier.toString()}`
);
if (liveAssets && !liveAssets.has(ticker)) {
console.log(`!!! ${ticker} is not whitelisted. Skipping`);
return;
}
if (new_price.multiplier > 0) {
const price_update = {
asset_id: ticker,
price: {
multiplier: Math.round(new_price.multiplier).toString(),
decimals: new_price.decimals,
},
};
all_prices_updates.push(price_update);
if (IsDifferentEnough(relativeDiff, old_price, new_price)) {
console.log(`!!! Update ${ticker} price`);
prices_to_update.push(price_update);
}
}
});
if (
state.lastFullUpdateTimestamp + config.FULL_UPDATE_PERIOD <=
current_time
) {
prices_to_update = all_prices_updates;
state.lastFullUpdateTimestamp = current_time;
console.log(`!!! Executing full price update`);
}
const txParameters = {
prices: prices_to_update,
};
if (
pjson?.version &&
prices_to_update.length &&
state.lastVersionReportTimestamp + config.VERSION_REPORT_PERIOD <= current_time
) {
state.lastVersionReportTimestamp = current_time;
txParameters.version = pjson?.version;
console.log(`!!! Reporting version of the bot: ${pjson?.version}`);
}
const currentBalance = parseFloat(await near.CurrentBalance(config.NEAR_ACCOUNT_ID)) / 1e24;
if (currentBalance < config.MIN_CLAIM_NEAR_BALANCE) {
console.log(`!!! Current balance ${currentBalance} is less than ${config.MIN_CLAIM_NEAR_BALANCE}. Claiming NEAR`);
txParameters.claim_near = true;
}
if (prices_to_update.length || !!txParameters.version ) {
await near.NearCall(
config.NEAR_ACCOUNT_ID,
config.CONTRACT_ID,
"report_prices",
txParameters
);
}
},
};