Skip to content

Commit

Permalink
Fixing setting nonce in agent0 (#1551)
Browse files Browse the repository at this point in the history
Using `eth_transaction_count` should be querying the `pending` block,
not `latest` block to set nonce. This allows a single account to send
multiple transactions on the same block with proper nonce setting.

This matches default `web3py` behavior.
  • Loading branch information
Sheng Lundquist authored Jun 20, 2024
1 parent 5d6d0cd commit 88ec09d
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ async def async_execute_agent_trades(
# To do this, we need to manually set the nonce, so we get the base transaction count here
# and pass in an incrementing nonce per call
# TODO figure out which exception here to retry on
base_nonce = retry_call(DEFAULT_READ_RETRY_COUNT, None, interface.web3.eth.get_transaction_count, account.address)
base_nonce = retry_call(
DEFAULT_READ_RETRY_COUNT, None, interface.web3.eth.get_transaction_count, account.address, "pending"
)

# Here, gather returns results based on original order of trades due to nonce getting explicitly set based
# on iterating through the list
Expand Down Expand Up @@ -234,7 +236,9 @@ async def async_execute_single_trade(

# TODO we likely need to bookkeep nonces here to avoid a race condition when this function
# is being called asynchronously
nonce = retry_call(DEFAULT_READ_RETRY_COUNT, None, interface.web3.eth.get_transaction_count, account.address)
nonce = retry_call(
DEFAULT_READ_RETRY_COUNT, None, interface.web3.eth.get_transaction_count, account.address, "pending"
)

try:
receipt_or_exception = await _async_match_contract_call_to_trade(
Expand Down
6 changes: 3 additions & 3 deletions src/agent0/ethpy/base/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def build_transaction(
txn_options_priority_fee_multiple = DEFAULT_PRIORITY_FEE_MULTIPLE
signer_checksum_address = Web3.to_checksum_address(signer.address)
# TODO figure out which exception here to retry on
base_nonce = retry_call(read_retry_count, None, web3.eth.get_transaction_count, signer_checksum_address)
base_nonce = retry_call(read_retry_count, None, web3.eth.get_transaction_count, signer_checksum_address, "pending")
if nonce is None:
nonce = base_nonce
# We explicitly check to ensure explicit nonce is larger than what web3 is reporting
Expand Down Expand Up @@ -954,7 +954,7 @@ async def async_eth_transfer(
if read_retry_count is None:
read_retry_count = DEFAULT_READ_RETRY_COUNT
signer_checksum_address = Web3.to_checksum_address(signer.address)
base_nonce = retry_call(read_retry_count, None, web3.eth.get_transaction_count, signer_checksum_address)
base_nonce = retry_call(read_retry_count, None, web3.eth.get_transaction_count, signer_checksum_address, "pending")
if nonce is None:
nonce = base_nonce
# We explicitly check to ensure explicit nonce is larger than what web3 is reporting
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def eth_transfer(
signer_checksum_address = Web3.to_checksum_address(signer.address)
if nonce is None:
# TODO figure out which exception here to retry on
nonce = retry_call(read_retry_count, None, web3.eth.get_transaction_count, signer_checksum_address)
nonce = retry_call(read_retry_count, None, web3.eth.get_transaction_count, signer_checksum_address, "pending")
unsent_txn: TxParams = {
"from": signer_checksum_address,
"to": to_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress, constructorAr

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
2 changes: 1 addition & 1 deletion src/agent0/hypertypes/types/ERC20MintableContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2547,7 +2547,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress, constructorAr

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress, constructorAr

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
2 changes: 1 addition & 1 deletion src/agent0/hypertypes/types/HyperdriveFactoryContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7313,7 +7313,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress, constructorAr

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
2 changes: 1 addition & 1 deletion src/agent0/hypertypes/types/HyperdriveRegistryContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress, constructorAr

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
2 changes: 1 addition & 1 deletion src/agent0/hypertypes/types/IHyperdriveContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6971,7 +6971,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
2 changes: 1 addition & 1 deletion src/agent0/hypertypes/types/LPMathContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress) -> Self:

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down
2 changes: 1 addition & 1 deletion src/agent0/hypertypes/types/MockERC4626Contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3587,7 +3587,7 @@ def deploy(cls, w3: Web3, account: LocalAccount | ChecksumAddress, constructorAr

# otherwise use the account provided.
deployment_tx = constructor_fn.build_transaction()
current_nonce = w3.eth.get_transaction_count(account.address)
current_nonce = w3.eth.get_transaction_count(account.address, "pending")
deployment_tx.update({"nonce": current_nonce})

# Sign the transaction with local account private key
Expand Down

0 comments on commit 88ec09d

Please sign in to comment.