Skip to content

Commit

Permalink
feat: add conflux list (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhanseng authored Apr 13, 2023
1 parent c07d190 commit c807fe7
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jobs:
run: npm run install
if: steps.npm-cache.outputs.cache-hit != 'true'

- name: Sync Conflux Token List
id: sync-token-list-conflux
run: |
node node_modules/puppeteer/install.js && npm run cli generate -- --include=1030 && npm run lint-fix
- name: Sync Solana Token List
id: sync-token-list-solana
run: |
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/read_contract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ jobs:
run: |
node node_modules/puppeteer/install.js && npm run cli read-contract -- --include=42161 && npm run lint-fix
- name: Sync Conflux Token List
id: sync-token-list-conflux
run: |
node node_modules/puppeteer/install.js && npm run cli read-contract -- --include=1030 && npm run lint-fix
- name: Prettier Check
run: npm run prettier

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mask-token-lists",
"version": "0.0.59",
"version": "0.0.60",
"repository": "https://github.com/DimensionDev/Mask-Token-Lists.git",
"license": "MIT",
"author": "guanbinrui <[email protected]>",
Expand Down
5 changes: 3 additions & 2 deletions scripts/providers/coinmarketcap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ export class CoinMarketCap implements Provider {
})
.filter((x) => !!x) as FungibleToken[]

const fetchTokenDecimalPage = explorerDecimalPageMapping[chainId]!
const fetchTokenDecimal = explorerFetchTokenDecimalMapping[chainId]!
const fetchTokenDecimalPage = explorerDecimalPageMapping[chainId]
const fetchTokenDecimal = explorerFetchTokenDecimalMapping[chainId]
const browser = await puppeteer.launch({ executablePath: executablePath(), timeout: 1000000 })

