Hi I just started to study solidity. Can you tell me what is using SafeMath for uint256;
?
https://github.com/decentraland/mana/blob/master/contracts/ContinuousSale.sol#L10
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityHi I just started to study solidity. Can you tell me what is using SafeMath for uint256;
?
https://github.com/decentraland/mana/blob/master/contracts/ContinuousSale.sol#L10
SafeMath
is a solidity math library especially designed to support safe math operations: safe means that it prevents overflow when working with uint
. You can find it in zeppelin-solidity SafeMath.
From the official documentation:
The directive
using A for B;
can be used to attach library functions (from the libraryA
) to any type (B
). These functions will receive the object they are called on as their first parameter (like the self variable in Python).The effect of using
A for *;
is that the functions from the library A are attached to any type.
Hope it helps~
using SafeMath for uint256;
. You must also replace each +
, -
, *
, and /
with the equivalent .add()
, .sub()
, .mul()
and .div()
functions.
Feb 5, 2018 at 19:09
using ... for ...
is explained in Solidity's documentation, here, and is a way to attach library functions to a given type.
So in your case:
using SafeMath for uint256;
...means that the functions contained in the SafeMath.sol
library can be used directly on objects of type uint256
.
Taking an example from that contract, here we can see that weiAmount
is declared as a uint256
, and we can then directly call the attached .mul()
function on it, rather that passing the variable as a function argument.
uint256 weiAmount = msg.value;
// calculate token amount to be created
uint256 tokens = weiAmount.mul(rate);