Skip to content

Commit

Permalink
chore: remove Arith for now
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Dec 18, 2022
1 parent 8caa034 commit fd0620a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
8 changes: 7 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ FOUNDRY_ETH_RPC_URL=https://polygon-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}
test:
forge test -vvv

.PHONY: test
build:
forge build --sizes

create:
forge create --constructor-args ${CONSTRUCTOR_ARGS} --private-key ${PRIVATE_KEY} src/Spleth.sol:Spleth

.PHONY: test create
8 changes: 0 additions & 8 deletions src/Arith.sol

This file was deleted.

8 changes: 5 additions & 3 deletions src/Spleth.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "./Arith.sol";
import "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import "forge-std/console.sol";

contract Spleth {
using Arith for uint256;

bool public running;
address public runningToken;
Expand All @@ -20,6 +18,10 @@ contract Spleth {
participants = addresses;
}

function divUp(uint256 x, uint256 y) private pure returns (uint256) {
return (x + y - 1) / y;
}

function initializeGroupPayWithoutApprove(address token, uint256 amount, address receiver) public {
require (!running);

Expand All @@ -46,7 +48,7 @@ contract Spleth {
require (approvals[msg.sender] == false, "you already approved");

uint amount = runningAmount;
uint256 shareOfAmount = amount.divUp(participants.length);
uint256 shareOfAmount = divUp(amount, participants.length);

IERC20(runningToken).transferFrom(msg.sender, address(this), shareOfAmount);
approvals[msg.sender] = true;
Expand Down
13 changes: 8 additions & 5 deletions test/TestArith.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import "forge-std/Vm.sol";
import "forge-std/console.sol";
import "forge-std/Test.sol";

import "../src/Arith.sol";
import "../src/Spleth.sol";

contract TestArith is Test {
using Arith for uint256;

function divUp(uint256 x, uint256 y) private pure returns (uint256) {
return (x + y - 1) / y;
}

function testFailDivZero() public pure {
uint256 x = 2.4 ether + 1;
x.divUp(0);
divUp(x, 0);
}

function testDivision() public {
uint256 x = 14;
assertEq(x.divUp(2), 7);
assertEq(x.divUp(3), 5);
assertEq(divUp(x, 2), 7);
assertEq(divUp(x, 3), 5);
}

}
8 changes: 5 additions & 3 deletions test/TestSpleth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";

import "forge-std/Test.sol";
import "forge-std/Vm.sol";
import "../src/Arith.sol";
import "../src/Spleth.sol";

contract TestSpleth is Test {
using Arith for uint256;

Spleth public spleth;
address user1 = address(123);
address user2 = address(978);
address DAI = address(0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063);
address receiver = address(444);

function divUp(uint256 x, uint256 y) private pure returns (uint256) {
return (x + y - 1) / y;
}

function setUp() public {
address[] memory users = new address[](2);
users[0] = user1;
Expand Down Expand Up @@ -69,6 +71,6 @@ contract TestSpleth is Test {
uint256 balanceSpleth = IERC20(DAI).balanceOf(address(spleth));

assertEq(balanceReceiver, amount, "transferred amount");
assertEq(balanceSpleth, 2 * amount.divUp(2) - amount, "transferred amount");
assertEq(balanceSpleth, 2 * divUp(amount, 2) - amount, "transferred amount");
}
}

0 comments on commit fd0620a

Please sign in to comment.