-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Split up BridgeAdapter by chain * Fix BridgeProtocols enum * Add isSupportedSource to bridge adapter contracts * Add bridge adapter tests * Fix FQT test
- Loading branch information
1 parent
2d16f83
commit cf8fc0f
Showing
23 changed files
with
1,296 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
contracts/zero-ex/contracts/src/transformers/bridges/AbstractBridgeAdapter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* | ||
Copyright 2022 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; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "./IBridgeAdapter.sol"; | ||
|
||
abstract contract AbstractBridgeAdapter is IBridgeAdapter { | ||
|
||
constructor( | ||
uint256 expectedChainId, | ||
string memory expectedChainName | ||
) | ||
public | ||
{ | ||
uint256 chainId; | ||
assembly { chainId := chainid() } | ||
// Allow testing on Ganache | ||
if (chainId != expectedChainId && chainId != 1337) { | ||
revert(string(abi.encodePacked(expectedChainName, "BridgeAdapter.constructor: wrong chain ID"))); | ||
} | ||
} | ||
|
||
function isSupportedSource(bytes32 source) | ||
external | ||
override | ||
returns (bool isSupported) | ||
{ | ||
BridgeOrder memory placeholderOrder; | ||
placeholderOrder.source = source; | ||
IERC20TokenV06 placeholderToken = IERC20TokenV06(address(0)); | ||
|
||
(, isSupported) = _trade( | ||
placeholderOrder, | ||
placeholderToken, | ||
placeholderToken, | ||
0, | ||
true | ||
); | ||
} | ||
|
||
function trade( | ||
BridgeOrder memory order, | ||
IERC20TokenV06 sellToken, | ||
IERC20TokenV06 buyToken, | ||
uint256 sellAmount | ||
) | ||
public | ||
override | ||
returns (uint256 boughtAmount) | ||
{ | ||
(boughtAmount, ) = _trade( | ||
order, | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
false | ||
); | ||
} | ||
|
||
function _trade( | ||
BridgeOrder memory order, | ||
IERC20TokenV06 sellToken, | ||
IERC20TokenV06 buyToken, | ||
uint256 sellAmount, | ||
bool dryRun | ||
) | ||
internal | ||
virtual | ||
returns (uint256 boughtAmount, bool supportedSource); | ||
} |
141 changes: 141 additions & 0 deletions
141
contracts/zero-ex/contracts/src/transformers/bridges/AvalancheBridgeAdapter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* | ||
Copyright 2022 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 "./AbstractBridgeAdapter.sol"; | ||
import "./BridgeProtocols.sol"; | ||
import "./mixins/MixinCurve.sol"; | ||
import "./mixins/MixinCurveV2.sol"; | ||
import "./mixins/MixinGMX.sol"; | ||
import "./mixins/MixinKyberDmm.sol"; | ||
import "./mixins/MixinAaveV2.sol"; | ||
import "./mixins/MixinNerve.sol"; | ||
import "./mixins/MixinPlatypus.sol"; | ||
import "./mixins/MixinUniswapV2.sol"; | ||
import "./mixins/MixinZeroExBridge.sol"; | ||
|
||
contract AvalancheBridgeAdapter is | ||
AbstractBridgeAdapter(43114, "Avalanche"), | ||
MixinCurve, | ||
MixinCurveV2, | ||
MixinGMX, | ||
MixinKyberDmm, | ||
MixinAaveV2, | ||
MixinNerve, | ||
MixinPlatypus, | ||
MixinUniswapV2, | ||
MixinZeroExBridge | ||
{ | ||
constructor(IEtherTokenV06 weth) | ||
public | ||
MixinCurve(weth) | ||
{} | ||
|
||
function _trade( | ||
BridgeOrder memory order, | ||
IERC20TokenV06 sellToken, | ||
IERC20TokenV06 buyToken, | ||
uint256 sellAmount, | ||
bool dryRun | ||
) | ||
internal | ||
override | ||
returns (uint256 boughtAmount, bool supportedSource) | ||
{ | ||
uint128 protocolId = uint128(uint256(order.source) >> 128); | ||
if (protocolId == BridgeProtocols.CURVE) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeCurve( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.CURVEV2) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeCurveV2( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.UNISWAPV2) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeUniswapV2( | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.NERVE) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeNerve( | ||
sellToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.KYBERDMM) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeKyberDmm( | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.AAVEV2) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeAaveV2( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.GMX) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeGMX( | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.PLATYPUS) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradePlatypus( | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.UNKNOWN) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeZeroExBridge( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} | ||
|
||
emit BridgeFill( | ||
order.source, | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
boughtAmount | ||
); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
contracts/zero-ex/contracts/src/transformers/bridges/BSCBridgeAdapter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* | ||
Copyright 2022 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 "./AbstractBridgeAdapter.sol"; | ||
import "./BridgeProtocols.sol"; | ||
import "./mixins/MixinCurve.sol"; | ||
import "./mixins/MixinDodo.sol"; | ||
import "./mixins/MixinDodoV2.sol"; | ||
import "./mixins/MixinKyberDmm.sol"; | ||
import "./mixins/MixinMooniswap.sol"; | ||
import "./mixins/MixinNerve.sol"; | ||
import "./mixins/MixinUniswapV2.sol"; | ||
import "./mixins/MixinZeroExBridge.sol"; | ||
|
||
contract BSCBridgeAdapter is | ||
AbstractBridgeAdapter(56, "BSC"), | ||
MixinCurve, | ||
MixinDodo, | ||
MixinDodoV2, | ||
MixinKyberDmm, | ||
MixinMooniswap, | ||
MixinNerve, | ||
MixinUniswapV2, | ||
MixinZeroExBridge | ||
{ | ||
constructor(IEtherTokenV06 weth) | ||
public | ||
MixinCurve(weth) | ||
MixinMooniswap(weth) | ||
{} | ||
|
||
function _trade( | ||
BridgeOrder memory order, | ||
IERC20TokenV06 sellToken, | ||
IERC20TokenV06 buyToken, | ||
uint256 sellAmount, | ||
bool dryRun | ||
) | ||
internal | ||
override | ||
returns (uint256 boughtAmount, bool supportedSource) | ||
{ | ||
uint128 protocolId = uint128(uint256(order.source) >> 128); | ||
if (protocolId == BridgeProtocols.CURVE) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeCurve( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.UNISWAPV2) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeUniswapV2( | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.MOONISWAP) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeMooniswap( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.DODO) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeDodo( | ||
sellToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.DODOV2) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeDodoV2( | ||
sellToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.NERVE) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeNerve( | ||
sellToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.KYBERDMM) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeKyberDmm( | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} else if (protocolId == BridgeProtocols.UNKNOWN) { | ||
if (dryRun) { return (0, true); } | ||
boughtAmount = _tradeZeroExBridge( | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
order.bridgeData | ||
); | ||
} | ||
|
||
emit BridgeFill( | ||
order.source, | ||
sellToken, | ||
buyToken, | ||
sellAmount, | ||
boughtAmount | ||
); | ||
} | ||
} |
Oops, something went wrong.