Skip to main content

High-level objects built on fastweb3.

Project description

fastweb3-objects

High-level EVM objects built on fastweb3.

NOTE: This library is still in early alpha development. Prior to a v1.0.0 (which may never come), expect breaking changes and no backward compatibility between versions.

Installation

You can install the latest release via pip:

pip install fastweb3-objects

Or clone the repository for the most up-to-date version:

git clone https://github.com/iamdefinitelyahuman/fastweb3-objects.git
cd fastweb3-objects
pip install -e .

Usage

The main entry points are available directly from fw3_objects:

>>> from fw3_objects import Account, Accounts, Chain, Contract, Transaction

1. Connect to a chain

Chain is the canonical object for interacting with a specific EVM chain.

>>> chain = Chain(1)
>>> chain
Chain(1)

The underlying fastweb3.Web3 client is created lazily. By default, it uses fastweb3's public RPC discovery and endpoint pooling.

>>> chain.height()
23100000

You can access blocks by number, hash, tag, or index syntax:

>>> chain.get_block("latest")
{'number': 23100000, ...}

>>> chain[-1]
{'number': 23100000, ...}

To use your own RPC endpoint, configure the chain before first use:

>>> from fw3_objects.chain import configure_chain

>>> configure_chain(1, endpoints=["http://localhost:8545"], use_public_pool=False)
>>> chain = Chain(1)

2. Use a default chain context

Objects can be bound explicitly to a chain, or they can use the active default chain.

>>> mainnet = Chain(1)

>>> with mainnet.as_default():
...     vitalik = Account("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
...     vitalik.balance()
...
32131215082101779377

Strict mode rejects accidental access through another chain while the context is active.

>>> with mainnet.as_default(strict=True):
...     Account("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045").balance()

3. Manage accounts

Accounts is a fastweb3-keypass database wrapper. With no arguments, it opens the default database, creating it first if needed.

>>> accounts = Accounts()
Create password for new Accounts database 'default': ***

>>> accounts
<Accounts unlocked>

Accounts created or imported through the database can sign transactions.

>>> acct = accounts.create_account(alias="deployer", set_as_default=True)
>>> acct
<Account 0x0D3AaC9458167352493864Af54D1CBD7F1B8fF68 signable>

You can also create watch-only account objects directly from an address.

>>> vitalik = Account("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", chain=1)
>>> vitalik
<Account 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 watch-only>

Chain-aware account helpers expose common RPC operations.

>>> vitalik.balance()
32131215082101779377

>>> vitalik.nonce()
1832

>>> vitalik.bytecode()
b''

4. Send transactions

Signing accounts can build, sign, broadcast, and watch transactions in a single call.

>>> tx = acct.transact(
...     to="0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
...     value=10**18,
...     chain=1,
... )

The returned Transaction object tracks mempool, receipt, replacement, and revert state.

>>> tx.status
<TxStatus.PENDING: -1>

>>> tx.wait()
>>> tx.status
<TxStatus.CONFIRMED: 1>

>>> tx.block_number
23100012

>>> tx.gas_used
21000

Pending transactions can be replaced with a higher-fee transaction when the sender is available in an open Accounts database.

>>> replacement = tx.replace()

5. Interact with contracts

Contract binds an address, ABI, and chain. ABI can be provided directly, loaded from a JSON file, loaded from cache, or fetched from a supported block explorer.

>>> usdc = Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", chain=1)

Function ABIs are installed as callable attributes.

>>> usdc.name()
'USD Coin'

>>> usdc.symbol()
'USDC'

>>> usdc.balanceOf("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
123456789

Read-only functions perform eth_call. Nonpayable and payable functions build, sign, broadcast, and return a Transaction.

>>> tx = usdc.transfer(
...     "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
...     1_000_000,
...     sender=acct,
... )

>>> tx.wait()

Contract methods also expose encoding and decoding helpers.

>>> calldata = usdc.transfer.encode_input(
...     "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
...     1_000_000,
... )

>>> usdc.transfer.decode_input(calldata)
('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', 1000000)

6. Inspect transactions

A transaction hash can be wrapped directly.

>>> tx = Transaction("0xf3063ed07203bc337636f1aa8cb521aa819478df764100950eebea00760a296c", chain=1)

Transaction properties wait for the first monitor update before returning.

>>> tx.sender
<Account 0x0D3AaC9458167352493864Af54D1CBD7F1B8fF68 watch-only>

>>> tx.receiver
<Account 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 watch-only>

>>> tx.value
1000000000000000000

Decoded receipt events are available lazily from tx.events.

>>> len(tx.events)
1

Tests

First, install the dev dependencies:

pip install -e ".[dev]"

To run the test suite:

pytest

License

This project is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastweb3_objects-0.1.1.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastweb3_objects-0.1.1-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file fastweb3_objects-0.1.1.tar.gz.

File metadata

  • Download URL: fastweb3_objects-0.1.1.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for fastweb3_objects-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1998098309ff5978452bb148a5c921478596dcb35c96ca3cf210b0aad60c68a6
MD5 2a0efc7ea6de7b12a1fad7089b072e8d
BLAKE2b-256 300282db3543016f8dd90cc47846f7922706f3fa9b0b4b6163bd985c47649e6a

See more details on using hashes here.

File details

Details for the file fastweb3_objects-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastweb3_objects-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c7e7cf7f0a165cd821fac8abdad3eea1b4d99da5dda34f6221764e345158fc3
MD5 c0316ee64083479618d588e1db5c0a3e
BLAKE2b-256 4ce6be77fce28afab9224acb17c2f1e3715bab6a775f8107b66aba46dfa54cdb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page