diff --git a/CHANGELOG.md b/CHANGELOG.md index bbbafb7b..15b58fae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,12 @@ +# 0.21.4 + +- Added `eth_defi.enzyme.erc_20` helpers + # 0.21.3 -- Fix error message `fetch_transaction_revert_reason()` crashing -- +- Fix error message `fetch_transaction_revert_reason()` crashing. + Also made the error message prettier and more helpful. + # 0.21.2 - Add `AssetDelta.__mul__` method diff --git a/eth_defi/enzyme/erc20.py b/eth_defi/enzyme/erc20.py new file mode 100644 index 00000000..1fc9b9ab --- /dev/null +++ b/eth_defi/enzyme/erc20.py @@ -0,0 +1,67 @@ +"""Enzyme vault ERC-20 helpers.""" +from eth_typing import HexAddress +from web3.contract import Contract +from web3.contract.contract import ContractFunction + +from eth_defi.abi import encode_function_call +from eth_defi.enzyme.deployment import EnzymeDeployment +from eth_defi.enzyme.generic_adapter import execute_calls_for_generic_adapter +from eth_defi.enzyme.vault import Vault + + +def prepare_transfer( + enzyme: EnzymeDeployment, + vault: Vault, + generic_adapter: Contract, + token: Contract, + receiver: HexAddress | str, + amount: int) -> ContractFunction: + """Prepare an ERC-20 transfer out from the Enzyme vault. + + - Tells the Enzyme vault to move away som etokes + + :param enzyme: + Enzyme deploymeent + + :param vault: + Vault that needs to perform the swap + + :param generic_adapter: + GenericAdapter contract we use for swaps + + :param token: + ERC-20 token we send + + :param receiver: + The receiver of tokens + + :param amount: + Token amount, raw + + :return: + Transaction object that can be signed and executed + """ + + # Prepare the swap parameters + spend_asset_amounts = [amount] + spend_assets = [token.address] + incoming_assets = [] + min_incoming_assets_amounts = [] + + # The vault performs a swap on Uniswap v2 + encoded_transfer = encode_function_call(token.functions.transfer, [receiver, amount]) + + bound_call = execute_calls_for_generic_adapter( + comptroller=vault.comptroller, + external_calls=( + (token, encoded_transfer), + ), + generic_adapter=generic_adapter, + incoming_assets=incoming_assets, + integration_manager=enzyme.contracts.integration_manager, + min_incoming_asset_amounts=min_incoming_assets_amounts, + spend_asset_amounts=spend_asset_amounts, + spend_assets=spend_assets, + ) + + return bound_call diff --git a/eth_defi/enzyme/uniswap_v2.py b/eth_defi/enzyme/uniswap_v2.py index f6bf308c..9e17bc12 100644 --- a/eth_defi/enzyme/uniswap_v2.py +++ b/eth_defi/enzyme/uniswap_v2.py @@ -99,3 +99,5 @@ def prepare_swap(enzyme: EnzymeDeployment, vault: Vault, uniswap_v2: UniswapV2De ) return bound_call + +