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

Eth2DaiBridge #2221

Merged
Merged

Conversation

dorothy-zbornak
Copy link
Contributor

@dorothy-zbornak dorothy-zbornak commented Sep 28, 2019

Description

Here's our first ever ERC20BridgeProxy (#2220) bridge: Eth2DaiBridge!.

Overview

This bridge contract is designed to be the makerAddress in all transactions, so:

  1. Taker tokens get transferred to this contract.
  2. ERC20BridgeProxy calls this contract, which:
    1. Tries to sell its entire WETH or DAI balance to Eth2Dai.
    2. Transfers everything it bought to the taker.

Because this contract should be the makerAddress of the order, it must sign for itself using the Wallet signature type. It implements the isValidSignature() callback but always succeeds.

Testing instructions

There are a whole 8 tests for this package. There really isn't much to unit test since the balance checks are enforced by the ERC20BridgeProxy and we just rely on Eth2Dai to do the actual conversion.

Types of changes

Checklist:

  • Prefix PR title with [WIP] if necessary.
  • Add tests to cover changes as needed.
  • Update documentation as needed.
  • Add new entries to the relevant CHANGELOG.jsons.

Relates to 0xProject/ZEIPs/issues/47

@coveralls
Copy link

coveralls commented Sep 28, 2019

Coverage Status

Coverage remained the same at 75.286% when pulling 65e1d0e on feat/erc20-bridge/eth2dai into 6da48be on feat/erc20-bridge/hello-world.

@dorothy-zbornak dorothy-zbornak marked this pull request as ready for review September 28, 2019 06:05
@dorothy-zbornak dorothy-zbornak changed the title [WIP] Eth2DaiBridge Eth2DaiBridge Sep 28, 2019
@dorothy-zbornak dorothy-zbornak force-pushed the feat/erc20-bridge/eth2dai branch from b1098ca to 6355a8a Compare September 30, 2019 01:59
Copy link
Member

@abandeali1 abandeali1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on creating a new integrations package and moving this there? This can be used for all external integrations, and also provides a place for us to write end-to-end integration tests for the Exchange/Staking/Forwarder/Coordinator. CC @jalextowle

ERC20Bridge,
IWallet
{
bytes4 private constant LEGACY_WALLET_MAGIC_VALUE = 0xb0671381;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just include this in IWallet.sol and inherit that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interfaces can't have/expose constants though, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abstract contracts can though :trollface:, which is what we already use for most interfaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, IWallet is actually a contract.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up just making a copy in asset-proxy since it would be a circular dep.

bytes4 private constant LEGACY_WALLET_MAGIC_VALUE = 0xb0671381;
/* Mainnet addresses */
address constant public ETH2DAI_ADDRESS = 0x39755357759cE0d7f32dC8dC45414CCa409AE24e;
address constant public WETH_ADDRESS = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
Copy link
Member

@abandeali1 abandeali1 Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we don't have to hard code any token addresses. It looks like Eth2Dai supports other token pairs in theory, and those should be able to be used as well. The fromToken can probably be encoded in the bridgeData.

amount
);
// Transfer the converted `toToken`s to `to`.
toToken.transfer(to, boughtAmount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably check the return value here, just in case a token isn't 100% ERC20 compliant.

Copy link
Contributor

@jalextowle jalextowle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dorothy-zbornak dorothy-zbornak force-pushed the feat/erc20-bridge/eth2dai branch from 6355a8a to 729a8b5 Compare October 1, 2019 00:13
)
private
{
mapping (address => bool) storage spenderHasAllowance = _hasAllowance[spender];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the allowance decreases to less than the required amount, but this is still set to true?

I think that this check actually just ends up being an extra expense most of the time. I'd prefer just calling IERC20Token(tokenAddress).approve(spender, uint256(-1)) every single time. If an allowance is already set, then this won't cost much extra gas at all. This also allows us to remove the _hasAllowance mapping altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with this.


contract IWallet {

bytes4 internal constant LEGACY_WALLET_MAGIC_VALUE = 0xb0671381;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can't import this due to circular dependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. The alternative is to have exchange import this file from the asset-proxy package, but that just feels wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably create a task for moving this to exchange-libs, but doesn't have to be in this PR.

@dorothy-zbornak dorothy-zbornak mentioned this pull request Oct 2, 2019
4 tasks
Copy link
Member

@abandeali1 abandeali1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more small nit, then LGTM!


IEth2Dai exchange = _getEth2DaiContract();
// Grant an allowance to the exchange to spend `fromTokenAddress` token.
_grantAllowanceForToken(address(exchange), fromTokenAddress);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can probably just remove this function. It's pretty simple, and we also use IERC20Token(fromTokenAddress).balanceOf below.

@dorothy-zbornak dorothy-zbornak merged commit 96aa149 into feat/erc20-bridge/hello-world Oct 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants