Skip to content

feat: Add Clober mixin to Ethereum/Polygon/ArbitrumBridge Adapters #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/test-utils/src/web3_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const txDefaults = {
export const providerConfigs: Web3Config = {
total_accounts: constants.NUM_TEST_ACCOUNTS,
shouldUseInProcessGanache: true,
shouldUseFakeGasEstimate: false,
shouldAllowUnlimitedContractSize: true,
hardfork: 'istanbul',
gasLimit: 100e6,
Expand Down
9 changes: 9 additions & 0 deletions contracts/zero-ex/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"timestamp": 1676384530,
"version": "0.38.7",
"changes": [
{
"note": "Add support for Clober to bridge adapter"
}
]
},
{
"timestamp": 1675210931,
"version": "0.38.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import "./mixins/MixinUniswapV3.sol";
import "./mixins/MixinUniswapV2.sol";
import "./mixins/MixinWOOFi.sol";
import "./mixins/MixinZeroExBridge.sol";
import "./mixins/MixinClober.sol";

contract ArbitrumBridgeAdapter is
AbstractBridgeAdapter(42161, "Arbitrum"),
MixinAaveV3,
MixinBalancerV2Batch,
MixinClober,
MixinCurve,
MixinCurveV2,
MixinDodoV2,
Expand All @@ -45,7 +47,7 @@ contract ArbitrumBridgeAdapter is
MixinWOOFi,
MixinZeroExBridge
{
constructor(IEtherTokenV06 weth) public MixinCurve(weth) MixinAaveV3(true) {}
constructor(IEtherTokenV06 weth) public MixinClober(weth) MixinCurve(weth) MixinAaveV3(true) {}

function _trade(
BridgeOrder memory order,
Expand Down Expand Up @@ -115,6 +117,11 @@ contract ArbitrumBridgeAdapter is
return (0, true);
}
boughtAmount = _tradeWOOFi(sellToken, buyToken, sellAmount, order.bridgeData);
} else if (protocolId == BridgeProtocols.CLOBER) {
if (dryRun) {
return (0, true);
}
boughtAmount = _tradeClober(sellToken, buyToken, sellAmount, order.bridgeData);
}

emit BridgeFill(order.source, sellToken, buyToken, sellAmount, boughtAmount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ library BridgeProtocols {
uint128 internal constant SYNTHETIX = 30;
uint128 internal constant WOOFI = 31;
uint128 internal constant AAVEV3 = 32;
uint128 internal constant CLOBER = 33;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import "./mixins/MixinUniswap.sol";
import "./mixins/MixinUniswapV2.sol";
import "./mixins/MixinUniswapV3.sol";
import "./mixins/MixinZeroExBridge.sol";
import "./mixins/MixinClober.sol";

contract EthereumBridgeAdapter is
AbstractBridgeAdapter(1, "Ethereum"),
Expand All @@ -47,6 +48,7 @@ contract EthereumBridgeAdapter is
MixinBalancerV2Batch,
MixinBancor,
MixinBancorV3,
MixinClober,
MixinCompound,
MixinCurve,
MixinCurveV2,
Expand All @@ -71,6 +73,7 @@ contract EthereumBridgeAdapter is
public
MixinBancor(weth)
MixinBancorV3(weth)
MixinClober(weth)
MixinCompound(weth)
MixinCurve(weth)
MixinLido(weth)
Expand Down Expand Up @@ -195,6 +198,11 @@ contract EthereumBridgeAdapter is
return (0, true);
}
boughtAmount = _tradeZeroExBridge(sellToken, buyToken, sellAmount, order.bridgeData);
} else if (protocolId == BridgeProtocols.CLOBER) {
if (dryRun) {
return (0, true);
}
boughtAmount = _tradeClober(sellToken, buyToken, sellAmount, order.bridgeData);
}

emit BridgeFill(order.source, sellToken, buyToken, sellAmount, boughtAmount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ import "./mixins/MixinUniswapV2.sol";
import "./mixins/MixinUniswapV3.sol";
import "./mixins/MixinWOOFi.sol";
import "./mixins/MixinZeroExBridge.sol";
import "./mixins/MixinClober.sol";

contract PolygonBridgeAdapter is
AbstractBridgeAdapter(137, "Polygon"),
MixinAaveV3,
MixinAaveV2,
MixinBalancerV2Batch,
MixinClober,
MixinCurve,
MixinCurveV2,
MixinDodo,
Expand All @@ -51,7 +53,7 @@ contract PolygonBridgeAdapter is
MixinWOOFi,
MixinZeroExBridge
{
constructor(IEtherTokenV06 weth) public MixinCurve(weth) MixinAaveV3(false) {}
constructor(IEtherTokenV06 weth) public MixinClober(weth) MixinCurve(weth) MixinAaveV3(false) {}

function _trade(
BridgeOrder memory order,
Expand Down Expand Up @@ -136,6 +138,11 @@ contract PolygonBridgeAdapter is
return (0, true);
}
boughtAmount = _tradeAaveV3(sellToken, buyToken, sellAmount, order.bridgeData);
} else if (protocolId == BridgeProtocols.CLOBER) {
if (dryRun) {
return (0, true);
}
boughtAmount = _tradeClober(sellToken, buyToken, sellAmount, order.bridgeData);
}

emit BridgeFill(order.source, sellToken, buyToken, sellAmount, boughtAmount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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/contracts/src/v06/LibERC20TokenV06.sol";
import "@0x/contracts-erc20/contracts/src/v06/IERC20TokenV06.sol";
import "@0x/contracts-erc20/contracts/src/v06/IEtherTokenV06.sol";
import "@0x/contracts-utils/contracts/src/v06/LibSafeMathV06.sol";

interface ICloberOrderBook {
function quoteToRaw(uint256 quoteAmount, bool roundingUp) external view returns (uint64 rawAmount);
}

interface ICloberRouter {
struct MarketOrderParams {
address market;
uint64 deadline;
address user;
uint16 limitPriceIndex;
uint64 rawAmount;
bool expendInput;
bool useNative;
uint256 baseAmount;
}

function marketBid(MarketOrderParams calldata params) external payable;

function marketAsk(MarketOrderParams calldata params) external payable;
}

contract MixinClober {
using LibERC20TokenV06 for IERC20TokenV06;
using LibSafeMathV06 for uint256;

IEtherTokenV06 private immutable WETH;

constructor(IEtherTokenV06 weth) public {
WETH = weth;
}

function _tradeClober(
IERC20TokenV06 sellToken,
IERC20TokenV06 buyToken,
uint256 sellAmount,
bytes memory bridgeData
) internal returns (uint256 boughtAmount) {
(ICloberRouter router, ICloberOrderBook market, bool isTakingBid) = abi.decode(
bridgeData,
(ICloberRouter, ICloberOrderBook, bool)
);

// Grant the Clober router an allowance to sell the sell token.
sellToken.approveIfBelow(address(router), sellAmount);

bool useNative = address(sellToken) == address(WETH) ? true : false;
uint256 beforeBalance = buyToken.balanceOf(address(this));
if (isTakingBid) {
router.marketAsk(
ICloberRouter.MarketOrderParams({
market: address(market),
deadline: uint64(block.timestamp + 100),
user: msg.sender,
limitPriceIndex: 0,
rawAmount: 0,
expendInput: true,
useNative: useNative,
baseAmount: sellAmount
})
);
} else {
router.marketBid(
ICloberRouter.MarketOrderParams({
market: address(market),
deadline: uint64(block.timestamp + 100),
user: msg.sender,
limitPriceIndex: type(uint16).max,
rawAmount: market.quoteToRaw(sellAmount, true),
expendInput: true,
useNative: useNative,
baseAmount: 0
})
);
}
return buyToken.balanceOf(address(this)).safeSub(beforeBalance);
}
}
1 change: 1 addition & 0 deletions packages/protocol-utils/src/transformer_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export enum BridgeProtocol {
Synthetix,
WOOFi,
AaveV3,
Clober
}

/**
Expand Down