Skip to content

Commit

Permalink
Moving to decentralized subgraphs + Update Web3js to 4.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianbayart committed Aug 16, 2024
1 parent 45ec76f commit 67b149e
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 46 deletions.
71 changes: 55 additions & 16 deletions back/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import express from 'express'
import fetch from 'node-fetch'
import { createClient } from 'redis'

import dotenv from 'dotenv'
dotenv.config({
path: '.env'
})
console.log(process.env.THE_GRAPH_API)

/********************************
Expand Down Expand Up @@ -38,6 +43,16 @@ const HISTORY_SIZE = process.env.NODE_ENV === 'production' ? 320 : 96 // more da
const HISTORY_SIZE_24H = 96 // 24h / 15min
const TOP_SIZE = 6

const THE_GRAPH_API = process.env.THE_GRAPH_API
const THE_GRAPH_BASE_URL = `https://gateway.thegraph.com/api/${THE_GRAPH_API}/subgraphs/id`

const THE_GRAPH_UniswapV3Ethereum = THE_GRAPH_BASE_URL + '/5zvR82QoaXYFyDEKLZ9t6v9adgnptxYpKpSbxtgVENFV'
const THE_GRAPH_UniswapV3Arbitrum = THE_GRAPH_BASE_URL + '/FbCGRftH4a3yZugY7TnbYgPJVEv2LvMT6oF1fxPe9aJM'
const THE_GRAPH_UniswapV3Optimism = THE_GRAPH_BASE_URL + '/Cghf4LfVqPiFw6fp6Y5X5Ubc8UpmUhSfJL82zwiBFLaj'
const THE_GRAPH_UniswapV3Polygon = THE_GRAPH_BASE_URL + '/3hCPRGf4z88VC5rsBKU5AA9FBBq5nF3jbKJG7VZCbhjm'
const THE_GRAPH_PancakeswapV3BNB = THE_GRAPH_BASE_URL + '/A1fvJWQLBeUAggX2WQTMm3FKjXTekNXo77ZySun4YN2m'
const THE_GRAPH_HoneyswapGnosis = THE_GRAPH_BASE_URL + '/HTxWvPGcZ5oqWLYEVtWnVJDfnai2Ud1WaABiAR72JaSJ'



/* Redis */
Expand Down Expand Up @@ -77,7 +92,7 @@ async function get(url, query = null) {
const pancakeswap_request = `
query
{
tokens(first: 1000, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "10000", derivedETH_gt: "0", totalValueLockedUSD_gt: "25000" } ) {
tokens(first: 400, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "10000", derivedETH_gt: "0", totalValueLockedUSD_gt: "25000" } ) {
id
name
symbol
Expand All @@ -92,7 +107,7 @@ query

// Use TheGraph API - https://thegraph.com/hosted-service/subgraph/pancakeswap/exchange-v3-bsc
async function getPancakeswapTopTokens() {
return await get('https://api.thegraph.com/subgraphs/name/pancakeswap/exchange-v3-bsc', pancakeswap_request)
return await get(THE_GRAPH_PancakeswapV3BNB, pancakeswap_request)
}


Expand Down Expand Up @@ -147,7 +162,7 @@ async function getUniswapV3BNBTopTokens() {
const uniswapV3_arbitrum_request = `
query
{
tokens(first: 1000, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "50000", derivedETH_gt: "0", totalValueLockedUSD_gt: "25000" } ) {
tokens(first: 400, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "50000", derivedETH_gt: "0", totalValueLockedUSD_gt: "25000" } ) {
id
name
symbol
Expand All @@ -163,14 +178,14 @@ query
// Use TheGraph API - https://thegraph.com/hosted-service/subgraph/revert-finance/uniswap-v3-arbitrum
// https://github.com/revert-finance/uniswap-v3-subgraph
async function getUniswapV3ArbitrumTopTokens() {
return await get('https://api.thegraph.com/subgraphs/name/revert-finance/uniswap-v3-arbitrum', uniswapV3_arbitrum_request)
return await get(THE_GRAPH_UniswapV3Arbitrum, uniswapV3_arbitrum_request)
}

// Get Uniswap v3 top
const uniswapV3_request = `
query
{
tokens(first: 1000, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "10000", derivedETH_gt: "0", totalValueLockedUSD_gt: "50000" } ) {
tokens(first: 400, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "10000", derivedETH_gt: "0", totalValueLockedUSD_gt: "50000" } ) {
id
name
symbol
Expand All @@ -185,7 +200,7 @@ query

// Use TheGraph API - https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v3
async function getUniswapV3TopTokens() {
return await get('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3', uniswapV3_request)
return await get(THE_GRAPH_UniswapV3Ethereum, uniswapV3_request)
}

// Get Uniswap v2 top
Expand Down Expand Up @@ -254,6 +269,30 @@ async function getPolygonSushiSwapTopTokens() {
return await get('https://api.thegraph.com/subgraphs/name/sushiswap/matic-exchange', polygon_sushiswap_request)
}


// Get Uniswap v3 top on Polygon/Matic
const uniswapV3Polygon_request = `
query
{
tokens(first: 400, orderBy: totalValueLockedUSD, orderDirection: desc, where: { volumeUSD_gt: "10000", derivedETH_gt: "0", totalValueLockedUSD_gt: "50000" } ) {
id
name
symbol
derivedETH
volumeUSD
}
bundle(id: "1" ) {
ethPriceUSD
}
}
`

// Use TheGraph API - https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v3
async function getUniswapV3PolygonTopTokens() {
return await get(THE_GRAPH_UniswapV3Polygon, uniswapV3Polygon_request)
}


// Get Spiritswap's top
const spiritswap_request = `
query
Expand All @@ -280,22 +319,22 @@ async function getSpiritswapTopTokens() {
const honeyswap_request = `
query
{
tokens(first: 1000, orderBy: tradeVolumeUSD, orderDirection: desc, where: { tradeVolumeUSD_gt: "100000", derivedETH_gt: "0" } ) {
tokens(first: 250, orderBy: tradeVolumeUSD, orderDirection: desc, where: { tradeVolumeUSD_gt: "100000", derivedNativeCurrency_gt: "0" } ) {
id
name
symbol
derivedETH
derivedNativeCurrency
tradeVolumeUSD
}
bundle(id: "1" ) {
ethPrice
nativeCurrencyPrice
}
}
`

// Use TheGraph API - https://thegraph.com/explorer/subgraph/kirkins/honeyswap
async function getHoneyswapTopTokens() {
return await get('https://api.thegraph.com/subgraphs/name/kirkins/honeyswap', honeyswap_request)
return await get(THE_GRAPH_HoneyswapGnosis, honeyswap_request)
}


Expand Down Expand Up @@ -431,7 +470,7 @@ async function launchGnosis() {
const time = Date.now()
const tokens = top.data ? top.data.tokens : []

const xdai_price = top.data ? top.data.bundle.ethPrice : 0
const xdai_price = top.data ? top.data.bundle.nativeCurrencyPrice : 0
if(xdai_price === 0 || tokens.length === 0) return

try {
Expand All @@ -446,7 +485,7 @@ async function launchGnosis() {
address: token.id,
symbol: token.symbol,
name: token.name,
price: token.derivedETH * xdai_price,
price: token.derivedNativeCurrency * xdai_price,
volumeUSD: token.tradeVolumeUSD
}

Expand Down Expand Up @@ -480,12 +519,12 @@ async function launchPolygon() {
let data = {}

// get data from Uniswap
const top = await getQuickswapV3TopTokens()
const top = await getUniswapV3PolygonTopTokens()

const time = Date.now()
const tokens = top.data ? top.data.tokens : []

const matic_price = top.data ? top.data.bundle.maticPriceUSD : 0
const matic_price = top.data ? top.data.bundle.ethPriceUSD : 0
if(matic_price === 0 || tokens.length === 0) return

try {
Expand All @@ -500,7 +539,7 @@ async function launchPolygon() {
address: token.id,
symbol: token.symbol,
name: token.name,
price: token.derivedMatic * matic_price,
price: token.derivedETH * matic_price,
volumeUSD: token.volumeUSD
}

Expand Down Expand Up @@ -790,7 +829,7 @@ async function main() {

setTimeout(launchPolygon, 20000)

setTimeout(launchFantom, 25000)
// setTimeout(launchFantom, 25000)

setTimeout(launchArbitrum, 30000)

Expand Down
2 changes: 1 addition & 1 deletion beta/charts.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-+H4iLjY3JsKiF2V6N366in5IQHj2uEsGV7Pp/GRcm0fn76aPAk5V8xB6n8fQhhSonTqTXs/klFz4D0GIn6Br9g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js" integrity="sha512-ElRFoEQdI5Ht6kZvyzXhYG9NqjtkmlkfYk0wr6wHxU9JEHakS7UJZNeml5ALk+8IKlU6jDgMabC3vkumRokgJA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-adapter-moment/1.0.0/chartjs-adapter-moment.min.js" integrity="sha512-oh5t+CdSBsaVVAvxcZKy3XJdP7ZbYUBSRCXDTVn0ODewMDDNnELsrG9eDm8rVZAQg7RsDD/8K3MjPAFB13o6eA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.8.0/web3.min.js" integrity="sha512-bSQ2kf76XufUYS/4XinoHLp5S4lNOyRv0/x5UJACiOMy8ueqTNwRFfUZWmWpwnczjRp9SjiF1jrXbGEim7Y0Xg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/web3/4.11.1/web3.min.js" integrity="sha512-aMBus8+qvFQ/A/UcQcerC8uknRV6jv4GarzGkvMVdl73/tsTIcccchjW0zjoXgk1Wbg71L+c7FKyN4RBwF4+2w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="./public/common.js"></script>
<script defer src="./public/js/paraswap.js"></script>
<script defer src="./public/connect.js"></script>
Expand Down
17 changes: 12 additions & 5 deletions beta/public/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const NETWORK = {
shortName: 'bnb',
img: '/img/bsc-icon.svg',
color: '#f0b931',
rpc: 'https://1rpc.io/bnb',
rpc: 'https://bsc.meowrpc.com', // https://1rpc.io/bnb
explorer: 'https://bscscan.com/token/',
normaltx: 'https://api.bscscan.com/api?module=account&action=txlist&address=WALLET_ADDRESS&startblock=START_BLOCK&sort=asc',
tokentx: 'https://api.bscscan.com/api?module=account&action=tokentx&address=WALLET_ADDRESS&startblock=START_BLOCK&sort=asc',
Expand Down Expand Up @@ -232,10 +232,10 @@ const NETWORK = {
tokenPriceContract: '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83',
subgraph_url: 'https://thegraph.com/hosted-service/subgraph/layer3org/spiritswap-analytics',
coingecko_name: 'fantom',
tokens: {
token: '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83',
base: '0x04068da6c83afcfa0e13ba15a6696662335d5b75'
},
// tokens: {
// token: '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83',
// base: '0x04068da6c83afcfa0e13ba15a6696662335d5b75'
// },
url: 'https://www.spiritswap.finance/',
url_swap: 'https://swap.spiritswap.finance/#/swap'
},
Expand Down Expand Up @@ -439,6 +439,13 @@ async function get(url, query = null) {
})
}

Object.defineProperty(BigInt.prototype, "toJSON", {
get() {
"use strict"
return () => String(this)
}
})



document.addEventListener('DOMContentLoaded', function() {
Expand Down
4 changes: 2 additions & 2 deletions beta/public/js/paraswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const configureParaswap = (srcToken, destToken, amount, srcDecimals, destDecimal
}

const setParaswapAmount = (amount) => {
paraswapAmount = Math.floor(Number(amount) * Math.pow(10, paraswapSrcDecimals))
paraswapAmount = Math.floor(Number(amount) * Math.pow(10, Number(paraswapSrcDecimals)))
setQueries()
}

Expand All @@ -49,5 +49,5 @@ const getParaswapPrices = async () => {
let response = await get(paraswapPricesQuery)
console.log(response)
paraswapTransferProxy = response.priceRoute.tokenTransferProxy
return Number(response.priceRoute.destAmount) * Math.pow(10, -paraswapDestDecimals)
return Number(response.priceRoute.destAmount) * Math.pow(10, -Number(paraswapDestDecimals))
}
2 changes: 1 addition & 1 deletion beta/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ const setSwapper = async () => {
// document.getElementById('paraswap_dest_amount').value = null
const srcDecimals = await getTokenDecimals(selectedToken, chain)
const destDecimals = await getTokenDecimals(selectedBase, chain)
let srcBalance = (await getTokenBalanceWeb3(selectedToken, walletConnected, chain)) * Math.pow(10, -srcDecimals)
let srcBalance = (await getTokenBalanceWeb3(selectedToken, walletConnected, chain)) * Math.pow(10, -Number(srcDecimals))
if(!srcBalance || srcBalance === 0) {
//document.getElementById('paraswap_src_max_amount').classList.toggle('none', true)
} else {
Expand Down
36 changes: 18 additions & 18 deletions beta/public/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ async function getContractAddressPrice(transaction, network, balance = 1) {
}
// Balancer Pool
else if(transaction.tokenSymbol?.startsWith('B-') && balance > 0) {
price = await getPriceFromBalancerPool(transaction.contractAddress, transaction.tokenSymbol, balance, network)
//price = await getPriceFromBalancerPool(transaction.contractAddress, transaction.tokenSymbol, balance, network)
if(price) {
return price
}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ function displayTransactions() {
if(tx.tokenDecimal) {
spanBalance.innerHTML = sign + calculateBalance(tx.value, tx.tokenDecimal)
} else {
spanBalance.innerHTML = sign + web3.utils.fromWei(tx.value)
spanBalance.innerHTML = sign + web3.utils.fromWei(tx.value, 'ether')
}
spanBalance.classList.add('txBalance')
li.appendChild(spanBalance)
Expand Down Expand Up @@ -1549,22 +1549,22 @@ function simpleDataTimers() {
Object.keys(NETWORK).filter((network) => typeof NETWORK[network].url_data === 'string').forEach((network, i) => {
setTimeout(() => getSimpleData(NETWORK[network].enum, displayWallet), (i+1) * 250)
if(network === NETWORK.ETHEREUM.enum) {
getAaveEthereumUnderlyingAddresses(displayWallet)
getCompoundEthereumUnderlyingAddresses(displayWallet)
getAaveV3UnderlyingAddresses(displayWallet)
//getAaveEthereumUnderlyingAddresses(displayWallet)
//getCompoundEthereumUnderlyingAddresses(displayWallet)
//getAaveV3UnderlyingAddresses(displayWallet)
} else if(network === NETWORK.POLYGON.enum) {
getAavePolygonUnderlyingAddresses(displayWallet)
getAaveV3PolygonUnderlyingAddresses(displayWallet)
//getAavePolygonUnderlyingAddresses(displayWallet)
//getAaveV3PolygonUnderlyingAddresses(displayWallet)
} else if(network === NETWORK.XDAI.enum) {
getRmmGnosisUnderlyingAddresses(displayWallet)
//getRmmGnosisUnderlyingAddresses(displayWallet)
} else if(network === NETWORK.BSC.enum) {
getVenusBscUnderlyingAddresses(displayWallet)
//getVenusBscUnderlyingAddresses(displayWallet)
} else if(network === NETWORK.ARBITRUM_ONE.enum) {
getAaveV3ArbitrumUnderlyingAddresses(displayWallet)
//getAaveV3ArbitrumUnderlyingAddresses(displayWallet)
} /*else if(network === NETWORK.OPTIMISM.enum) {
getAaveV3OptimismUnderlyingAddresses(displayWallet)
}*/ else if(network === NETWORK.AVALANCHE.enum) {
getAaveV3AvalancheUnderlyingAddresses(displayWallet)
//getAaveV3AvalancheUnderlyingAddresses(displayWallet)
}
})
setTimeout(simpleDataTimers, 100000)
Expand Down Expand Up @@ -2039,8 +2039,8 @@ const sortWallet = (a, b) => {
if(NETWORK[b.network].tokenContract === b.contract) return 1
// then sort by price value
if(a.price && b.price) {
if(a.value * a.price > b.value * b.price) return -1
if(a.value * a.price < b.value * b.price) return 1
if(Number(a.value) * a.price > Number(b.value) * b.price) return -1
if(Number(a.value) * a.price < Number(b.value) * b.price) return 1
}
if(!a.price && b.price) return 1
if(a.price && !b.price) return -1
Expand Down Expand Up @@ -2129,15 +2129,15 @@ const filteredNFTTokens = () => {

/* Utils - Calculate balance from value */
const calculateBalance = (balance, decimal) => {
if(balance && Math.abs(balance) > 0) {
return precise(balance * (decimal ? Math.pow(10, -decimal) : 1))
if(balance && Math.abs(Number(balance)) > 0) {
return precise(Number(balance) * (decimal ? Math.pow(10, -decimal) : 1))
}
return 0
}
/* Utils - Calculate value from value */
const calculateValue = (balance, price, decimal) => {
if(balance && price && Math.abs(balance * price) > 0) {
return calculateBalance(balance * price, decimal)
if(balance && price && Math.abs(Number(balance) * price) > 0) {
return calculateBalance(Number(balance) * price, decimal)
}
return 0
}
Expand All @@ -2150,7 +2150,7 @@ const displayBalance = (value, decimal) => {
}
/* Utils - Display dollar value readable by human */
const displayValue = (balance, price, decimal) => {
const value = calculateBalance(balance * price, decimal)
const value = calculateBalance(Number(balance) * price, decimal)
if(value === 0) return 0
if(Math.abs(value) < 0.01) return '≈ 0'
return '$' + value
Expand Down
2 changes: 1 addition & 1 deletion beta/wallet.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-+H4iLjY3JsKiF2V6N366in5IQHj2uEsGV7Pp/GRcm0fn76aPAk5V8xB6n8fQhhSonTqTXs/klFz4D0GIn6Br9g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js" integrity="sha512-ElRFoEQdI5Ht6kZvyzXhYG9NqjtkmlkfYk0wr6wHxU9JEHakS7UJZNeml5ALk+8IKlU6jDgMabC3vkumRokgJA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-adapter-moment/1.0.0/chartjs-adapter-moment.min.js" integrity="sha512-oh5t+CdSBsaVVAvxcZKy3XJdP7ZbYUBSRCXDTVn0ODewMDDNnELsrG9eDm8rVZAQg7RsDD/8K3MjPAFB13o6eA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.8.0/web3.min.js" integrity="sha512-bSQ2kf76XufUYS/4XinoHLp5S4lNOyRv0/x5UJACiOMy8ueqTNwRFfUZWmWpwnczjRp9SjiF1jrXbGEim7Y0Xg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/web3/4.11.1/web3.min.js" integrity="sha512-aMBus8+qvFQ/A/UcQcerC8uknRV6jv4GarzGkvMVdl73/tsTIcccchjW0zjoXgk1Wbg71L+c7FKyN4RBwF4+2w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="./public/common.js"></script>
<script defer src="./public/connect.js"></script>
<script defer src="./public/protocols.js"></script>
Expand Down
5 changes: 3 additions & 2 deletions cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import cp from 'child_process'
const backFolder = './back'


const MAIN_TIMEOUT = process.env.NODE_ENV === 'production' ? 45000 : 120000 // 45 or 120 seconds
const COINGECKO_TIMEOUT = process.env.NODE_ENV === 'production' ? 20000 : 45000 // 20 or 45 seconds

const MAIN_TIMEOUT = process.env.NODE_ENV === 'production' ? 720000 : 360000 // 12 minutes or 6 minutes
const COINGECKO_TIMEOUT = process.env.NODE_ENV === 'production' ? 25000 : 45000 // 25 or 45 seconds


start()
Expand Down
Loading

0 comments on commit 67b149e

Please sign in to comment.