Commit b14e6ce5 authored by kirtisingh's avatar kirtisingh

Linked backend with Blockchains

parent 49466459
...@@ -2,6 +2,7 @@ const express = require('express'); ...@@ -2,6 +2,7 @@ const express = require('express');
const morgan = require('morgan'); const morgan = require('morgan');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const txns = require('./models/txns'); const txns = require('./models/txns');
const Web3 = require("web3");
// express app // express app
const app = express(); const app = express();
...@@ -18,10 +19,297 @@ const connectionParams={ ...@@ -18,10 +19,297 @@ const connectionParams={
mongoose.connect(dbURI,connectionParams) mongoose.connect(dbURI,connectionParams)
.then( () => { .then( () => {
console.log('Connected to the database ') console.log('Connected to the database ')
console.log('Listening on Port 3000')
}) })
.catch( (err) => { .catch( (err) => {
console.error(`Error connecting to the database. n${err}`); console.error(`Error connecting to the database. n${err}`);
}) })
const ethNetwork = 'https://goerli.infura.io/v3/069b7d73ec604e52a5092610c9f410ea';
const web3 = new Web3(new Web3.providers.HttpProvider(ethNetwork));
const web3ont = new Web3(new Web3.providers.HttpProvider('https://polaris2.ont.io:10339'));
async function func(hashlock){
var contract = await new web3.eth.Contract([
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "address",
"name": "buyerOntAddress",
"type": "address"
}
],
"name": "bid",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "string",
"name": "secret",
"type": "string"
}
],
"name": "claimEth",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
}
],
"name": "initiateBidding",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"name": "orderList",
"outputs": [
{
"internalType": "address",
"name": "initiatorEthAddress",
"type": "address"
},
{
"internalType": "address",
"name": "buyerOntAddress",
"type": "address"
},
{
"internalType": "address",
"name": "buyerEthAddress",
"type": "address"
},
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "mxbid",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "refundTimelock",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "bidTimelock",
"type": "uint256"
},
{
"internalType": "string",
"name": "secret",
"type": "string"
},
{
"internalType": "enum EthereumContract.Status",
"name": "status",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
}
],
"name": "refundEth",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "address",
"name": "initiatorEthAddress",
"type": "address"
}
],
"name": "setInitiatorAddr",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
], '0x8909Dff24b48790c9A0590F706Dc8f3B1709ce88');
var x = await contract.methods.orderList('0xfb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603').call();
var contractont = await new web3ont.eth.Contract([
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "string",
"name": "secret",
"type": "string"
}
],
"name": "claimOnt",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "address",
"name": "initiatorEthAddress",
"type": "address"
}
],
"name": "initiateOrder",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"name": "orderList",
"outputs": [
{
"internalType": "address",
"name": "initiatorOntAddress",
"type": "address"
},
{
"internalType": "address",
"name": "initiatorEthAddress",
"type": "address"
},
{
"internalType": "address",
"name": "buyerOntAddress",
"type": "address"
},
{
"internalType": "address",
"name": "buyerEthAddress",
"type": "address"
},
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amountOnt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "refundTimelock",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "initiatedTime",
"type": "uint256"
},
{
"internalType": "string",
"name": "secret",
"type": "string"
},
{
"internalType": "enum OntologyContract.Status",
"name": "status",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
}
],
"name": "refundOnt",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hashlock",
"type": "bytes32"
},
{
"internalType": "address",
"name": "buyerOntAddress",
"type": "address"
}
],
"name": "setBuyerAddr",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
], '0xC6CD342cEba445bBfB98B9d5Da99c62B2B98CFbe');
var y = await contractont.methods.orderList('0xfb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603').call();
return {'hashlock':hashlock, 'amountOnt': y['amountOnt'], 'bidTimelock':x['bidTimelock']};
}
// register view engine // register view engine
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
...@@ -47,7 +335,21 @@ app.post('/txns', (req, res) => { ...@@ -47,7 +335,21 @@ app.post('/txns', (req, res) => {
app.get('/txns', (req,res) => { app.get('/txns', (req,res) => {
txns.find().then((result) => { txns.find().then((result) => {
res.render('orderbook', {title: 'All Transactions', txns: result}) // console.log(typeof(result));
var arr = [];
result.forEach(async txn => {
// console.log(txn.hashlock);
// var obj = {'hashlock': txn.hashlock,
// 'amountOnt': txn.amountOnt,
// 'bidTimelock': txn.bidTimelock};
var obj = await func(txn.hashlock);
if(obj['amountOnt']!=""){
arr.push(obj);
}
// console.log(obj);
});
console.log(arr);
res.render('orderbook', {title: 'All Transactions', txns: arr});
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
...@@ -62,3 +364,5 @@ app.get('/', (req, res) => { ...@@ -62,3 +364,5 @@ app.get('/', (req, res) => {
app.use((req, res) => { app.use((req, res) => {
res.status(404).render('404', { title: '404' }); res.status(404).render('404', { title: '404' });
}); });
...@@ -150,14 +150,14 @@ async function printOrderListOnt(hashlock){ ...@@ -150,14 +150,14 @@ async function printOrderListOnt(hashlock){
} }
// console.log(typeof(val)); // console.log(typeof(val));
ls.innerHTML = `<table> ls.innerHTML = `<table>
<tr><td>Initiator ONT Address:</td> <td>${orderList["initiatorOntAddress"]}</td></tr> <tr><td class="pd">Initiator ONT Address:</td> <td>${orderList["initiatorOntAddress"]}</td></tr>
<tr><td>Initiator ETH Address:</td> <td>${orderList["initiatorEthAddress"]}</td></tr> <tr><td class="pd">Initiator ETH Address:</td> <td>${orderList["initiatorEthAddress"]}</td></tr>
<tr><td>Buyer ONT Address:</td> <td>${orderList["buyerOntAddress"]}</td></tr> <tr><td class="pd">Buyer ONT Address:</td> <td>${orderList["buyerOntAddress"]}</td></tr>
<tr><td>Buyer ETH Address:</td> <td>${orderList["buyerEthAddress"]}</td></tr> <tr><td class="pd">Buyer ETH Address:</td> <td>${orderList["buyerEthAddress"]}</td></tr>
<tr><td>Amount Ont:</td> <td>${orderList["amountOnt"]}</td></tr> <tr><td class="pd">Amount Ont:</td> <td>${orderList["amountOnt"]}</td></tr>
<tr><td>Refund Time:</td> <td>${t}</td></tr> <tr><td class="pd">Refund Time:</td> <td>${t}</td></tr>
<tr><td>Secret:</td> <td>${orderList["secret"]}</td></tr> <tr><td class="pd">Secret:</td> <td>${orderList["secret"]}</td></tr>
<tr><td>Status:</td> <td>${orderList["status"]}</td></tr> <tr><td class="pd">Status:</td> <td>${orderList["status"]}</td></tr>
</table>`; </table>`;
// console.log(orderList); // console.log(orderList);
var slides = document.getElementsByName("hashlock"); var slides = document.getElementsByName("hashlock");
...@@ -247,19 +247,20 @@ async function notifyforstateineth(){ ...@@ -247,19 +247,20 @@ async function notifyforstateineth(){
// }, 10000); // }, 10000);
// } // }
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); // const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function getBidTimeLock(hashlock, tid){ async function getBidTimeLock(hashlock, tid){
if(tid == "bidtimer1"){ if(tid == "bidtimer1"){
await initiateBidding(hashlock); await initiateBidding(hashlock);
await sleep(10000); document.getElementById('todb').submit();
// await sleep(10000);
} }
const orderList = await window.contract2.methods.orderList(hashlock).call(); const orderList = await window.contract2.methods.orderList(hashlock).call();
var tl = parseInt(orderList["bidTimelock"]); var tl = parseInt(orderList["bidTimelock"]);
if(tid == "bidtimer1"){ // if(tid == "bidtimer1"){
document.getElementById('bidTimelock3').value=timeConverter(tl); // document.getElementById('bidTimelock3').value=timeConverter(tl);
document.getElementById('todb').submit();
} // }
countdown(tid, tl); countdown(tid, tl);
} }
......
...@@ -40,6 +40,9 @@ td, th { ...@@ -40,6 +40,9 @@ td, th {
text-align: left; text-align: left;
padding: 8px; padding: 8px;
} }
.pd{
padding: 8px;
}
tr:nth-child(even) { tr:nth-child(even) {
background-color: #dddddd; background-color: #dddddd;
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head> </head>
<body> <body>
<h1 class="centeralign"">Atomic Swap</h1> <h1 class="centeralign"">Atomic Swap</h1>
<br > <br >
<div class="centeralign"> <div class="centeralign">
...@@ -22,7 +21,7 @@ ...@@ -22,7 +21,7 @@
<button type="button" class="btn btn-primary" id="ETHBut" onclick="hideElement('ONT'); showElement('ETH');">ETH</button> <button type="button" class="btn btn-primary" id="ETHBut" onclick="hideElement('ONT'); showElement('ETH');">ETH</button>
</div> </div>
<hr> <hr>
<div class="centeralign" id = "ONT"> <div class="centeralign setmargin" id = "ONT">
<!-- ORDER STATUS --> <!-- ORDER STATUS -->
<div> <div>
...@@ -109,10 +108,10 @@ ...@@ -109,10 +108,10 @@
</div> </div>
<iframe name="votar" style="display:none;"></iframe> <iframe name="votar" style="display:none;"></iframe>
<form id="todb" action="/txns" method="post" target="votar"> <form id="todb" action="/txns" method="post" target="votar" style="display:none;">
<input type="hidden" name="hashlock" id="hash12"><br><br> <input type="hidden" name="hashlock" id="hash12"><br><br>
<input type="hidden" name="amountOnt" id="amountOnt3"><br><br> <!-- <input type="hidden" name="amountOnt" id="amountOnt3"><br><br>
<input type="hidden" name="bidTimelock" id="bidTimelock3"><br><br> <input type="hidden" name="bidTimelock" id="bidTimelock3"><br><br> -->
<input style="display: none;" type="submit" value="Send to Database"> <input style="display: none;" type="submit" value="Send to Database">
</form> </form>
...@@ -154,7 +153,7 @@ ...@@ -154,7 +153,7 @@
</div> </div>
<hr> <hr>
</div> </div>
<div> <div class="setmargin">
Status: <span id="status">Loading...</span> Status: <span id="status">Loading...</span>
</div> </div>
......
<html lang="en"> <html lang="en">
<head> <head>
<meta charset='utf-8'> <meta charset='utf-8'>
<!-- <meta http-equiv='X-UA-Compatible' content='IE=edge'> --> <meta http-equiv='X-UA-Compatible' content='IE=edge'>
<!-- <meta http-equiv="refresh" content="15"> --> <meta http-equiv="refresh" content="10">
<title><%= title %></title> <title><%= title %></title>
<meta name='viewport' content='width=device-width, initial-scale=1'> <meta name='viewport' content='width=device-width, initial-scale=1'>
<script src='/web3.min.js'></script>
<link rel="stylesheet" href="/style.css"> <link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
...@@ -22,6 +23,7 @@ ...@@ -22,6 +23,7 @@
<% txns.forEach(txn => { %> <% txns.forEach(txn => { %>
<tr> <tr>
<td><%= txn.hashlock %></td> <td><%= txn.hashlock %></td>
<% %>
<td><%= txn.amountOnt %></td> <td><%= txn.amountOnt %></td>
<td><%= txn.bidTimelock %></td> <td><%= txn.bidTimelock %></td>
</tr> </tr>
...@@ -30,7 +32,6 @@ ...@@ -30,7 +32,6 @@
<% } else { %> <% } else { %>
<p>There are no transactions to display...</p> <p>There are no transactions to display...</p>
<% } %> <% } %>
</div> </div>
</body> </body>
</html> </html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment