Assignment - SafeMath

Post your code here for the safemath assignment.

6 Likes

Hello @filip, great course, I am really enjoying it!

Here is my solution for the SafeMath assignment.

BankWithSafeMath.sol
``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./SafeMath.sol";

contract Bank{

using SafeMath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from].sub(amount); //As soon as both operations are using SafeMath Warning pops up that
balance[to].add(amount);   // function can be restricted to view...
}

}
``````

An interesting observation I made is, that with solidity 0.8.0 under- and overflow operations are not possible even without SafeMath. Is it still better to use it? And if yes, why?

OverUnderFlow Solidity 0.5.12 vs Solidity 0.8.0

2 Likes

Ok, here is my take on the assignment:

``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";

contract Bank is Ownable {

using Safemath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] =balance[from].sub(amount);
}

}
``````
1 Like

Here is my solution:

``````contract Bank is Ownable {

using SafeMath for uint;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
balance[msg.sender] = balance[msg.sender].add(msg.value); // balance[msg.sender] += msg.value;
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount); // balance[msg.sender] -= value;
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like

My solution

Bank_SafeMath.sol
``````pragma solidity 0.8.0;
pragma abicoder v2;

/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
contract Ownable{

/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () {
_owner = msgSender;
}

/**
* @dev Returns the address of the current owner.
*/
function owner() public view returns (address) {
return _owner;
}

/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(_owner == msg.sender, "Ownable: caller is not the owner");
_;
}

/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
}

/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}

/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");

return c;
}

/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}

/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;

return c;
}

/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}

uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");

return c;
}

/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}

/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold

return c;
}

/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}

/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}

// import "./Ownable.sol";
// import "./SafeMath.sol";

contract Bank is Ownable {

using SafeMath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public{
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance.sub(amount));
}

balance[from] = balance[from].sub(amount);
}

}
``````

But, with solidity v0.8.0 there is no need for using SafeMath library anymore, since all its methods are now inherit into solidity programming.

https://docs.soliditylang.org/en/latest/080-breaking-changes.html?highlight=safemath#

Carlos Z.

8 Likes

Arithmetic operations revert on underflow and overflow. You can use `unchecked { ... }` to use the previous wrapping behaviour.

Checks for overflow are very common, so we made them the default to increase readability of code, even if it comes at a slight increase of gas costs.

• Optional: If you use SafeMath or a similar library, change `x.add(y)` to `x + y` , `x.mul(y)` to `x * y` etc.

https://docs.soliditylang.org/en/latest/080-breaking-changes.html?highlight=safemath#

So basically itâ€™s optional to keep using SafeMath library, but honestly, in terms of programming, is quite useless since all its functionality is now inherit by default, so is not need it anymore because its safe to just use the arithmetic operators

Carlos Z

1 Like

Alright, so SafeMath only makes sense to use with Solidity versions below 0.8.0. Thank you for linking to the documentation. it is going to be my new bed-time lecture seriously

2 Likes
``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";

contract Bank is Ownable {
using SafeMath for uint;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance.sub(amount));
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like

Hereâ€™s my quick edit of the code.

``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";

contract Bank is Ownable {

using SafeMath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from].sub(amount);
}

}
``````
1 Like
``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";

contract Bank is Ownable {

using SafeMath for uint256;

event depositDone(uint256 amount, address indexed depositedTo);

function deposit() public payable returns (uint256)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint256 amount) public onlyOwner returns (uint256){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint256){
return balance[msg.sender];
}

function transfer(address recipient, uint256 amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint256 previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like

bank-safemath.sol

``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./safemath.sol";

contract Bank is Ownable {

using SafeMath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like

My answer to the SafeMath assignment:

``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./Safemath.sol";

contract Bank is Ownable {

using SafeMath for uint;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
balance[msg.sender] += msg.value;
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] -= amount;
balance[to] += amount;
}

}
``````

Ben

1 Like
``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";

import "./safemath.sol";

contract Bank  {

using SafeMath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {

emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like
``````pragma solidity 0.8.0;
pragma abicoder v2;

import "./Ownable.sol";
import "./ZeplinMath.sol";

contract Bank is Ownable {

using SafeMath for uint256;

event depositeDone(uint amount, address indexed depositedTo);

function deposit() public payable returns(uint) {
emit depositeDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns(uint) {
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns(uint) {
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "balance not sufficient");
require(msg.sender != recipient, "Don't transfer to yourself");
uint previousBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousBalance);
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like

First Pass:
The assignment is to apply SafeMath.sol to the given Bank.sol. So disregarding the â€śshould you really?â€ť and â€śis this necessaryâ€ť discussion above the first step here is to â€śalways use SafeMathâ€ť. This compiles:

``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";

contract Bank is Ownable {

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = SafeMath.sub(balance[msg.sender],amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] = SafeMath.sub(balance[from], amount);
}

}
``````

Second Pass:
reviewing vs. checks --> effects --> interactions pattern it appears at first glance that the transfer method has problems. But the _transfer() function is really an effect, not an interaction. Thereâ€™s no external call.

Third Pass:

``````payable(msg.sender).transfer(amount);
``````

Reads suspiciously. Gramar suggests that itâ€™s a command to send the amount FROM msg.sender, but upon reading the .transfer() docs in 0.8.0, the local context is the implied payer. Thatâ€™s awkward syntax, but not a mistake.

Fourth Pass vs. EIP1884:

``````    function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = SafeMath.sub(balance[msg.sender],amount);
// Update "payable(msg.sender).transfer(amount);" vs. EIP1884:
(bool worked, ) = msg.sender.call{value:amount}(""); // Send raw ETH.
require(worked == true, "Transfer failed.");
return balance[msg.sender];
}
``````

Fifth Pass:
We donâ€™t really need a â€śdepositâ€ť function do we. Substitute the following:

``````    receive() external payable {
emit depositDone(msg.value, msg.sender);
}
``````

Why I canâ€™t deploy bank contract. HelloWorld.sol should be Safemath.sol but itâ€™s just changed name od file.

1 Like
``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./Ownable.sol";
import "./SafeMath.sol";

contract Bank is Ownable {

using SafeMath for uint;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "balance not sufficient");
require(msg.sender != recipient, "Why transfer money to yourself?");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance - amount);
}

balance[from] = balance[from].sub(amount);
}
}
``````

Solution.

1 Like

Hi there!

``````pragma solidity 0.8.0;
pragma abicoder v2;
import "./ownable.sol";
import "./safemath.sol";

contract Bank is Ownable {

using SafeMath for uint256;

event depositDone(uint amount, address indexed depositedTo);

function deposit() public payable returns (uint)  {
emit depositDone(msg.value, msg.sender);
return balance[msg.sender];
}

function withdraw(uint amount) public onlyOwner returns (uint){
require(balance[msg.sender] >= amount);
balance[msg.sender] = balance[msg.sender].sub(amount);
payable(msg.sender).transfer(amount);
return balance[msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");

uint previousSenderBalance = balance[msg.sender];

_transfer(msg.sender, recipient, amount);

assert(balance[msg.sender] == previousSenderBalance.sub(amount));
}

balance[from] = balance[from].sub(amount);
}

}
``````
1 Like

Hey @Lane11

Please post your code here so that we can have a look.
Also try to run remix from an incognito window to exclude browser related issues.

Cheers,
Dani

You can use SafeMath also in your `withdraw` function and in the assert statement of your `transfer` function

Well done anyway.

Cheers,
Dani