solidity之msg.sender总结(一个实时变化的变量)

务必注意:msg.sender是一个实时变化的变量!在合约中,方法的调用者不一样,msg.sender就会不一样! 

pragma solidity >=0.5.0 <0.7.0;

import "../../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

contract TestToken is ERC20{
    string public name = "KirinToken";
    string public symbol = "KIT";
    constructor() public{
        _totalSupply = 100000000;
        // 这里的msg.sender是合约部署者(初始化构造函数)
        _balances[msg.sender] = _totalSupply;
    }

    function transfer(address to, uint256 value) public returns (bool) {
        // 这里的msg.sender是调用transfer方法的人
         _transfer(msg.sender, to, value);
        return true;
    }

    function _transfer(address from, address to, uint256 value) internal {
        require(to != address(0));
        // 注意:using SafeMath for uint256 可以被继承!
        _balances[from] = _balances[from].sub(value);
        _balances[to] = _balances[to].add(value);
        emit Transfer(from, to, value);
    }
}

 

你可能感兴趣的:(以太坊solidity)