github.com/diadata-org/diadata@v1.4.593/cmd/interlay/index.js (about) 1 const { ApiPromise, WsProvider } = require("@polkadot/api"); 2 const { getInterlayValues } = require("./interlayhelper"); 3 const { getBiFrostValues } = require("./bifrosthelper"); 4 5 const { cronstart } = require("./cron"); 6 let cron = require("node-cron"); 7 8 const { tokenkey, redis, allowedTokens, pricekey } = require("./utils"); 9 10 let cache = redis(); 11 12 13 /* 14 15 Env variables 16 BIFROST_PARACHAIN_NODE_URL 17 BIFROST_POLKADOT_NODE_URL 18 INTERLAY_NODE_URL 19 KITSUNGI_NODE_URL 20 ETHEREUM_NODE_URL 21 22 23 */ 24 cronstart(); 25 26 cron.schedule("* * * * *", () => { 27 console.log("running a task every minute"); 28 cronstart(); 29 }); 30 31 let express = require("express"); 32 let app = express(); 33 34 app.get("/customer/interlay/state/:token", async function (req, res) { 35 // kbtc, ibtc 36 const allowedTokens = ["IBTC", "KBTC", "DOT"]; 37 38 let token = req.params.token; 39 token = token.toUpperCase(); 40 41 if (allowedTokens.includes(token)) { 42 // let values = await getInterlayValues(token); 43 let values = JSON.parse(await cache.get("interlayraw" + token)); 44 45 res.send(values); 46 } else { 47 res.send({ err: "invalid token use, IBTC or KBTC" }); 48 } 49 }); 50 51 app.get("/customer/bifrost/state/:token", async function (req, res) { 52 // kbtc, ibtc 53 const allowedTokens = ["KSM","DOT","MOVR","BNC","GLMR","ASTR","FIL"]; 54 55 let token = req.params.token; 56 token = token.toUpperCase(); 57 58 if (allowedTokens.includes(token)) { 59 try{ 60 let values = await getBiFrostValues(token); 61 res.send(values); 62 }catch(e){ 63 res.send({ err: "err getting value from bifrost" }); 64 } 65 66 } else { 67 res.send({ err: "invalid token use, KSM or DOT" }); 68 } 69 }); 70 71 let values = { 72 Token: "", 73 FairPrice: null, 74 Collateralratio: { 75 IssuedToken: "", 76 LockedToken: "", 77 Ratio: "", 78 }, 79 BaseAssetSymbol: "", 80 BaseAssetPrice: "", 81 Issuer: "", 82 TimeStamp: "", 83 84 }; 85 86 function findTokenByVTokenAndIssuer(vtoken, issuer) { 87 const lowerCaseIssuer = issuer.toLowerCase(); 88 const lowerCaseVtoken = vtoken.toLowerCase(); 89 90 for (let i = 0; i < allowedTokens.length; i++) { 91 if ( 92 allowedTokens[i].vtoken.toLowerCase() === lowerCaseVtoken && 93 allowedTokens[i].issuer.toLowerCase() === lowerCaseIssuer 94 ) { 95 return allowedTokens[i]; 96 } 97 } 98 return null; 99 } 100 app.get("/xlsd/:issuer/:vtoken", async function (req, res) { 101 let issuer = req.params.issuer; 102 let vtoken = req.params.vtoken; 103 104 let token = findTokenByVTokenAndIssuer(vtoken, issuer); 105 106 if (token) { 107 let cacheddata = JSON.parse( 108 await cache.get(tokenkey(token.source, token.vtoken)) 109 ); 110 111 112 let tokenvalues = await createXResponse(cacheddata, token); 113 res.send(tokenvalues); 114 } else { 115 res.send({ err: "invalid token and issuer" }); 116 } 117 }); 118 119 async function createXResponse(cacheddata, token) { 120 let tokenvalues = JSON.parse(JSON.stringify(values)); 121 let baseassetprice; 122 baseassetprice = await cache.get(pricekey(token.token)); 123 124 if (cacheddata && cacheddata.collateral_ratio) { 125 tokenvalues.Collateralratio = { 126 IssuedToken: cacheddata.collateral_ratio.issued_token, 127 LockedToken: cacheddata.collateral_ratio.locked_token, 128 Ratio: cacheddata.collateral_ratio.ratio, 129 }; 130 } 131 if (cacheddata && cacheddata.fair_price) { 132 tokenvalues.FairPrice = cacheddata.fair_price; 133 } 134 135 if (cacheddata && cacheddata.timestamp) { 136 tokenvalues.TimeStamp = cacheddata.timestamp; 137 } 138 if (cacheddata && cacheddata.decimal) { 139 tokenvalues.decimal = cacheddata.decimal; 140 } 141 // if (cacheddata && cacheddata.ratio) { 142 // tokenvalues.ratio = cacheddata.ratio; 143 // } 144 tokenvalues.BaseAssetPrice = parseFloat(baseassetprice); 145 tokenvalues.Token = token.vtoken; 146 tokenvalues.BaseAssetSymbol = token.token; 147 tokenvalues.Issuer = token.issuer; 148 149 150 return tokenvalues; 151 } 152 153 app.get("/xlsd", async function (req, res) { 154 // kbtc, ibtc 155 156 let response = []; 157 158 for (const token of allowedTokens) { 159 let cacheddata = JSON.parse( 160 await cache.get(tokenkey(token.source, token.vtoken)) 161 ); 162 163 164 let tokenvalues = await createXResponse(cacheddata, token); 165 166 response.push(tokenvalues); 167 } 168 res.send(response); 169 }); 170 171 let server = app.listen(8081, function () { 172 var host = server.address().address; 173 var port = server.address().port; 174 console.log(" App listening at http://%s:%s", host, port); 175 });