Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
397 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
pragma solidity ^0.5.0; | ||
|
||
import "./Token.sol"; | ||
|
||
/** | ||
* @dev This contract showcases a simple Try-catch call in Solidity | ||
*/ | ||
contract Example { | ||
Token public token; | ||
uint256 public lastAmount; | ||
|
||
constructor(Token _token) public { | ||
token = _token; | ||
} | ||
|
||
event TransferFromFailed(address _from, uint256 _amount); | ||
|
||
function tryTransferFrom(address _from, address _to, uint256 _amount) public returns(bool returnedBool, uint256 returnedAmount) { | ||
lastAmount = _amount; // We can query this after transferFrom reverts to confirm that the whole transaction did NOT revert | ||
// and the changes we made to the state are still present. | ||
|
||
(bool success, bytes memory returnData) = | ||
address(token).call( // This creates a low level call to the token | ||
abi.encodePacked( // This encodes the function to call and the parameters to pass to that function | ||
token.transferFrom.selector, // This is the function identifier of the function we want to call | ||
abi.encode(_from, _to, _amount) // This encodes the parameter we want to pass to the function | ||
) | ||
); | ||
if (success) { // transferFrom completed successfully (did not revert) | ||
(returnedBool, returnedAmount) = abi.decode(returnData, (bool, uint256)); | ||
} else { // transferFrom reverted. However, the complete tx did not revert and we can handle the case here. | ||
// I will emit an event here to show this | ||
emit TransferFromFailed(_from, _amount); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pragma solidity ^0.5.0; | ||
|
||
contract Migrations { | ||
address public owner; | ||
|
||
uint public lastCompletedMigration; | ||
|
||
modifier restricted() { | ||
require(msg.sender == owner, "Sender must be owner"); | ||
_; | ||
} | ||
|
||
constructor() public { | ||
owner = msg.sender; | ||
} | ||
|
||
function setCompleted(uint _completed) public restricted { | ||
lastCompletedMigration = _completed; | ||
} | ||
|
||
function upgrade(address _newAddress) public restricted { | ||
Migrations upgraded = Migrations(_newAddress); | ||
upgraded.setCompleted(lastCompletedMigration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
pragma solidity ^0.5.0; | ||
|
||
/** | ||
* @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: | ||
* - Addition cannot overflow. | ||
*/ | ||
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) { | ||
require(b <= a, "SafeMath: subtraction overflow"); | ||
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-solidity/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) { | ||
// Solidity only automatically asserts when dividing by 0 | ||
require(b > 0, "SafeMath: division by zero"); | ||
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) { | ||
require(b != 0, "SafeMath: modulo by zero"); | ||
return a % b; | ||
} | ||
} | ||
|
Oops, something went wrong.