Solidity

openzeplin 導入混音

  • April 25, 2022

我正在從 openzeplin 導入 erc20,但它不起作用!

這是程式碼

   // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";

contract lock is ERC20{
   
}

這是錯誤

   TypeError: Contract "lock" should be marked as abstract.
--> learning/lock.sol:6:1:
|
6 | contract lock is ERC20{
| ^ (Relevant source part starts here and spans across multiple lines).
Note: Missing implementation:
--> https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol:54:5:
|
54 | constructor(string memory name_, string memory symbol_) {
| ^ (Relevant source part starts here and spans across multiple lines).

如 open Zeppelin 的文件中所述,您應該實現建構子,否則您的契約需要標記為抽象,因為您的錯誤也指定。

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract lock is ERC20 {
constructor()...
}

我想用它來鎖定契約,我不需要名字,供應。當我從建構子中刪除參數時,它給了我同樣的錯誤

我假設你想在你的智能合約中鎖定一個特定的代幣。為了解決這個問題,您可以使用 ERC20 智能合約之類的狀態變數並訪問 ERC20 智能合約方法。你可以看到這個例子:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";

contract lock {
   ERC20 _token;

   constructor(address _tokenAddress) { 
       _token = ERC20(_tokenAddress);
   }

   // Get name about ERC20 token
   function getName() external view returns(string memory) {
       return _token.name();
   }
}

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