faucet/src/server.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-10-20 20:56:31 +02:00
const express = require("express");
const bodyParser = require("body-parser");
const url = require("url");
const HDWalletProvider = require("truffle-hdwallet-provider");
const Web3 = require("web3");
const path = require("path");
const abi = require("./abi/token");
const { connection, insert, find } = require("./db");
const { isAllowed } = require("./util");
var client = null;
require("dotenv").config();
2020-10-12 16:42:30 +02:00
2020-10-20 20:56:31 +02:00
const infura_apikey = process.env.INFURA_NODE_ID;
2020-10-12 16:42:30 +02:00
const provider = new HDWalletProvider(
process.env.SEED_PHRASE,
2020-10-20 20:56:31 +02:00
"https://rinkeby.infura.io/v3/" + infura_apikey
);
const web3 = new Web3(provider);
2020-10-12 16:42:30 +02:00
2020-10-20 20:56:31 +02:00
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
2020-10-12 16:42:30 +02:00
2020-10-20 20:56:31 +02:00
app.get("/", (req, res) => {
//res.send(template());
res.render("index.ejs", { message: null, success: false });
});
2020-10-12 16:42:30 +02:00
2020-10-20 20:56:31 +02:00
app.get("/send", async (req, res) => {
2020-10-14 15:50:17 +02:00
try {
2020-10-20 20:56:31 +02:00
let ipAddress =
req.headers["x-forwarded-for"] || req.connection.remoteAddress;
console.log(`ip address - `, ipAddress);
const url_parts = url.parse(req.url, true);
const query = url_parts.query;
2020-10-12 16:42:30 +02:00
2020-10-20 20:56:31 +02:00
const from = process.env.FROM;
const to = query.address;
const value = web3.utils.toWei(process.env.TOKEN_AMOUNT, "ether");
2020-10-12 16:42:30 +02:00
2020-10-14 15:50:17 +02:00
//check if this user is in cool down period
2020-10-20 20:56:31 +02:00
await find(
{
$or: [{ wallet: query.address }]
},
async records => {
console.log(records[0]);
if (records[0] && !isAllowed(records[0].lastUpdatedOn)) {
res.render("index.ejs", {
message: "You have to wait 24 hours between faucet requests",
success: false
});
} else {
//insert ip address into db
await insert(
{ ip: ipAddress, wallet: to, lastUpdatedOn: Date.now() },
result => console.log(result)
);
2020-10-12 16:42:30 +02:00
2020-10-20 20:56:31 +02:00
//create token instance from abi and contract address
const tokenInstance = new web3.eth.Contract(
abi,
process.env.TOKEN_CONTRACT_ADDRESS
);
2020-10-14 15:50:17 +02:00
2020-10-20 20:56:31 +02:00
tokenInstance.methods
.transfer(to, value)
.send({ from }, function(error, txHash) {
if (!error) {
console.log("txHash - ", txHash);
res.render("index.ejs", {
message: `Great!! test OCEANs are on the way - ${txHash}`,
link: `https://rinkeby.etherscan.io/tx/${txHash}`,
success: true
});
} else {
console.error(err);
}
});
}
2020-10-14 15:50:17 +02:00
}
2020-10-20 20:56:31 +02:00
);
2020-10-14 15:50:17 +02:00
} catch (err) {
2020-10-20 20:56:31 +02:00
console.error(err);
2020-10-14 15:50:17 +02:00
}
2020-10-20 20:56:31 +02:00
});
const port = process.env.PORT || 4000;
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/public"));
app.use(express.static(__dirname + "/public"));
2020-10-12 16:42:30 +02:00
app.listen(port, async () => {
2020-10-20 20:56:31 +02:00
client = await connection();
console.log("Listening on port - ", port);
});