Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(token-lists): Automated Top100 token list generation #134

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/token-lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions packages/token-lists/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
4 changes: 4 additions & 0 deletions packages/token-lists/src/index.ts
Original file line number Diff line number Diff line change
@@ -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];
Comment on lines 5 to 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal style but I prefer doing this. No need to change.

const [,, command, listName] = process.argv

Expand All @@ -11,6 +12,9 @@ switch (command) {
case "generate":
saveList(buildList(listName), listName);
break;
case "fetch":
topTokens();
break;
default:
console.info("Unknown command");
break;
Expand Down
124 changes: 124 additions & 0 deletions packages/token-lists/src/top-100.ts
Original file line number Diff line number Diff line change
@@ -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[] = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple 'blacklist' to remove default tokens already included in PancakeSwap exchange default list; plus broken/unverified BEP-20 tokens.

"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[] => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the simpler/only way to do this without requiring an external library.

const today = new Date();
const todayISO = today.toISOString();
today.setMonth(today.getMonth() - 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this works even when the month is January.

ChefKai marked this conversation as resolved.
Show resolved Hide resolved
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,
Copy link
Contributor Author

@ChefKai ChefKai Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now() - 1 month
Specified as 1 month, and not 30 days, for specific months like February, ...

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,
Copy link
Contributor Author

@ChefKai ChefKai Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default to chainId: 56; as PancakeSwap v2 isn't on the testnet (yet) + no data is indexed on Chapel.

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;