rename pairId to more general poolId

This commit is contained in:
pistomat
2023-12-06 15:12:43 +01:00
parent 223df970d3
commit 9105b898d6
8 changed files with 58 additions and 58 deletions

View File

@@ -50,7 +50,7 @@ instances:
# getTokens are not implemented. # getTokens are not implemented.
tests: tests:
instances: instances:
- pair_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc" - pool_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"
sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
block: 17000000 block: 17000000
@@ -61,7 +61,7 @@ tests:
#### Price (optional) #### Price (optional)
Calculates pair prices for specified amounts (optional). Calculates pool prices for specified amounts (optional).
The returned prices should include all protocol fees, in case the fee is dynamic, the returned price is expected to include the minimum fee.  The returned prices should include all protocol fees, in case the fee is dynamic, the returned price is expected to include the minimum fee. 
@@ -71,7 +71,7 @@ The method needs to be implemented as view as this is usually more efficient and
```solidity ```solidity
function price( function price(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
uint256[] memory sellAmounts uint256[] memory sellAmounts
@@ -80,7 +80,7 @@ function price(
#### Swap #### Swap
Simulates swapping tokens on a given pair. Simulates swapping tokens on a given pool.
This function should be state modifying meaning it should actually execute the swap and change the state of the vm accordingly. This function should be state modifying meaning it should actually execute the swap and change the state of the vm accordingly.
@@ -90,7 +90,7 @@ The return type Trade, has a price attribute which should contain the value of p
```solidity ```solidity
function swap( function swap(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
OrderSide side, OrderSide side,
@@ -105,29 +105,29 @@ Retrieves the limits for each token.
This method returns the maximum limits of a token that can be traded. The limit is reached when the change in the received amounts is zero or close to zero. If in doubt over estimate the limit. The swap function should not error with LimitExceeded if called with any amounts below the limit. This method returns the maximum limits of a token that can be traded. The limit is reached when the change in the received amounts is zero or close to zero. If in doubt over estimate the limit. The swap function should not error with LimitExceeded if called with any amounts below the limit.
```solidity ```solidity
function getLimits(bytes32 pairId, OrderSide side) function getLimits(bytes32 poolId, OrderSide side)
external external
returns (uint256[] memory); returns (uint256[] memory);
``` ```
#### getCapabilities #### getCapabilities
Retrieves the capabilities of the selected pair. Retrieves the capabilities of the selected pool.
```solidity ```solidity
function getCapabilities(bytes32 pairId, IERC20 sellToken, IERC20 buyToken) function getCapabilities(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external external
returns (Capability[] memory); returns (Capability[] memory);
``` ```
#### getTokens (optional) #### getTokens (optional)
Retrieves the tokens for the given pair. Retrieves the tokens for the given pool.
_Mainly used for testing as this is redundant with the required substreams implementation._ _Mainly used for testing as this is redundant with the required substreams implementation._
```solidity ```solidity
function getTokens(bytes32 pairId) function getTokens(bytes32 poolId)
external external
returns (IERC20[] memory tokens); returns (IERC20[] memory tokens);
``` ```

View File

@@ -22,21 +22,21 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
/// Also this function is not 'view' because Balancer V2 simulates the swap /// Also this function is not 'view' because Balancer V2 simulates the swap
/// and /// and
/// then returns the amount diff in revert data. /// then returns the amount diff in revert data.
/// @param pairId The ID of the trading pool. /// @param poolId The ID of the trading pool.
/// @param sellToken The token being sold. /// @param sellToken The token being sold.
/// @param buyToken The token being bought. /// @param buyToken The token being bought.
/// @param sellAmount The amount of tokens being sold. /// @param sellAmount The amount of tokens being sold.
/// @return calculatedPrice The price of the buy token in terms of the sell /// @return calculatedPrice The price of the buy token in terms of the sell
/// as a Fraction struct. /// as a Fraction struct.
function priceSingle( function priceSingle(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
uint256 sellAmount uint256 sellAmount
) public returns (Fraction memory calculatedPrice) { ) public returns (Fraction memory calculatedPrice) {
IVault.BatchSwapStep[] memory swapSteps = new IVault.BatchSwapStep[](1); IVault.BatchSwapStep[] memory swapSteps = new IVault.BatchSwapStep[](1);
swapSteps[0] = IVault.BatchSwapStep({ swapSteps[0] = IVault.BatchSwapStep({
poolId: pairId, poolId: poolId,
assetInIndex: 0, assetInIndex: 0,
assetOutIndex: 1, assetOutIndex: 1,
amount: sellAmount, amount: sellAmount,
@@ -65,14 +65,14 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
} }
function getSellAmount( function getSellAmount(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
uint256 buyAmount uint256 buyAmount
) public returns (uint256 sellAmount) { ) public returns (uint256 sellAmount) {
IVault.BatchSwapStep[] memory swapSteps = new IVault.BatchSwapStep[](1); IVault.BatchSwapStep[] memory swapSteps = new IVault.BatchSwapStep[](1);
swapSteps[0] = IVault.BatchSwapStep({ swapSteps[0] = IVault.BatchSwapStep({
poolId: pairId, poolId: poolId,
assetInIndex: 0, assetInIndex: 0,
assetOutIndex: 1, assetOutIndex: 1,
amount: buyAmount, amount: buyAmount,
@@ -98,14 +98,14 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
} }
function priceBatch( function priceBatch(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
uint256[] memory specifiedAmounts uint256[] memory specifiedAmounts
) external returns (Fraction[] memory calculatedPrices) { ) external returns (Fraction[] memory calculatedPrices) {
for (uint256 i = 0; i < specifiedAmounts.length; i++) { for (uint256 i = 0; i < specifiedAmounts.length; i++) {
calculatedPrices[i] = calculatedPrices[i] =
priceSingle(pairId, sellToken, buyToken, specifiedAmounts[i]); priceSingle(poolId, sellToken, buyToken, specifiedAmounts[i]);
} }
} }
@@ -119,7 +119,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
} }
function swap( function swap(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
OrderSide side, OrderSide side,
@@ -135,7 +135,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
} else { } else {
kind = IVault.SwapKind.GIVEN_OUT; kind = IVault.SwapKind.GIVEN_OUT;
sellAmount = sellAmount =
getSellAmount(pairId, sellToken, buyToken, specifiedAmount); getSellAmount(poolId, sellToken, buyToken, specifiedAmount);
limit = type(uint256).max; limit = type(uint256).max;
} }
@@ -145,7 +145,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
uint256 gasBefore = gasleft(); uint256 gasBefore = gasleft();
trade.calculatedAmount = vault.swap( trade.calculatedAmount = vault.swap(
IVault.SingleSwap({ IVault.SingleSwap({
poolId: pairId, poolId: poolId,
kind: kind, kind: kind,
assetIn: address(sellToken), assetIn: address(sellToken),
assetOut: address(buyToken), assetOut: address(buyToken),
@@ -162,10 +162,10 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
block.timestamp + SWAP_DEADLINE_SEC block.timestamp + SWAP_DEADLINE_SEC
); );
trade.gasUsed = gasBefore - gasleft(); trade.gasUsed = gasBefore - gasleft();
trade.price = priceSingle(pairId, sellToken, buyToken, specifiedAmount); trade.price = priceSingle(poolId, sellToken, buyToken, specifiedAmount);
} }
function getLimits(bytes32 pairId, IERC20 sellToken, IERC20 buyToken) function getLimits(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external external
view view
override override
@@ -173,7 +173,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
{ {
limits = new uint256[](2); limits = new uint256[](2);
(IERC20[] memory tokens, uint256[] memory balances,) = (IERC20[] memory tokens, uint256[] memory balances,) =
vault.getPoolTokens(pairId); vault.getPoolTokens(poolId);
for (uint256 i = 0; i < tokens.length; i++) { for (uint256 i = 0; i < tokens.length; i++) {
if (tokens[i] == sellToken) { if (tokens[i] == sellToken) {
@@ -196,13 +196,13 @@ contract BalancerV2SwapAdapter is ISwapAdapter, Test {
capabilities[1] = Capability.BuyOrder; capabilities[1] = Capability.BuyOrder;
} }
function getTokens(bytes32 pairId) function getTokens(bytes32 poolId)
external external
view view
override override
returns (IERC20[] memory tokens) returns (IERC20[] memory tokens)
{ {
(tokens,,) = vault.getPoolTokens(pairId); (tokens,,) = vault.getPoolTokens(poolId);
} }
/// @dev Balancer V2 does not support enumerating pools, they have to be /// @dev Balancer V2 does not support enumerating pools, they have to be

View File

@@ -27,7 +27,7 @@ instances:
# getTokens are not implemented. # getTokens are not implemented.
tests: tests:
instances: instances:
- pair_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc" - pool_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"
sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
block: 17000000 block: 17000000

View File

@@ -6,18 +6,18 @@ import {ISwapAdapterTypes} from "src/interfaces/ISwapAdapterTypes.sol";
/// @title ISwapAdapter /// @title ISwapAdapter
/// @dev Implement this interface to support propeller routing through your /// @dev Implement this interface to support propeller routing through your
/// pairs. Before implementing the interface we need to introduce three function /// pools. Before implementing the interface we need to introduce three function
/// for a given pair: The swap(x), gas(x) and price(x) functions: The swap /// for a given pool: The swap(x), gas(x) and price(x) functions: The swap
/// function accepts some specified token amount x and returns the amount y a /// function accepts some specified token amount x and returns the amount y a
/// user can get by swapping x through the venue. The gas function simply /// user can get by swapping x through the venue. The gas function simply
/// returns the estimated gas cost given a specified amount x. Last but not /// returns the estimated gas cost given a specified amount x. Last but not
/// least, the price function is the derivative of the swap function. It /// least, the price function is the derivative of the swap function. It
/// represents the best possible price a user can get from a pair after swapping /// represents the best possible price a user can get from a pool after swapping
/// x of the specified token. During calls to swap and getLimits, the caller can /// x of the specified token. During calls to swap and getLimits, the caller can
/// be assumed to have the required sell or buy token balance as well as /// be assumed to have the required sell or buy token balance as well as
/// unlimited approvals to this contract. /// unlimited approvals to this contract.
interface ISwapAdapter is ISwapAdapterTypes { interface ISwapAdapter is ISwapAdapterTypes {
/// @notice Calculates pair prices for specified amounts (optional). /// @notice Calculates pool prices for specified amounts (optional).
/// @dev The returned prices should include all dex fees, in case the fee is /// @dev The returned prices should include all dex fees, in case the fee is
/// dynamic, the returned price is expected to include the minimum fee. /// dynamic, the returned price is expected to include the minimum fee.
/// Ideally this method should be implemented, although it is optional as /// Ideally this method should be implemented, although it is optional as
@@ -26,7 +26,7 @@ interface ISwapAdapter is ISwapAdapterTypes {
/// calling it should revert using the `NotImplemented` error. The method /// calling it should revert using the `NotImplemented` error. The method
/// needs to be implemented as view as this is usually more efficient and /// needs to be implemented as view as this is usually more efficient and
/// can be run in parallel. all. /// can be run in parallel. all.
/// @param pairId The ID of the trading pair. /// @param poolId The ID of the trading pool.
/// @param sellToken The token being sold. /// @param sellToken The token being sold.
/// @param buyToken The token being bought. /// @param buyToken The token being bought.
/// @param specifiedAmounts The specified amounts used for price /// @param specifiedAmounts The specified amounts used for price
@@ -34,14 +34,14 @@ interface ISwapAdapter is ISwapAdapterTypes {
/// @return prices array of prices as fractions corresponding to the /// @return prices array of prices as fractions corresponding to the
/// provided amounts. /// provided amounts.
function price( function price(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
uint256[] memory specifiedAmounts uint256[] memory specifiedAmounts
) external view returns (Fraction[] memory prices); ) external view returns (Fraction[] memory prices);
/** /**
* @notice Simulates swapping tokens on a given pair. * @notice Simulates swapping tokens on a given pool.
* @dev This function should be state modifying meaning it should actually * @dev This function should be state modifying meaning it should actually
* execute the swap and change the state of the evm accordingly. Please * execute the swap and change the state of the evm accordingly. Please
* include a gas usage estimate for each amount. This can be achieved e.g. * include a gas usage estimate for each amount. This can be achieved e.g.
@@ -50,7 +50,7 @@ interface ISwapAdapter is ISwapAdapterTypes {
* this is optional, defined via `Capability.PriceFunction`, it is valid to * this is optional, defined via `Capability.PriceFunction`, it is valid to
* return a zero value for this price in that case it will be estimated * return a zero value for this price in that case it will be estimated
* numerically. To return zero use Fraction(0, 1). * numerically. To return zero use Fraction(0, 1).
* @param pairId The ID of the trading pair. * @param poolId The ID of the trading pool.
* @param sellToken The token being sold. * @param sellToken The token being sold.
* @param buyToken The token being bought. * @param buyToken The token being bought.
* @param side The side of the trade (Sell or Buy). * @param side The side of the trade (Sell or Buy).
@@ -58,7 +58,7 @@ interface ISwapAdapter is ISwapAdapterTypes {
* @return trade Trade struct representing the executed trade. * @return trade Trade struct representing the executed trade.
*/ */
function swap( function swap(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
OrderSide side, OrderSide side,
@@ -71,27 +71,27 @@ interface ISwapAdapter is ISwapAdapterTypes {
/// close to zero. Overestimate if in doubt rather than underestimate. The /// close to zero. Overestimate if in doubt rather than underestimate. The
/// swap function should not error with `LimitExceeded` if called with /// swap function should not error with `LimitExceeded` if called with
/// amounts below the limit. /// amounts below the limit.
/// @param pairId The ID of the trading pair. /// @param poolId The ID of the trading pool.
/// @param sellToken The token being sold. /// @param sellToken The token being sold.
/// @param buyToken The token being bought. /// @param buyToken The token being bought.
/// @return limits An array of limits. /// @return limits An array of limits.
function getLimits(bytes32 pairId, IERC20 sellToken, IERC20 buyToken) function getLimits(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external external
returns (uint256[] memory limits); returns (uint256[] memory limits);
/// @notice Retrieves the capabilities of the selected pair. /// @notice Retrieves the capabilities of the selected pool.
/// @param pairId The ID of the trading pair. /// @param poolId The ID of the trading pool.
/// @return capabilities An array of Capability. /// @return capabilities An array of Capability.
function getCapabilities(bytes32 pairId, IERC20 sellToken, IERC20 buyToken) function getCapabilities(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external external
returns (Capability[] memory capabilities); returns (Capability[] memory capabilities);
/// @notice Retrieves the tokens in the selected pair. /// @notice Retrieves the tokens in the selected pool.
/// @dev Mainly used for testing as this is redundant with the required /// @dev Mainly used for testing as this is redundant with the required
/// substreams implementation. /// substreams implementation.
/// @param pairId The ID of the trading pair. /// @param poolId The ID of the trading pool.
/// @return tokens An array of IERC20 contracts. /// @return tokens An array of IERC20 contracts.
function getTokens(bytes32 pairId) function getTokens(bytes32 poolId)
external external
returns (IERC20[] memory tokens); returns (IERC20[] memory tokens);

View File

@@ -13,7 +13,7 @@ interface ISwapAdapterTypes {
} }
/// @dev The Capability enum represents possible features of a trading /// @dev The Capability enum represents possible features of a trading
/// pair. /// pool.
enum Capability { enum Capability {
Unset, Unset,
// Support OrderSide.Sell values (required) // Support OrderSide.Sell values (required)
@@ -24,10 +24,10 @@ interface ISwapAdapterTypes {
PriceFunction, PriceFunction,
// Support tokens that charge a fee on transfer (optional) // Support tokens that charge a fee on transfer (optional)
FeeOnTransfer, FeeOnTransfer,
// The pair does not suffer from price impact and maintains a constant // The pool does not suffer from price impact and maintains a constant
// price for increasingly larger specified amounts. (optional) // price for increasingly larger specified amounts. (optional)
ConstantPrice, ConstantPrice,
// Indicates that the pair does not read it's own token balances while // Indicates that the pool does not read it's own token balances while
// swapping. (optional) // swapping. (optional)
TokenBalanceIndependent, TokenBalanceIndependent,
// Indicates that prices are returned scaled, else it is assumed prices // Indicates that prices are returned scaled, else it is assumed prices
@@ -49,7 +49,7 @@ interface ISwapAdapterTypes {
uint256 calculatedAmount; uint256 calculatedAmount;
// The amount of gas used in the trade. // The amount of gas used in the trade.
uint256 gasUsed; uint256 gasUsed;
// The price of the pair after the trade. For zero use Fraction(0, 1). // The price of the pool after the trade. For zero use Fraction(0, 1).
Fraction price; Fraction price;
} }

View File

@@ -13,13 +13,13 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
} }
function price( function price(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
uint256[] memory specifiedAmounts uint256[] memory specifiedAmounts
) external view override returns (Fraction[] memory prices) { ) external view override returns (Fraction[] memory prices) {
prices = new Fraction[](specifiedAmounts.length); prices = new Fraction[](specifiedAmounts.length);
IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(pairId))); IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(poolId)));
uint112 r0; uint112 r0;
uint112 r1; uint112 r1;
if (sellToken < buyToken) { if (sellToken < buyToken) {
@@ -51,7 +51,7 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
} }
function swap( function swap(
bytes32 pairId, bytes32 poolId,
IERC20 sellToken, IERC20 sellToken,
IERC20 buyToken, IERC20 buyToken,
OrderSide side, OrderSide side,
@@ -62,7 +62,7 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
// expected zero Fraction(0, 1) // expected zero Fraction(0, 1)
} }
IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(pairId))); IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(poolId)));
uint112 r0; uint112 r0;
uint112 r1; uint112 r1;
bool zero2one = sellToken < buyToken; bool zero2one = sellToken < buyToken;
@@ -168,13 +168,13 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
amountIn = (numerator / denominator) + 1; amountIn = (numerator / denominator) + 1;
} }
function getLimits(bytes32 pairId, IERC20 sellToken, IERC20 buyToken) function getLimits(bytes32 poolId, IERC20 sellToken, IERC20 buyToken)
external external
view view
override override
returns (uint256[] memory limits) returns (uint256[] memory limits)
{ {
IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(pairId))); IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(poolId)));
limits = new uint256[](2); limits = new uint256[](2);
(uint256 r0, uint256 r1,) = pair.getReserves(); (uint256 r0, uint256 r1,) = pair.getReserves();
if (sellToken < buyToken) { if (sellToken < buyToken) {
@@ -198,14 +198,14 @@ contract UniswapV2SwapAdapter is ISwapAdapter {
capabilities[2] = Capability.PriceFunction; capabilities[2] = Capability.PriceFunction;
} }
function getTokens(bytes32 pairId) function getTokens(bytes32 poolId)
external external
view view
override override
returns (IERC20[] memory tokens) returns (IERC20[] memory tokens)
{ {
tokens = new IERC20[](2); tokens = new IERC20[](2);
IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(pairId))); IUniswapV2Pair pair = IUniswapV2Pair(address(bytes20(poolId)));
tokens[0] = IERC20(pair.token0()); tokens[0] = IERC20(pair.token0());
tokens[1] = IERC20(pair.token1()); tokens[1] = IERC20(pair.token1());
} }

View File

@@ -27,7 +27,7 @@ instances:
# getTokens are not implemented. # getTokens are not implemented.
tests: tests:
instances: instances:
- pair_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc" - pool_id: "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"
sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" sell_token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" buy_token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
block: 17000000 block: 17000000

View File

@@ -202,11 +202,11 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes {
assert(limits[1] > 0); assert(limits[1] > 0);
} }
function testGetCapabilitiesFuzz(bytes32 pair, address t0, address t1) function testGetCapabilitiesFuzz(bytes32 pool, address t0, address t1)
public public
{ {
Capability[] memory res = Capability[] memory res =
adapter.getCapabilities(pair, IERC20(t0), IERC20(t1)); adapter.getCapabilities(pool, IERC20(t0), IERC20(t1));
assertEq(res.length, 2); assertEq(res.length, 2);
assertEq(uint256(res[0]), uint256(Capability.SellOrder)); assertEq(uint256(res[0]), uint256(Capability.SellOrder));