Web3ProviderWallet

Documentation for eth_defi.provider_wallet.Web3ProviderWallet Python class.

class Web3ProviderWallet

Bases: eth_defi.basewallet.BaseWallet

Wallet implementation that delegates operations to a connected Web3 provider.

This wallet is designed to work with browser wallets (like MetaMask) or other external signers connected via a Web3 provider. It manages nonce locally but relies on the provider for actual transaction signing and submission.

Example: ```python # Connect to a Web3 provider with an account already unlocked web3 = Web3(Web3.HTTPProvider(”http://localhost:8545”)) wallet = Web3ProviderWallet(web3) wallet.sync_nonce(web3)

# Use with contract functions bound_call = token_contract.functions.transfer(recipient, amount) tx_hash = wallet.transact_and_broadcast_with_contract(bound_call) ```

Create a wallet using a connected Web3 provider.

web3Web3

Web3 instance with connected accounts

Attributes summary

address

Get the wallet's Ethereum address.

Methods summary

__init__(web3)

Create a wallet using a connected Web3 provider.

allocate_nonce()

Get the next available nonce.

fill_in_gas_price(web3, tx)

Fill in gas price details for a transaction.

get_main_address()

Get the main Ethereum address for this wallet.

get_native_currency_balance(web3)

Get the wallet's native currency balance.

send_transaction(unsigned_tx)

Send a transaction using the provider.

sign_bound_call_with_new_nonce(func[, ...])

Sign a contract function call with a new nonce.

sign_transaction_with_new_nonce(tx)

Sign a transaction with a new nonce.

sync_nonce(web3)

Synchronize the nonce with the blockchain.

transact_and_broadcast_with_contract(func[, ...])

Transact with a contract and broadcast the transaction.

__init__(web3)

Create a wallet using a connected Web3 provider.

web3Web3

Web3 instance with connected accounts

Parameters

web3 (web3.main.Web3) –

property address: eth_typing.evm.ChecksumAddress

Get the wallet’s Ethereum address.

get_main_address()

Get the main Ethereum address for this wallet.

Return type

eth_typing.evm.HexAddress

sync_nonce(web3)

Synchronize the nonce with the blockchain.

Parameters

web3 (web3.main.Web3) –

Return type

None

allocate_nonce()

Get the next available nonce.

Return type

int

sign_transaction_with_new_nonce(tx)

Sign a transaction with a new nonce.

Note: With Web3ProviderWallet, this method doesn’t actually sign the transaction, as the signing is handled by the provider when the transaction is sent. Instead, it prepares the transaction with a nonce and returns a placeholder.

Call send_raw_transaction on the returned object’s rawTransaction to broadcast.

Parameters

tx (dict) –

Return type

eth_defi.hotwallet.SignedTransactionWithNonce

sign_bound_call_with_new_nonce(func, tx_params=None, web3=None, fill_gas_price=False)

Sign a contract function call with a new nonce.

Parameters
  • func (web3.contract.contract.ContractFunction) –

  • tx_params (Optional[dict]) –

  • web3 (Optional[web3.main.Web3]) –

  • fill_gas_price (bool) –

Return type

eth_defi.hotwallet.SignedTransactionWithNonce

get_native_currency_balance(web3)

Get the wallet’s native currency balance.

Parameters

web3 (web3.main.Web3) –

Return type

decimal.Decimal

static fill_in_gas_price(web3, tx)

Fill in gas price details for a transaction.

Parameters
  • web3 (web3.main.Web3) –

  • tx (dict) –

Return type

dict

send_transaction(unsigned_tx)

Send a transaction using the provider.

This method delegates the actual sending to the web3 provider.

Parameters

unsigned_tx (dict) –

Return type

hexbytes.main.HexBytes

transact_and_broadcast_with_contract(func, gas_limit=None)

Transact with a contract and broadcast the transaction.

funcContractFunction

Bound contract function call

gas_limitint, optional

Gas limit for the transaction

HexBytes

Transaction hash

Parameters
  • func (web3.contract.contract.ContractFunction) –

  • gas_limit (Optional[int]) –

Return type

hexbytes.main.HexBytes