From 7c09549d645861dc7a8e894d3ec56e3a7660c049 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 25 Jan 2022 12:03:15 +1000 Subject: [PATCH] Update for buys --- .../contracts/src/BalancerSampler.sol | 27 +++- .../contracts/src/BalancerV2Sampler.sol | 26 +++- .../contracts/src/BancorSampler.sol | 7 +- .../contracts/src/CompoundSampler.sol | 21 ++- .../contracts/src/CurveSampler.sol | 27 +++- .../contracts/src/DODOSampler.sol | 29 +++- .../contracts/src/DODOV2Sampler.sol | 32 ++++- .../contracts/src/KyberDmmSampler.sol | 25 +++- .../contracts/src/KyberSampler.sol | 28 +++- .../contracts/src/LidoSampler.sol | 27 +++- .../src/LiquidityProviderSampler.sol | 27 +++- .../contracts/src/MStableSampler.sol | 27 +++- .../contracts/src/MakerPSMSampler.sol | 21 ++- .../contracts/src/MooniswapSampler.sol | 28 +++- .../contracts/src/ShellSampler.sol | 27 +++- .../contracts/src/SmoothySampler.sol | 27 +++- .../contracts/src/TwoHopSampler.sol | 12 +- .../contracts/src/UniswapSampler.sol | 26 +++- .../contracts/src/UniswapV2Sampler.sol | 24 +++- .../contracts/src/UniswapV3Sampler.sol | 27 +++- .../sampler_operations.ts | 126 +++++++++--------- 21 files changed, 514 insertions(+), 107 deletions(-) diff --git a/packages/asset-swapper/contracts/src/BalancerSampler.sol b/packages/asset-swapper/contracts/src/BalancerSampler.sol index 17693f8cdb..4ed50fb135 100644 --- a/packages/asset-swapper/contracts/src/BalancerSampler.sol +++ b/packages/asset-swapper/contracts/src/BalancerSampler.sol @@ -130,6 +130,29 @@ contract BalancerSampler is } } + /// @dev Sample buy quotes from Balancer. + /// @param poolAddress Address of the Balancer pool to query. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromBalancerGlobal( + address poolAddress, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromBalancer( + poolAddress, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Balancer. /// @param poolAddress Address of the Balancer pool to query. /// @param takerToken Address of the taker token (what to sell). @@ -137,13 +160,13 @@ contract BalancerSampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromBalancer( + function _sampleBuysFromBalancer( address poolAddress, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/BalancerV2Sampler.sol b/packages/asset-swapper/contracts/src/BalancerV2Sampler.sol index 03f78f316f..806bab9370 100644 --- a/packages/asset-swapper/contracts/src/BalancerV2Sampler.sol +++ b/packages/asset-swapper/contracts/src/BalancerV2Sampler.sol @@ -135,6 +135,28 @@ contract BalancerV2Sampler is } } + /// @dev Sample buy quotes from Balancer V2. + /// @param poolInfo Struct with pool related data + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromBalancerV2Global( + BalancerV2PoolInfo memory poolInfo, + address takerToken, + address makerToken + ) + public + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromBalancerV2( + poolInfo, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Balancer V2. /// @param poolInfo Struct with pool related data /// @param takerToken Address of the taker token (what to sell). @@ -142,13 +164,13 @@ contract BalancerV2Sampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromBalancerV2( + function _sampleBuysFromBalancerV2( BalancerV2PoolInfo memory poolInfo, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal returns (uint256[] memory takerTokenAmounts) { _assertValidPair(makerToken, takerToken); diff --git a/packages/asset-swapper/contracts/src/BancorSampler.sol b/packages/asset-swapper/contracts/src/BancorSampler.sol index 5bd52c6965..d106831a6a 100644 --- a/packages/asset-swapper/contracts/src/BancorSampler.sol +++ b/packages/asset-swapper/contracts/src/BancorSampler.sol @@ -112,21 +112,20 @@ contract BancorSampler is /// @param opts BancorSamplerOpts The Bancor registry contract address and paths /// @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 bancorNetwork the Bancor Network address /// @return path the selected conversion path from bancor /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromBancor( + function sampleBuysFromBancorGlobal( BancorSamplerOpts memory opts, address takerToken, - address makerToken, - uint256[] memory makerTokenAmounts + address makerToken ) public view returns (address bancorNetwork, address[] memory path, uint256[] memory takerTokenAmounts) { + takerTokenAmounts = new uint256[](SAMPLE_VALUES.length); } function _findBestPath( diff --git a/packages/asset-swapper/contracts/src/CompoundSampler.sol b/packages/asset-swapper/contracts/src/CompoundSampler.sol index 2822e8d015..7de55af154 100644 --- a/packages/asset-swapper/contracts/src/CompoundSampler.sol +++ b/packages/asset-swapper/contracts/src/CompoundSampler.sol @@ -86,13 +86,30 @@ contract CompoundSampler is } } - function sampleBuysFromCompound( + function sampleBuysFromCompoundGlobal( + ICToken cToken, + IERC20TokenV06 takerToken, + IERC20TokenV06 makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromCompound( + cToken, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + + function _sampleBuysFromCompound( ICToken cToken, IERC20TokenV06 takerToken, IERC20TokenV06 makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/CurveSampler.sol b/packages/asset-swapper/contracts/src/CurveSampler.sol index 53446fce0a..9160d61343 100644 --- a/packages/asset-swapper/contracts/src/CurveSampler.sol +++ b/packages/asset-swapper/contracts/src/CurveSampler.sol @@ -105,6 +105,29 @@ contract CurveSampler is } } + /// @dev Sample buy quotes from Curve. + /// @param curveInfo Curve information specific to this token pair. + /// @param fromTokenIdx Index of the taker token (what to sell). + /// @param toTokenIdx Index of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromCurveGlobal( + CurveInfo memory curveInfo, + int128 fromTokenIdx, + int128 toTokenIdx + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromCurve( + curveInfo, + fromTokenIdx, + toTokenIdx, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Curve. /// @param curveInfo Curve information specific to this token pair. /// @param fromTokenIdx Index of the taker token (what to sell). @@ -112,13 +135,13 @@ contract CurveSampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromCurve( + function _sampleBuysFromCurve( CurveInfo memory curveInfo, int128 fromTokenIdx, int128 toTokenIdx, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/DODOSampler.sol b/packages/asset-swapper/contracts/src/DODOSampler.sol index bad6655266..fc76cd41bf 100644 --- a/packages/asset-swapper/contracts/src/DODOSampler.sol +++ b/packages/asset-swapper/contracts/src/DODOSampler.sol @@ -134,6 +134,31 @@ contract DODOSampler is } } + /// @dev Sample buy quotes from DODO. + /// @param opts DODOSamplerOpts DODO Registry and helper addresses + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return sellBase whether the bridge needs to sell the base token + /// @return pool the DODO pool address + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromDODOGlobal( + DODOSamplerOpts memory opts, + address takerToken, + address makerToken + ) + public + view + returns (bool sellBase, address pool, uint256[] memory takerTokenAmounts) + { + (sellBase, pool, takerTokenAmounts) = _sampleBuysFromDODO( + opts, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from DODO. /// @param opts DODOSamplerOpts DODO Registry and helper addresses /// @param takerToken Address of the taker token (what to sell). @@ -143,13 +168,13 @@ contract DODOSampler is /// @return pool the DODO pool address /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromDODO( + function _sampleBuysFromDODO( DODOSamplerOpts memory opts, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (bool sellBase, address pool, uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/DODOV2Sampler.sol b/packages/asset-swapper/contracts/src/DODOV2Sampler.sol index 8ed2680886..f0863fc2fc 100644 --- a/packages/asset-swapper/contracts/src/DODOV2Sampler.sol +++ b/packages/asset-swapper/contracts/src/DODOV2Sampler.sol @@ -125,6 +125,34 @@ contract DODOV2Sampler is } } + /// @dev Sample buy quotes from DODO. + /// @param registry Address of the registry to look up. + /// @param offset offset index for the pool in the registry. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return sellBase whether the bridge needs to sell the base token + /// @return pool the DODO pool address + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromDODOV2Global( + address registry, + uint256 offset, + address takerToken, + address makerToken + ) + public + view + returns (bool sellBase, address pool, uint256[] memory takerTokenAmounts) + { + (sellBase, pool, takerTokenAmounts) = _sampleBuysFromDODOV2( + registry, + offset, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from DODO. /// @param registry Address of the registry to look up. /// @param offset offset index for the pool in the registry. @@ -135,14 +163,14 @@ contract DODOV2Sampler is /// @return pool the DODO pool address /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromDODOV2( + function _sampleBuysFromDODOV2( address registry, uint256 offset, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (bool sellBase, address pool, uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/KyberDmmSampler.sol b/packages/asset-swapper/contracts/src/KyberDmmSampler.sol index a60ff16d44..45e00dd89d 100644 --- a/packages/asset-swapper/contracts/src/KyberDmmSampler.sol +++ b/packages/asset-swapper/contracts/src/KyberDmmSampler.sol @@ -122,6 +122,27 @@ contract KyberDmmSampler is } } + /// @dev Sample buy quotes from KyberDmm. + /// @param router Router to look up tokens and amounts + /// @param path Token route. Should be takerToken -> makerToken. + /// @return pools The pool addresses involved in the multi path trade + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromKyberDmmGlobal( + address router, + address[] memory path + ) + public + view + returns (address[] memory pools, uint256[] memory takerTokenAmounts) + { + (pools, takerTokenAmounts) = _sampleBuysFromKyberDmm( + router, + path, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from KyberDmm. /// @param router Router to look up tokens and amounts /// @param path Token route. Should be takerToken -> makerToken. @@ -129,12 +150,12 @@ contract KyberDmmSampler is /// @return pools The pool addresses involved in the multi path trade /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromKyberDmm( + function _sampleBuysFromKyberDmm( address router, address[] memory path, uint256[] memory makerTokenAmounts ) - public + internal view returns (address[] memory pools, uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/KyberSampler.sol b/packages/asset-swapper/contracts/src/KyberSampler.sol index 39af643cef..27997e9c28 100644 --- a/packages/asset-swapper/contracts/src/KyberSampler.sol +++ b/packages/asset-swapper/contracts/src/KyberSampler.sol @@ -111,6 +111,30 @@ contract KyberSampler is } } + /// @dev Sample buy quotes from Kyber. + /// @param opts KyberSamplerOpts The nth reserve + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return reserveId The id of the reserve found at reserveOffset + /// @return hint The hint for the selected reserve + /// @return takerTokenAmounts Taker amounts sold at each maker token amount. + function sampleBuysFromKyberNetworkGlobal( + KyberSamplerOpts memory opts, + address takerToken, + address makerToken + ) + public + view + returns (bytes32 reserveId, bytes memory hint, uint256[] memory takerTokenAmounts) + { + (reserveId, hint, takerTokenAmounts) = _sampleBuysFromKyberNetwork( + opts, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Kyber. /// @param opts KyberSamplerOpts The nth reserve /// @param takerToken Address of the taker token (what to sell). @@ -119,13 +143,13 @@ contract KyberSampler is /// @return reserveId The id of the reserve found at reserveOffset /// @return hint The hint for the selected reserve /// @return takerTokenAmounts Taker amounts sold at each maker token amount. - function sampleBuysFromKyberNetwork( + function _sampleBuysFromKyberNetwork( KyberSamplerOpts memory opts, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (bytes32 reserveId, bytes memory hint, uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/LidoSampler.sol b/packages/asset-swapper/contracts/src/LidoSampler.sol index e0c9d41124..c92f57ca10 100644 --- a/packages/asset-swapper/contracts/src/LidoSampler.sol +++ b/packages/asset-swapper/contracts/src/LidoSampler.sol @@ -85,6 +85,29 @@ contract LidoSampler is return takerTokenAmounts; } + /// @dev Sample buy quotes from Lido. + /// @param lidoInfo Info regarding a specific Lido deployment + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromLidoGlobal( + LidoInfo memory lidoInfo, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromLido( + lidoInfo, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Lido. /// @param lidoInfo Info regarding a specific Lido deployment /// @param takerToken Address of the taker token (what to sell). @@ -92,13 +115,13 @@ contract LidoSampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromLido( + function _sampleBuysFromLido( LidoInfo memory lidoInfo, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal pure returns (uint256[] memory) { diff --git a/packages/asset-swapper/contracts/src/LiquidityProviderSampler.sol b/packages/asset-swapper/contracts/src/LiquidityProviderSampler.sol index d4e2595ba3..2fbeaadbfb 100644 --- a/packages/asset-swapper/contracts/src/LiquidityProviderSampler.sol +++ b/packages/asset-swapper/contracts/src/LiquidityProviderSampler.sol @@ -102,6 +102,29 @@ contract LiquidityProviderSampler is } } + /// @dev Sample buy quotes from an arbitrary on-chain liquidity provider. + /// @param providerAddress Address of the liquidity provider. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromLiquidityProviderGlobal( + address providerAddress, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + (takerTokenAmounts) = _sampleBuysFromLiquidityProvider( + providerAddress, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from an arbitrary on-chain liquidity provider. /// @param providerAddress Address of the liquidity provider. /// @param takerToken Address of the taker token (what to sell). @@ -109,13 +132,13 @@ contract LiquidityProviderSampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromLiquidityProvider( + function _sampleBuysFromLiquidityProvider( address providerAddress, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/MStableSampler.sol b/packages/asset-swapper/contracts/src/MStableSampler.sol index be86254008..f71fc71c42 100644 --- a/packages/asset-swapper/contracts/src/MStableSampler.sol +++ b/packages/asset-swapper/contracts/src/MStableSampler.sol @@ -97,6 +97,29 @@ contract MStableSampler is } } + /// @dev Sample buy quotes from MStable contract + /// @param router Address of the mStable contract + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromMStableGlobal( + address router, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + (takerTokenAmounts) = _sampleBuysFromMStable( + router, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from MStable contract /// @param router Address of the mStable contract /// @param takerToken Address of the taker token (what to sell). @@ -104,13 +127,13 @@ contract MStableSampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromMStable( + function _sampleBuysFromMStable( address router, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/MakerPSMSampler.sol b/packages/asset-swapper/contracts/src/MakerPSMSampler.sol index b656fbec94..9823c545b6 100644 --- a/packages/asset-swapper/contracts/src/MakerPSMSampler.sol +++ b/packages/asset-swapper/contracts/src/MakerPSMSampler.sol @@ -157,13 +157,30 @@ contract MakerPSMSampler is } } - function sampleBuysFromMakerPsm( + function sampleBuysFromMakerPsmGlobal( + MakerPsmInfo memory psmInfo, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + (takerTokenAmounts) = _sampleBuysFromMakerPsm( + psmInfo, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + + function _sampleBuysFromMakerPsm( MakerPsmInfo memory psmInfo, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/MooniswapSampler.sol b/packages/asset-swapper/contracts/src/MooniswapSampler.sol index 65a19c356e..fb387f64e0 100644 --- a/packages/asset-swapper/contracts/src/MooniswapSampler.sol +++ b/packages/asset-swapper/contracts/src/MooniswapSampler.sol @@ -138,6 +138,30 @@ contract MooniswapSampler is } } + /// @dev Sample buy quotes from Mooniswap. + /// @param registry Address of the Mooniswap Registry. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return pool The contract address for the pool + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromMooniswapGlobal( + address registry, + address takerToken, + address makerToken + ) + public + view + returns (IMooniswap pool, uint256[] memory takerTokenAmounts) + { + (pool, takerTokenAmounts) = _sampleBuysFromMooniswap( + registry, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Mooniswap. /// @param registry Address of the Mooniswap Registry. /// @param takerToken Address of the taker token (what to sell). @@ -146,13 +170,13 @@ contract MooniswapSampler is /// @return pool The contract address for the pool /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromMooniswap( + function _sampleBuysFromMooniswap( address registry, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (IMooniswap pool, uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/ShellSampler.sol b/packages/asset-swapper/contracts/src/ShellSampler.sol index 73c7e37c06..864b6aadda 100644 --- a/packages/asset-swapper/contracts/src/ShellSampler.sol +++ b/packages/asset-swapper/contracts/src/ShellSampler.sol @@ -98,6 +98,29 @@ contract ShellSampler is } } + /// @dev Sample buy quotes from Shell pool contract + /// @param pool Address of the Shell pool contract + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromShellGlobal( + address pool, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromShell( + pool, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Shell pool contract /// @param pool Address of the Shell pool contract /// @param takerToken Address of the taker token (what to sell). @@ -105,13 +128,13 @@ contract ShellSampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromShell( + function _sampleBuysFromShell( address pool, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/SmoothySampler.sol b/packages/asset-swapper/contracts/src/SmoothySampler.sol index df6f99165b..18aafab0e3 100644 --- a/packages/asset-swapper/contracts/src/SmoothySampler.sol +++ b/packages/asset-swapper/contracts/src/SmoothySampler.sol @@ -123,6 +123,29 @@ contract SmoothySampler is } } + /// @dev Sample buy quotes from Smoothy. + /// @param smoothyInfo Smoothy information specific to this token pair. + /// @param fromTokenIdx Index of the taker token (what to sell). + /// @param toTokenIdx Index of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromSmoothyGlobal( + SmoothyInfo memory smoothyInfo, + int128 fromTokenIdx, + int128 toTokenIdx + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromSmoothy( + smoothyInfo, + fromTokenIdx, + toTokenIdx, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Smoothy. /// @param smoothyInfo Smoothy information specific to this token pair. /// @param fromTokenIdx Index of the taker token (what to sell). @@ -130,13 +153,13 @@ contract SmoothySampler is /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromSmoothy( + function _sampleBuysFromSmoothy( SmoothyInfo memory smoothyInfo, int128 fromTokenIdx, int128 toTokenIdx, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/TwoHopSampler.sol b/packages/asset-swapper/contracts/src/TwoHopSampler.sol index 8fcdcaaa84..807d7cfede 100644 --- a/packages/asset-swapper/contracts/src/TwoHopSampler.sol +++ b/packages/asset-swapper/contracts/src/TwoHopSampler.sol @@ -86,7 +86,8 @@ contract TwoHopSampler is bytes[] memory firstHopCalls, bytes[] memory secondHopCalls, uint256 buyAmount - ) + ) + resetsSampleValues() public returns ( HopInfo memory firstHop, @@ -96,8 +97,11 @@ contract TwoHopSampler is { sellAmount = uint256(-1); uint256 intermediateAssetAmount = uint256(-1); + uint256[] memory tmpSampleValues = new uint256[](1); for (uint256 j = 0; j != secondHopCalls.length; ++j) { - secondHopCalls[j].writeUint256(secondHopCalls[j].length - 32, buyAmount); + // Set the temporary global sample values + tmpSampleValues[0] = buyAmount; + SAMPLE_VALUES = tmpSampleValues; (bool didSucceed, bytes memory returnData) = address(this).call(secondHopCalls[j]); if (didSucceed) { uint256 amount = returnData.readUint256(returnData.length - 32); @@ -115,7 +119,9 @@ contract TwoHopSampler is return (firstHop, secondHop, sellAmount); } for (uint256 i = 0; i != firstHopCalls.length; ++i) { - firstHopCalls[i].writeUint256(firstHopCalls[i].length - 32, intermediateAssetAmount); + // Set the temporary global sample values + tmpSampleValues[0] = intermediateAssetAmount; + SAMPLE_VALUES = tmpSampleValues; (bool didSucceed, bytes memory returnData) = address(this).call(firstHopCalls[i]); if (didSucceed) { uint256 amount = returnData.readUint256(returnData.length - 32); diff --git a/packages/asset-swapper/contracts/src/UniswapSampler.sol b/packages/asset-swapper/contracts/src/UniswapSampler.sol index bc80bd4563..a299d47bd2 100644 --- a/packages/asset-swapper/contracts/src/UniswapSampler.sol +++ b/packages/asset-swapper/contracts/src/UniswapSampler.sol @@ -128,19 +128,41 @@ contract UniswapSampler is } } + /// @dev Sample buy quotes from Uniswap. + /// @param takerToken Address of the taker token (what to sell). + /// @param makerToken Address of the maker token (what to buy). + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromUniswapGlobal( + address router, + address takerToken, + address makerToken + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromUniswap( + router, + takerToken, + makerToken, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from Uniswap. /// @param takerToken Address of the taker token (what to sell). /// @param makerToken Address of the maker token (what to buy). /// @param makerTokenAmounts Maker token sell amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromUniswap( + function _sampleBuysFromUniswap( address router, address takerToken, address makerToken, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/UniswapV2Sampler.sol b/packages/asset-swapper/contracts/src/UniswapV2Sampler.sol index b9460ba452..a78f406180 100644 --- a/packages/asset-swapper/contracts/src/UniswapV2Sampler.sol +++ b/packages/asset-swapper/contracts/src/UniswapV2Sampler.sol @@ -86,18 +86,38 @@ contract UniswapV2Sampler is } } + /// @dev Sample buy quotes from UniswapV2. + /// @param router Router to look up tokens and amounts + /// @param path Token route. Should be takerToken -> makerToken. + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromUniswapV2Global( + address router, + address[] memory path + ) + public + view + returns (uint256[] memory takerTokenAmounts) + { + takerTokenAmounts = _sampleBuysFromUniswapV2( + router, + path, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from UniswapV2. /// @param router Router to look up tokens and amounts /// @param path Token route. Should be takerToken -> makerToken. /// @param makerTokenAmounts Maker token buy amount for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromUniswapV2( + function _sampleBuysFromUniswapV2( address router, address[] memory path, uint256[] memory makerTokenAmounts ) - public + internal view returns (uint256[] memory takerTokenAmounts) { diff --git a/packages/asset-swapper/contracts/src/UniswapV3Sampler.sol b/packages/asset-swapper/contracts/src/UniswapV3Sampler.sol index f58f6d1cfe..541fc6435c 100644 --- a/packages/asset-swapper/contracts/src/UniswapV3Sampler.sol +++ b/packages/asset-swapper/contracts/src/UniswapV3Sampler.sol @@ -128,6 +128,29 @@ contract UniswapV3Sampler is } } + /// @dev Sample buy quotes from UniswapV3. + /// @param quoter UniswapV3 Quoter contract. + /// @param path Token route. Should be takerToken -> makerToken. + /// @return uniswapPaths The encoded uniswap path for each sample. + /// @return takerTokenAmounts Taker amounts sold at each maker token + /// amount. + function sampleBuysFromUniswapV3Global( + IUniswapV3Quoter quoter, + IERC20TokenV06[] memory path + ) + public + returns ( + bytes[] memory uniswapPaths, + uint256[] memory takerTokenAmounts + ) + { + (uniswapPaths, takerTokenAmounts) = _sampleBuysFromUniswapV3( + quoter, + path, + SAMPLE_VALUES + ); + } + /// @dev Sample buy quotes from UniswapV3. /// @param quoter UniswapV3 Quoter contract. /// @param path Token route. Should be takerToken -> makerToken. @@ -135,12 +158,12 @@ contract UniswapV3Sampler is /// @return uniswapPaths The encoded uniswap path for each sample. /// @return takerTokenAmounts Taker amounts sold at each maker token /// amount. - function sampleBuysFromUniswapV3( + function _sampleBuysFromUniswapV3( IUniswapV3Quoter quoter, IERC20TokenV06[] memory path, uint256[] memory makerTokenAmounts ) - public + internal returns ( bytes[] memory uniswapPaths, uint256[] memory takerTokenAmounts diff --git a/packages/asset-swapper/src/utils/market_operation_utils/sampler_operations.ts b/packages/asset-swapper/src/utils/market_operation_utils/sampler_operations.ts index 74916af796..4fddeee06d 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/sampler_operations.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/sampler_operations.ts @@ -286,17 +286,17 @@ export class SamplerOperations { reserveOffset: BigNumber, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.Kyber, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromKyberNetwork, - params: [{ ...kyberOpts, reserveOffset, hint: NULL_BYTES }, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromKyberNetworkGlobal, + params: [{ ...kyberOpts, reserveOffset, hint: NULL_BYTES }, takerToken, makerToken], callback: (callResults: string, fillData: KyberFillData): BigNumber[] => { const [reserveId, hint, samples] = this._samplerContract.getABIDecodedReturnData< [string, string, BigNumber[]] - >('sampleBuysFromKyberNetwork', callResults); + >('sampleBuysFromKyberNetworkGlobal', callResults); fillData.hint = hint; fillData.reserveId = reserveId; fillData.networkProxy = kyberOpts.networkProxy; @@ -331,16 +331,16 @@ export class SamplerOperations { public getKyberDmmBuyQuotes( router: string, tokenAddressPath: string[], - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.KyberDmm, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromKyberDmm, - params: [router, tokenAddressPath, makerFillAmounts], + function: this._samplerContract.sampleBuysFromKyberDmmGlobal, + params: [router, tokenAddressPath], callback: (callResults: string, fillData: KyberDmmFillData): BigNumber[] => { const [pools, samples] = this._samplerContract.getABIDecodedReturnData<[string[], BigNumber[]]>( - 'sampleBuysFromKyberDmm', + 'sampleBuysFromKyberDmmGlobal', callResults, ); fillData.poolsPath = pools; @@ -373,7 +373,7 @@ export class SamplerOperations { router: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { // Uniswap uses ETH instead of WETH, represented by address(0) const uniswapTakerToken = takerToken === NATIVE_FEE_TOKEN_BY_CHAIN_ID[this.chainId] ? NULL_ADDRESS : takerToken; @@ -382,8 +382,8 @@ export class SamplerOperations { source: ERC20BridgeSource.Uniswap, fillData: { router }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromUniswap, - params: [router, uniswapTakerToken, uniswapMakerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromUniswapGlobal, + params: [router, uniswapTakerToken, uniswapMakerToken], }); } @@ -405,15 +405,15 @@ export class SamplerOperations { public getUniswapV2BuyQuotes( router: string, tokenAddressPath: string[], - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], source: ERC20BridgeSource = ERC20BridgeSource.UniswapV2, ): SourceQuoteOperation { return new SamplerContractOperation({ source, fillData: { tokenAddressPath, router }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromUniswapV2, - params: [router, tokenAddressPath, makerFillAmounts], + function: this._samplerContract.sampleBuysFromUniswapV2Global, + params: [router, tokenAddressPath], }); } @@ -441,7 +441,7 @@ export class SamplerOperations { providerAddress: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], gasCost: number, source: ERC20BridgeSource = ERC20BridgeSource.LiquidityProvider, ): SourceQuoteOperation { @@ -452,8 +452,8 @@ export class SamplerOperations { gasCost, }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromLiquidityProvider, - params: [providerAddress, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromLiquidityProviderGlobal, + params: [providerAddress, takerToken, makerToken], }); } @@ -489,7 +489,7 @@ export class SamplerOperations { pool: CurveInfo, fromTokenIdx: number, toTokenIdx: number, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], source: ERC20BridgeSource = ERC20BridgeSource.Curve, ): SourceQuoteOperation { return new SamplerContractOperation({ @@ -500,7 +500,7 @@ export class SamplerOperations { toTokenIdx, }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromCurve, + function: this._samplerContract.sampleBuysFromCurveGlobal, params: [ { poolAddress: pool.poolAddress, @@ -509,7 +509,6 @@ export class SamplerOperations { }, new BigNumber(fromTokenIdx), new BigNumber(toTokenIdx), - makerFillAmounts, ], }); } @@ -545,7 +544,7 @@ export class SamplerOperations { pool: CurveInfo, fromTokenIdx: number, toTokenIdx: number, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.Smoothy, @@ -555,7 +554,7 @@ export class SamplerOperations { toTokenIdx, }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromSmoothy, + function: this._samplerContract.sampleBuysFromSmoothyGlobal, params: [ { poolAddress: pool.poolAddress, @@ -564,7 +563,6 @@ export class SamplerOperations { }, new BigNumber(fromTokenIdx), new BigNumber(toTokenIdx), - makerFillAmounts, ], }); } @@ -589,15 +587,15 @@ export class SamplerOperations { poolInfo: BalancerV2PoolInfo, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], source: ERC20BridgeSource, ): SourceQuoteOperation { return new SamplerContractOperation({ source, fillData: poolInfo, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromBalancerV2, - params: [poolInfo, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromBalancerV2Global, + params: [poolInfo, takerToken, makerToken], }); } @@ -621,15 +619,15 @@ export class SamplerOperations { poolAddress: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], source: ERC20BridgeSource, ): SourceQuoteOperation { return new SamplerContractOperation({ source, fillData: { poolAddress }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromBalancer, - params: [poolAddress, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromBalancerGlobal, + params: [poolAddress, takerToken, makerToken], }); } @@ -652,14 +650,14 @@ export class SamplerOperations { router: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.MStable, fillData: { router }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromMStable, - params: [router, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromMStableGlobal, + params: [router, takerToken, makerToken], }); } @@ -691,17 +689,17 @@ export class SamplerOperations { registry: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.Bancor, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromBancor, - params: [{ registry, paths: [] }, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromBancorGlobal, + params: [{ registry, paths: [] }, takerToken, makerToken], callback: (callResults: string, fillData: BancorFillData): BigNumber[] => { const [networkAddress, path, samples] = this._samplerContract.getABIDecodedReturnData< [string, string[], BigNumber[]] - >('sampleBuysFromBancor', callResults); + >('sampleBuysFromBancorGlobal', callResults); fillData.networkAddress = networkAddress; fillData.path = path; return samples; @@ -740,7 +738,7 @@ export class SamplerOperations { registry: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { // Mooniswap uses ETH instead of WETH, represented by address(0) const mooniswapTakerToken = @@ -750,11 +748,11 @@ export class SamplerOperations { return new SamplerContractOperation({ source: ERC20BridgeSource.Mooniswap, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromMooniswap, - params: [registry, mooniswapTakerToken, mooniswapMakerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromMooniswapGlobal, + params: [registry, mooniswapTakerToken, mooniswapMakerToken], callback: (callResults: string, fillData: MooniswapFillData): BigNumber[] => { const [poolAddress, samples] = this._samplerContract.getABIDecodedReturnData<[string, BigNumber[]]>( - 'sampleBuysFromMooniswap', + 'sampleBuysFromMooniswapGlobal', callResults, ); fillData.poolAddress = poolAddress; @@ -801,11 +799,11 @@ export class SamplerOperations { return new SamplerContractOperation({ source, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromUniswapV3, - params: [quoter, tokenAddressPath, makerFillAmounts], + function: this._samplerContract.sampleBuysFromUniswapV3Global, + params: [quoter, tokenAddressPath], callback: (callResults: string, fillData: UniswapV3FillData): BigNumber[] => { const [paths, samples] = this._samplerContract.getABIDecodedReturnData<[string[], BigNumber[]]>( - 'sampleBuysFromUniswapV3', + 'sampleBuysFromUniswapV3Global', callResults, ); fillData.router = router; @@ -952,15 +950,15 @@ export class SamplerOperations { poolAddress: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], source: ERC20BridgeSource = ERC20BridgeSource.Shell, ): SourceQuoteOperation { return new SamplerContractOperation({ source, fillData: { poolAddress }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromShell, - params: [poolAddress, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromShellGlobal, + params: [poolAddress, takerToken, makerToken], }); } @@ -978,7 +976,7 @@ export class SamplerOperations { callback: (callResults: string, fillData: DODOFillData): BigNumber[] => { const [isSellBase, pool, samples] = this._samplerContract.getABIDecodedReturnData< [boolean, string, BigNumber[]] - >('sampleSellsFromDODO', callResults); + >('sampleSellsFromDODOGlobal', callResults); fillData.isSellBase = isSellBase; fillData.poolAddress = pool; fillData.helperAddress = opts.helper; @@ -991,17 +989,17 @@ export class SamplerOperations { opts: { registry: string; helper: string }, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.Dodo, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromDODO, - params: [opts, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromDODOGlobal, + params: [opts, takerToken, makerToken], callback: (callResults: string, fillData: DODOFillData): BigNumber[] => { const [isSellBase, pool, samples] = this._samplerContract.getABIDecodedReturnData< [boolean, string, BigNumber[]] - >('sampleBuysFromDODO', callResults); + >('sampleBuysFromDODOGlobal', callResults); fillData.isSellBase = isSellBase; fillData.poolAddress = pool; fillData.helperAddress = opts.helper; @@ -1038,17 +1036,17 @@ export class SamplerOperations { offset: BigNumber, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.DodoV2, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromDODOV2, - params: [registry, offset, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromDODOV2Global, + params: [registry, offset, takerToken, makerToken], callback: (callResults: string, fillData: DODOFillData): BigNumber[] => { const [isSellBase, pool, samples] = this._samplerContract.getABIDecodedReturnData< [boolean, string, BigNumber[]] - >('sampleSellsFromDODOV2', callResults); + >('sampleSellsFromDODOV2Global', callResults); fillData.isSellBase = isSellBase; fillData.poolAddress = pool; return samples; @@ -1080,7 +1078,7 @@ export class SamplerOperations { psmInfo: PsmInfo, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.MakerPsm, @@ -1091,8 +1089,8 @@ export class SamplerOperations { ...psmInfo, }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromMakerPsm, - params: [psmInfo, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromMakerPsmGlobal, + params: [psmInfo, takerToken, makerToken], }); } @@ -1118,7 +1116,7 @@ export class SamplerOperations { lidoInfo: LidoInfo, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.Lido, @@ -1127,8 +1125,8 @@ export class SamplerOperations { stEthTokenAddress: lidoInfo.stEthToken, }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromLido, - params: [lidoInfo, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromLidoGlobal, + params: [lidoInfo, takerToken, makerToken], }); } @@ -1179,14 +1177,14 @@ export class SamplerOperations { cToken: string, makerToken: string, takerToken: string, - makerFillAmounts: BigNumber[], + _makerFillAmounts: BigNumber[], ): SourceQuoteOperation { return new SamplerContractOperation({ source: ERC20BridgeSource.Compound, fillData: { cToken, takerToken, makerToken }, contract: this._samplerContract, - function: this._samplerContract.sampleBuysFromCompound, - params: [cToken, takerToken, makerToken, makerFillAmounts], + function: this._samplerContract.sampleBuysFromCompoundGlobal, + params: [cToken, takerToken, makerToken], }); }