Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
More tests for UniswapV2Bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
xianny committed Jun 2, 2020
1 parent 46a8c65 commit c375398
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 134 deletions.
21 changes: 11 additions & 10 deletions contracts/asset-proxy/contracts/src/bridges/UniswapV2Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,19 @@ contract UniswapV2Bridge is
// Get our balance of `fromTokenAddress` token.
state.fromTokenBalance = IERC20Token(state.fromTokenAddress).balanceOf(address(this));

// // Grant the Uniswap router an allowance. // FIXME: REVERTING
// LibERC20Token.approve(
// state.fromTokenAddress,
// _getUniswapV2Router01Address(),
// // state.fromTokenBalance
// uint256(-1)
// );
require(state.fromTokenBalance > 0, 'must have balance');

// Grant the Uniswap router an allowance.
LibERC20Token.approve(
state.fromTokenAddress,
_getUniswapV2Router01Address(),
state.fromTokenBalance
);

// Convert directly from fromTokenAddress to toTokenAddress
address[] memory path = new address[](2);
path = path.append(state.fromTokenAddress);
path = path.append(toTokenAddress);
path[0] = state.fromTokenAddress;
path[1] = toTokenAddress;

// Buy as much `toTokenAddress` token with `fromTokenAddress` token
// and transfer it to `to`.
Expand All @@ -107,7 +108,7 @@ contract UniswapV2Bridge is
block.timestamp
);

state.boughtAmount = amounts[1];
state.boughtAmount = amounts[0];

