Code Snippets for Node scripts
Useful code snippets for developers
Get MINT token price via PancakeSwap
MINT token liquidity is provided with BNB token on PancakeSwap, so we can query the USD value of one MINT token by calling minimumAmountOut() function on PancakeSwap SDK with MINT -> WBNB -> USDT routes.
const { JsonRpcProvider } = require('@ethersproject/providers');
const { Token, Fetcher, Percent, Trade, TradeType, Route, TokenAmount, WETH } = require('@pancakeswap/sdk');
// Constants
const MINT_CONTRACT = '0x1f3Af095CDa17d63cad238358837321e95FC5915';
const USDT_CONTRACT = '0x55d398326f99059fF775485246999027B3197955';
const BSC_CHAIN_ID = 56;
const DECIMALS = 18;
const ONE_UNIT = new BigNumber('1000000000000000000'); // 1e18
async function getMintPrice() {
const provider = new JsonRpcProvider(getRandomRPCServer());
const MINT = new Token(BSC_CHAIN_ID, MINT_CONTRACT, DECIMALS);
const USDT = new Token(BSC_CHAIN_ID, USDT_CONTRACT, DECIMALS);
const MINT_BNB_PAIR = await Fetcher.fetchPairData(MINT, WETH[BSC_CHAIN_ID], provider);
const BNB_USDT_PAIR = await Fetcher.fetchPairData(USDT, WETH[BSC_CHAIN_ID], provider);
const route = new Route([MINT_BNB_PAIR, BNB_USDT_PAIR], MINT);
const trade = new Trade(route, new TokenAmount(MINT, ONE_UNIT), TradeType.EXACT_INPUT);
const slippageTolerance = new Percent('0', '10000');
const amountOut = new BigNumber(trade.minimumAmountOut(slippageTolerance).raw.toString(10))
.dividedBy(ONE_UNIT)
.toNumber();
return amountOut;
}Transfer BEP20 tokens
Transfer BEP20 tokens using a JS node script (with a private key)
Get balance of a wallet
Monitor events
Following example monitors all Transfer events emitted from a token contract.
We can use contract.on(event, listener) if a WebSocket endpoint is available (there's no reliable one on BSC), but we should check blocks manually if we only have HTTP RPC endpoints. This example will use the manual approach with HTTP endpoints.