trustwallet-assets/script/blockchain/smartchain.ts

89 lines
3.3 KiB
TypeScript
Raw Normal View History

[Internal] Retrieve trading pairs from PancakeSwap (#5355) * Retrieve trading pairs from PancakeSwap. * Retrieve trading pairs from Uniswap. * Add doc link * Lint fix * Include decimals * Move binance tokenlist generation to BinanceAction. * Generate smartChain tokenlist, from base and pancakeswap pairs. * Fix array types in tokenlists.ts * Common writeToFile method, sort. * Type fixes * Revert tokenlist.json to master * Include pairs with allowlisted coins only. * Move assetID functions to common asset.ts * Use common assetID functions. * Use dynamic generation time. * Keep constant generation time; Version in tokenlist. * Count additions * Update tokenlist version and timestamp if needed before save. * No counting is needed in addXxxIfNeeded() functions. * Tokenlist timestamp: take over if previous * Update ethereum tokenlist with pairs from Uniswap. * Increase query limit to 400 pairs. * iDecimals always number, add decimal fields to Eth also. * Include only pairs with primary tokens, add pair info to primary token only. * Update base tokenlist (BSC) to current full list, in order not to remove any coins. * Update base tokenlist (ETH) to current full list, in order not to remove any coins. * Move out pair checks into common code. * Add checks for volume and txCount. * Assets tokenlist: Adjust token names for added tokens. * Move parameters to central config.ts * Nicer query string, compact * Prevent change if subgraph API fails * Reduce max limit in Pancakeswap query, with 400 often times out. * Stricter error handling * Reduce code duplication. * Minor comment * Display number of original tokens * Lint fix Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2021-01-29 06:45:43 +00:00
import { ActionInterface, CheckStepInterface } from "../generic/interface";
import {
checkTradingPair,
getTradingPairs,
PairInfo,
primaryTokenIndex,
TokenInfo
} from "../generic/subgraph";
import { SmartChain } from "../generic/blockchains";
import {
parseForceList,
[Internal] Retrieve trading pairs from PancakeSwap (#5355) * Retrieve trading pairs from PancakeSwap. * Retrieve trading pairs from Uniswap. * Add doc link * Lint fix * Include decimals * Move binance tokenlist generation to BinanceAction. * Generate smartChain tokenlist, from base and pancakeswap pairs. * Fix array types in tokenlists.ts * Common writeToFile method, sort. * Type fixes * Revert tokenlist.json to master * Include pairs with allowlisted coins only. * Move assetID functions to common asset.ts * Use common assetID functions. * Use dynamic generation time. * Keep constant generation time; Version in tokenlist. * Count additions * Update tokenlist version and timestamp if needed before save. * No counting is needed in addXxxIfNeeded() functions. * Tokenlist timestamp: take over if previous * Update ethereum tokenlist with pairs from Uniswap. * Increase query limit to 400 pairs. * iDecimals always number, add decimal fields to Eth also. * Include only pairs with primary tokens, add pair info to primary token only. * Update base tokenlist (BSC) to current full list, in order not to remove any coins. * Update base tokenlist (ETH) to current full list, in order not to remove any coins. * Move out pair checks into common code. * Add checks for volume and txCount. * Assets tokenlist: Adjust token names for added tokens. * Move parameters to central config.ts * Nicer query string, compact * Prevent change if subgraph API fails * Reduce max limit in Pancakeswap query, with 400 often times out. * Stricter error handling * Reduce code duplication. * Minor comment * Display number of original tokens * Lint fix Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2021-01-29 06:45:43 +00:00
rebuildTokenlist,
TokenItem
} from "../generic/tokenlists";
import { toChecksum } from "../generic/eth-address";
import { assetID, logoURI } from "../generic/asset";
import * as config from "../config"
const PrimaryTokens: string[] = ["WBNB", "BNB"];
async function retrievePancakeSwapPairs(): Promise<PairInfo[]> {
console.log(`Retrieving pairs from PancakeSwap, limit liquidity USD ${config.PancakeSwap_MinLiquidity} volume ${config.PancakeSwap_MinVol24} txcount ${config.PancakeSwap_MinTxCount24}`);
console.log(` forceIncludeList: ${config.PancakeSwap_ForceInclude}`);
const includeList = parseForceList(config.PancakeSwap_ForceInclude);
[Internal] Retrieve trading pairs from PancakeSwap (#5355) * Retrieve trading pairs from PancakeSwap. * Retrieve trading pairs from Uniswap. * Add doc link * Lint fix * Include decimals * Move binance tokenlist generation to BinanceAction. * Generate smartChain tokenlist, from base and pancakeswap pairs. * Fix array types in tokenlists.ts * Common writeToFile method, sort. * Type fixes * Revert tokenlist.json to master * Include pairs with allowlisted coins only. * Move assetID functions to common asset.ts * Use common assetID functions. * Use dynamic generation time. * Keep constant generation time; Version in tokenlist. * Count additions * Update tokenlist version and timestamp if needed before save. * No counting is needed in addXxxIfNeeded() functions. * Tokenlist timestamp: take over if previous * Update ethereum tokenlist with pairs from Uniswap. * Increase query limit to 400 pairs. * iDecimals always number, add decimal fields to Eth also. * Include only pairs with primary tokens, add pair info to primary token only. * Update base tokenlist (BSC) to current full list, in order not to remove any coins. * Update base tokenlist (ETH) to current full list, in order not to remove any coins. * Move out pair checks into common code. * Add checks for volume and txCount. * Assets tokenlist: Adjust token names for added tokens. * Move parameters to central config.ts * Nicer query string, compact * Prevent change if subgraph API fails * Reduce max limit in Pancakeswap query, with 400 often times out. * Stricter error handling * Reduce code duplication. * Minor comment * Display number of original tokens * Lint fix Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2021-01-29 06:45:43 +00:00
const pairs = await getTradingPairs(config.PancakeSwap_TradingPairsUrl, config.PancakeSwap_TradingPairsQuery);
const filtered: PairInfo[] = [];
pairs.forEach(x => {
try {
if (typeof(x) === "object") {
const pairInfo = x as PairInfo;
if (pairInfo) {
if (checkTradingPair(pairInfo, config.PancakeSwap_MinLiquidity, config.PancakeSwap_MinVol24, config.PancakeSwap_MinTxCount24, PrimaryTokens, includeList)) {
[Internal] Retrieve trading pairs from PancakeSwap (#5355) * Retrieve trading pairs from PancakeSwap. * Retrieve trading pairs from Uniswap. * Add doc link * Lint fix * Include decimals * Move binance tokenlist generation to BinanceAction. * Generate smartChain tokenlist, from base and pancakeswap pairs. * Fix array types in tokenlists.ts * Common writeToFile method, sort. * Type fixes * Revert tokenlist.json to master * Include pairs with allowlisted coins only. * Move assetID functions to common asset.ts * Use common assetID functions. * Use dynamic generation time. * Keep constant generation time; Version in tokenlist. * Count additions * Update tokenlist version and timestamp if needed before save. * No counting is needed in addXxxIfNeeded() functions. * Tokenlist timestamp: take over if previous * Update ethereum tokenlist with pairs from Uniswap. * Increase query limit to 400 pairs. * iDecimals always number, add decimal fields to Eth also. * Include only pairs with primary tokens, add pair info to primary token only. * Update base tokenlist (BSC) to current full list, in order not to remove any coins. * Update base tokenlist (ETH) to current full list, in order not to remove any coins. * Move out pair checks into common code. * Add checks for volume and txCount. * Assets tokenlist: Adjust token names for added tokens. * Move parameters to central config.ts * Nicer query string, compact * Prevent change if subgraph API fails * Reduce max limit in Pancakeswap query, with 400 often times out. * Stricter error handling * Reduce code duplication. * Minor comment * Display number of original tokens * Lint fix Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2021-01-29 06:45:43 +00:00
filtered.push(pairInfo);
}
}
}
} catch (err) {
console.log("Exception:", err);
}
});
console.log("Retrieved & filtered", filtered.length, "pairs:");
filtered.forEach(p => {
console.log(`pair: ${p.token0.symbol} -- ${p.token1.symbol} \t USD ${Math.round(p.reserveUSD)} ${Math.round(p.volumeUSD)} ${p.txCount}`);
});
return filtered;
}
function tokenInfoFromSubgraphToken(token: TokenInfo): TokenItem {
const idChecksum = toChecksum(token.id);
return new TokenItem(
assetID(20000714, idChecksum),
"BEP20",
idChecksum, token.name, token.symbol, parseInt(token.decimals.toString()),
logoURI(idChecksum, "smartchain", "--"),
[]);
}
// Retrieve trading pairs from PancakeSwap
async function generateTokenlist(): Promise<void> {
// note: if [] is returned here for some reason, all pairs will be *removed*. In case of error (e.g. timeout) it should throw
const tradingPairs = await retrievePancakeSwapPairs();
// convert
const pairs2: [TokenItem, TokenItem][] = [];
tradingPairs.forEach(p => {
let tokenItem0 = tokenInfoFromSubgraphToken(p.token0);
let tokenItem1 = tokenInfoFromSubgraphToken(p.token1);
if (primaryTokenIndex(p, PrimaryTokens) == 2) {
// reverse
const tmp = tokenItem1; tokenItem1 = tokenItem0; tokenItem0 = tmp;
}
pairs2.push([tokenItem0, tokenItem1]);
});
await rebuildTokenlist(SmartChain, pairs2, "Smart Chain", config.PancakeSwap_ForceExclude);
[Internal] Retrieve trading pairs from PancakeSwap (#5355) * Retrieve trading pairs from PancakeSwap. * Retrieve trading pairs from Uniswap. * Add doc link * Lint fix * Include decimals * Move binance tokenlist generation to BinanceAction. * Generate smartChain tokenlist, from base and pancakeswap pairs. * Fix array types in tokenlists.ts * Common writeToFile method, sort. * Type fixes * Revert tokenlist.json to master * Include pairs with allowlisted coins only. * Move assetID functions to common asset.ts * Use common assetID functions. * Use dynamic generation time. * Keep constant generation time; Version in tokenlist. * Count additions * Update tokenlist version and timestamp if needed before save. * No counting is needed in addXxxIfNeeded() functions. * Tokenlist timestamp: take over if previous * Update ethereum tokenlist with pairs from Uniswap. * Increase query limit to 400 pairs. * iDecimals always number, add decimal fields to Eth also. * Include only pairs with primary tokens, add pair info to primary token only. * Update base tokenlist (BSC) to current full list, in order not to remove any coins. * Update base tokenlist (ETH) to current full list, in order not to remove any coins. * Move out pair checks into common code. * Add checks for volume and txCount. * Assets tokenlist: Adjust token names for added tokens. * Move parameters to central config.ts * Nicer query string, compact * Prevent change if subgraph API fails * Reduce max limit in Pancakeswap query, with 400 often times out. * Stricter error handling * Reduce code duplication. * Minor comment * Display number of original tokens * Lint fix Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2021-01-29 06:45:43 +00:00
}
export class SmartchainAction implements ActionInterface {
getName(): string { return "Binance Smartchain"; }
getSanityChecks(): CheckStepInterface[] { return []; }
async updateManual(): Promise<void> {
await generateTokenlist();
[Internal] Retrieve trading pairs from PancakeSwap (#5355) * Retrieve trading pairs from PancakeSwap. * Retrieve trading pairs from Uniswap. * Add doc link * Lint fix * Include decimals * Move binance tokenlist generation to BinanceAction. * Generate smartChain tokenlist, from base and pancakeswap pairs. * Fix array types in tokenlists.ts * Common writeToFile method, sort. * Type fixes * Revert tokenlist.json to master * Include pairs with allowlisted coins only. * Move assetID functions to common asset.ts * Use common assetID functions. * Use dynamic generation time. * Keep constant generation time; Version in tokenlist. * Count additions * Update tokenlist version and timestamp if needed before save. * No counting is needed in addXxxIfNeeded() functions. * Tokenlist timestamp: take over if previous * Update ethereum tokenlist with pairs from Uniswap. * Increase query limit to 400 pairs. * iDecimals always number, add decimal fields to Eth also. * Include only pairs with primary tokens, add pair info to primary token only. * Update base tokenlist (BSC) to current full list, in order not to remove any coins. * Update base tokenlist (ETH) to current full list, in order not to remove any coins. * Move out pair checks into common code. * Add checks for volume and txCount. * Assets tokenlist: Adjust token names for added tokens. * Move parameters to central config.ts * Nicer query string, compact * Prevent change if subgraph API fails * Reduce max limit in Pancakeswap query, with 400 often times out. * Stricter error handling * Reduce code duplication. * Minor comment * Display number of original tokens * Lint fix Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2021-01-29 06:45:43 +00:00
}
}