Skip to content

Commit

Permalink
Merge pull request #43 from The-Poolz/issue-42
Browse files Browse the repository at this point in the history
updated multisender tests
  • Loading branch information
YouStillAlive authored Nov 8, 2022
2 parents 24dd7e5 + e039f71 commit 70dde29
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
42 changes: 38 additions & 4 deletions contracts/MultiSender/MultiSender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "poolz-helper-v2/contracts/Array.sol";
import "./MultiManageable.sol";

/// @title main multi transfer settings
/// @author The-Poolz contract team
contract MultiSender is MultiManageable {
event MultiTransferredERC20(
address token,
uint256 userCount,
uint256 totalAmount
);
event MultiTransferredETH(uint256 userCount, uint256 totalAmount);

constructor() {
UserLimit = 500;
}
Expand All @@ -17,6 +25,11 @@ contract MultiSender is MultiManageable {
_;
}

modifier notZeroLength(uint256 _length) {
require(_length != 0, "array can't be zero length");
_;
}

modifier checkUserLimit(uint256 _userLength) {
require(UserLimit >= _userLength, "Invalid user limit");
_;
Expand All @@ -30,37 +43,58 @@ contract MultiSender is MultiManageable {
payable
whenNotPaused
checkArrLength(_users.length, _balances.length)
notZeroLength(_users.length)
checkUserLimit(_users.length)
{
uint256 fee = _calcFee();
uint256 value = msg.value;
PayFee(fee);
if (fee > 0 && FeeToken == address(0)) value -= fee;
uint256 amount = Array.getArraySum(_balances);
require(
value >= Array.getArraySum(_balances),
"Insufficient eth value sent!"
value == amount,
string.concat(
"Reqired Amount=",
Strings.toString(amount),
" Fee=",
Strings.toString(fee)
)
);
for (uint256 i; i < _users.length; i++) {
_users[i].transfer(_balances[i]);
}
emit MultiTransferredETH(_users.length, amount);
}

function MultiSendERC20(
address _token,
address payable[] memory _users,
address[] memory _users,
uint256[] calldata _balances
)
public
payable
whenNotPaused
checkArrLength(_users.length, _balances.length)
notZeroLength(_users.length)
checkUserLimit(_users.length)
{
require(_token != address(0), "Invalid token address");
PayFee(_calcFee());
uint256 fee = _calcFee();
PayFee(fee);
if (FeeToken == address(0)) {
require(
msg.value == fee,
string.concat("Reqired ETH fee=", Strings.toString(fee))
);
}
for (uint256 i; i < _users.length; i++) {
IERC20(_token).transferFrom(msg.sender, _users[i], _balances[i]);
}
emit MultiTransferredERC20(
_token,
_users.length,
Array.getArraySum(_balances)
);
}

function _calcFee() internal returns (uint256) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"poolz-whitelist": "^1.0.5",
"poolz-whitelist-converter": "^1.0.2",
"uniswap-v2-test": "^1.0.2",
"poolz-multi-sender": "1.0.2",
"poolz-multi-sender": "1.0.5",
"poolz-delay-vault": "1.0.2",
"poolz-multi-withdraw": "1.0.0"
},
Expand Down
36 changes: 32 additions & 4 deletions test/16_MultiSender.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,42 @@ contract("MultiSender and WhiteList integration tests", (accounts) => {

it("(multi ETH) pay with eth dicount", async () => {
await instance.SetFeeToken(constants.ZERO_ADDRESS)
await truffleAssert.reverts(
instance.MultiSendEth(accounts, amounts, { value: amount * amounts.length + discount }),
"Insufficient eth value sent!"
)
await truffleAssert.reverts(instance.MultiSendEth(accounts, amounts, {value: amount * amounts.length + discount}),
"Reqired Amount=" + amount * amounts.length + " Fee=" + amount)
await whiteList.AddAddress(whiteListId, [accounts[0]], [discount])
const oldEthBal = await web3.eth.getBalance(instance.address)
await instance.MultiSendEth(accounts, amounts, { value: amount * amounts.length + discount })
const ethBal = await web3.eth.getBalance(instance.address)
assert.equal(parseInt(oldEthBal) + discount, ethBal)
})

it('should pass MultiSendEth', async () => {
await truffleAssert.passes(instance.MultiSendEth(accounts, amounts, {value: amount * amounts.length + discount * 2}))
await instance.SetFeeAmount(0)
await truffleAssert.passes(instance.MultiSendEth(accounts, amounts, {value: amount * amounts.length}))
await instance.SetFeeToken(feeToken.address)
await truffleAssert.passes(instance.MultiSendEth(accounts, amounts, {value: amount * amounts.length}))
})

it("should pass MultiSendERC20", async () => {
await instance.SetFeeToken(constants.ZERO_ADDRESS)
await token.approve(instance.address, amount * amounts.length)
await truffleAssert.passes(instance.MultiSendERC20(token.address, accounts, amounts))
await instance.SetFeeAmount(amount)
await token.approve(instance.address, amount * amounts.length)
await truffleAssert.passes(instance.MultiSendERC20(token.address, accounts, amounts, {value: amount}))
})

it("should revert MultiSendERC20 when sending the wrong fee", async () => {
await truffleAssert.reverts(instance.MultiSendERC20(token.address, accounts, amounts, {value: amount + 1}),
"Reqired ETH fee=" + amount)
await truffleAssert.reverts(instance.MultiSendERC20(token.address, accounts, amounts, {value: amount - 1}), "Not Enough Fee Provided")
})

it("should revert MultiSendETH when sending the wrong fee", async () => {
await truffleAssert.reverts(instance.MultiSendEth(accounts, amounts, {value: amount * amounts.length + amount + 1}),
"Reqired Amount=" + amount * amounts.length + " Fee=" + amount)
await truffleAssert.reverts(instance.MultiSendEth(accounts, amounts, {value: amount * amounts.length + amount - 1}),
"Reqired Amount=" + amount * amounts.length + " Fee=" + amount)
})
})

0 comments on commit 70dde29

Please sign in to comment.