iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 24
1
影片教學

Smart Contract 實戰教學系列 第 24

ERC20 Mintable

剛好有個夥伴問到 total supply 可不可以調整,那今天我們就來談談可以隨時發行新的 token 數量的合約吧!且還需要實作可以控制誰能發行的 modifier 呢!

接下來幾天都會是預錄的方式,因為目前我在河口湖旅遊中XDDDD 網路不是很好,需要先錄好提前上傳影片才行QQ

browser/ERC20.sol 請參考昨天的文章。

本日合約:

pragma solidity ^0.4.25;

import "browser/ERC20.sol";

contract Mintable is ERC20 {
    address private owner;
    mapping (address => bool) minters;
    
    constructor() public {
        owner = msg.sender;
    }
    
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }
    
    modifier onlyMinter() {
        require(minters[msg.sender]);
        _;
    }
    
    function addMinter(address addr) public onlyOwner returns (bool) {
        minters[addr] = true;
        return true;
    }
    
    function mint(address to, uint256 tokens) public onlyMinter returns (bool) {
        // 增加 total supply
        _totalSupply = _totalSupply.add(tokens);
        // 轉移新增出來的 token 到某人身上
        _balances[to] = _balances[to].add(tokens);
        emit Transfer(address(0), to, tokens);
        return true;
    }
}

本日影片:
https://youtu.be/t96vTnAJ4HM

Smart Contract 實戰教學播放清單:
https://www.youtube.com/playlist?list=PLHmOMPRfmOxSJcrlwyandWYiuP9ZAMYoF


上一篇
ERC20 optional functions
下一篇
ERC20 Burnable
系列文
Smart Contract 實戰教學30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
tommy70927
iT邦見習生 ‧ 2019-11-14 21:06:01

想請問一下 _totalSupply這行 會顯示錯誤 : DeclarationError : Undeclared identifier. _totalSupple =......
是否可以把ERC20.sol 的 uint256 private _totalSupply改成public
謝謝

kiancaca iT邦新手 5 級 ‧ 2020-10-16 13:33:47 檢舉

tommy70927 我也遇到相同問題,我改成 internal

hydai iT邦新手 4 級 ‧ 2020-10-16 15:07:29 檢舉

因為繼承的關係,改成是 internal 。就可以解決了

0
kiancaca
iT邦新手 5 級 ‧ 2020-10-16 13:35:21

hydai 有個疑問,這樣 minter 不就可以一直呼叫 mint() 幫自己加薪?

hydai iT邦新手 4 級 ‧ 2020-10-16 15:01:59 檢舉

在這個例子裡確實是如此,不過一定有某些人有權進行 mint,你可以設計不同的權限來讓特定人士呼叫 mint

kiancaca iT邦新手 5 級 ‧ 2020-10-16 18:41:51 檢舉

hydai 了解~ 感謝回覆!
那在真實的例子裡面,通常是一群 verifier 驗證真的有挖到幣後再進行 mint 的動作嗎?

hydai iT邦新手 4 級 ‧ 2020-10-17 20:41:51 檢舉

不一定,就各個組織都有他們自己的做法。
通常都需要有理由的增發,不然會讓供給總量出問題

我要留言

立即登入留言