From 3e36ea5d9255cd31138151cc75a139bbae7798bd Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Wed, 11 May 2022 14:44:17 +0530 Subject: [PATCH] remove npm dependency: vortex --- package-lock.json | 11 ---- package.json | 1 - projects/mojitoswap/utils.js | 6 +- projects/vortex/tzkt.api.js | 10 --- projects/vortex/vortex.api.js | 111 ++++++---------------------------- 5 files changed, 22 insertions(+), 117 deletions(-) delete mode 100644 projects/vortex/tzkt.api.js diff --git a/package-lock.json b/package-lock.json index 3b3dd7ef6..60b47cd49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,6 @@ "bignumber.js": "^9.0.1", "borsh": "^0.7.0", "caver-js": "^1.6.5", - "dayjs": "^1.11.0", "dotenv": "^8.6.0", "ethers": "^5.6.5", "graphql": "^15.5.0", @@ -4748,11 +4747,6 @@ "node": ">=0.10" } }, - "node_modules/dayjs": { - "version": "1.11.2", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.2.tgz", - "integrity": "sha512-F4LXf1OeU9hrSYRPTTj/6FbO4HTjPKXvEIC1P2kcnFurViINCVk3ZV0xAS3XVx9MkMsXbbqlK6hjseaYbgKEHw==" - }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -15669,11 +15663,6 @@ "assert-plus": "^1.0.0" } }, - "dayjs": { - "version": "1.11.2", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.2.tgz", - "integrity": "sha512-F4LXf1OeU9hrSYRPTTj/6FbO4HTjPKXvEIC1P2kcnFurViINCVk3ZV0xAS3XVx9MkMsXbbqlK6hjseaYbgKEHw==" - }, "debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index e0a5bc9b2..a3df72b06 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "bignumber.js": "^9.0.1", "borsh": "^0.7.0", "caver-js": "^1.6.5", - "dayjs": "^1.11.0", "dotenv": "^8.6.0", "ethers": "^5.6.5", "graphql": "^15.5.0", diff --git a/projects/mojitoswap/utils.js b/projects/mojitoswap/utils.js index 09daac9bd..d05f9a822 100644 --- a/projects/mojitoswap/utils.js +++ b/projects/mojitoswap/utils.js @@ -1,5 +1,4 @@ const { request, gql } = require("graphql-request"); -const dayjs = require("dayjs"); const { toUSDTBalances } = require("../helper/balances"); const Ethers = require("ethers"); @@ -22,14 +21,15 @@ const { JsonRpcProvider } = require("@ethersproject/providers"); async function getLatestBlock(timestamp) { // a few blocks behind the blockchain,so we write a hack here - const utcCurrentTime = timestamp ?? dayjs().unix(); + const unixTimeNow = Math.floor(Date.now()/1000) + const utcCurrentTime = timestamp ?? unixTimeNow; const res = await request(KCC_BLOCK_GRAPH, GET_BLOCK, { timestampFrom: utcCurrentTime, timestampTo: utcCurrentTime + 600, }); const block = - res?.blocks[0]?.number ?? (await getLatestBlock(dayjs().unix() - 600)); + res?.blocks[0]?.number ?? (await getLatestBlock(unixTimeNow - 600)); return Number(block); } diff --git a/projects/vortex/tzkt.api.js b/projects/vortex/tzkt.api.js deleted file mode 100644 index 259d0851f..000000000 --- a/projects/vortex/tzkt.api.js +++ /dev/null @@ -1,10 +0,0 @@ -const http = require("./http"); - -const baseURL = "https://api.tzkt.io/v1"; -const api = http(baseURL); - -const getXtzPrice = async () => api.get("quotes/last"); - -module.exports = { - getXtzPrice, -}; diff --git a/projects/vortex/vortex.api.js b/projects/vortex/vortex.api.js index 70f4748f4..323d674d1 100644 --- a/projects/vortex/vortex.api.js +++ b/projects/vortex/vortex.api.js @@ -1,116 +1,43 @@ const http = require("./http"); const queries = require("./queries"); -const dayjs = require("dayjs"); -const utc = require("dayjs/plugin/utc"); - -dayjs.extend(utc); - const baseUrl = "https://api.smartlink.so/v1"; + const endpoint = "graphql"; const api = http(baseUrl); -const inheritLastData = (tvls) => { - const dayjsToday = dayjs().minute(0); - const today = dayjsToday.format("YYYY-MM-DD"); - - if (!tvls.length) return []; - - const lastPoolData = tvls[0]; - const poolLastDay = dayjs(lastPoolData.timestamp * 1000).format("YYYY-MM-DD"); - const poolHasToday = poolLastDay === today; - - if (poolHasToday && lastPoolData.tvl) return tvls; - if (poolHasToday && !lastPoolData.tvl) { - tvls[0].tvl = tvls[1]?.tvl || 0; - return tvls; - } - - return [{ ...lastPoolData, timestamp: dayjsToday.unix() }, ...tvls]; -}; - -const groupTvlsByTimestamp = (tvls) => { - return tvls.reduce((acc, cur) => { - const timestamp = dayjs(cur.timestamp * 1000) - .startOf("day") - .valueOf(); - - if (!acc[timestamp]) { - acc[timestamp] = { - ...cur, - timestamp, - }; - return acc; - } - - acc[timestamp] = { - ...acc[timestamp], - tvl: +acc[timestamp].tvl + +cur.tvl, - }; - return acc; - }, {}); -}; - -const flatPoolsData = (poolsTvl) => { - return poolsTvl - .map((pool) => { - const data = pool.pairDayData || pool.pairHourData || []; - - return inheritLastData(data); - }) - .flat(Infinity); -}; +function formatDate(unixTimestamp) { + const timestamp = unixTimestamp ? unixTimestamp * 1000 : Date.now() + return new Date(timestamp).toISOString().slice(0, 10) +} const getTotalStakingTvl = async () => { - try { - const response = await api.post(endpoint, { - ...queries.getTotalStakingTvl(), - }); + const response = await api.post(endpoint, { + ...queries.getTotalStakingTvl(), + }); - return response.aggregate.sum.total; - } catch (error) { - return 0; - } -}; - -const getFarmsTvl = async () => { - const [farmsTvl, farmsV2Tvl] = await Promise.all([ - api.post(endpoint, { - ...queries.getFarmsTvl(), - }), - api.post(endpoint, { - ...queries.getFarmsV2Tvl(), - }), - ]); - - return [...farmsTvl, ...farmsV2Tvl].reduce((acc, cur) => { - const liquidityPairs = cur.liquidity.pairs[0] || { reserve0: 0 }; - const lpXtz = (liquidityPairs.reserve0 * 2) / cur.liquidity.totalSupply; - const tvlXtz = lpXtz * cur.totalStaked; - - return acc + tvlXtz; - }, 0); + return response.aggregate.sum.total || 0; }; const getDexTvl = async () => { - const start = dayjs().utc().subtract(1, "month").unix(); - const today = dayjs().utc().unix(); + const today = Math.floor(Date.now() / 1000); + const start = today - 1 * 24 * 3600; // from one day ago + const todayStr = formatDate(today) + let tvl = 0 const poolsTvl = await api.post(endpoint, { ...queries.tvlPerPool(start, today), }); - const dexTvl = groupTvlsByTimestamp(flatPoolsData(poolsTvl)); + poolsTvl.forEach(pool => { + const todayTvl = pool.pairDayData.find(i => formatDate(i.timestamp) === todayStr) + if (todayTvl) + tvl += todayTvl.tvl + }) - const dexTvlSorted = Object.values(dexTvl).sort((a, b) => { - if (a.timestamp === b.timestamp) return 0; - return a.timestamp < b.timestamp ? -1 : 1; - }); - - return dexTvlSorted.length ? dexTvlSorted[dexTvlSorted.length - 1].tvl : 0; + return tvl; }; module.exports = { getTotalStakingTvl, - getFarmsTvl, getDexTvl, };