2 years ago

#55062

test-img

xotix

Listening to PairCreated Event on PancakeSwap's Factory Contract with DefaultProvider

I'm trying to listen to the PairCreated event on the Factory Contract of PancakeSwap s.t. I get a warning if a new token is being issued.

I basically do this:

const ethers = require('ethers');

const addresses = {
    WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
    factory: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73",
}

const mnemonic = "my_seed_phrase"

const provider = new ethers.getDefaultProvider()
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
);

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

Now I'm not sure if that works because I'm not sure if I can use any provider for that or if I need a WebSocketProvider.

From what I gather from the ethers documentation, the code above should be fine and it should work. Now it doesn't seem to work since I never get an alarm. I do get this warning after some time though:

========= NOTICE =========

Request-Rate Exceeded (this message will not be repeated)

The default API keys for each service are provided as a highly-throttled, community resource for low-traffic projects and early prototyping.

While your application will continue to function, we highly recommended signing up for your own API keys to improve performance, increase your request rate/limit and enable other perks, such as metrics and advanced APIs.

For more details: https://docs.ethers.io/api-keys/

==========================

Which I ignored because for now, this is just an exercise and it says that it still works. It's just slow.

I used publicly available resources that list new tokens to check if I should have gotten an execution of the event.

javascript

ethers.js

pancakeswap

0 Answers

Your Answer

Accepted video resources