diff --git a/packages/token-lists/README.md b/packages/token-lists/README.md index bd3b2e987..3d0eba8df 100644 --- a/packages/token-lists/README.md +++ b/packages/token-lists/README.md @@ -12,7 +12,7 @@ URLs to external lists are stored in `token-lists.json`, if you want your list t - Add `checksum:newlistname`, `generate:newlistname`, `makelist:newlistname` command to `package.json` analogous to PancakeSwap default and extended list scripts. - Modify `checksum.ts`, `buildList.ts` and `default.test.ts` to handle new list -## How to add new tokens to pancakeswap (extended) token list +## How to add new tokens to PancakeSwap (extended) token list Note - this is not something we expect pull requests for. Unless you've been specifically asked by someone from PCS team please do no submit PRs to be listed on default PCS list. You can still trade your tokens on PCS exchange by pasting your address into the token field. @@ -28,6 +28,18 @@ For list to be considered valid it need to satisfy the following criteria: - There are no duplicate addresses, symbols or token names in the list - All addresses are valid and checksummed (`yarn checksum:pcs` automatically converts addresses to checksummed versions, it is also part of `yarn makelist:pcs`) +## How to update Top100 Token list + +Note - this is not something we expect pull requests for. + +```shell script +# Fetch the Top100 Tokens on PancakeSwap v2, and update list. +$ yarn fetch:pcs-top-100 + +# Build token list (pancakeswap-top-100.json) +$ yarn makelist:pcs-top-100 +``` + ## Deploying Token lists will be auto-deployed via netlify when PR is merged to master. Be sure to build the list with `yarn makelist:list-name` before submitting/merging the PR since it doesn't make much sense building lists within Netlify (because most errors are related to wrong token information and should be fixed prior to landing into master) diff --git a/packages/token-lists/package.json b/packages/token-lists/package.json index e47c17654..1940c6305 100644 --- a/packages/token-lists/package.json +++ b/packages/token-lists/package.json @@ -15,8 +15,16 @@ "checksum:pcs-top-100": "yarn build && node ./dist checksum pancakeswap-top-100", "generate:pcs-top-100": "yarn test && yarn build && node ./dist generate pancakeswap-top-100", "makelist:pcs-top-100": "yarn checksum:pcs-top-100 && yarn generate:pcs-top-100", + "fetch:pcs-top-100": "yarn build && node ./dist fetch", "test": "jest" }, + "dependencies": { + "@ethersproject/address": "^5.1.0", + "@uniswap/token-lists": "^1.0.0-beta.21", + "ajv": "6.12.2", + "graphql": "^15.5.0", + "graphql-request": "^3.4.0" + }, "devDependencies": { "@babel/preset-env": "^7.14.1", "@babel/preset-typescript": "^7.13.0", @@ -29,10 +37,5 @@ "jest": "^26.6.3", "rollup": "^2.47.0", "ts-jest": "^26.5.6" - }, - "dependencies": { - "@ethersproject/address": "^5.1.0", - "@uniswap/token-lists": "^1.0.0-beta.21", - "ajv": "6.12.2" } } diff --git a/packages/token-lists/src/index.ts b/packages/token-lists/src/index.ts index ec2223b12..04942fa8e 100644 --- a/packages/token-lists/src/index.ts +++ b/packages/token-lists/src/index.ts @@ -1,5 +1,6 @@ import { buildList, saveList } from "./buildList"; import checksumAddresses from "./checksum"; +import topTokens from "./top-100"; const command = process.argv[2]; const listName = process.argv[3]; @@ -11,6 +12,9 @@ switch (command) { case "generate": saveList(buildList(listName), listName); break; + case "fetch": + topTokens(); + break; default: console.info("Unknown command"); break; diff --git a/packages/token-lists/src/top-100.ts b/packages/token-lists/src/top-100.ts new file mode 100644 index 000000000..43a2d4504 --- /dev/null +++ b/packages/token-lists/src/top-100.ts @@ -0,0 +1,124 @@ +import fs from "fs"; +import path from "path"; +import { request, gql } from "graphql-request"; +import { getAddress } from "@ethersproject/address"; + +// Interface for Bitquery GraphQL response. +interface BitqueryEntity { + Total_USD: number; + baseCurrency: { + address: string; + name: string; + symbol: string; + decimals: number; + }; +} + +// Default token list for exchange + manual exclusion of broken BEP-20 token(s) +const blacklist: string[] = [ + "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c", // WBNB + "0xe9e7cea3dedca5984780bafc599bd69add087d56", // BUSD + "0x2170ed0880ac9a755fd29b2688956bd959f933f8", // ETH + "0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c", // BTCB + "0x55d398326f99059ff775485246999027b3197955", // USDT + "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82", // Cake + "0xcf6bb5389c92bdda8a3747ddb454cb7a64626c63", // XVS + "0x4bd17003473389a42daf6a0a729f6fdb328bbbd7", // VAI + "0xc9849e6fdb743d08faee3e34dd2d1bc69ea11a51", // BUNNY + "0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3", // SAFEMOON + "0x8f0528ce5ef7b51152a59745befdd91d97091d2f", // ALPACA + "0xE0e514c71282b6f4e823703a39374Cf58dc3eA4f", // BELT + "0x9f589e3eabe42ebc94a44727b3f3531c0c877809", // TKO + "0x42f6f551ae042cbe50c739158b4f0cac0edb9096", // NRV + "0xa7f552078dcc247c2684336020c03648500c6d9f", // EPS + + "0x4269e4090ff9dfc99d8846eb0d42e67f01c3ac8b", // BROKEN TOKEN +]; + +/** + * Return today / 1 month ago ISO-8601 DateTime. + * + * @returns string[] + */ +const getDateRange = (): string[] => { + const today = new Date(); + const todayISO = today.toISOString(); + today.setMonth(today.getMonth() - 1); + const monthAgoISO = today.toISOString(); + + return [todayISO, monthAgoISO]; +}; + +/** + * Fetch Top100 Tokens traded on PancakeSwap v2, ordered by trading volume, + * for the past 30 days, filtered to remove default / broken tokens. + * + * @returns BitqueryEntity[]] + */ +const getTokens = async () => { + try { + const [today, monthAgo] = getDateRange(); + + const { ethereum } = await request( + "https://graphql.bitquery.io/", + gql` + query ($from: ISO8601DateTime, $till: ISO8601DateTime, $blacklist: [String!]) { + ethereum(network: bsc) { + dexTrades( + options: { desc: "Total_USD", limit: 100 } + exchangeName: { is: "Pancake v2" } + baseCurrency: { notIn: $blacklist } + date: { since: $from, till: $till } + ) { + Total_USD: tradeAmount(calculate: sum, in: USD) + baseCurrency { + address + name + symbol + decimals + } + } + } + } + `, + { + from: monthAgo, + till: today, + blacklist: blacklist, + } + ); + + return ethereum.dexTrades; + } catch (error) { + console.error(`Error when fetching Top100 Tokens by volume for the past 30 days, error: ${error.message}`); + } +}; + +/** + * Main function. + * Fetch tokems, build list, save list. + */ +const main = async () => { + const tokens = await getTokens(); + + const sanitizedTokens = tokens.reduce((list, item: BitqueryEntity) => { + const checksummedAddress = getAddress(item.baseCurrency.address); + + const updatedToken = { + name: item.baseCurrency.name, + symbol: item.baseCurrency.symbol.toUpperCase(), + address: checksummedAddress, + chainId: 56, + decimals: item.baseCurrency.decimals, + logoURI: `https://assets.trustwalletapp.com/blockchains/smartchain/assets/${checksummedAddress}/logo.png`, + }; + return [...list, updatedToken]; + }, []); + + const tokenListPath = `${path.resolve()}/src/tokens/pancakeswap-top-100.json`; + console.info("Saving updated list to ", tokenListPath); + const stringifiedList = JSON.stringify(sanitizedTokens, null, 2); + fs.writeFileSync(tokenListPath, stringifiedList); +}; + +export default main;