protocol/contracts/zero-ex/tests/transformers/AffiliateFeeTransformerTest.t.sol
Elena 9f30823d70
Migrate erc20-contracts to foundry (#664)
* Strip erc20 package of legacy nonsense and add foundry basics

* Make foundry build

* Remove obsoleted test/UntransferrableDummyERC20Token.sol contract

* Remove obsoleted ERC20 lib variant contracts

* Remove obsoleted DummyMultipleReturnERC20Token and DummyNoReturnERC20Token contracts

* Move test contract to dedicated folder
and remove obsoleted TypeScript contract wrappers

* Remove src/interfaces/IEtherToken.sol only used in
v3 staking which is being obsoleted [skip ci]

* Add foundry test for token

* Migrate ZRX token tests to foundry

* Fix paths to erc20 contracts

* Remove obsoleted references

* Pin erc20-contracts package on treasury

* Ignore foundry imports in link checker

* Run only forge tests for erc20 contracts

* Remove DummyERC20Token and its dependencies

* Merge IERC20TokenV06 and IERC20TokenV08
into range pragma to cover solidity 0.6.5 to 0.8.x

* Merge IEtherTokenV06 and IEtherTokenV08
into range pragma to cover solidity 0.6.5 to 0.8.x

* Migrate weth9 tests to foundry

* Upload code coverage for erc20 package

* Update changelog

* Fix review comments

Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com>

---------

Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com>
2023-02-19 20:04:24 +02:00

187 lines
6.9 KiB
Solidity

// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2023 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6.5;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/src/v06/WETH9V06.sol";
import "utils/BaseTest.sol";
import "../../contracts/src/transformers/AffiliateFeeTransformer.sol";
import "../../contracts/src/transformers/IERC20Transformer.sol";
contract AffiliateFeeTransformerTest is BaseTest {
address public owner = account1;
address public feeRecipient = account2;
WETH9V06 weth = new WETH9V06();
IERC20Token token1 = IERC20Token(address(weth));
AffiliateFeeTransformer target = new AffiliateFeeTransformer();
function setUp() public {
vm.deal(address(this), 1e19);
weth.deposit{value: 10}();
}
function test_affiliateFee() public {
// Send positive slippage to contract which executes AffiliateFeeTransformer
weth.transfer(address(target), 10);
uint256 affiliateFeeAmount = 1;
AffiliateFeeTransformer.TokenFee[] memory fees = new AffiliateFeeTransformer.TokenFee[](1);
fees[0] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(token1),
amount: affiliateFeeAmount,
recipient: payable(feeRecipient)
});
bytes4 result = target.transform(
IERC20Transformer.TransformContext({
sender: payable(address(this)),
recipient: payable(address(this)),
data: abi.encode(fees)
})
);
assertEq(token1.balanceOf(feeRecipient), affiliateFeeAmount);
assertEq(result, LibERC20Transformer.TRANSFORMER_SUCCESS);
}
function test_affiliateFee_entireBalance() public {
// Send positive slippage to contract which executes AffiliateFeeTransformer
weth.transfer(address(target), 10);
AffiliateFeeTransformer.TokenFee[] memory fees = new AffiliateFeeTransformer.TokenFee[](1);
fees[0] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(token1),
amount: uint256(-1),
recipient: payable(feeRecipient)
});
bytes4 result = target.transform(
IERC20Transformer.TransformContext({
sender: payable(address(this)),
recipient: payable(address(this)),
data: abi.encode(fees)
})
);
assertEq(token1.balanceOf(feeRecipient), 10);
assertEq(result, LibERC20Transformer.TRANSFORMER_SUCCESS);
}
function test_affiliateFee_multipleFees() public {
// Send positive slippage to contract which executes AffiliateFeeTransformer
weth.transfer(address(target), 10);
AffiliateFeeTransformer.TokenFee[] memory fees = new AffiliateFeeTransformer.TokenFee[](2);
fees[0] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(token1),
amount: uint256(1),
recipient: payable(feeRecipient)
});
fees[1] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(token1),
amount: uint256(1),
recipient: payable(feeRecipient)
});
bytes4 result = target.transform(
IERC20Transformer.TransformContext({
sender: payable(address(this)),
recipient: payable(address(this)),
data: abi.encode(fees)
})
);
assertEq(token1.balanceOf(feeRecipient), 2);
assertEq(result, LibERC20Transformer.TRANSFORMER_SUCCESS);
}
function test_affiliateFee_ethFee() public {
// Send positive slippage ETH to contract which executes AffiliateFeeTransformer
vm.deal(address(target), 1);
uint256 ethBalanceBefore = feeRecipient.balance;
AffiliateFeeTransformer.TokenFee[] memory fees = new AffiliateFeeTransformer.TokenFee[](1);
fees[0] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(LibERC20Transformer.ETH_TOKEN_ADDRESS),
amount: uint256(1),
recipient: payable(feeRecipient)
});
bytes4 result = target.transform(
IERC20Transformer.TransformContext({
sender: payable(address(this)),
recipient: payable(address(this)),
data: abi.encode(fees)
})
);
assertEq(feeRecipient.balance, ethBalanceBefore + 1);
assertEq(result, LibERC20Transformer.TRANSFORMER_SUCCESS);
}
function test_affiliateFee_multipleFeesEthToken() public {
// Send positive slippage to contract which executes AffiliateFeeTransformer
weth.transfer(address(target), 10);
vm.deal(address(target), 10);
uint256 ethBalanceBefore = feeRecipient.balance;
AffiliateFeeTransformer.TokenFee[] memory fees = new AffiliateFeeTransformer.TokenFee[](2);
fees[0] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(LibERC20Transformer.ETH_TOKEN_ADDRESS),
amount: uint256(1),
recipient: payable(feeRecipient)
});
fees[1] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(token1),
amount: uint256(1),
recipient: payable(feeRecipient)
});
bytes4 result = target.transform(
IERC20Transformer.TransformContext({
sender: payable(address(this)),
recipient: payable(address(this)),
data: abi.encode(fees)
})
);
assertEq(feeRecipient.balance, ethBalanceBefore + 1);
assertEq(token1.balanceOf(feeRecipient), 1);
assertEq(result, LibERC20Transformer.TRANSFORMER_SUCCESS);
}
function test_affiliateFee_zeroAmount() public {
// Send positive slippage to contract which executes AffiliateFeeTransformer
weth.transfer(address(target), 10);
AffiliateFeeTransformer.TokenFee[] memory fees = new AffiliateFeeTransformer.TokenFee[](1);
fees[0] = AffiliateFeeTransformer.TokenFee({
token: IERC20Token(token1),
amount: 0,
recipient: payable(feeRecipient)
});
bytes4 result = target.transform(
IERC20Transformer.TransformContext({
sender: payable(address(this)),
recipient: payable(address(this)),
data: abi.encode(fees)
})
);
assertEq(token1.balanceOf(feeRecipient), 0);
assertEq(result, LibERC20Transformer.TRANSFORMER_SUCCESS);
}
}