mirror of
https://github.com/oceanprotocol/faucet.git
synced 2024-06-13 08:03:22 +02:00
+ balance indicator
This commit is contained in:
parent
8e39ae0a7b
commit
242d18c80a
|
@ -131,3 +131,8 @@
|
||||||
.error {
|
.error {
|
||||||
color: "red";
|
color: "red";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.balance {
|
||||||
|
text-align: end;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-12 col-xl-12 text-center">
|
<div class="col-md-12 col-xl-12 text-center">
|
||||||
<button type="submit" class="btn btn-primary skip-dark" id="createBtn">Get 10 OCEAN</button>
|
<button type="submit" class="btn btn-primary skip-dark" id="createBtn">Get 10 OCEAN</button>
|
||||||
|
<h6 class="balance">Faucet Balance - <%= balance %> OCEAN</h6>
|
||||||
</div>
|
</div>
|
||||||
<% if(message && success) { %>
|
<% if(message && success) { %>
|
||||||
<div class="notify text-center">
|
<div class="notify text-center">
|
||||||
|
|
|
@ -10,6 +10,7 @@ const { isAllowed } = require("./util");
|
||||||
var client = null;
|
var client = null;
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
|
var tokenInstance = null;
|
||||||
const infura_apikey = process.env.INFURA_NODE_ID;
|
const infura_apikey = process.env.INFURA_NODE_ID;
|
||||||
|
|
||||||
const provider = new HDWalletProvider(
|
const provider = new HDWalletProvider(
|
||||||
|
@ -21,9 +22,9 @@ const web3 = new Web3(provider);
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", async (req, res) => {
|
||||||
//res.send(template());
|
let balance = await getBalance();
|
||||||
res.render("index.ejs", { message: null, success: false });
|
res.render("index.ejs", { message: null, success: false, balance });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/send", async (req, res) => {
|
app.get("/send", async (req, res) => {
|
||||||
|
@ -46,9 +47,11 @@ app.get("/send", async (req, res) => {
|
||||||
async records => {
|
async records => {
|
||||||
console.log(records[0]);
|
console.log(records[0]);
|
||||||
if (records[0] && !isAllowed(records[0].lastUpdatedOn)) {
|
if (records[0] && !isAllowed(records[0].lastUpdatedOn)) {
|
||||||
|
let balance = await getBalance();
|
||||||
res.render("index.ejs", {
|
res.render("index.ejs", {
|
||||||
message: "You have to wait 24 hours between faucet requests",
|
message: "You have to wait 24 hours between faucet requests",
|
||||||
success: false
|
success: false,
|
||||||
|
balance
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
//insert ip address into db
|
//insert ip address into db
|
||||||
|
@ -58,20 +61,19 @@ app.get("/send", async (req, res) => {
|
||||||
);
|
);
|
||||||
|
|
||||||
//create token instance from abi and contract address
|
//create token instance from abi and contract address
|
||||||
const tokenInstance = new web3.eth.Contract(
|
const tokenInst = getTokenInstance();
|
||||||
abi,
|
|
||||||
process.env.TOKEN_CONTRACT_ADDRESS
|
|
||||||
);
|
|
||||||
|
|
||||||
tokenInstance.methods
|
tokenInst.methods
|
||||||
.transfer(to, value)
|
.transfer(to, value)
|
||||||
.send({ from }, function(error, txHash) {
|
.send({ from }, async function(error, txHash) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
console.log("txHash - ", txHash);
|
console.log("txHash - ", txHash);
|
||||||
|
let balance = await getBalance();
|
||||||
res.render("index.ejs", {
|
res.render("index.ejs", {
|
||||||
message: `Great!! test OCEANs are on the way - ${txHash}`,
|
message: `Great!! test OCEANs are on the way - ${txHash}`,
|
||||||
link: `https://rinkeby.etherscan.io/tx/${txHash}`,
|
link: `https://rinkeby.etherscan.io/tx/${txHash}`,
|
||||||
success: true
|
success: true,
|
||||||
|
balance
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
@ -85,6 +87,24 @@ app.get("/send", async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function getBalance() {
|
||||||
|
let tokenInst = getTokenInstance();
|
||||||
|
let bal = await tokenInst.methods.balanceOf(process.env.FROM).call();
|
||||||
|
let balance = web3.utils.fromWei(bal, "ether");
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTokenInstance() {
|
||||||
|
if (!tokenInstance) {
|
||||||
|
//create token instance from abi and contract address
|
||||||
|
tokenInstance = new web3.eth.Contract(
|
||||||
|
abi,
|
||||||
|
process.env.TOKEN_CONTRACT_ADDRESS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return tokenInstance;
|
||||||
|
}
|
||||||
|
|
||||||
const port = process.env.PORT || 4000;
|
const port = process.env.PORT || 4000;
|
||||||
|
|
||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user