From 04cd2da05f6aaf3975d9c52159dafe246ad5c23c Mon Sep 17 00:00:00 2001 From: Sheng Lundquist Date: Tue, 4 Jun 2024 17:21:08 -0500 Subject: [PATCH] Adding in agent0 signature to extra data on trade calls (#1513) --- .../core/hyperdrive/interactive/chain.py | 10 +++++++-- .../core/hyperdrive/interactive/hyperdrive.py | 1 + .../hyperdrive/interface/_contract_calls.py | 22 +++++++++---------- .../hyperdrive/interface/read_interface.py | 21 +++++++++++++----- .../interface/read_write_interface.py | 17 ++++++++------ 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/agent0/core/hyperdrive/interactive/chain.py b/src/agent0/core/hyperdrive/interactive/chain.py index 2dee9bc673..ad17370a7f 100644 --- a/src/agent0/core/hyperdrive/interactive/chain.py +++ b/src/agent0/core/hyperdrive/interactive/chain.py @@ -67,8 +67,6 @@ class Config: """ preview_before_trade: bool = False """Whether to preview the position before executing a trade. Defaults to False.""" - txn_receipt_timeout: float | None = None - """The timeout for waiting for a transaction receipt in seconds. Defaults to 120.""" # Logging and crash reporting log_to_rollbar: bool = False @@ -117,6 +115,10 @@ class Config: The experiment's stateful random number generator. Defaults to creating a generator from the provided random seed if not set. """ + + # Txn config + txn_receipt_timeout: float | None = None + """The timeout for waiting for a transaction receipt in seconds. Defaults to 120.""" gas_limit: int | None = None """ The maximum gas to use when executing transactions. This gas limit controls @@ -125,6 +127,10 @@ class Config: """ # TODO we only use gas_limit currently for policy trades and `create_checkpoint` in advance time, # need to propagate this to other trades + txn_signature: bytes | None = None + """ + The signature for transactions. Defaults to `0xa0`. + """ def __post_init__(self): """Create the random number generator if not set.""" diff --git a/src/agent0/core/hyperdrive/interactive/hyperdrive.py b/src/agent0/core/hyperdrive/interactive/hyperdrive.py index 5da85848d2..1d46db0ddf 100644 --- a/src/agent0/core/hyperdrive/interactive/hyperdrive.py +++ b/src/agent0/core/hyperdrive/interactive/hyperdrive.py @@ -103,6 +103,7 @@ def _initialize(self, chain: Chain, hyperdrive_address: ChecksumAddress, name: s rpc_uri=chain.rpc_uri, web3=chain._web3, # pylint: disable=protected-access txn_receipt_timeout=self.chain.config.txn_receipt_timeout, + txn_signature=self.chain.config.txn_signature, ) # Register the username if it was provided diff --git a/src/agent0/ethpy/hyperdrive/interface/_contract_calls.py b/src/agent0/ethpy/hyperdrive/interface/_contract_calls.py index dac76a892a..dda3e0cd6a 100644 --- a/src/agent0/ethpy/hyperdrive/interface/_contract_calls.py +++ b/src/agent0/ethpy/hyperdrive/interface/_contract_calls.py @@ -202,7 +202,7 @@ async def _async_open_long( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) @@ -233,7 +233,7 @@ async def _async_open_long( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) try: @@ -289,7 +289,7 @@ async def _async_close_long( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) @@ -318,7 +318,7 @@ async def _async_close_long( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) try: @@ -378,7 +378,7 @@ async def _async_open_short( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) @@ -407,7 +407,7 @@ async def _async_open_short( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) try: @@ -462,7 +462,7 @@ async def _async_close_short( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) @@ -491,7 +491,7 @@ async def _async_close_short( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) try: @@ -552,7 +552,7 @@ async def _async_add_liquidity( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) @@ -618,7 +618,7 @@ async def _async_remove_liquidity( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) # To catch any solidity errors, we always preview transactions on the current block @@ -684,7 +684,7 @@ async def _async_redeem_withdraw_shares( ( # IHyperdrive.Options agent_checksum_address, # destination as_base_option, # asBase - bytes(0), # extraData + interface.txn_signature, # extraData ), ) # To catch any solidity errors, we always preview transactions on the current block diff --git a/src/agent0/ethpy/hyperdrive/interface/read_interface.py b/src/agent0/ethpy/hyperdrive/interface/read_interface.py index 026b2488bc..5e0197b9cd 100644 --- a/src/agent0/ethpy/hyperdrive/interface/read_interface.py +++ b/src/agent0/ethpy/hyperdrive/interface/read_interface.py @@ -63,6 +63,13 @@ _calc_time_stretch, ) +AGENT0_SIGNATURE = bytes.fromhex("a0") + +if TYPE_CHECKING: + from eth_account.signers.local import LocalAccount + from eth_typing import BlockNumber, ChecksumAddress + from web3 import Web3 + # We expect to have many instance attributes & public methods since this is a large API. # pylint: disable=too-many-lines # pylint: disable=too-many-instance-attributes @@ -73,12 +80,6 @@ # pylint: disable=protected-access -if TYPE_CHECKING: - from eth_account.signers.local import LocalAccount - from eth_typing import BlockNumber, ChecksumAddress - from web3 import Web3 - - class HyperdriveReadInterface: """Read-only end-point API for interfacing with a deployed Hyperdrive pool.""" @@ -91,6 +92,7 @@ def __init__( web3: Web3 | None = None, read_retry_count: int | None = None, txn_receipt_timeout: float | None = None, + txn_signature: bytes | None = None, ) -> None: """Initialize the HyperdriveReadInterface API. @@ -109,7 +111,14 @@ def __init__( The number of times to retry the read call if it fails. Defaults to 5. txn_receipt_timeout: float | None, optional The timeout for waiting for a transaction receipt in seconds. Defaults to 120. + txn_signature: bytes | None, optional + The signature for transactions. Defaults to `0xa0`. """ + if txn_signature is None: + self.txn_signature = AGENT0_SIGNATURE + else: + self.txn_signature = txn_signature + # Handle defaults for config and addresses. self.hyperdrive_address = hyperdrive_address diff --git a/src/agent0/ethpy/hyperdrive/interface/read_write_interface.py b/src/agent0/ethpy/hyperdrive/interface/read_write_interface.py index 3785cfda73..2bd58465c6 100644 --- a/src/agent0/ethpy/hyperdrive/interface/read_write_interface.py +++ b/src/agent0/ethpy/hyperdrive/interface/read_write_interface.py @@ -17,13 +17,6 @@ ) from .read_interface import HyperdriveReadInterface -# We have no control over the number of arguments since it is specified by the smart contracts -# pylint: disable=too-many-arguments -# ruff: noqa: PLR0913 -# We only worry about protected access for anyone outside of this folder. -# pylint: disable=protected-access - - if TYPE_CHECKING: from eth_account.signers.local import LocalAccount from eth_typing import BlockNumber, ChecksumAddress @@ -33,6 +26,12 @@ from agent0.ethpy.hyperdrive.receipt_breakdown import ReceiptBreakdown +# We have no control over the number of arguments since it is specified by the smart contracts +# pylint: disable=too-many-arguments +# ruff: noqa: PLR0913 +# We only worry about protected access for anyone outside of this folder. +# pylint: disable=protected-access + class HyperdriveReadWriteInterface(HyperdriveReadInterface): """Read-write end-point API for interfacing with a deployed Hyperdrive pool.""" @@ -45,6 +44,7 @@ def __init__( read_retry_count: int | None = None, write_retry_count: int | None = None, txn_receipt_timeout: float | None = None, + txn_signature: bytes | None = None, ) -> None: """Initialize the primary endpoint for users to execute transactions on Hyperdrive smart contracts. @@ -63,6 +63,8 @@ def __init__( The number of times to retry the transact call if it fails. Defaults to no retries. txn_receipt_timeout: float | None, optional The timeout for waiting for a transaction receipt in seconds. Defaults to 120. + txn_signature: bytes | None, optional + The signature for transactions. Defaults to `0xa0`. """ super().__init__( hyperdrive_address=hyperdrive_address, @@ -70,6 +72,7 @@ def __init__( web3=web3, read_retry_count=read_retry_count, txn_receipt_timeout=txn_receipt_timeout, + txn_signature=txn_signature, ) self.write_retry_count = write_retry_count