-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBasic Function Excercise..
More file actions
47 lines (41 loc) · 1.51 KB
/
Copy pathBasic Function Excercise..
File metadata and controls
47 lines (41 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Create File Name Called BasicMath.sol in Remix Studio..
Copy this below code And Deploy that on Base Sepolia..Don't Forgot You need to Deploy this on Base sepolia Testnet
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title BasicMath
* @dev Provides safe addition and subtraction without using SafeMath library.
* Returns a result and a boolean error flag.
*/
contract BasicMath {
/// @notice Adds two unsigned integers and checks for overflow.
/// @param _a First number
/// @param _b Second number
/// @return sum The sum of the inputs or 0 if overflow occurs
/// @return error True if overflow occurred, false otherwise
function adder(uint _a, uint _b) public pure returns (uint sum, bool error) {
unchecked {
sum = _a + _b;
if (sum < _a) {
// Overflow occurred
return (0, true);
}
return (sum, false);
}
}
/// @notice Subtracts two unsigned integers and checks for underflow.
/// @param _a First number
/// @param _b Second number
/// @return difference The difference or 0 if underflow occurs
/// @return error True if underflow occurred, false otherwise
function subtractor(uint _a, uint _b) public pure returns (uint difference, bool error) {
unchecked {
if (_b > _a) {
// Underflow occurred
return (0, true);
}
difference = _a - _b;
return (difference, false);
}
}
}