-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
161 lines (142 loc) · 3.79 KB
/
app.js
File metadata and controls
161 lines (142 loc) · 3.79 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import axios from "axios";
import Fastify from "fastify";
import { formatEther, formatUnits } from "ethers";
import known_tokens from "./tokens.js";
import chains from "./chains.js";
// import dotenv from "dotenv"; // uncomment this line if you want to use .env file
// dotenv.config();
let app = Fastify({
logger: true
});
app.get("/", async (req, res) => {
res.send({
message: "API is working"
})
})
function getChainName(chain_id) {
let chain = chains.find(chain => chain.id == chain_id);
if (chain) {
return chain.name;
}
else {
return chain_id;
}
}
function getContractDetails(chain_id, contract_address) {
console.log("contract_address", contract_address)
console.log("chain_id", chain_id)
let contract = known_tokens.find(token => token.chainId == chain_id && token.address.toLowerCase() == contract_address.toLowerCase());
if (contract) {
return contract;
}
else {
return null;
}
};
function formatAmount(amount_in_wei, is_native, chain_id, token_contract) {
if (is_native) {
let decimals = 18;
let amount = formatEther(amount_in_wei);
return amount;
} else {
let contract = getContractDetails(chain_id, token_contract);
if (contract) {
let decimals = contract.decimals;
let amount = formatUnits(amount_in_wei, decimals);
return amount;
}
else {
return "Unknown Token";
}
}
}
app.post("/discord", async function (req, res) {
if (!req.body) {
return Error("No request body");
}
const { from, to, is_native, chain_id, amount_as_string, transaction_hash, token_contract } = req.body;
let discord_webhook = process.env.DISCORD_WEBHOOK_URL;
if (!discord_webhook) {
return Error("Discord webhook url is not set");
}
let transferType = is_native ? "native" : "Token";
let chainName = getChainName(chain_id);
let contract = getContractDetails(chain_id, token_contract);
let formatted_amount = formatAmount(amount_as_string, is_native, chain_id, token_contract);
let embeds = [
{
name: "Chain",
value: chainName
},
{
name: "From",
value: from
},
{
name: "To",
value: to
},
{
name: "Transaction Hash",
value: transaction_hash
},
{
name: "Amount in Wei",
value: amount_as_string
},
{
name: "Formatted Amount",
value: formatted_amount
},
]
console.log("contract", contract)
if (token_contract) {
embeds.push({
name: "Token Contract Address",
value: token_contract
})
}
if (contract) {
embeds.push({
name: "Token Name",
value: contract.name
})
embeds.push({
name: "Token Symbol",
value: contract.symbol
})
embeds.push({
name: "Token Decimals",
value: contract.decimals
})
}
try {
await axios.post(discord_webhook, {
content: `New ${transferType} transfer on ${getChainName(chain_id)} network`,
embeds: [
{
title: "Transfer Details",
fields: embeds
}
]
})
res.send({
message: "success"
})
}
catch (e) {
console.log(e)
return Error("Error sending message to discord");
}
})
const start = async () => {
try {
app.listen({
port: process.env.PORT || 80,
host: "0.0.0.0"
})
} catch (e) {
console.log(e)
}
}
start();