Lesson 5 capture 1 SafeMathパート3
winCountとlossCountはuint16で、levelはuint32の問題。
SafeMath32ライブラリの使用
using SafeMath32 for uint32;
function mul(uint32 a, uint32 b) internal pure returns (uint32) {
if (a == 0) {
return 0;
}
uint32 c = a * b;
assert(c / a == b);
return c;
}
function div(uint32 a, uint32 b) internal pure returns (uint32) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint32 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint32 a, uint32 b) internal pure returns (uint32) {
assert(b <= a);
return a - b;
}
function add(uint32 a, uint32 b) internal pure returns (uint32) {
uint32 c = a + b;
assert(c >= a);
return c;
}
}
SafeMath16ライブラリの使用
library SafeMath16 {
function mul(uint16 a, uint16 b) internal pure returns (uint16) {
if (a == 0) {
return 0;
}
uint16 c = a * b;
assert(c / a == b);
return c;
}
function div(uint16 a, uint16 b) internal pure returns (uint16) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint16 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint16 a, uint16 b) internal pure returns (uint16) {
assert(b <= a);
return a - b;
}
function add(uint16 a, uint16 b) internal pure returns (uint16) {
uint16 c = a + b;
assert(c >= a);
return c;
}
}
ownerZombieCount[msg.sender] = ownerZombieCount[msg.sender].add(1);
prev
-
クリプトゾンビLesson 5 capture 10 SafeMathパート2
Lesson 5 capture 10 SafeMathパート2 SafeMathライブラリのコード openzeppelin-solidity/contracts/math/SafeMath.sol ...
next
-
クリプトゾンビLesson 5 capture 13 コメント(natspec)
Lesson 5 capture 13 コメント コメントのシンタックス 1行コメント。 // thankyou 複数行コメント /* This is a multi-lined comment. I ...