Skip to content

Commit

Permalink
add exact out
Browse files Browse the repository at this point in the history
  • Loading branch information
ipatka committed Nov 27, 2023
1 parent 0b3a8dd commit 5a2f02c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
32 changes: 16 additions & 16 deletions ape_uniswap/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ def _get_recipient(
function_recipient: FunctionRecipient,
custom_recipient: Optional[ChecksumAddress] = None) -> ChecksumAddress:

print(f"Getting recipient")
print(f"Function recipient: {function_recipient}")
print(f"Custom recipient: {custom_recipient}")
# print(f"Getting recipient")
# print(f"Function recipient: {function_recipient}")
# print(f"Custom recipient: {custom_recipient}")
recipient_mapping = {
FunctionRecipient.SENDER: _RouterConstant.MSG_SENDER.value,
FunctionRecipient.ROUTER: _RouterConstant.ADDRESS_THIS.value,
FunctionRecipient.CUSTOM: custom_recipient,
}
print(f"Recipient mapping: {recipient_mapping}")
# print(f"Recipient mapping: {recipient_mapping}")
recipient = recipient_mapping[function_recipient]
print(f"Recipient: {recipient}")
# print(f"Recipient: {recipient}")
if recipient:
print(f"Recipient is not None")
# print(f"Recipient is not None")
return Web3.to_checksum_address(recipient)
else:
print(f"Recipient is None")
# print(f"Recipient is None")
raise ValueError(
f"Invalid function_recipient: {function_recipient} or custom_recipient: {custom_recipient}: "
f"custom_recipient address must be provided if FunctionRecipient.CUSTOM is selected."
Expand Down Expand Up @@ -301,15 +301,15 @@ def _encode_v3_swap_exact_in_sub_contract(
payer_is_user: bool) -> HexStr:
print(f"Encoding swap")
encoded_v3_path = _Encoder.v3_path(_RouterFunction.V3_SWAP_EXACT_IN.name, path)
print(f"Encoded path: {encoded_v3_path}")
# print(f"Encoded path: {encoded_v3_path}")
args = (recipient, amount_in, amount_out_min, encoded_v3_path, payer_is_user)
print(f"Args: {args}")
# print(f"Args: {args}")
abi_mapping = self._abi_map[_RouterFunction.V3_SWAP_EXACT_IN]
print(f"ABI mapping: {abi_mapping}")
# print(f"ABI mapping: {abi_mapping}")
sub_contract = self._w3.eth.contract(abi=abi_mapping.fct_abi.get_full_abi())
print(f"Sub contract: {sub_contract}")
# print(f"Sub contract: {sub_contract}")
contract_function: ContractFunction = sub_contract.functions.V3_SWAP_EXACT_IN(*args)
print(f"Contract function: {contract_function}")
# print(f"Contract function: {contract_function}")
return remove_0x_prefix(encode_abi(self._w3, contract_function.abi, args))

def v3_swap_exact_in(
Expand All @@ -333,11 +333,11 @@ def v3_swap_exact_in(
:param payer_is_sender: True if the in tokens come from the sender, False if they already are in the router
:return: The chain link corresponding to this function call.
"""
print(f"Building swap")
# print(f"Building swap")
recipient = self._get_recipient(function_recipient, custom_recipient)
print(f"Recipient: {recipient}")
# print(f"Recipient: {recipient}")
self.commands.append(_RouterFunction.V3_SWAP_EXACT_IN)
print(f"Commands: {self.commands}")
# print(f"Commands: {self.commands}")
self.arguments.append(
Web3.to_bytes(
hexstr=self._encode_v3_swap_exact_in_sub_contract(
Expand All @@ -349,7 +349,7 @@ def v3_swap_exact_in(
)
)
)
print(f"Arguments: {self.arguments}")
# print(f"Arguments: {self.arguments}")
return self

def v3_swap_exact_in_from_balance(
Expand Down
41 changes: 37 additions & 4 deletions ape_uniswap/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,53 @@ def execute_v3_swap_exact_in_simple(
if (self.network_manager.network.chain_id != 1):
raise ValueError("Uniswap V3 is only supported on Ethereum Mainnet for now")

print(f"Chain ID is 1")
# print(f"Chain ID is 1")

builder = self.router_codec.encode.chain()
print(f"Builder initiated")
# print(f"Builder initiated")
builder = builder.v3_swap_exact_in(
function_recipient=FunctionRecipient.SENDER,
amount_in=amount_in,
amount_out_min=amount_out_min,
path=[to_checksum_address(token_in), fee, to_checksum_address(token_out)])

print(f"Swap added to chain")
# print(f"Swap added to chain")

encoded_data = builder.build(deadline=deadline)
print(f"Encoded data: {encoded_data}")
# print(f"Encoded data: {encoded_data}")
router_contract = Contract(UNI_UR, abi=_router_abi)
print(f"Calling router contract: {router_contract.address}")

return router_contract.__call__(data=encoded_data, sender=sender)

def execute_v3_swap_exact_out_simple(
self,
amount_out: int,
amount_in_max: int,
token_out: AnyAddress,
token_in: AnyAddress,
fee: int,
deadline: Optional[int] = None,
sender: Optional[AccountAPI] = None,
) -> ReceiptAPI:
print(f"Executing swap exact out: {amount_out} {token_out} for {token_in}")
if (self.network_manager.network.chain_id != 1):
raise ValueError("Uniswap V3 is only supported on Ethereum Mainnet for now")

# print(f"Chain ID is 1")

builder = self.router_codec.encode.chain()
# print(f"Builder initiated")
builder = builder.v3_swap_exact_out(
function_recipient=FunctionRecipient.SENDER,
amount_out=amount_out,
amount_in_max=amount_in_max,
path=[to_checksum_address(token_in), fee, to_checksum_address(token_out)])

# print(f"Swap added to chain")

encoded_data = builder.build(deadline=deadline)
# print(f"Encoded data: {encoded_data}")
router_contract = Contract(UNI_UR, abi=_router_abi)
print(f"Calling router contract: {router_contract.address}")

Expand Down

0 comments on commit 5a2f02c

Please sign in to comment.