@0x/contracts-asset-proxy: Add Eth2DaiBridge and tests.
This commit is contained in:
102
contracts/asset-proxy/contracts/src/bridges/Eth2DaiBridge.sol
Normal file
102
contracts/asset-proxy/contracts/src/bridges/Eth2DaiBridge.sol
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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.5.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol";
|
||||
import "./ERC20Bridge.sol";
|
||||
import "../interfaces/IEth2Dai.sol";
|
||||
|
||||
|
||||
contract Eth2DaiBridge is
|
||||
ERC20Bridge
|
||||
{
|
||||
/* Mainnet addresses */
|
||||
address constant public ETH2DAI_ADDRESS = 0x39755357759cE0d7f32dC8dC45414CCa409AE24e;
|
||||
address constant public WETH_ADDRESS = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
|
||||
address constant public DAI_ADDRESS = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
|
||||
|
||||
constructor() public {
|
||||
// Grant the Eth2Dai contract unlimited weth and dai allowances.
|
||||
_getWethContract().approve(address(_getEth2DaiContract()), uint256(-1));
|
||||
_getDaiContract().approve(address(_getEth2DaiContract()), uint256(-1));
|
||||
}
|
||||
|
||||
// solhint-disable space-after-comma
|
||||
function transfer(
|
||||
bytes calldata /* bridgeData */,
|
||||
address toTokenAddress,
|
||||
address /* from */,
|
||||
address to,
|
||||
uint256 amount
|
||||
)
|
||||
external
|
||||
returns (bytes4 success)
|
||||
{
|
||||
// The "from" token is the opposite of the "to" token.
|
||||
IERC20Token fromToken = _getWethContract();
|
||||
IERC20Token toToken = _getDaiContract();
|
||||
// Swap them if necessary.
|
||||
if (toTokenAddress == address(fromToken)) {
|
||||
(fromToken, toToken) = (toToken, fromToken);
|
||||
} else {
|
||||
require(
|
||||
toTokenAddress == address(toToken),
|
||||
"INVALID_ETH2DAI_TOKEN"
|
||||
);
|
||||
}
|
||||
// Try to sell all of this contract's `fromToken` balance.
|
||||
uint256 boughtAmount = _getEth2DaiContract().sellAllAmount(
|
||||
address(fromToken),
|
||||
fromToken.balanceOf(address(this)),
|
||||
address(toToken),
|
||||
amount
|
||||
);
|
||||
// Transfer the converted `toToken`s to `to`.
|
||||
toToken.transfer(to, boughtAmount);
|
||||
return BRIDGE_SUCCESS;
|
||||
}
|
||||
|
||||
/// @dev Overridable way to get the weth contract.
|
||||
function _getWethContract()
|
||||
internal
|
||||
view
|
||||
returns (IERC20Token)
|
||||
{
|
||||
return IERC20Token(WETH_ADDRESS);
|
||||
}
|
||||
|
||||
/// @dev Overridable way to get the dai contract.
|
||||
function _getDaiContract()
|
||||
internal
|
||||
view
|
||||
returns (IERC20Token)
|
||||
{
|
||||
return IERC20Token(DAI_ADDRESS);
|
||||
}
|
||||
|
||||
/// @dev Overridable way to get the eth2dai contract.
|
||||
function _getEth2DaiContract()
|
||||
internal
|
||||
view
|
||||
returns (IEth2Dai)
|
||||
{
|
||||
return IEth2Dai(ETH2DAI_ADDRESS);
|
||||
}
|
||||
}
|
||||
32
contracts/asset-proxy/contracts/src/interfaces/IEth2Dai.sol
Normal file
32
contracts/asset-proxy/contracts/src/interfaces/IEth2Dai.sol
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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.5.9;
|
||||
|
||||
|
||||
// solhint-disable func-param-name-mixedcase
|
||||
interface IEth2Dai {
|
||||
function sellAllAmount(
|
||||
address pay_gem,
|
||||
uint256 pay_amt,
|
||||
address buy_gem,
|
||||
uint256 min_fill_amount
|
||||
)
|
||||
external
|
||||
returns (uint256 fill_amt);
|
||||
}
|
||||
197
contracts/asset-proxy/contracts/test/TestEth2DaiBridge.sol
Normal file
197
contracts/asset-proxy/contracts/test/TestEth2DaiBridge.sol
Normal file
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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.5.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol";
|
||||
import "../src/bridges/Eth2DaiBridge.sol";
|
||||
import "../src/interfaces/IEth2Dai.sol";
|
||||
|
||||
|
||||
// solhint-disable no-simple-event-func-name
|
||||
/// @dev Interface that allows `TestToken` to call `raiseTransferEvent` on
|
||||
/// the `TestEth2DaiBridge` contract.
|
||||
interface IRaiseTransferEvent {
|
||||
function raiseTransferEvent(
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
)
|
||||
external;
|
||||
}
|
||||
|
||||
|
||||
/// @dev A minimalist ERC20 token.
|
||||
contract TestToken {
|
||||
|
||||
mapping (address => uint256) public balances;
|
||||
mapping (address => mapping (address => uint256)) public allowances;
|
||||
|
||||
/// @dev Just calls `raiseTransferEvent()` on the caller.
|
||||
function transfer(address to, uint256 amount)
|
||||
external
|
||||
returns (bool)
|
||||
{
|
||||
IRaiseTransferEvent(msg.sender).raiseTransferEvent(msg.sender, to, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @dev Set the balance for `owner`.
|
||||
function setBalance(address owner, uint256 balance)
|
||||
external
|
||||
{
|
||||
balances[owner] = balance;
|
||||
}
|
||||
|
||||
/// @dev Records allowance values.
|
||||
function approve(address spender, uint256 allowance)
|
||||
external
|
||||
returns (bool)
|
||||
{
|
||||
allowances[msg.sender][spender] = allowance;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @dev Retrieve the balance for `owner`.
|
||||
function balanceOf(address owner)
|
||||
external
|
||||
view
|
||||
returns (uint256)
|
||||
{
|
||||
return balances[owner];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// @dev Eth2DaiBridge overridden to mock tokens and
|
||||
/// implement IEth2Dai.
|
||||
contract TestEth2DaiBridge is
|
||||
IEth2Dai,
|
||||
Eth2DaiBridge
|
||||
{
|
||||
event SellAllAmount(
|
||||
address sellToken,
|
||||
uint256 sellTokenAmount,
|
||||
address buyToken,
|
||||
uint256 minimumFillAmount
|
||||
);
|
||||
|
||||
event TokenTransfer(
|
||||
address token,
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
);
|
||||
|
||||
TestToken public wethToken = new TestToken();
|
||||
TestToken public daiToken = new TestToken();
|
||||
string private _nextRevertReason;
|
||||
uint256 private _nextFillAmount;
|
||||
|
||||
/// @dev Set token balances for this contract.
|
||||
function setTokenBalances(uint256 wethBalance, uint256 daiBalance)
|
||||
external
|
||||
{
|
||||
wethToken.setBalance(address(this), wethBalance);
|
||||
daiToken.setBalance(address(this), daiBalance);
|
||||
}
|
||||
|
||||
/// @dev Set the behavior for `IEth2Dai.sellAllAmount()`.
|
||||
function setFillBehavior(string calldata revertReason, uint256 fillAmount)
|
||||
external
|
||||
{
|
||||
_nextRevertReason = revertReason;
|
||||
_nextFillAmount = fillAmount;
|
||||
}
|
||||
|
||||
/// @dev Implementation of `IEth2Dai.sellAllAmount()`
|
||||
function sellAllAmount(
|
||||
address sellTokenAddress,
|
||||
uint256 sellTokenAmount,
|
||||
address buyTokenAddress,
|
||||
uint256 minimumFillAmount
|
||||
)
|
||||
external
|
||||
returns (uint256 fillAmount)
|
||||
{
|
||||
emit SellAllAmount(
|
||||
sellTokenAddress,
|
||||
sellTokenAmount,
|
||||
buyTokenAddress,
|
||||
minimumFillAmount
|
||||
);
|
||||
if (bytes(_nextRevertReason).length != 0) {
|
||||
revert(_nextRevertReason);
|
||||
}
|
||||
return _nextFillAmount;
|
||||
}
|
||||
|
||||
function raiseTransferEvent(
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
)
|
||||
external
|
||||
{
|
||||
emit TokenTransfer(
|
||||
msg.sender,
|
||||
from,
|
||||
to,
|
||||
amount
|
||||
);
|
||||
}
|
||||
|
||||
/// @dev Retrieves the allowances of the test tokens.
|
||||
function getEth2DaiTokenAllowances()
|
||||
external
|
||||
view
|
||||
returns (uint256 wethAllowance, uint256 daiAllowance)
|
||||
{
|
||||
wethAllowance = wethToken.allowances(address(this), address(this));
|
||||
daiAllowance = daiToken.allowances(address(this), address(this));
|
||||
return (wethAllowance, daiAllowance);
|
||||
}
|
||||
|
||||
// @dev Use `wethToken`.
|
||||
function _getWethContract()
|
||||
internal
|
||||
view
|
||||
returns (IERC20Token)
|
||||
{
|
||||
return IERC20Token(address(wethToken));
|
||||
}
|
||||
|
||||
// @dev Use `daiToken`.
|
||||
function _getDaiContract()
|
||||
internal
|
||||
view
|
||||
returns (IERC20Token)
|
||||
{
|
||||
return IERC20Token(address(daiToken));
|
||||
}
|
||||
|
||||
// @dev This contract will double as the Eth2Dai contract.
|
||||
function _getEth2DaiContract()
|
||||
internal
|
||||
view
|
||||
returns (IEth2Dai)
|
||||
{
|
||||
return IEth2Dai(address(this));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user