-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (112 loc) · 3.96 KB
/
app.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
require("dotenv").config()
const express = require("express")
const cors = require("cors")
const { ethers } = require("ethers")
const calculateDripAmount = require("./utils/calculateDripAmount")
const getTokenContract = require("./utils/getTokenContract")
const getSigner = require("./utils/getSigner")
const isAllowedToWithdraw = require("./utils/isAllowedToWithdraw")
const fetchLogs = require("./utils/getTransferEvents")
const PORT = process.env.PORT || 3333
const app = express()
app.use(cors({ origin: "*", credentials: true, optionSuccessStatus: 200 }))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.get("/", (req, res) => {
try {
res.status(200).send(
'<h1>Hello! This API site is under construction</h1><a href="/api/">Go to api</a>'
)
} catch (e) {
const error = e.toString()
res.status(400).json({ error })
}
})
app.get("/v1/api/faucets", async (req, res) => {
try {
const { wallet: walletMumbai } = getSigner(80001)
const { wallet: walletSepolia } = getSigner(11155111)
const tokenContractSepolia = getTokenContract(80001)
const tokenContractMumbai = getTokenContract(11155111)
const faucetBalanceSepolia = await tokenContractSepolia.balanceOf(
walletSepolia.address
)
const faucetBalanceMumbai = await tokenContractMumbai.balanceOf(
walletMumbai.address
)
const sepoliaFaucetDripAmount =
calculateDripAmount(faucetBalanceSepolia)
const mumbaiFaucetDripAmount = calculateDripAmount(faucetBalanceMumbai)
res.status(200).json({
balanceSepolia: ethers.formatEther(faucetBalanceSepolia),
balanceMumbai: ethers.formatEther(faucetBalanceMumbai),
sepoliaFaucetDripAmount,
mumbaiFaucetDripAmount,
})
} catch (e) {
const error = e.toString()
res.status(400).json({ error })
}
})
app.get("/v1/api/transactions/:chainId", async (req, res) => {
const { chainId } = req.params
try {
const { logsData, feeData } = await fetchLogs(chainId)
res.status(200).json({
logsData,
feeData,
})
} catch (e) {
const error = e.toString()
res.status(400).json({ error })
}
})
app.post("/v1/api/request", async (req, res) => {
try {
res.writeHead(201, { "content-type": "application/json" })
const { chainId, address } = req.body
const regex = /[0-9A-Fa-f]{6}/g
if (address.match(regex)) {
const tokenContract = getTokenContract(chainId)
const { wallet } = getSigner(chainId)
const faucetBalance = await tokenContract.balanceOf(wallet.address)
const dripAmount = calculateDripAmount(faucetBalance)
const canWithdraw = isAllowedToWithdraw(
chainId,
address,
dripAmount,
res
)
if (Number(dripAmount) > 0 && canWithdraw) {
const transactionResponse = await tokenContract.transfer(
address,
ethers.parseEther(dripAmount),
{
gasLimit: 100000,
}
)
res.write(
JSON.stringify({
transactionResponse: transactionResponse,
success: true,
})
)
res.end()
}
} else {
res.write(
JSON.stringify({
message: "Please insert a valid address.",
})
)
res.end()
}
} catch (e) {
const error = e.toString()
res.status(400).json({ error })
}
})
app.all("*", (req, res) => {
res.status(404).send("Sorry, this resource doesn't exist!")
})
app.listen(PORT, () => console.log(`The sever is running on port ${PORT}`))