diff --git a/app.js b/app.js index 09b546f3..93b439d4 100644 --- a/app.js +++ b/app.js @@ -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")); @@ -79,6 +80,7 @@ app.use("/", swapRouter); app.use("/", pairRouter); app.use("/", erc20Router); app.use("/", coinsmarketcapapiRouter); +app.use("/", pathRouter); app.use( "/graphql", diff --git a/package-lock.json b/package-lock.json index 1b6be62a..cb747d34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2414,6 +2414,11 @@ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz", "integrity": "sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==" }, + "node-dijkstra": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/node-dijkstra/-/node-dijkstra-2.5.0.tgz", + "integrity": "sha1-D+t2xaBfNbVueG3m300zZK8o1Og=" + }, "node-fetch": { "version": "2.6.1", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", diff --git a/package.json b/package.json index 236be8af..462ccadd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes/pathroutes.js b/routes/pathroutes.js new file mode 100644 index 00000000..f1bc0941 --- /dev/null +++ b/routes/pathroutes.js @@ -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;iBigInt(0)) -{ - console.log(2 * 1 + (9 + 1) * 3); -} \ No newline at end of file +// 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')); \ No newline at end of file