emit ERC20BridgeTransfer(
// input token
Expand Down
178 changes: 98 additions & 80 deletions contracts/asset-proxy/contracts/test/TestUniswapV2Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ import "@0x/contracts-utils/contracts/src/LibAddressArray.sol";
import "../src/bridges/UniswapV2Bridge.sol";
import "../src/interfaces/IUniswapV2Router01.sol";


/// @dev A minimalist ERC20/WETH token.
contract TestToken {

using LibSafeMath for uint256;

mapping (address => uint256) public balances;
string private _nextRevertReason;

contract TestEventsRaiser {

event TokenTransfer(
address token,
address from,
Expand All @@ -46,29 +39,73 @@ contract TestToken {
uint256 allowance
);

/// @dev Set the balance for `owner`.
function setBalance(address owner)
event SwapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address toTokenAddress,
address to,
uint deadline
);

function raiseTokenTransfer(
address from,
address to,
uint256 amount
)
external
payable
{
balances[owner] = msg.value;
emit TokenTransfer(
msg.sender,
from,
to,
amount
);
}

/// @dev Set the revert reason for `transfer()`,
/// `deposit()`, and `withdraw()`.
function setRevertReason(string calldata reason)
function raiseTokenApprove(address spender, uint256 allowance) external {
emit TokenApprove(spender, allowance);
}

function raiseSwapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address toTokenAddress,
address to,
uint deadline
) external
{
emit SwapExactTokensForTokens(
amountIn,
amountOutMin,
toTokenAddress,
to,
deadline
);
}
}

/// @dev A minimalist ERC20 token.
contract TestToken {

using LibSafeMath for uint256;

mapping (address => uint256) public balances;
string private _nextRevertReason;

/// @dev Set the balance for `owner`.
function setBalance(address owner, uint256 balance)
external
payable
{
_nextRevertReason = reason;
balances[owner] = balance;
}

/// @dev Just emits a TokenTransfer event on the caller
function transfer(address to, uint256 amount)
external
returns (bool)
{
_revertIfReasonExists();
emit TokenTransfer(msg.sender, msg.sender, to, amount);
TestEventsRaiser(msg.sender).raiseTokenTransfer(msg.sender, to, amount);
return true;
}

Expand All @@ -77,32 +114,10 @@ contract TestToken {
external
returns (bool)
{
emit TokenApprove(spender, allowance);
TestEventsRaiser(msg.sender).raiseTokenApprove(spender, allowance);
return true;
}

/// @dev `IWETH.deposit()` that increases balances and calls
/// `raiseWethDeposit()` on the caller.
function deposit()
external
payable
{
_revertIfReasonExists();
balances[msg.sender] += balances[msg.sender].safeAdd(msg.value);
// TestEventsRaiser(msg.sender).raiseWethDeposit(msg.value);
}

/// @dev `IWETH.withdraw()` that just reduces balances and calls
/// `raiseWethWithdraw()` on the caller.
function withdraw(uint256 amount)
external
{
_revertIfReasonExists();
balances[msg.sender] = balances[msg.sender].safeSub(amount);
msg.sender.transfer(amount);
// TestEventsRaiser(msg.sender).raiseWethWithdraw(amount);
}

function allowance(address, address) external view returns (uint256) {
return 0;
}
Expand All @@ -115,30 +130,20 @@ contract TestToken {
{
return balances[owner];
}

function _revertIfReasonExists()
private
view
{
if (bytes(_nextRevertReason).length != 0) {
revert(_nextRevertReason);
}
}
}


contract TestRouter is
IUniswapV2Router01
{
string private _nextRevertReason;

event TokenToTokenTransferInput(
address exchange,
uint256 tokensSold,
uint256 minTokensBought,
uint256 deadline,
address recipient,
address toTokenAddress
);
/// @dev Set the revert reason for `swapExactTokensForTokens`.
function setRevertReason(string calldata reason)
external
{
_nextRevertReason = reason;
}

function swapExactTokensForTokens(
uint amountIn,
Expand All @@ -148,32 +153,41 @@ contract TestRouter is
uint deadline
) external returns (uint[] memory amounts)
{
amounts = new uint[](2);
amounts[0] = amountIn;
// amounts[1] = address(this).balance;
amounts[1] = amountOutMin;
_revertIfReasonExists();

emit TokenToTokenTransferInput(
msg.sender,
amounts = new uint[](1);
amounts[0] = amountOutMin;

TestEventsRaiser(msg.sender).raiseSwapExactTokensForTokens(
// tokens sold
amountIn,
// tokens bought
amountOutMin,
// deadline
deadline,
// output token (toTokenAddress)
path[1],
// recipient
to,
// output token (toTokenAddress)
path[1]
// deadline
deadline
);
}

function _revertIfReasonExists()
private
view
{
if (bytes(_nextRevertReason).length != 0) {
revert(_nextRevertReason);
}
}

}


/// @dev UniswapV2Bridge overridden to mock tokens and Uniswap router
contract TestUniswapV2Bridge is
UniswapV2Bridge
UniswapV2Bridge,
TestEventsRaiser
{

// Token address to TestToken instance.
Expand All @@ -185,22 +199,27 @@ contract TestUniswapV2Bridge is
_testRouter = new TestRouter();
}

/// @dev Sets the balance of this contract for an existing token.
/// The wei attached will be the balance.
function setTokenBalance(address tokenAddress)
function getRouterAddress()
external
payable
view
returns (address)
{
TestToken token = _testTokens[tokenAddress];
token.deposit.value(msg.value)();
return address(_testRouter);
}

/// @dev Sets the revert reason for an existing token.
function setTokenRevertReason(address tokenAddress, string calldata revertReason)
function setRouterRevertReason(string calldata revertReason)
external
{
_testRouter.setRevertReason(revertReason);
}

/// @dev Sets the balance of this contract for an existing token.
/// The wei attached will be the balance.
function setTokenBalance(address tokenAddress, uint256 balance)
external
{
TestToken token = _testTokens[tokenAddress];
token.setRevertReason(revertReason);
token.setBalance(address(this), balance);
}

/// @dev Create a new token
Expand All @@ -209,7 +228,6 @@ contract TestUniswapV2Bridge is
address tokenAddress
)
external
payable
returns (TestToken token)
{
token = TestToken(tokenAddress);
Expand Down
Loading

0 comments on commit c375398

Please sign in to comment.