当前位置: 首页 > 文档资料 > Open Zeppelin >

智能合约 - StandardToken

优质
小牛编辑
124浏览
2023-12-01

基于FirstBlood的代码:原始的firstBlood的代码在这里。

继承了合约SafeMath,实现了ERC20标准(标准参见:https://github.com/ethereum/EIPs/issues/20)。

原文地址:http://zeppelin-solidity.readthedocs.io/en/latest/standardtoken.html

源码地址:https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/StandardToken.sol

approve(address _spender, uint _value) returns (bool success)

批准传递的地址,可以花费合约调用者额度范围内的代币数量。

function approve(address _spender, uint256 _value) returns (bool) {
    // To change the approve amount you first have to reduce the addresses`
    //  allowance to zero by calling `approve(_spender, 0)` if it is not
    //  already 0 to mitigate the race condition described here:
    //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
    require((_value == 0) || (allowed[msg.sender][_spender] == 0));
    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
}

这里的竞态条件,是指原来授权N,打算改成M,有可能最终授权了M + N

allowance(address _owner, address _spender) constant returns (uint remaining)

返回被授权者可以使用的剩余的额度。

function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
    return allowed[_owner][_spender];
}

balanceOf(address _owner) constant returns (uint balance)

父类BasicToken方法。返回传入地址的余额。

function balanceOf(address _owner) constant returns (uint256 balance) {
    return balances[_owner];
}

transferFrom(address _from, address _to, uint _value) returns (bool success)

向方法调用者允许的地址转发代币。转发代币的额度不能超过允许的额度,和当前帐户的余额。

function transferFrom(address _from, address _to, uint256 _value) returns (bool) {
    var _allowance = allowed[_from][msg.sender];
    // Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
    // require (_value <= _allowance);
    balances[_to] = balances[_to].add(_value);
    balances[_from] = balances[_from].sub(_value);
    allowed[_from][msg.sender] = _allowance.sub(_value);
    Transfer(_from, _to, _value);
    return true;
}

function transfer(address _to, uint _value) returns (bool success)

父类BasicToken方法。从发送者的帐户转代币,额度不能超过发送者的余额。

function transfer(address _to, uint256 _value) returns (bool) {
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;
}