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

Commit

Permalink
path endpoint added with dijkstra algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammad-Mubeen committed Feb 3, 2022
1 parent 87fb140 commit 74e0016
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var swapRouter = require("./routes/swaproutes");
var pairRouter = require("./routes/pairroutes");
var erc20Router = require("./routes/erc20routes");
var coinsmarketcapapiRouter = require("./routes/coinsmarketcapapi");
var pathRouter = require("./routes/pathroutes");

// view engine setup
app.set("views", path.join(__dirname, "views"));
Expand Down Expand Up @@ -79,6 +80,7 @@ app.use("/", swapRouter);
app.use("/", pairRouter);
app.use("/", erc20Router);
app.use("/", coinsmarketcapapiRouter);
app.use("/", pathRouter);

app.use(
"/graphql",
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"jade": "~1.11.0",
"mongoose": "^6.0.8",
"morgan": "~1.9.1",
"node-dijkstra": "^2.5.0",
"nodemon": "^2.0.13",
"prettier": "^2.3.2",
"request": "^2.88.2",
Expand Down
108 changes: 108 additions & 0 deletions routes/pathroutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
require("dotenv").config();
var express = require("express");
var router = express.Router();
var pair = require("../models/pair");
const Graph = require("node-dijkstra");
const { consoleTestResultHandler } = require("tslint/lib/test");

router.route("/getpath").post(async function (req, res, next) {
try {
if (!req.body.tokenASymbol) {
return res.status(400).json({
success: false,
message: "tokenASymbol not found in the request Body.",
});
}
if (!req.body.tokenBSymbol) {
return res.status(400).json({
success: false,
message: "tokenBSymbol not found in the request Body.",
});
}

let pairs = await pair.find({});
if (pairs.length == 0) {
return res.status(400).json({
success: false,
message: "There is no pair in the database.",
});
} else {

const graph = new Map();

for (var i = 0; i < pairs.length; i++) {
let token0 = pairs[i].token0.symbol;
let token1 = pairs[i].token1.symbol;
let pairswithtoken0 = [];
let pairswithtoken1 = [];
for (var j = 0; j < pairs.length; j++) {
if (pairs[j].token0.symbol == token0) {
pairswithtoken0.push(pairs[j].token1.symbol);
}
if (pairs[j].token1.symbol == token0) {
pairswithtoken0.push(pairs[j].token0.symbol);
}
if (pairs[j].token0.symbol == token1) {
pairswithtoken1.push(pairs[j].token1.symbol);
}
if (pairs[j].token1.symbol == token1) {
pairswithtoken1.push(pairs[j].token0.symbol);
}
}
const a = new Map();
for (var z = 0; z < pairswithtoken0.length; z++) {
a.set(pairswithtoken0[z], 1);
}
graph.set(token0, a);

const b = new Map();
for (var z = 0; z < pairswithtoken1.length; z++) {
b.set(pairswithtoken1[z], 1);
}
graph.set(token1, b);
}
const route = new Graph(graph);
console.log("graph: ",graph);
let path = route.path(req.body.tokenASymbol, req.body.tokenBSymbol);
if (path != null) {
let pathwithcontractHash = [];
for(var i=0;i<path.length;i++)
{
for(var j=0;j<pairs.length;j++)
{
if(pairs[j].token0.symbol==path[i])
{
pathwithcontractHash.push(pairs[j].token0.id);
break;
}
if(pairs[j].token1.symbol==path[i])
{
pathwithcontractHash.push(pairs[j].token1.id);
break;
}
}
}
return res.status(200).json({
success: true,
message: "Path found against these tokens.",
path: path,
pathwithcontractHash: pathwithcontractHash,
});
} else {
return res.status(400).json({
success: false,
message: "Path not found against these tokens.",
path: path,
});
}
}
} catch (error) {
console.log("error (try-catch) : " + error);
return res.status(500).json({
success: false,
err: error,
});
}
});

module.exports = router;
73 changes: 55 additions & 18 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,58 @@
// var j=parseFloat(i);
// console.log("j: ",j);

var a = BigInt(
"782910138827292261791972728324982565756575668687686786786786787686786786"
);
var b = BigInt(
"782910138827292261791972728324982565756575668687686786786786787686786786"
);

console.log(a);
console.log(b.toString());
console.log((a * b).toString());
console.log(((a * b) / b).toString());

console.log(BigInt(000000000));
console.log(2 * 1 + (9 + 1) * 3);
if(BigInt(1)>BigInt(0))
{
console.log(2 * 1 + (9 + 1) * 3);
}
// var a = BigInt(
// "782910138827292261791972728324982565756575668687686786786786787686786786"
// );
// var b = BigInt(
// "782910138827292261791972728324982565756575668687686786786786787686786786"
// );

// console.log(a);
// console.log(b.toString());
// console.log((a * b).toString());
// console.log(((a * b) / b).toString());

// console.log(BigInt(000000000));
// console.log(2 * 1 + (9 + 1) * 3);
// if(BigInt(1)>BigInt(0))
// {
// console.log(2 * 1 + (9 + 1) * 3);
// }

const Graph = require('node-dijkstra');

const route = new Graph();

route.addNode('WCSPR', { WISER:1 });
route.addNode('WISER', { WCSPR:1 ,USDC:1 });
route.addNode('USDC', { WISER:1, USDT:1,WETH:1});
route.addNode('USDT', { USDC:1 });
route.addNode('ETH', { WETH:1 });
route.addNode('WETH', { ETH:1 ,USDC:1});

console.log("graph: ",route);
console.log("path: ",route.path('WCSPR', 'USDC'));
console.log("path: ",route.path('WISER', 'USDT'));
console.log("path: ",route.path('USDT', 'WISER'));
console.log("path: ",route.path('WCSPR', 'ETH'));
console.log("path: ",route.path('ETH','WETH'));
console.log("path: ",route.path('WETH','ETH'));



// const graph = new Map()

// const a = new Map()
// a.set('B', 1)

// const b = new Map()
// b.set('C', 1)


// graph.set('A', a)
// graph.set('B', b);

// const route = new Graph(graph)

// console.log("path: ",route.path('A', 'C'));

0 comments on commit 74e0016

Please sign in to comment.