Erc-20

在安全帽測試中從主網合約呼叫更新函式

  • March 28, 2022

我正在嘗試使用 Hardhat 測試我的代幣合約程式碼的一部分,但是我遇到了一個問題。

我正在嘗試做的事情

  1. 在我的測試程式碼中,我試圖為外部合約 (PancakeSwap) 添加流動性。
  2. 為此,我必須從我的合約和 BNB 代幣合約中呼叫approve 方法。
  3. 我家代幣的批准方法有效,但是當我在 BNB 代幣合約上呼叫它時它不起作用。(我在呼叫approval方法後呼叫BNB合約的allowance方法驗證了它不起作用,顯示permissions為0)。

程式碼片段

   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
   });

console.log(allow) 的返回值

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

我的問題

這種影響的原因是什麼,有沒有辦法解決這個問題?

非常感謝你們!

問題是op必須呼叫批准,而不是合約地址。

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);
});

顯然,要讓它工作,你需要有一些 WBNB。

引用自:https://ethereum.stackexchange.com/questions/99957