const allSettled = await Promise.allSettled(
toAddTokenList.map(async (x) => {
if (!fetchTokenDecimalPage || !fetchTokenDecimal) return x
const url = fetchTokenDecimalPage(x.address)
try {
const decimals = await fetchTokenDecimal(url, browser)
Expand Down
1 change: 1 addition & 0 deletions scripts/providers/cryptoRank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class CryptoRank implements Provider {

const allSettled = await Promise.allSettled(
toAddList.map(async (x) => {
if (!fetchTokenDecimalPage || !fetchTokenDecimal) return x
const url = fetchTokenDecimalPage(x.address)
try {
const decimals = await fetchTokenDecimal(url, browser)
Expand Down
3 changes: 2 additions & 1 deletion scripts/providers/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Explorer implements Provider {

let totalResults: FungibleToken[] = []

if (!fetch || !fetchPages || !fetchTokenDecimalPage || !fetchTokenDecimal) return totalResults
if (!fetch || !fetchPages) return totalResults

for (let i = 0; i < fetchPages.length; i++) {
const url = fetchPages[i]
Expand All @@ -43,6 +43,7 @@ export class Explorer implements Provider {
const browser = await puppeteer.launch({ executablePath: executablePath(), timeout: 1000000 })
const allSettled = await Promise.allSettled(
newAddedResults.map(async (x) => {
if (!fetchTokenDecimalPage || !fetchTokenDecimal) return x
const url = fetchTokenDecimalPage(x.address)
try {
const decimals = await fetchTokenDecimal(url, browser)
Expand Down
5 changes: 3 additions & 2 deletions scripts/providers/solanaFm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ export class SolanaFm implements Provider {
}
})
.filter((x) => !excludedTokenAddressList.includes(x.address.toLowerCase()))
const fetchTokenDecimalPage = explorerDecimalPageMapping[chainId]!
const fetchTokenDecimal = explorerFetchTokenDecimalMapping[chainId]!
const fetchTokenDecimalPage = explorerDecimalPageMapping[chainId]
const fetchTokenDecimal = explorerFetchTokenDecimalMapping[chainId]
const browser = await puppeteer.launch({ executablePath: executablePath(), timeout: 1000000 })

const allSettled = await Promise.allSettled(
list.map(async (x) => {
if (!fetchTokenDecimalPage || !fetchTokenDecimal) return x
const url = fetchTokenDecimalPage(x.address)
try {
const decimals = await fetchTokenDecimal(url, browser)
Expand Down
4 changes: 4 additions & 0 deletions scripts/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { fetchPolygon, fetchPolygonForTokenDecimal } from './explorers/polygon'
import { fetchBSC, fetchBSCForTokenDecimal } from './explorers/bsc'
import { fetchETH, fetchETHForTokenDecimal } from './explorers/eth'
import { fetchSolanaForTokenDecimal } from './explorers/solana'
import { fetchConflux } from './explorers/conflux'
import { Browser } from 'puppeteer'

export function convertEnumToArray(e: any) {
Expand All @@ -32,6 +33,7 @@ export const explorerPagesMapping: Partial<Record<ChainId, string[]>> = {
[ChainId.xDai]: ['https://gnosisscan.io/tokens?ps=100'],
[ChainId.Aurora]: ['https://explorer.aurora.dev/tokens'],
[ChainId.Optimistic]: ['https://optimistic.etherscan.io/tokens'],
[ChainId.Conflux]: ['https://evm.confluxscan.net/tokens'],
}

export const explorerDecimalPageMapping: Partial<Record<ChainId, (address: string) => string>> = {
Expand All @@ -57,6 +59,7 @@ export const explorerFetchMapping: Partial<Record<ChainId, (url: string) => Prom
[ChainId.xDai]: fetchGnosis,
[ChainId.Aurora]: fetchAurora,
[ChainId.Optimistic]: fetchOptimistic,
[ChainId.Conflux]: fetchConflux,
}

export const explorerFetchTokenDecimalMapping: Partial<
Expand Down Expand Up @@ -84,4 +87,5 @@ export const rpcMapping: Partial<Record<ChainId, string>> = {
[ChainId.xDai]: 'https://rpc.gnosischain.com',
[ChainId.Aurora]: 'https://mainnet.aurora.dev',
[ChainId.Optimistic]: 'https://node.onekey.so/optimism',
[ChainId.Conflux]: 'https://evm.confluxrpc.com',
}
43 changes: 43 additions & 0 deletions scripts/utils/explorers/conflux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ChainId, FungibleToken } from '../../type'
import { toChecksumAddress } from 'web3-utils'
import { createFungibleToken } from '../createFungibleToken'
import * as cheerio from 'cheerio'
import puppeteer from 'puppeteer-extra'
import { executablePath } from 'puppeteer'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import { Browser } from 'puppeteer'

puppeteer.use(StealthPlugin())

export async function fetchConflux(url: string) {
const browser = await puppeteer.launch({ executablePath: executablePath(), timeout: 1000000 })
const page = await browser.newPage()
await page.goto(url)
page.once('load', () => console.log('Conflux Page loaded!'))
page.once('error', (error) => console.log('Failed to load Conflux Page!', error))
await page.setViewport({ width: 1080, height: 1024 })

const tableSelector = '.ant-table-content'
const tableElementHandler = await page.waitForSelector(tableSelector)
const tableElement = await tableElementHandler?.evaluate((x) => x.innerHTML)

await browser.close()
const q = cheerio.load(tableElement ?? '')
const table = q('table tbody .ant-table-row td:nth-child(2)').map((_, x) => x)
let results: FungibleToken[] = []

for (const x of table) {
const logo = q('img', x).attr('src')
const fullName = q('a span', x).text()
if (!fullName) continue

const pageLink = q('a', x).attr('href')
if (!pageLink) continue

const address = toChecksumAddress(pageLink?.replace('/token/', ''))
if (!address) continue

results.push(createFungibleToken(ChainId.Conflux, address, fullName, 18, logo ?? ''))
}
return results
}

0 comments on commit c807fe7

Please sign in to comment.