This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
136 lines (108 loc) · 3.92 KB
/
index.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
133
134
135
136
"use strict";
import line from "@line/bot-sdk";
import express from "express";
import _ from "lodash";
import { getReward, getMirPrice, getPrices, getMarketPrices } from "./mirror.js";
import { listTemplate, generateRow, generateRow3, generateTemplate } from "./line.js";
// create LINE SDK config from env variables
const config = {
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET
};
// create LINE SDK client
const client = new line.Client(config);
// create Express app
// about Express itself: https://expressjs.com/
const app = express();
// register a webhook handler with middleware
// about the middleware, please refer to doc
app.post("/callback", line.middleware(config), (req, res) => {
Promise.all(req.body.events.map(handleEvent))
.then(result => res.json(result))
.catch(err => {
console.error(err);
res.status(500).end();
});
});
// event handler
async function handleEvent(event) {
if (event.type !== "message" || event.message.type !== "text") {
// ignore non-text-message event
return Promise.resolve(null);
}
// Test valid address
const text = event.message.text;
const terraAddressFormat = /^terra[a-z0-9]{39}$/;
if (terraAddressFormat.test(text)) {
return await getMirrorReward(text, event);
}
if (text.toLowerCase() === "mirror" || text.toLowerCase() === "mir") {
return await getMirrorPrices(event);
} else {
const replyMessage = { type: "text", text: "Invalid Command" };
return client.replyMessage(event.replyToken, replyMessage);
}
}
async function getMirrorReward(text, event) {
const mirrorReward = await getReward(text);
const priceData = await getMirPrice();
const mirPrice = priceData.asset.prices.price;
let replyContent = JSON.parse(JSON.stringify(listTemplate));
// Set current mir price
replyContent.body.contents[2].text = "MIR = $" + mirPrice;
// Push Reward List
let sum = 0;
mirrorReward.forEach(reward => {
sum += parseFloat(reward.reward);
replyContent.body.contents[4].contents.push(generateRow(reward.name, reward.reward));
});
replyContent.body.contents[4].contents.push(generateRow("Total(MIR)", "" + sum.toFixed(6), { bold: true }));
replyContent.body.contents[4].contents.push(
generateRow("Total(UST)", "" + (sum * parseFloat(mirPrice)).toFixed(6), {
bold: true
})
);
replyContent.body.contents[replyContent.body.contents.length - 1].contents[0].text = new Date().toUTCString();
const replyMessage = {
type: "flex",
altText: "Your mirror reward",
contents: replyContent
};
// use reply API
return client.replyMessage(event.replyToken, replyMessage);
}
async function getMirrorPrices(event) {
let template = generateTemplate("Market Prices", "Mirror", "to the moon");
let prices = await getMarketPrices();
template.body.contents[4].contents.push(generateRow3("Name", "Oracle", "AMM", "Diff%"));
prices = prices.assets;
prices = _.sortBy(prices, price => {
return price.symbol;
});
prices.forEach(price => {
let realPrice = price.prices.price;
realPrice = realPrice ? parseFloat(realPrice).toFixed(2) + "" : "-";
let oraclePrice = price.prices.oraclePrice;
oraclePrice = oraclePrice ? parseFloat(oraclePrice).toFixed(2) + "" : "-";
const diff = "" + (((realPrice - oraclePrice) * 100) / oraclePrice).toFixed(2);
template.body.contents[4].contents.push(
generateRow3(price.symbol, oraclePrice, realPrice, diff, {
thirdColor: "#FF2400",
firstBold: true
})
);
});
template.body.contents[template.body.contents.length - 1].contents[0].text = new Date().toUTCString();
const replyMessage = {
type: "flex",
altText: "Mirror Prices",
contents: template
};
// use reply API
return client.replyMessage(event.replyToken, replyMessage);
}
// listen on port
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`listening on ${port}`);
});