Mint Club
Search…
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.
1
const { JsonRpcProvider } = require('@ethersproject/providers');
2
const { Token, Fetcher, Percent, Trade, TradeType, Route, TokenAmount, WETH } = require('@pancakeswap/sdk');
3
4
// Constants
5
const MINT_CONTRACT = '0x1f3Af095CDa17d63cad238358837321e95FC5915';
6
const USDT_CONTRACT = '0x55d398326f99059fF775485246999027B3197955';
7
const BSC_CHAIN_ID = 56;
8
const DECIMALS = 18;
9
const ONE_UNIT = new BigNumber('1000000000000000000'); // 1e18
10
11
async function getMintPrice() {
12
const provider = new JsonRpcProvider(getRandomRPCServer());
13
14
const MINT = new Token(BSC_CHAIN_ID, MINT_CONTRACT, DECIMALS);
15
const USDT = new Token(BSC_CHAIN_ID, USDT_CONTRACT, DECIMALS);
16
const MINT_BNB_PAIR = await Fetcher.fetchPairData(MINT, WETH[BSC_CHAIN_ID], provider);
17
const BNB_USDT_PAIR = await Fetcher.fetchPairData(USDT, WETH[BSC_CHAIN_ID], provider);
18
19
const route = new Route([MINT_BNB_PAIR, BNB_USDT_PAIR], MINT);
20
const trade = new Trade(route, new TokenAmount(MINT, ONE_UNIT), TradeType.EXACT_INPUT);
21
const slippageTolerance = new Percent('0', '10000');
22
23
const amountOut = new BigNumber(trade.minimumAmountOut(slippageTolerance).raw.toString(10))
24
.dividedBy(ONE_UNIT)
25
.toNumber();
26
27
return amountOut;
28
}
Copied!

Transfer BEP20 tokens

Transfer BEP20 tokens using a JS node script (with a private key)
1
const { ethers } = require('ethers');
2
const BigNumber = require('bignumber.js');
3
const minABI = [{
4
"inputs": [
5
{
6
"internalType": "address",
7
"name": "recipient",
8
"type": "address"
9
},
10
{
11
"internalType": "uint256",
12
"name": "amount",
13
"type": "uint256"
14
}
15
],
16
"name": "transfer",
17
"outputs": [
18
{
19
"internalType": "bool",
20
"name": "",
21
"type": "bool"
22
}
23
],
24
"stateMutability": "nonpayable",
25
"type": "function"
26
}];
27
28
const ONE_UNIT = new BigNumber('1000000000000000000'); // 1e18
29
const PRIVATE_KEY = ""; // Private key of sender wallet
30
31
async function send(contract, to, amount) {
32
const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/');
33
const fromWallet = new ethers.Wallet(PRIVATE_KEY, provider);
34
const token = new ethers.Contract(contract, minABI, fromWallet);
35
36
const tx = await token.transfer(
37
to,
38
String(new BigNumber(amount).multipliedBy(DECIMALS)),
39
{ gasLimit: 100000, gasPrice: 5100000000 } // 5.1 GWei
40
);
41
42
console.log(`SENT -> TX: ${tx.hash}`)
43
}
Copied!

Get balance of a wallet

1
const BigNumber = require('bignumber.js');
2
const { ethers } = require("ethers");
3
const { JsonRpcProvider } = require('@ethersproject/providers');
4
5
const minABI = [{
6
"inputs": [
7
{
8
"internalType": "address",
9
"name": "account",
10
"type": "address"
11
}
12
],
13
"name": "balanceOf",
14
"outputs": [
15
{
16
"internalType": "uint256",
17
"name": "",
18
"type": "uint256"
19
}
20
],
21
"stateMutability": "view",
22
"type": "function"
23
}];
24
25
function toDecimalAmount(bn) {
26
return new BigNumber(bn.toString()).dividedBy(ETHER).toNumber();
27
}
28
29
function getBalance(tokenContractAddress, wallet) {
30
const provider = new JsonRpcProvider('https://bsc-dataseed.binance.org/');
31
const contract = new ethers.Contract(tokenContractAddress, minABI, provider);
32
33
return toDecimalAmount(await contract.balanceOf(wallet));
34
}
Copied!

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.
1
const { JsonRpcProvider } = require('@ethersproject/providers');
2
const { ethers } = require("ethers");
3
const erc20ABI = require('./abi/erc20.json');
4
const TARGET_CONTRACT = '0x1f3Af095CDa17d63cad238358837321e95FC5915';
5
6
function sleep(sec) {
7
return new Promise(resolve => setTimeout(resolve, sec * 1000));
8
}
9
10
async function checkEvents(contract, fromBlock, toBlock = 'latest') {
11
const events = await contract.queryFilter('*', fromBlock, toBlock);
12
for (event of events) {
13
if (event.removed) {
14
console.log(`[SKIP_REMOVED] ${event.transactionHash}`);
15
continue;
16
}
17
18
if (event.event === 'Transfer' && event.args) {
19
console.log('Transfer event detected:', event);
20
}
21
}
22
}
23
24
async function runForever() {
25
const provider = new JsonRpcProvider(getRandomRPCServer());
26
const contract = new ethers.Contract(TARGET_CONTRACT, erc20ABI, provider);
27
28
let recentBlock = await provider.getBlockNumber();
29
let lastCheckedBlock = recentBlock - 4999; // Max API limit: 5000
30
31
let fromBlock = lastCheckedBlock + 1;
32
logger(`First, fetch from the last checked block: #${fromBlock} - #${recentBlock}`);
33
34
// This will fetch all events from [fromBlock - recentBlock]
35
await checkEvents(contract, fromBlock);
36
await sleep(3); // BSC block speed: 3 secs
37
38
fromBlock = recentBlock + 1;
39
while(1) {
40
recentBlock = await provider.getBlockNumber();
41
42
// If we are faster than block mining
43
if (fromBlock >= recentBlock) {
44
console.log(`#${fromBlock} block is not mined yet. Wait..`);
45
await sleep(3);
46
continue;
47
}
48
49
console.log(`Check blocks: #${fromBlock} - #${recentBlock}`);
50
await checkEvents(contract, fromBlock); // Check [fromBlock - recentBlock]
51
52
fromBlock = recentBlock + 1;
53
await sleep(3);
54
}
55
}
56
57
runForever();
Copied!
Last modified 2mo ago