pragma solidity ^0.5.13;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
contract LibrariesExample {
using SafeMath for uint;
mapping(address => uint) public tokenBalance;
constructor() public {
tokenBalance[msg.sender] = tokenBalance[msg.sender].add(1);
// tokenBalance[msg.sender] = 1;
}
function sendToken(address _to, uint _amount) public returns(bool) {
tokenBalance[msg.sender] = tokenBalance[msg.sender].sub(_amount);
tokenBalance[_to] = tokenBalance[_to].add(_amount);
// tokenBalance[msg.sender] -= _amount;
// tokenBalance[_to] += _amount;
return true;
}
}
< 0.8 or >= 0.8 has differnet overfolw and underflow problem
pragma solidity >=0.4.16 <0.7.0;
library Search {
function indexOf(uint[] storage self, uint value)
public
view
returns (uint){
for (uint i = 0; i < self.length; i++)
if (self[i] == value) return i;
return uint(-1);
}
}
contract NotUsingForExample {
uint[] data;
function append(uint value) public {
data.push(value);
}
function replace(uint _old, uint _new) public {
// This performs the library function call
uint index = Search.indexOf(data,_old);
if (index == uint(-1))
data.push(_new);
else
data[index] = _new;
}
}
contract UsingForExample {
using Search for uint[];
uint[] data;
function append(uint value) public {
data.push(value);
}
function replace(uint _old, uint _new) public {
// This performs the library function call
uint index = data.indexOf(_old);
if (index == uint(-1))
data.push(_new);
else
data[index] = _new;
}
}