From 9a97717e4c2b261e5f4c73f046529a16458285b7 Mon Sep 17 00:00:00 2001 From: Michael Zhu Date: Thu, 20 Feb 2020 20:13:39 -0800 Subject: [PATCH] address comments --- .../contracts/src/ERC20BridgeSampler.sol | 69 ++----------------- .../contracts/src/IERC20BridgeSampler.sol | 34 +++++++++ 2 files changed, 39 insertions(+), 64 deletions(-) diff --git a/contracts/erc20-bridge-sampler/contracts/src/ERC20BridgeSampler.sol b/contracts/erc20-bridge-sampler/contracts/src/ERC20BridgeSampler.sol index e91b140d23..559d3f3c63 100644 --- a/contracts/erc20-bridge-sampler/contracts/src/ERC20BridgeSampler.sol +++ b/contracts/erc20-bridge-sampler/contracts/src/ERC20BridgeSampler.sol @@ -48,7 +48,7 @@ contract ERC20BridgeSampler is /// @dev Base gas limit for Curve calls. Some Curves have multiple tokens /// So a reasonable ceil is 150k per token. Biggest Curve has 4 tokens. uint256 constant internal CURVE_CALL_GAS = 600e3; // 600k - /// @dev Defaukt gas limit for liquidity provider calls. + /// @dev Default gas limit for liquidity provider calls. uint256 constant internal DEFAULT_CALL_GAS = 200e3; // 200k /// @dev Call multiple public functions on this contract in a single transaction. @@ -435,7 +435,6 @@ contract ERC20BridgeSampler is } /// @dev Sample sell quotes from an arbitrary on-chain liquidity provider. - /// Calls the provider contract with the default gas limit (200k). /// @param providerAddress Address of the liquidity provider contract. /// @param takerToken Address of the taker token (what to sell). /// @param makerToken Address of the maker token (what to buy). @@ -451,41 +450,12 @@ contract ERC20BridgeSampler is public view returns (uint256[] memory makerTokenAmounts) - { - return sampleSellsFromLiquidityProvider( - providerAddress, - takerToken, - makerToken, - takerTokenAmounts, - DEFAULT_CALL_GAS - ); - } - - /// @dev Sample sell quotes from an arbitrary on-chain liquidity provider. - /// @param providerAddress Address of the liquidity provider contract. - /// @param takerToken Address of the taker token (what to sell). - /// @param makerToken Address of the maker token (what to buy). - /// @param takerTokenAmounts Taker token sell amount for each sample. - /// @param callGasLimit Gas limit to use for calls to the provider's - /// `getSellQuote` method. - /// @return makerTokenAmounts Maker amounts bought at each taker token - /// amount. - function sampleSellsFromLiquidityProvider( - address providerAddress, - address takerToken, - address makerToken, - uint256[] memory takerTokenAmounts, - uint256 callGasLimit - ) - public - view - returns (uint256[] memory makerTokenAmounts) { uint256 numSamples = takerTokenAmounts.length; makerTokenAmounts = new uint256[](numSamples); for (uint256 i = 0; i < numSamples; i++) { (bool didSucceed, bytes memory resultData) = - providerAddress.staticcall.gas(callGasLimit)( + providerAddress.staticcall.gas(DEFAULT_CALL_GAS)( abi.encodeWithSelector( ILiquidityProvider(0).getSellQuote.selector, takerToken, @@ -496,6 +466,7 @@ contract ERC20BridgeSampler is if (didSucceed) { buyAmount = abi.decode(resultData, (uint256)); } else { + // Exit early if the amount is too high for the liquidity provider to serve break; } makerTokenAmounts[i] = buyAmount; @@ -503,7 +474,6 @@ contract ERC20BridgeSampler is } /// @dev Sample buy quotes from an arbitrary on-chain liquidity provider. - /// Calls the provider contract with the default gas limit (200k). /// @param providerAddress Address of the liquidity provider contract. /// @param takerToken Address of the taker token (what to sell). /// @param makerToken Address of the maker token (what to buy). @@ -519,42 +489,12 @@ contract ERC20BridgeSampler is public view returns (uint256[] memory takerTokenAmounts) - { - return sampleBuysFromLiquidityProvider( - providerAddress, - takerToken, - makerToken, - makerTokenAmounts, - DEFAULT_CALL_GAS - ); - } - - /// @dev Sample buy quotes from an arbitrary on-chain liquidity provider. - /// Calls the provider contract with the default gas limit (200k). - /// @param providerAddress Address of the liquidity provider contract. - /// @param takerToken Address of the taker token (what to sell). - /// @param makerToken Address of the maker token (what to buy). - /// @param makerTokenAmounts Maker token buy amount for each sample. - /// @param callGasLimit Gas limit to use for calls to the provider's - /// `getBuyQuote` method. - /// @return takerTokenAmounts Taker amounts sold at each maker token - /// amount. - function sampleBuysFromLiquidityProvider( - address providerAddress, - address takerToken, - address makerToken, - uint256[] memory makerTokenAmounts, - uint256 callGasLimit - ) - public - view - returns (uint256[] memory takerTokenAmounts) { uint256 numSamples = makerTokenAmounts.length; takerTokenAmounts = new uint256[](numSamples); for (uint256 i = 0; i < numSamples; i++) { (bool didSucceed, bytes memory resultData) = - providerAddress.staticcall.gas(callGasLimit)( + providerAddress.staticcall.gas(DEFAULT_CALL_GAS)( abi.encodeWithSelector( ILiquidityProvider(0).getBuyQuote.selector, takerToken, @@ -565,6 +505,7 @@ contract ERC20BridgeSampler is if (didSucceed) { sellAmount = abi.decode(resultData, (uint256)); } else { + // Exit early if the amount is too high for the liquidity provider to serve break; } takerTokenAmounts[i] = sellAmount; diff --git a/contracts/erc20-bridge-sampler/contracts/src/IERC20BridgeSampler.sol b/contracts/erc20-bridge-sampler/contracts/src/IERC20BridgeSampler.sol index 5d0435aab2..ac64b455f8 100644 --- a/contracts/erc20-bridge-sampler/contracts/src/IERC20BridgeSampler.sol +++ b/contracts/erc20-bridge-sampler/contracts/src/IERC20BridgeSampler.sol @@ -149,4 +149,38 @@ interface IERC20BridgeSampler { external view returns (uint256[] memory makerTokenAmounts); + + /// @dev Sample sell quotes from an arbitrary on-chain liquidity provider. + /// @param providerAddress Address of the liquidity provider contract. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @param takerTokenAmounts Taker token sell amount for each sample. + /// @return makerTokenAmounts Maker amounts bought at each taker token + /// amount. + function sampleSellsFromLiquidityProvider( + address providerAddress, + address takerToken, + address makerToken, + uint256[] memory takerTokenAmounts + ) + public + view + returns (uint256[] memory makerTokenAmounts); + + /// @dev Sample buy quotes from an arbitrary on-chain liquidity provider. + /// @param providerAddress Address of the liquidity provider contract. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @param makerTokenAmounts Maker token buy amount for each sample. + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromLiquidityProvider( + address providerAddress, + address takerToken, + address makerToken, + uint256[] memory makerTokenAmounts + ) + public + view + returns (uint256[] memory takerTokenAmounts); }