0

im trying to test out a portion of my token contract code using hardhat, however im facing an issue.

What im trying to do

  1. In my testing code, im trying to add liquidity to an external contract (Pancakeswap).

  2. To do that, i have to call the approve method from both my contract and the BNB token contract.

  3. The approve method from my house token works, but it does not work when i call it on the BNB token contract. (I verified that it didnt work by calling the allowance method of the BNB contract after the approve method was called, it showed that the allowances is 0 ).

Code snippet

    let pancakeContract = new ethers.Contract('0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F', pancakeABI, ethers.getDefaultProvider());
    let wbnbContract = new ethers.Contract('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', wbnbABI, ethers.getDefaultProvider());
    
    it('Retrieving LP token pair', async () => {
        //Connecting to pancake contract
        await hre.network.provider.request({
            method: "hardhat_impersonateAccount",
            params: ["0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F"]
        })
        const pancakeSwapSigner = await ethers.getSigner("0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F")
        const pancakeSwap = pancakeContract.connect(pancakeSwapSigner);
        
        //Connecting to wbnb contract
        await hre.network.provider.request({
            method: "hardhat_impersonateAccount",
            params: ["0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"]
        })
        const wbnbSigner = await ethers.getSigner("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c")
        const wbnb = wbnbContract.connect(wbnbSigner);

        await houseToken.approve(pancakeSwap.address, '1000');
        await wbnb.approve(pancakeSwap.address, '1000');
        allow = await wbnb.allowance(op,pancakeSwap.address)
        console.log(allow) // <---- HERE IS WHERE I CHECK THAT IT DIDNT WORK
    });

Return value of console.log(allow)

 gives BigNumber { _hex: '0x00', _isBigNumber: true }

My questions

What's the cause of this effect, and is there a way to resolve this?

Thank you guys so much in advance!

1

The problem is that op has to call approve, not the contract addresses.

let pancakeContract = new ethers.Contract('0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F', pancakeABI, ethers.getDefaultProvider());
let wbnbContract = new ethers.Contract('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', wbnbABI, ethers.getDefaultProvider());

it('Retrieving LP token pair', async () => {
    //Connecting to pancake contract
    const pancakeSwapSigner = await ethers.getSigner(op)
    const pancakeSwap = pancakeContract.connect(pancakeSwapSigner);
    
    //Connecting to wbnb contract
    const wbnbSigner = await ethers.getSigner(op)
    const wbnb = wbnbContract.connect(wbnbSigner);

    await houseToken.approve(pancakeSwap.address, '1000');
    await wbnb.approve(pancakeSwap.address, '1000');
    allow = await wbnb.allowance(op,pancakeSwap.address);
    console.log(allow);
});

Obviously for it to work you need to have some WBNB.

3
  • Hey! thanks for responding, yes this is indeed correct thank you very much. If you don't mind I have another question... What currency does hardhat prefund my default wallets with when I change the network to fork from binance instead? ANd is there a way to easily prefund wallets with my selected currencies?
    – neowenshun
    May 31 at 9:06
  • @neowenshun that is a legit question and it's worth it having as a separate question in this SE. If you create a new one, share the link in our discord and I'll answer it. May 31 at 10:11
  • ethereum.stackexchange.com/questions/100004/… Here is a link in case someone on this stack has similar questions
    – neowenshun
    May 31 at 10:28

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.