Practicing Safemath for Smart Contracts with Solidity and Openzeppelin

Joshua Herman
May 18, 2018 · 4 min read
Image for post
Image for post
View of Navy Pier from the Distributed Confrence

pragma solidity ^0.4.22;contract Lucas {uint[] lucseries;  // n = how many in the series to return
function generateLuc(uint n) public {
// set 1st and 2nd entries
lucseries.push(2);
lucseries.push(1);
// generate subsequent entries
for (uint i=2; i < n ; i++) {
lucseries.push(lucseries[i-1] + lucseries[i-2]);
}
}}
pragma solidity ^0.4.22;
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol";
contract Lucas {

using SafeMath for uint; //
uint[] lucseries;
// n = how many in the series to return
function generateLuc(uint n) public {
// set 1st and 2nd entries
lucseries.push(2);
lucseries.push(1);
// generate subsequent entries
for (uint i=2; i < n ; i++) {
lucseries.push(lucseries[i-1].add( lucseries[i-2]));
}
}
}
Image for post
Image for post
Computation of the Lucas Number in Solidity fails at the 111th iteration
pragma solidity ^0.4.22;
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol";
contract Exception {

using SafeMath for uint; //
uint two_to_the_256_power_minus_one = 115792089237316195423570985008687907853269984665640564039457584007913129639935; //2 to the 256 power minus 1
uint more = two_to_the_256_power_minus_one + two_to_the_256_power_minus_one;

}
Image for post
Image for post
Practicing Safemath and getting the exception you desire.

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Crypto News

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Joshua Herman

Written by

Amateur data scientist. Lover of code and data .

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Joshua Herman

Written by

Amateur data scientist. Lover of code and data .

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store