Skip to content

Commit

Permalink
Format VelodromeSampler.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu-c committed Jun 7, 2022
1 parent cec26ad commit 0a2967f
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions packages/asset-swapper/contracts/src/VelodromeSampler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;

import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";

import './ApproximateBuys.sol';
import './SamplerUtils.sol';

struct VeloRoute {
address from;
Expand All @@ -31,12 +30,19 @@ struct VeloRoute {
}

interface IVelodromeRouter {
function getAmountOut(uint256 amountIn, address tokenIn, address tokenOut) external view returns (uint256 amount, bool stable);
function getAmountsOut(uint amountIn, VeloRoute[] calldata routes) external view returns (uint[] memory amounts);
function getAmountOut(
uint256 amountIn,
address tokenIn,
address tokenOut
) external view returns (uint256 amount, bool stable);

function getAmountsOut(uint256 amountIn, VeloRoute[] calldata routes)
external
view
returns (uint256[] memory amounts);
}

contract VelodromeSampler is SamplerUtils, ApproximateBuys {

/// @dev Sample sell quotes from Velodrome
/// @param router Address of Velodrome router.
/// @param takerToken Address of the taker token (what to sell).
Expand All @@ -49,11 +55,7 @@ contract VelodromeSampler is SamplerUtils, ApproximateBuys {
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (bool stable, uint256[] memory makerTokenAmounts)
{
) public view returns (bool stable, uint256[] memory makerTokenAmounts) {
_assertValidPair(makerToken, takerToken);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
Expand All @@ -62,7 +64,7 @@ contract VelodromeSampler is SamplerUtils, ApproximateBuys {
// Find the most liquid pool based on max(takerTokenAmounts) and stick with it.
stable = _isMostLiquidPoolStablePool(router, takerToken, makerToken, takerTokenAmounts);
VeloRoute[] memory routes = new VeloRoute[](1);
routes[0] = VeloRoute({from: takerToken, to: makerToken, stable: stable});
routes[0] = VeloRoute({ from: takerToken, to: makerToken, stable: stable });

for (uint256 i = 0; i < numSamples; i++) {
makerTokenAmounts[i] = router.getAmountsOut(takerTokenAmounts[i], routes)[1];
Expand All @@ -85,11 +87,7 @@ contract VelodromeSampler is SamplerUtils, ApproximateBuys {
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (bool stable, uint256[] memory takerTokenAmounts)
{
) public view returns (bool stable, uint256[] memory takerTokenAmounts) {
_assertValidPair(makerToken, takerToken);

// Sampling should not mix stable and volatile pools.
Expand All @@ -99,8 +97,8 @@ contract VelodromeSampler is SamplerUtils, ApproximateBuys {
// takerTokenAmounts = new uint256[](numSamples);
takerTokenAmounts = _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
takerTokenData: abi.encode(router, VeloRoute({from: takerToken,to:makerToken,stable:stable })),
makerTokenData: abi.encode(router, VeloRoute({to: takerToken,from:makerToken,stable:stable })),
takerTokenData: abi.encode(router, VeloRoute({ from: takerToken, to: makerToken, stable: stable })),
makerTokenData: abi.encode(router, VeloRoute({ to: takerToken, from: makerToken, stable: stable })),
getSellQuoteCallback: _sampleSellForApproximateBuyFromVelodrome
}),
makerTokenAmounts
Expand All @@ -109,18 +107,16 @@ contract VelodromeSampler is SamplerUtils, ApproximateBuys {

function _sampleSellForApproximateBuyFromVelodrome(
bytes memory takerTokenData,
bytes memory /* makerTokenData */,
bytes memory, /* makerTokenData */
uint256 sellAmount
) internal view returns (uint256) {
(IVelodromeRouter router, VeloRoute memory route) = abi.decode(
takerTokenData, (IVelodromeRouter, VeloRoute));
(IVelodromeRouter router, VeloRoute memory route) = abi.decode(takerTokenData, (IVelodromeRouter, VeloRoute));

VeloRoute[] memory routes = new VeloRoute[](1);
routes[0] = route;
return router.getAmountsOut(sellAmount, routes)[1];
}


/// @dev Returns whether the most liquid pool is a stable pool.
/// @param router Address of Velodrome router.
/// @param takerToken Address of the taker token (what to sell).
Expand All @@ -134,7 +130,6 @@ contract VelodromeSampler is SamplerUtils, ApproximateBuys {
uint256[] memory takerTokenAmounts
) internal view returns (bool stable) {
uint256 numSamples = takerTokenAmounts.length;
(, stable) = router.getAmountOut(takerTokenAmounts[numSamples-1], takerToken, makerToken);
(, stable) = router.getAmountOut(takerTokenAmounts[numSamples - 1], takerToken, makerToken);
}

}

0 comments on commit 0a2967f

Please sign in to comment.