Solidity

從另一個非 ERC20 合約中鑄造 ERC20

  • October 19, 2022

我正在嘗試建立一個合約,鑄造不同的 LP 代幣來代表流動資金池中的頭寸。

我的主合約創建了我的 lpToken 合約(ERC20)的實例,並將它們儲存在 Pool 資料結構列表中。

出於某種原因,我找不到從我的主契約中鑄造新 lpToken 的方法,我收到錯誤消息:

TypeError:在合約 ERC20 中進行參數相關查找後,成員“mint”未找到或不可見。

這是我的程式碼的精簡版本:

struct Pool {
   IERC20 tokenA;
   IERC20 tokenB;

   uint256 tokenALocked;
   uint256 tokenBLocked;

   ERC20 lpToken;
}
contract lpToken is ERC20, Ownable {
   constructor(string memory name, string memory ticker) ERC20(name, ticker) {}

   function mint(address to, uint256 amount) public onlyOwner {
       _mint(to, amount);
   }
   
   function getBalance() public view returns (uint256) {
       return balanceOf(msg.sender);
   }
}
contract ShittySwap {
   Pool[] public pools;

   function addPool(address tokenA, address tokenB) public {
       lpToken newLp = new lpToken("ShittyLP", "SLP");
       pools.push(Pool(IERC20(tokenA), IERC20(tokenB), 0, 0, newLp));
   }

   function addLiquidity(
           uint pool,
           IERC20 tokenA,
           IERC20 tokenB,
           uint qtyA,
           uint qtyB
       ) public {
       Pool memory swapPool = pools[pool];

       { ... doing stuff here ... }

       uint lpTokensToMint = {...someNumber...};

       swapPool.lpToken.mint(msg.sender, lpTokensToMint); // The error comes from this line
   }

   { ... }
}

謝謝

IERC20/ERC20界面中沒有造幣功能。您應該將 Pool 的 lptoken 更改為:

struct Pool {
   IERC20 tokenA;
   IERC20 tokenB;

   uint256 tokenALocked;
   uint256 tokenBLocked;

   lptoken lpToken;
}

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