Skip to content

Commit

Permalink
feat: add withdraw event to warehouse
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ohafza committed Jan 18, 2024
1 parent 532bb93 commit 09dbb2b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/splits-v2/src/SplitsWarehouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ contract SplitsWarehouse is ERC6909X {

event WithdrawalsPaused(address indexed owner, bool paused);
event WithdrawConfigUpdated(address indexed owner, WithdrawConfig config);
event Withdraw(
address indexed owner, address indexed token, address indexed withdrawer, uint256 amount, uint256 reward
);

/* -------------------------------------------------------------------------- */
/* STRUCTS */
Expand Down Expand Up @@ -177,7 +180,7 @@ contract SplitsWarehouse is ERC6909X {
* @param _amount The amount of the token to be withdrawn.
*/
function withdraw(address _token, uint256 _amount) external {
_withdraw(msg.sender, _token.toUint256(), _token, _amount);
_withdraw(msg.sender, _token.toUint256(), _token, _amount, msg.sender);
}

/**
Expand All @@ -190,7 +193,7 @@ contract SplitsWarehouse is ERC6909X {
if (_tokens.length != _amounts.length) revert LengthMismatch();

for (uint256 i; i < _tokens.length;) {
_withdraw(msg.sender, _tokens[i].toUint256(), _tokens[i], _amounts[i]);
_withdraw(msg.sender, _tokens[i].toUint256(), _tokens[i], _amounts[i], msg.sender);

unchecked {
++i;
Expand All @@ -214,7 +217,7 @@ contract SplitsWarehouse is ERC6909X {
uint256 reward = _amount * config.incentive / PERCENTAGE_SCALE;

if (reward > 0) _withdraw(_owner, _token.toUint256(), _token, _amount, reward, _withdrawer);
else _withdraw(_owner, _token.toUint256(), _token, _amount);
else _withdraw(_owner, _token.toUint256(), _token, _amount, _withdrawer);
}

/**
Expand Down Expand Up @@ -250,7 +253,7 @@ contract SplitsWarehouse is ERC6909X {
}
} else {
for (uint256 i; i < _tokens.length;) {
_withdraw(_owner, _tokens[i].toUint256(), _tokens[i], _amounts[i]);
_withdraw(_owner, _tokens[i].toUint256(), _tokens[i], _amounts[i], _withdrawer);

unchecked {
++i;
Expand Down Expand Up @@ -310,14 +313,16 @@ contract SplitsWarehouse is ERC6909X {
/* INTERNAL/PRIVATE */
/* -------------------------------------------------------------------------- */

function _withdraw(address _owner, uint256 _id, address _token, uint256 _amount) internal {
function _withdraw(address _owner, uint256 _id, address _token, uint256 _amount, address _withdrawer) internal {
_burn(_owner, _id, _amount);

if (_token == NATIVE_TOKEN) {
payable(_owner).sendValue(_amount);
} else {
IERC20(_token).safeTransfer(_owner, _amount);
}

emit Withdraw(_owner, _token, _withdrawer, _amount, 0);
}

function _withdraw(
Expand All @@ -332,12 +337,16 @@ contract SplitsWarehouse is ERC6909X {
{
_burn(_owner, _id, _amount);

uint256 amount = _amount - _reward;

if (_token == NATIVE_TOKEN) {
payable(_owner).sendValue(_amount - _reward);
payable(_owner).sendValue(amount);
payable(_withdrawer).sendValue(_reward);
} else {
IERC20(_token).safeTransfer(_owner, _amount - _reward);
IERC20(_token).safeTransfer(_owner, amount);
IERC20(_token).safeTransfer(_withdrawer, _reward);
}

emit Withdraw(_owner, _token, _withdrawer, amount, _reward);
}
}
24 changes: 24 additions & 0 deletions packages/splits-v2/test/warehouse/SplitsWarehouse.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {
error FailedInnerCall();
error CastOverflow(uint256 value);

event Withdraw(
address indexed owner, address indexed token, address indexed withdrawer, uint256 amount, uint256 reward
);

address public token;
address[] public defaultTokens;

Expand Down Expand Up @@ -146,6 +150,8 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {
testFuzz_depositSingleOwner_whenERC20(_owner, _owner, _amount);

vm.prank(_owner);
vm.expectEmit();
emit Withdraw(_owner, token, _owner, _amount, 0);
warehouse.withdraw(token, _amount);

assertEq(warehouse.balanceOf(_owner, tokenToId(token)), 0);
Expand All @@ -158,6 +164,8 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {
testFuzz_depositSingleOwner_whenNativeToken(_owner, _owner, _amount);

vm.prank(_owner);
vm.expectEmit();
emit Withdraw(_owner, native, _owner, _amount, 0);
warehouse.withdraw(native, _amount);

assertEq(warehouse.balanceOf(_owner, tokenToId(native)), 0);
Expand Down Expand Up @@ -202,6 +210,10 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {
depositDefaultTokens(owner, _amount);

vm.prank(owner);
vm.expectEmit();
for (uint256 i = 0; i < defaultTokens.length; i++) {
emit Withdraw(owner, defaultTokens[i], owner, _amount, 0);
}
warehouse.withdraw(defaultTokens, getAmounts(_amount));

for (uint256 i = 0; i < defaultTokens.length; i++) {
Expand Down Expand Up @@ -253,6 +265,8 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

testFuzz_depositSingleOwner_whenERC20(_owner, _owner, _amount);

vm.expectEmit();
emit Withdraw(_owner, token, address(this), _amount, 0);
warehouse.withdraw(_owner, token, _amount, address(this));

assertEq(warehouse.balanceOf(_owner, tokenToId(token)), 0);
Expand All @@ -265,6 +279,8 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

testFuzz_depositSingleOwner_whenNativeToken(_owner, _owner, _amount);

vm.expectEmit();
emit Withdraw(_owner, native, address(this), _amount, 0);
warehouse.withdraw(_owner, native, _amount, address(this));

assertEq(warehouse.balanceOf(_owner, tokenToId(native)), 0);
Expand Down Expand Up @@ -335,6 +351,8 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

uint256 reward = uint256(_amount) * uint256(_incentive) / warehouse.PERCENTAGE_SCALE();

vm.expectEmit();
emit Withdraw(_owner, token, _withdrawer, _amount - reward, reward);
warehouse.withdraw(_owner, token, _amount, _withdrawer);

assertEq(warehouse.balanceOf(_owner, tokenToId(token)), 0);
Expand All @@ -361,6 +379,8 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

uint256 reward = uint256(_amount) * uint256(_incentive) / warehouse.PERCENTAGE_SCALE();

vm.expectEmit();
emit Withdraw(_owner, native, _withdrawer, _amount - reward, reward);
warehouse.withdraw(_owner, native, _amount, _withdrawer);

assertEq(warehouse.balanceOf(_owner, tokenToId(native)), 0);
Expand All @@ -378,6 +398,10 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

depositDefaultTokens(_owner, _amount);

vm.expectEmit();
for (uint256 i = 0; i < defaultTokens.length; i++) {
emit Withdraw(_owner, defaultTokens[i], address(this), _amount, 0);
}
warehouse.withdraw(_owner, defaultTokens, getAmounts(_amount), address(this));

for (uint256 i = 0; i < defaultTokens.length; i++) {
Expand Down

0 comments on commit 09dbb2b

Please sign in to comment.