Overflow
Uint256 的問題
這是我在這裡的第一篇文章,當我搜尋時我無法找到我的問題的答案。我正在使用帶有 Metamask 的 Remix,我創建了契約並使用以下程式碼進行鑄造。我在契約中有鑄造限制。但是,代幣向我顯示了總供應量為一個巨大的值“10000000000000000000”,這已經高於總供應量限制,因此對鑄幣的檢查會停止執行。
$$ ![enter image description here $$$$ 1 $$]$$ 1 $$
function mint(uint noOfTokens) returns (bool) { checkTokens = _totalSupply.add(noOfTokens); // Check the max limit and the owner before minting require(_totalSupply <= maxLimit && checkTokens <= maxLimit && (msg.sender == address(owner))); balances[owner] = balances[owner].add(noOfTokens); _totalSupply = _totalSupply.add(noOfTokens); return true;
}
變數定義為 uint256。
感謝您的幫助和支持。NKB。
$$ ![enter image description here $$$$ 2 $$]$$ 2 $$ pragma 可靠性 ^0.4.11;
//ERC20介面導入’./ERC20.sol’; // 高低攻擊的安全數學。導入’./SafeMath.sol';
contract Test is ERC20 { // 對 uint256 使用安全數學,對 uint256
使用 SafeMath;
uint256 public constant maxLimit = 500000000; uint256 public _totalSupply = 0; string public constant symbol = "Test"; string public constant name = "TestCoin"; uint8 public constant decimals = 0; // Rate -> 1 Ether = 1000 Just uint256 public RATE; // Ether reciever address public owner; uint public start; uint public startWeek; uint public startTwoWeek; uint public startFourWeek; uint256 public tokens; uint256 public checkTokens; mapping(address=> uint256) balances; mapping(address=>mapping(address=>uint256)) allowed; // Fallback function // The function without name is the default function that is called // whenever anyone sends funds to a contract function() payable{ createTokens(); } function Test(){ // balances[msg.sender] = _totalSupply; owner = msg.sender; // get the owner of the token start = now; // Get the current block time startWeek = start + 30 minutes ; // Get the scond block time startTwoWeek = start + 60 minutes; // Get the third block time startFourWeek = start + 90 minutes; // Get the fourth block time . } // Create new tokens function mint(uint noOfTokens) returns (bool) { checkTokens = _totalSupply.add(noOfTokens); // Check the max limit and the owner before minting require(_totalSupply/1000000000000000000 <= maxLimit && checkTokens/1000000000000000000 <= maxLimit && (msg.sender == address(owner))); balances[owner] = balances[owner].add(noOfTokens); _totalSupply = _totalSupply.add(noOfTokens); return true;
}
function createTokens() payable { require(msg.value >0 && _totalSupply <= maxLimit); if (now <= startWeek && now > start) { RATE = 1150; tokens = msg.value.mul(RATE); balances[msg.sender] = balances[msg.sender].add(tokens); _totalSupply = _totalSupply.add(tokens); owner.transfer(msg.value); } if (now <= startTwoWeek && now > startWeek ) { RATE = 1100; tokens = msg.value.mul(RATE); balances[msg.sender] = balances[msg.sender].add(tokens); _totalSupply = _totalSupply.add(tokens); owner.transfer(msg.value); } if (now <= startFourWeek && now > startTwoWeek ) { RATE = 1050; tokens = msg.value.mul(RATE); balances[msg.sender] = balances[msg.sender].add(tokens); _totalSupply = _totalSupply.add(tokens); owner.transfer(msg.value); } if (now > startFourWeek ) { RATE = 1000; tokens = msg.value.mul(RATE); balances[msg.sender] = balances[msg.sender].add(tokens); _totalSupply = _totalSupply.add(tokens); owner.transfer(msg.value); } } function totalSupply() constant returns (uint totalSupply){ return _totalSupply; } function balanceOf(address _owner) constant returns (uint balance){ return balances[_owner]; } function transfer(address _to, uint _value) returns (bool success){ require( balances[msg.sender] >= _value && _value > 0 ); balances[msg.sender] = balances[msg.sender].sub(_value) ; balances[_to] = balances[_to].add(_value); Transfer(msg.sender,_to,_value); return true; } function transferFrom(address _from, address _to, uint _value) returns (bool success){ require( allowed[_from][msg.sender] >= _value && balances[_from] >= _value && _value > 0 ); balances[_from] = balances[_from].sub(_value); balances[_to] = balances[_to].add(_value); allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); Transfer(_from,_to,_value); return true; } function approve(address _spender, uint _value) returns (bool success){ allowed[msg.sender][_spender] = _value; Approval(msg.sender,_spender,_value); return true; } function allowance(address _owner, address _spender) constant returns (uint remaining){ return allowed [_owner][_spender]; } event GoalReached(address beneficiary, uint amountRaised); event Transfer(address indexed _from, address indexed _to, uint _value); event Approval(address indexed _owner, address indexed _spender, uint _value);
}
因為您**
decimals
設置為18
,所以您必須除以*(1 後跟 18 個零)以獲得整數值1,000,000,000,000,000,000
*_totalSupply.
18 位小數是乙太坊的標準。最小的單位被稱為一個Wei。1,000,000,000,000,000,000 wei 等於 1 Ether。這是一個單位地圖。
為了簡化您的契約,您可以將小數設置為**
0
,在這種情況下,您將只處理不可整除的整數值。不利的一面是,您不能再提供代幣的一部分。**