ethereum - Solidity - 添加总供应量

标签 ethereum solidity erc20

我正在 remix 上学习 Solidity 我还引用了这个open source用于创建 token 的 API。

here他们提供了一个 _totalSupply() 函数,我想将其连接到我的智能合约,以便它显示我部署它的代币总量。

这里做错了什么?

pragma solidity ^0.8.0;

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

contract Foobar is ERC20 {
    constructor(uint256 initialSupply) public ERC20("Foobar", "FOO") {
        _mint(msg.sender, initialSupply);
       // add totalSupply here
        _totalSupply(uint256 5000000000000000000000000000000000000000);
    }
}

最佳答案

OpenZeppelin ERC20 _totalSupply 是私有(private)属性(property),这意味着您无法从派生合约(在您的情况下为 Foobar)访问它。

此外,您的语法也不正确。如果该属性至少是内部,您可以将其值设置为

_totalSupply = 5000000000000000000000000000000000000000;

或者以更易读的方式

_totalSupply = 5 * 1e39;

如果您想更改其可见性,则需要将(父)ERC20 合约复制到 IDE 并更改导入语句以反射(reflect)新的(本地)位置。然后,您将能够在本地契约(Contract)副本中更新属性可见性。

请注意,OpenZeppelin ERC20 包含相对导入路径(例如 import "./IERC20.sol";)。您还需要在本地副本中重写这些内容,以便它们指向 GitHub 位置。否则,编译器将尝试导入不存在的本地文件。

关于ethereum - Solidity - 添加总供应量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67292530/

相关文章:

token - 在 ERC20 合约中设置代币最大数量的问题

node.js - 发送 ERC-20 代币失败。交易成功,但没有发送 token

ethereum - 如何将 BSCScan 中的合约部署到币安智能链上?

ethereum - 以太坊什么时候会切换到权益证明?

python - 一次发送多个交易

node.js - 以太坊错误 : Transaction was not mined within 50 blocks

javascript - 在 MetaMask 中显示自定义 ERC20 函数的交易值

执行智能合约时的 Solidity 错误消息 : “The called function should be payable if you send value...”

java - Web3j : Writing operations on a contract always throw "java.lang.ArrayIndexOutOfBoundsException"

javascript - web3.eth.accounts[0] 返回未定义和 app.vote(1,{ from :web3. eth.accounts[0] }) 给出错误