0

PLEASE DO NOT HELP ME BY SUGGESTING PYTHON CODE. I'M ONLY INTERESTED IN SOLUTIONS THAT RELY EXCLUSIVELY ON NODE.JS.

I am trying to build out a sniping bot in Node.js using a snippet from EatTheBlocks YouTube video and sample code on Github (eattheblocks/screencast/348-pancakeswap-trading-bot). The code EatTheBlocks presents has a few little problems but at the end of the day, it's just a beginning because it will buy new tokens every few seconds but there's not enough code there to sell anything.

At this point, I've cleaned up the typos and missing blocks and it correctly consoles the addresses of new token pairs every few seconds. As a very preliminary step, I'd like to enhance that console output with the symbol of the token. The ERC-20 token standard outlined by the Ethereum Org shows 9 methods, including function name() public view returns (string) and function symbol() public view returns (string)

The answers I find in my research seem to require an ABI to get the symbol, but that is nowhere mentioned in the Ethereum and I think it should be doable by calling the function in the token contract from the address.

Here is the code. I'd like to simply add to the console message that reports the addresses at line 50.

const ethers = require('ethers');

const addresses = {
  WBNB: '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c',
  factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73',
  router: '0x10ED43C718714eb63d5aA57B78B54704E256024E',
  recipient: 'recipient of the profit here'
}

//First address of this mnemonic must have enough BNB to pay for tx fess
const mnemonic = 'your mnemonic here, to send';

const provider = new ethers.providers.WebSocketProvider('Ankr websocket url to mainnet');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);
const factory = new ethers.Contract(
  addresses.factory,
  ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'],
  account
);
const router = new ethers.Contract(
  addresses.router,
  [
    'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
    'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
  ],
  account
);

const wbnb = new ethers.Contract(
  addresses.WBNB,
  [
    'function approve(address spender, uint amount) public returns(bool)',
  ],
  account
);

const init = async () => {
  const tx = await wbnb.approve(
    router.address, 
    'replace by amount covering several trades'
  );
  const receipt = await tx.wait(); 
  console.log('Transaction receipt');
  console.log(receipt);
}

factory.on('PairCreated', async (token0, token1, pairAddress) => {
  console.log(`
    New pair detected
    =================
    token0: ${token0}
    token1: ${token1}
    pairAddress: ${pairAddress}
  `);

Sorry about the formatting problems, I can't seem to fix those.

2
  • you will soon discover that the liquidity on the markets is currently very low and that bots hardly make any money
    – Nulik
    Jul 26 '21 at 3:58
  • Yes, that's a key question but it's a question to solve after the instant one. Jul 26 '21 at 16:47
0

You can search in bsc scan for any token whose contract is open source. If you examine the code, you can find something like below:

interface IERC20 {

    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}



/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.

And you can use these abi to get the methods you need.

6
  • I'm sure the symbol is an element that is callable on the token contract. I just don't know how to do it in the context of the above code. Jul 26 '21 at 1:49
  • @jeromekjerome of course symbol() is callable, this is how we build our ERC20 explorers. How come this example doesn't include this function, I don't understand.
    – Nulik
    Jul 26 '21 at 3:55
  • @jeromekjerome get the ABI from OpenZeppelin contracts and you will be able to call symbol()
    – Nulik
    Jul 26 '21 at 3:56
  • @jeromekjerome You can get the symbol using code like const token = new web3.eth.Contract(address, IERCAbi) and const symbol = await token.methods.symbol().call()
    – Jettie
    Jul 26 '21 at 11:19
  • Modified the above code: factory.on('PairCreated', async (token0, token1, pairAddress) => { const token0Contract = new web3.eth.Contract(token0, IERCAbi) const token1Contract = new web3.eth.Contract(token1, IERCAbi) const token0Symbol = await token0Contract.methods.symbol().call() const token1Symbol = await token1Contract.methods.symbol().call() console.log(` New pair detected on PancakeSwap ================= symbolToken0: ${token0Symbol} token0Address: ${token0} symbolToken1: ${token0Symbol} token1Address: ${token1} pairAddress: ${pairAddress} `); Jul 26 '21 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.