From 5cc53597de0d28ad0650cec1e42d32fdc2c8e830 Mon Sep 17 00:00:00 2001 From: Kyu Date: Wed, 12 Apr 2023 20:42:55 -0700 Subject: [PATCH] feat: Add Barter support to EthereumBridgeAdapter [LIT-981] (#703) --- .gitignore | 1 + contracts/zero-ex/CHANGELOG.json | 8 ++++ .../transformers/bridges/BridgeProtocols.sol | 1 + .../bridges/EthereumBridgeAdapter.sol | 7 ++++ .../bridges/mixins/MixinBarter.sol | 41 +++++++++++++++++++ packages/protocol-utils/CHANGELOG.json | 8 ++++ .../protocol-utils/src/transformer_utils.ts | 1 + 7 files changed, 67 insertions(+) create mode 100644 contracts/zero-ex/contracts/src/transformers/bridges/mixins/MixinBarter.sol diff --git a/.gitignore b/.gitignore index 565b6f4f58..9937ee084b 100644 --- a/.gitignore +++ b/.gitignore @@ -99,6 +99,7 @@ foundry-artifacts/ # foundry cache cache/ +foundry-cache/ #foundry output artifacts out/ diff --git a/contracts/zero-ex/CHANGELOG.json b/contracts/zero-ex/CHANGELOG.json index 674580eb49..422d7a0965 100644 --- a/contracts/zero-ex/CHANGELOG.json +++ b/contracts/zero-ex/CHANGELOG.json @@ -1,4 +1,12 @@ [ + { + "version": "0.40.0", + "changes": [ + { + "note": "Add Barter support on Ethereum" + } + ] + }, { "version": "0.39.2", "changes": [ diff --git a/contracts/zero-ex/contracts/src/transformers/bridges/BridgeProtocols.sol b/contracts/zero-ex/contracts/src/transformers/bridges/BridgeProtocols.sol index 4eaa1e41a7..9b68b76f6c 100644 --- a/contracts/zero-ex/contracts/src/transformers/bridges/BridgeProtocols.sol +++ b/contracts/zero-ex/contracts/src/transformers/bridges/BridgeProtocols.sol @@ -55,4 +55,5 @@ library BridgeProtocols { uint128 internal constant WOOFI = 31; uint128 internal constant AAVEV3 = 32; uint128 internal constant KYBERELASTIC = 33; + uint128 internal constant BARTER = 34; } diff --git a/contracts/zero-ex/contracts/src/transformers/bridges/EthereumBridgeAdapter.sol b/contracts/zero-ex/contracts/src/transformers/bridges/EthereumBridgeAdapter.sol index 9fc5be14ab..c5854b27b2 100644 --- a/contracts/zero-ex/contracts/src/transformers/bridges/EthereumBridgeAdapter.sol +++ b/contracts/zero-ex/contracts/src/transformers/bridges/EthereumBridgeAdapter.sol @@ -22,6 +22,7 @@ import "./mixins/MixinBalancer.sol"; import "./mixins/MixinBalancerV2Batch.sol"; import "./mixins/MixinBancor.sol"; import "./mixins/MixinBancorV3.sol"; +import "./mixins/MixinBarter.sol"; import "./mixins/MixinCompound.sol"; import "./mixins/MixinCurve.sol"; import "./mixins/MixinCurveV2.sol"; @@ -48,6 +49,7 @@ contract EthereumBridgeAdapter is MixinBalancerV2Batch, MixinBancor, MixinBancorV3, + MixinBarter, MixinCompound, MixinCurve, MixinCurveV2, @@ -197,6 +199,11 @@ contract EthereumBridgeAdapter is return (0, true); } boughtAmount = _tradeSynthetix(sellAmount, order.bridgeData); + } else if (protocolId == BridgeProtocols.BARTER) { + if (dryRun) { + return (0, true); + } + boughtAmount = _tradeBarter(sellToken, sellAmount, order.bridgeData); } else if (protocolId == BridgeProtocols.UNKNOWN) { if (dryRun) { return (0, true); diff --git a/contracts/zero-ex/contracts/src/transformers/bridges/mixins/MixinBarter.sol b/contracts/zero-ex/contracts/src/transformers/bridges/mixins/MixinBarter.sol new file mode 100644 index 0000000000..420cbb185f --- /dev/null +++ b/contracts/zero-ex/contracts/src/transformers/bridges/mixins/MixinBarter.sol @@ -0,0 +1,41 @@ +// 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/src/v06/LibERC20TokenV06.sol"; +import "@0x/contracts-erc20/src/IERC20Token.sol"; +import "@0x/contracts-utils/contracts/src/v06/errors/LibRichErrorsV06.sol"; + +contract MixinBarter { + using LibERC20TokenV06 for IERC20Token; + using LibRichErrorsV06 for bytes; + + function _tradeBarter( + IERC20Token sellToken, + uint256 sellAmount, + bytes memory bridgeData + ) internal returns (uint256 boughtAmount) { + (address barterRouter, bytes memory data) = abi.decode(bridgeData, (address, bytes)); + sellToken.approveIfBelow(barterRouter, sellAmount); + + (bool success, bytes memory resultData) = barterRouter.call(data); + if (!success) { + resultData.rrevert(); + } + + return abi.decode(resultData, (uint256)); + } +} diff --git a/packages/protocol-utils/CHANGELOG.json b/packages/protocol-utils/CHANGELOG.json index dfd0221691..82461d4b62 100644 --- a/packages/protocol-utils/CHANGELOG.json +++ b/packages/protocol-utils/CHANGELOG.json @@ -1,4 +1,12 @@ [ + { + "version": "11.19.0", + "changes": [ + { + "note": "Add Barter support" + } + ] + }, { "version": "11.18.2", "changes": [ diff --git a/packages/protocol-utils/src/transformer_utils.ts b/packages/protocol-utils/src/transformer_utils.ts index 4d3759533b..dc3cee9d89 100644 --- a/packages/protocol-utils/src/transformer_utils.ts +++ b/packages/protocol-utils/src/transformer_utils.ts @@ -164,6 +164,7 @@ export enum BridgeProtocol { WOOFi, AaveV3, KyberElastic, + Barter, } /**