-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
132 lines (121 loc) · 2.9 KB
/
utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require("isomorphic-fetch");
require("process");
const rwClient = require("./twitterClient.js");
function formatFloat(value) {
return parseFloat(value).toFixed(2);
}
function toDollar(num) {
if (num >= 1000000) {
return "$" + (num / 1000000).toFixed(2) + "M";
} else if (num >= 1000 && num < 1000000) {
return "$" + (num / 1000).toFixed(2) + "K";
}
return "$" + num.toString();
}
function getToday() {
var today = new Date();
var dd = today.getDate();
const month = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let mm = month[today.getMonth()];
return dd + " " + mm;
}
function farmUrl(farm) {
const utmLink =
"&utm_campaign=yieldbay-bot&utm_source=twitter&utm_medium=textlink";
return `https://list.yieldbay.io/farm/${farm.id}/?addr=${farm.asset.address}${utmLink}`.replace(
" ",
"%20"
);
}
function getTweetText(farms, type) {
let text = "";
for (let i = 0; i < 3; i++) {
text += `→ ${
farms[i].protocol.charAt(0).toUpperCase() + farms[i].protocol.slice(1)
} on ${
farms[i].chain.charAt(0).toUpperCase() + farms[i].chain.slice(1)
} : ${farms[i].asset.symbol} - `;
if (type == "tvl") {
text += `${toDollar(farms[i].tvl)}\n`;
} else if (type == "apr") {
text += `${formatFloat(farms[i].apr.reward + farms[i].apr.base)}%\n`;
} else {
text += `${formatFloat(farms[i].safetyScore * 10)}/10\n`;
}
}
return text;
}
function getReplyText(farms) {
let text = "Here you can find more about these farms ↓\n\n";
for (let i = 0; i < 3; i++) {
text += `→ ${farms[i].asset.symbol} - ${farmUrl(farms[i])}\n`;
}
return text;
}
async function tweeter(media, tweet_text, reply_text) {
try {
const mediaID = await rwClient.v1.uploadMedia(`assets/${media}.png`);
const { data: tweet } = await rwClient.v2.tweet({
text: tweet_text,
media: { media_ids: [mediaID] },
});
await rwClient.v2.reply(reply_text, tweet.id);
} catch (e) {
console.error(e);
}
}
async function fetchData() {
console.log("fetching api...");
console.log(process.env.API_URL);
const data = await (
await fetch(process.env.API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: `
{
farms {
id
chain
tvl
protocol
asset {
symbol
address
}
safetyScore
apr {
base
reward
}
}
}`,
}),
})
).json();
let farms = data.data.farms;
return farms;
}
module.exports = {
formatFloat,
toDollar,
getToday,
farmUrl,
getTweetText,
getReplyText,
tweeter,
fetchData,
};