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

Commit

Permalink
pairList added
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammad-Mubeen committed Dec 30, 2021
1 parent e8a74e2 commit fba3fb9
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 98 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { graphqlHTTP } = require("express-graphql");
const schema = require("./graphql/schema");
var listenerRouter = require('./routes/listenerroutes');
var tokensListRouter = require('./routes/tokenslist');
var pairsListRouter = require('./routes/pairslist');
var coinsmarketcapapiRouter = require('./routes/coinsmarketcapapi');

// view engine setup
Expand Down Expand Up @@ -68,6 +69,7 @@ app.get("/", (req, res) => {
});
app.use('/', listenerRouter);
app.use('/', tokensListRouter);
app.use('/', pairsListRouter);
app.use('/', coinsmarketcapapiRouter);

app.use('/graphql', graphqlHTTP({
Expand Down
133 changes: 133 additions & 0 deletions routes/pairslist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require('dotenv').config()
var express = require('express');
var router = express.Router();
var pairModel=require('../models/pair');
var hashesofpairsModel=require('../models/hashesofpairs');

router.route("/getpairagainsttokens").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,
});
}
});

router.route("/getpairlist").get(async function (req, res, next) {
try {

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
{
return res.status(200).json({
success: true,
message: "Pair List: ",
pairList:pairresult
});

}

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

module.exports = router;
98 changes: 0 additions & 98 deletions routes/tokenslist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ 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 @@ -59,100 +57,4 @@ 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;

0 comments on commit fba3fb9

Please sign in to comment.