From eeb989026774d9b376362376fe7989e2d0ff7a07 Mon Sep 17 00:00:00 2001 From: Shebin John Date: Sat, 10 Feb 2024 13:27:37 +0530 Subject: [PATCH] Smart Contract Update --- .github/workflows/node.js.yml | 2 +- .gitignore | 3 +- .solhint.json | 2 +- package.json | 15 ++++---- src/ISplitter.sol | 64 +++++++++++++++++++++++++++++++++++ src/Splitter.sol | 48 +++++--------------------- 6 files changed, 84 insertions(+), 50 deletions(-) create mode 100644 src/ISplitter.sol diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 0c75861..617a8e3 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -22,7 +22,7 @@ jobs: with: node-version: ${{ matrix.node-version }} - name: Installing Packages - run: yarn install + run: yarn install --frozen-lockfile - name: Checking Formatting run: yarn lint && yarn prettier-check - name: Install Foundry diff --git a/.gitignore b/.gitignore index f3df457..ca269d8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ cache script out .vscode -yarn.lock \ No newline at end of file +yarn.lock +lcov.info diff --git a/.solhint.json b/.solhint.json index a62e2b0..8ed7361 100644 --- a/.solhint.json +++ b/.solhint.json @@ -4,6 +4,6 @@ "rules": { "prettier/prettier": "error", "max-line-length": ["warn", 140], - "compiler-version": ["error", "0.8.13"] + "compiler-version": ["error", "^0.8.23"] } } diff --git a/package.json b/package.json index 3bd559e..a8fb19c 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ }, "license": "ISC", "author": "Shebin John", - "main": "truffle-config.js", "directories": { "test": "test" }, @@ -35,12 +34,12 @@ "yarn": "^1.22.10" }, "devDependencies": { - "coveralls": "^3.1.1", - "prettier": "^2.8.8", - "prettier-plugin-solidity": "^1.1.3", - "sol2uml": "^2.5.4", - "solhint": "^4.0.0", - "solhint-plugin-prettier": "^0.0.5", - "solidity-coverage": "^0.8.2" + "coveralls": "3.1.1", + "prettier": "3.2.5", + "prettier-plugin-solidity": "1.3.1", + "sol2uml": "2.5.20", + "solhint": "4.1.1", + "solhint-plugin-prettier": "0.1.0", + "solidity-coverage": "0.8.7" } } diff --git a/src/ISplitter.sol b/src/ISplitter.sol new file mode 100644 index 0000000..6e171c3 --- /dev/null +++ b/src/ISplitter.sol @@ -0,0 +1,64 @@ +/** + * @title Splitter + * @author Shebin John + * @notice This code implements a simple version of Splitter. + * @dev Takes an ETH input and divides it to two specified address. + */ + +pragma solidity 0.8.23; + +interface ISplitter { + // **************************** // + // * Events * // + // **************************** // + + /** + * @notice This event is Logged when a Split happens. + * @dev Takes the address of the receivers and the value. + * @param _first The address of first receiver. + * @param _second The address of second receiver. + * @param _value The total value received by both. + */ + event Splitted(address indexed _first, address indexed _second, uint256 _value); + + /** + * @notice This event is Logged when a withdraw is done. + * @dev Takes the address of user and the value user is withdrawing. + * @param _to The address of the User. + * @param _value The value being withdrawn. + */ + event Transfered(address indexed _to, uint256 _value); + + // **************************** // + // * Custom Errors * // + // **************************** // + + /** + * @notice This error is thrown when Zero Address is passed. + */ + error ZeroAddressNotAllowed(); + + /** + * @notice This error is thrown when Zero Amount is passed. + */ + error ZeroAmountNotAllowed(); + + // **************************** // + // * Functions * // + // **************************** // + + /** + * @notice The function splits the value sent to two addresses. + * @param _first The address of first receiver. + * @param _second The address of second receiver. + * @return _status Returns the status of the Operation. + */ + function split(address _first, address _second) external payable returns (bool _status); + + /** + * @notice This function helps to withdraw money from the contract. + * @dev https://stackoverflow.com/a/52438518/7520013. + * @param _amount The amount to withdraw. + */ + function withdraw(uint256 _amount) external; +} diff --git a/src/Splitter.sol b/src/Splitter.sol index 75b4fe8..e9adba6 100644 --- a/src/Splitter.sol +++ b/src/Splitter.sol @@ -5,48 +5,24 @@ * @dev Takes an ETH input and divides it to two specified address. */ -pragma solidity 0.8.13; +pragma solidity 0.8.23; -contract Splitter { +import { ISplitter } from "./ISplitter.sol"; + +contract Splitter is ISplitter { // **************************** // // * Contract variable * // // **************************** // mapping(address => uint256) public balances; - // **************************** // - // * Events * // - // **************************** // - - /** - * @notice This event is Logged when a Split happens. - * @dev Takes the address of the receivers and the value. - * @param _first The address of first receiver. - * @param _second The address of second receiver. - * @param _value The total value received by both. - */ - event Splitted(address indexed _first, address indexed _second, uint256 _value); - - /** - * @notice This event is Logged when a withdraw is done. - * @dev Takes the address of user and the value user is withdrawing. - * @param _to The address of the User. - * @param _value The value being withdrawn. - */ - event Transfered(address indexed _to, uint256 _value); - // **************************** // // * Functions * // // **************************** // - /** - * @notice The function splits the value sent to two addresses. - * @param _first The address of first receiver. - * @param _second The address of second receiver. - * @return _status Returns the status of the Operation. - */ + /// @inheritdoc ISplitter function split(address _first, address _second) public payable returns (bool _status) { - require(_first != address(0) && _second != address(0), "Zero Address not Allowed"); + if (_first == address(0) || _second == address(0)) revert ZeroAddressNotAllowed(); // To divide the amount to be send to _first and _second. uint256 msgValueAmountByTwo = msg.value / 2; @@ -61,14 +37,9 @@ contract Splitter { return true; } - /** - * @notice This function helps to withdraw money from the contract. - * @dev // https://stackoverflow.com/a/52438518/7520013. - * @param _amount The amount to withdraw. - * @return _status in bool. - */ - function withdraw(uint256 _amount) public returns (bool _status) { - require(_amount > 0, "Amount cannot be Zero"); + /// @inheritdoc ISplitter + function withdraw(uint256 _amount) public { + if (_amount == 0) revert ZeroAmountNotAllowed(); uint256 balance = balances[msg.sender]; @@ -77,6 +48,5 @@ contract Splitter { emit Transfered(msg.sender, _amount); payable(msg.sender).transfer(_amount); - return true; } }