1
0
mirror of https://github.com/oceanprotocol/faucet.git synced 2024-12-22 09:13:13 +01:00

+ error handling

This commit is contained in:
“ciscodacunha” 2020-10-21 20:32:58 +02:00
parent c6e11b3b17
commit bc7c87bf89
3 changed files with 61 additions and 59 deletions

View File

@ -124,14 +124,6 @@
margin-top: 5%;
}
.success {
color: "green";
}
.error {
color: "red";
}
.balance {
text-align: end;
margin-top: 10px;
@ -147,6 +139,7 @@
border-color: #f0267e;
}
.btn-primary:visited,
.btn-primary:active {
background-color: #7b1173;
box-shadow: 0 5px #666;

View File

@ -48,16 +48,16 @@
<button type="submit" class="btn btn-primary skip-dark" id="createBtn">Get 10 OCEAN</button>
<h6 class="balance">Faucet Balance - <%= balance %> OCEAN</h6>
</div>
<% if(message && success) { %>
<div class="notify text-center">
<h5 class="success"><%= message %></h5>
<a target="_blank" href="https://rinkeby.etherscan.io/tx/<%= txHash %>"><%= txHash%></a>
</div>
<% } else { %>
<div class="notify text-center">
<h5 class="error"><%= message %></h5>
</div>
<div class="notify text-center">
<% if(message) { %>
<% if(status) { %>
<h5 class="text-success"><%= message %></h5>
<a target="_blank" href="https://rinkeby.etherscan.io/tx/<%= txHash %>"><%= txHash%></a>
<% } else { %>
<h5 class="text-danger"><%= message %></h5>
<% } %>
<% } %>
</div>
</form>

View File

@ -24,11 +24,12 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", async (req, res) => {
let balance = await getBalance();
res.render("index.ejs", { message: null, success: false, balance });
res.render("index.ejs", { message: null, status: false, balance });
});
app.get("/send", async (req, res) => {
try {
let balance = await getBalance();
let ipAddress =
req.headers["x-forwarded-for"] || req.connection.remoteAddress;
console.log(`ip address - `, ipAddress);
@ -38,50 +39,58 @@ app.get("/send", async (req, res) => {
const from = process.env.FROM;
const to = query.address;
const value = web3.utils.toWei(process.env.TOKEN_AMOUNT, "ether");
//check if its valid ETH address
//check if this user is in cool down period
await find(
{
$or: [{ wallet: query.address }]
},
async records => {
console.log(records[0]);
if (records[0] && !isAllowed(records[0].lastUpdatedOn)) {
let balance = await getBalance();
res.render("index.ejs", {
message: "You have to wait 24 hours between faucet requests",
success: false,
balance
});
} else {
//insert ip address into db
await insert(
{ ip: ipAddress, wallet: to, lastUpdatedOn: Date.now() },
result => console.log(result)
);
//create token instance from abi and contract address
const tokenInst = getTokenInstance();
tokenInst.methods
.transfer(to, value)
.send({ from }, async function(error, txHash) {
if (!error) {
console.log("txHash - ", txHash);
let balance = await getBalance();
res.render("index.ejs", {
message: `Great!! test OCEANs are on the way !!`,
txHash,
success: true,
balance
});
} else {
console.error(error);
}
if (web3.utils.isAddress(to)) {
//check if this user is in cool down period
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",
status: false,
balance
});
} else {
//insert ip address into db
await insert(
{ ip: ipAddress, wallet: to, lastUpdatedOn: Date.now() },
result => console.log(result)
);
//create token instance from abi and contract address
const tokenInst = getTokenInstance();
tokenInst.methods
.transfer(to, value)
.send({ from }, async function(error, txHash) {
if (!error) {
console.log("txHash - ", txHash);
res.render("index.ejs", {
message: `Great!! test OCEANs are on the way !!`,
txHash,
status: true,
balance
});
} else {
console.error(error);
}
});
}
}
}
);
);
} else {
//handle incorrect address response
res.render("index.ejs", {
message: `Please enter valid Ethereum Wallet Address`,
status: false,
balance
});
}
} catch (err) {
console.error(err);
}