feat: Update with new interface

Give more context in the swap-encoder.md
This commit is contained in:
Diana Carvalho
2024-08-30 10:42:13 +01:00
parent 41f20f14b0
commit 7ba63bf404
4 changed files with 54 additions and 18 deletions

View File

@@ -14,7 +14,7 @@ runs:
id: env-cache id: env-cache
with: with:
path: ${{ env.pythonLocation }} path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('./requirements/requirements_dev.txt') }}-${{ hashFiles('./requirements/requirements_data.txt') }}-${{ hashFiles('./requirements/requirements_api.txt') }}-${{ hashFiles('./requirements/requirements.txt') }}-${{ hashFiles('./requirements/requirements_internal.txt') }} key: ${{ env.pythonLocation }}-${{ hashFiles('./propeller-swap-encoders/requirements.txt') }}
- name: Configure AWS Credentials - name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4.0.1 uses: aws-actions/configure-aws-credentials@v4.0.1
@@ -28,5 +28,5 @@ runs:
run: | run: |
aws codeartifact login --tool pip --domain propeller --domain-owner 827659017777 --repository protosim aws codeartifact login --tool pip --domain propeller --domain-owner 827659017777 --repository protosim
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install -r propeller-swap-encoders/requirements.txt pip install -r propeller-swap-encoders/requirements.txt --no-cache
shell: bash shell: bash

View File

@@ -31,16 +31,59 @@ class SwapStructEncoder(ABC):
@abstractmethod @abstractmethod
def encode_swap_struct( def encode_swap_struct(
self, swap: dict[str, Any], receiver: Address, **kwargs self, swap: dict[str, Any], receiver: Address, encoding_context: EncodingContext
) -> bytes: ) -> bytes:
"""
Parameters:
----------
swap
The swap to encode
receiver
The receiver of the buy token
encoding_context
Additional context information necessary for encoding
"""
pass pass
``` ```
- **encode_swap_struct** - **encode_swap_struct**
- **Purpose**: To encode the swap details into a bytes object that the SwapExecutor can use to execute the swap. - **Purpose**: To encode the swap details into a bytes object that the SwapExecutor can use to execute the swap.
- **Parameters**: - **Parameters**:
- `swap`: A dictionary containing the swap details, such as input/output token addresses, amounts, and pool information. - `swap`: A dictionary containing the swap details. These are the fields present in this dict:
- `pool_id: str`: The identifier for the liquidity pool where the swap will occur.
- `sell_token: EthereumToken`: The token that will be sold in the swap (e.g., DAI).
- `buy_token: EthereumToken`: The token that will be bought in the swap (e.g., WETH).
- `split: float`: Indicates how the swap should be split between pools (often set to 0).
- `sell_amount: int`: The amount of the sell token to be used in the swap.
- `buy_amount: int`: The amount of the buy token to be received from the swap.
- `token_approval_needed: bool`: A boolean indicating if token approval is needed before the swap.
- `pool_tokens`: An optional tuple containing additional token data specific to the pool.
- `pool_type: str`: The type of pool where the swap will be executed (e.g., "BalancerStablePoolState").
- `receiver`: The address that will receive the output tokens from the swap. - `receiver`: The address that will receive the output tokens from the swap.
- `encoding_context`: Additional context information necessary for encoding.
```python
class EncodingContext(DefibotBaseModel):
exact_out: Optional[bool] = None
"""Whether the amount encoded is the exact amount out"""
in_transfer: bool = False
"""Whether we need to transfer to the pool"""
payment: bool = False
"""Whether this is a payment (if so the pool will use transferFrom(msg.sender, ..))"""
supply_owed_action_data: bytes = b""
"""Actions to perform with the owed amount using the USV3 callback."""
forward_received_action_data: bytes = b""
"""Actions to perform with the received amount using the USV3 callback."""
supply_owed_action_type: BatchRouterActionType = BatchRouterActionType.TRANSFER
"""The action kind to take with the owed amount"""
forward_received_action_type: BatchRouterActionType = BatchRouterActionType.SWAP
"""The action kind to take with the received amount"""
```
- `**kwargs`: Any additional protocol-specific parameters that need to be included in the encoding. - `**kwargs`: Any additional protocol-specific parameters that need to be included in the encoding.
- **Returns**: A bytes object containing the encoded swap data. - **Returns**: A bytes object containing the encoded swap data.

View File

@@ -1,6 +1,6 @@
from typing import Any from typing import Any
from core.encoding.interface import SwapStructEncoder from core.encoding.interface import SwapStructEncoder, EncodingContext
from core.type_aliases import Address from core.type_aliases import Address
from eth_abi.packed import encode_abi_packed from eth_abi.packed import encode_abi_packed
from hexbytes import HexBytes from hexbytes import HexBytes
@@ -8,18 +8,8 @@ from hexbytes import HexBytes
class BalancerSwapStructEncoder(SwapStructEncoder): class BalancerSwapStructEncoder(SwapStructEncoder):
def encode_swap_struct( def encode_swap_struct(
self, swap: dict[str, Any], receiver: Address, exact_out: bool, **kwargs self, swap: dict[str, Any], receiver: Address, encoding_context: EncodingContext
) -> bytes: ) -> bytes:
"""
Parameters:
----------
swap
The swap to encode
receiver
The receiver of the buy token
exact_out
Whether the amount encoded is the exact amount out
"""
return encode_abi_packed( return encode_abi_packed(
["address", "address", "bytes32", "address", "bool", "bool"], ["address", "address", "bytes32", "address", "bool", "bool"],
[ [
@@ -27,7 +17,7 @@ class BalancerSwapStructEncoder(SwapStructEncoder):
swap["buy_token"].address, swap["buy_token"].address,
HexBytes(swap["pool_id"]), HexBytes(swap["pool_id"]),
receiver, receiver,
exact_out, encoding_context.exact_out,
swap["token_approval_needed"], swap["token_approval_needed"],
], ],
) )

View File

@@ -1,5 +1,6 @@
from core.models.evm.ethereum_token import EthereumToken from core.models.evm.ethereum_token import EthereumToken
from propeller_swap_encoders.balancer import BalancerSwapStructEncoder from propeller_swap_encoders.balancer import BalancerSwapStructEncoder
from core.encoding.interface import EncodingContext
def test_encode_balancer(): def test_encode_balancer():
@@ -29,7 +30,9 @@ def test_encode_balancer():
"pool_fee": None, "pool_fee": None,
} }
balancer_encoder = BalancerSwapStructEncoder() balancer_encoder = BalancerSwapStructEncoder()
encoded = balancer_encoder.encode_swap_struct(swap, receiver=bob, exact_out=False) encoded = balancer_encoder.encode_swap_struct(
swap, receiver=bob, encoding_context=EncodingContext(exact_out=False)
)
assert ( assert (
encoded.hex() encoded.hex()
== ==