Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
endpoint added for getpair in tokensList route and listenroute addpac…
Browse files Browse the repository at this point in the history
…kageandcontrachash endpoint updated
  • Loading branch information
Hammad-Mubeen committed Dec 24, 2021
1 parent 7e9f994 commit e6e556c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 16 deletions.
6 changes: 4 additions & 2 deletions routes/listenerroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ router.route("/addpaircontractandpackageHash").post(async function (req, res, ne
message: "There is no packageHash specified in the req body.",
});
}

var newpair = new hashesofpairsModel({contractHash:req.body.contractHash,packageHash:req.body.packageHash});

let contractHash=(req.body.contractHash).toLowerCase();
let packageHash=(req.body.packageHash).toLowerCase();
var newpair = new hashesofpairsModel({contractHash:contractHash,packageHash:packageHash});
await hashesofpairsModel.create(newpair);

return res.status(200).json({
Expand Down
103 changes: 98 additions & 5 deletions routes/tokenslist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require('dotenv').config()
var express = require('express');
var router = express.Router();
var tokensListModel=require('../models/tokensList');
var pairModel=require('../models/pair');
var hashesofpairsModel=require('../models/hashesofpairs');

router.route("/addtokensList").post(async function (req, res, next) {
try {
Expand Down Expand Up @@ -43,11 +45,7 @@ router.route("/tokensList").get(async function (req, res, next) {
message: "There is no data in tokensList Collection.",
});
}
// return res.status(200).json({
// success: true,
// message: "Token List Data: ",
// data: tokensList.data
// });

return res.send(tokensList.data);

} catch (error) {
Expand All @@ -59,5 +57,100 @@ router.route("/tokensList").get(async function (req, res, next) {
}
});

router.route("/getpair").post(async function (req, res, next) {
try {

if(!req.body.token0)
{
return res.status(400).json({
success: false,
message: "token0 not found in the body.",
});
}
if(!req.body.token1)
{
return res.status(400).json({
success: false,
message: "token1 not found in the body.",
});
}

var pairresult= await pairModel.find({});
if(pairresult.length==0)
{
return res.status(400).json({
success: false,
message: "There is no pair in the database.",
});
}
else
{
let paircontractHash=null;
for(var i=0;i<pairresult.length;i++)
{
if(pairresult[i].token0.id == req.body.token0 && pairresult[i].token1.id == req.body.token1)
{
paircontractHash=pairresult[i].id;
}
if(i==pairresult.length-1)
{
if(paircontractHash==null)
{
return res.status(400).json({
success: false,
message: "There is no pair against this token0 and token1.",
});
}
}
}
if(paircontractHash!=null)
{
var pairsresult2=await hashesofpairsModel.find({});
if(pairsresult2.length==0)
{
console.log("there are no contract and package hash found in the database.");
return res.status(400).json({
success: false,
message: "there are no contract and package hash found in the database."
});
}
var pairpackageHash=null;
for(var i=0;i<pairsresult2.length;i++)
{
if((pairsresult2[i].contractHash).toLowerCase()==paircontractHash.toLowerCase())
{
pairpackageHash=pairsresult2[i].packageHash;
return res.status(200).json({
success: true,
message: "Pair has been found on this token0 and token1.",
contractHash:paircontractHash,
packageHash:pairpackageHash
});
}
if(i==pairsresult2.length-1)
{
if(pairpackageHash==null)
{
return res.status(400).json({
success: false,
message: "There is no pair against this contract Hash in the hashesofpairs collection.",
});
}
}
}
}

}



} catch (error) {
console.log("error (try-catch) : " + error);
return res.status(500).json({
success: false,
err: error,
});
}
});

module.exports = router;
20 changes: 11 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function splitdata(data)
{
var temp=data.split('(');
var result=temp[1].split(')');
return result[0];
}
// function splitdata(data)
// {
// var temp=data.split('(');
// var result=temp[1].split(')');
// return result[0];
// }

var date = new Date("2021-12-14T18:42:24.445Z");
var seconds = date.getTime();
console.log("time: ",seconds);
// var date = new Date("2021-12-14T18:42:24.445Z");
// var seconds = date.getTime();
// console.log("time: ",seconds);
let contractHash="F41eea03821F59de6a3d48161dA1ba267Fc4048B7d6761Cb18E80C5C68049388".toLowerCase();
console.log(contractHash);

0 comments on commit e6e556c

Please sign in to comment.