Skip to content

Commit

Permalink
feat: compatible wallet construction api with web3py v7
Browse files Browse the repository at this point in the history
  • Loading branch information
darwintree committed Nov 15, 2024
1 parent 6f8d9c2 commit 1c00cff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
6 changes: 4 additions & 2 deletions conflux_web3/middleware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from conflux_web3.middleware.wallet import (
Wallet,
construct_sign_and_send_raw_middleware,
)
from conflux_web3.middleware.names import (
name_to_address_middleware
Expand All @@ -23,12 +24,13 @@ def conflux_default_middlewares(w3: "Web3") -> Sequence[Tuple[Middleware, str]]:
return [
(name_to_address_middleware(w3), "name_to_address"),
(PendingTransactionMiddleware, "PendingTransactionMiddleware"),
(Wallet(), "wallet"),
(Wallet(), "wallet"), # type: ignore
]


__all__ = [
"PendingTransactionMiddleware",
"Wallet",
"conflux_default_middlewares"
"conflux_default_middlewares",
"construct_sign_and_send_raw_middleware",
]
11 changes: 7 additions & 4 deletions conflux_web3/middleware/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
)
import warnings

from toolz import curry

from eth_keys.datatypes import (
PrivateKey,
)
Expand Down Expand Up @@ -192,5 +190,10 @@ def pop(self, address: str) -> LocalAccount:
def construct_sign_and_send_raw_middleware(
account_or_accounts: Union[Sequence[_PrivateKey], _PrivateKey],
forced_chain_id: Optional[int]=None
) -> Wallet:
return Wallet(account_or_accounts, forced_chain_id)
):
return Wallet(account_or_accounts, forced_chain_id) # type: ignore

class SignAndSendRawMiddlewareBuilder:
@classmethod
def build(cls, account_or_accounts: Union[Sequence[_PrivateKey], _PrivateKey], forced_chain_id: Optional[int]=None):
return Wallet(account_or_accounts, forced_chain_id) # type: ignore
16 changes: 12 additions & 4 deletions conflux_web3/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import (
TYPE_CHECKING,
Any,
Type,
List,
NewType,
Optional,
Sequence,
Union,
Dict
Dict,
Callable,
Protocol,
)
from typing_extensions import (
Literal,
Expand All @@ -18,6 +19,7 @@
from web3.datastructures import (
NamedElementOnion,
)
from web3.types import RPCResponse, RPCEndpoint

from cfx_address import Base32Address
from cfx_utils.types import (
Expand Down Expand Up @@ -371,9 +373,15 @@ class BlockData(TypedDict):
posReference: Hash32
transactions: Sequence[Union[Hash32, TxData]]
baseFeePerGas: Drip


Middleware = Type["ConfluxWeb3Middleware"]
class Web3MiddlewareProtocol(Protocol):
def request_processor(self, method: RPCEndpoint, params: Any) -> Any:
...

def response_processor(self, method: RPCEndpoint, response: RPCResponse) -> RPCResponse:
...

Middleware = Callable[["Web3"], Web3MiddlewareProtocol]
MiddlewareOnion = NamedElementOnion[str, Middleware]

class StorageRoot(TypedDict):
Expand Down
22 changes: 13 additions & 9 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

## Overview

Python-conflux-sdk helps to interact with Conflux network using python. It is built over [web3.py](https://github.com/ethereum/web3.py) and most of its APIs are consistent with [web3.py](https://github.com/ethereum/web3.py).
Python-conflux-sdk (or conflux-web3) helps to interact with Conflux network using python. It is built over [web3.py](https://github.com/ethereum/web3.py) and most of its APIs are consistent with [web3.py](https://github.com/ethereum/web3.py).

> Note: python-conflux-sdk v1.3.0 is the version compatible with web3.py v6.x. If you are using web3.py v7.x, please use the v1.4.0 or later.
## Quickstart

Requirements: python version >= 3.7
Requirements: python version >= 3.8

```bash
$ pip3 install conflux-web3
Expand All @@ -35,24 +37,26 @@ w3 = Web3(Web3.HTTPProvider("https://test.confluxrpc.com"))

acct = w3.account.from_key("0xxxxxxxxxxxxxx")
w3.cfx.default_account = acct
w3.cfx.contract(name="Faucet").claimCfx().transact().executed()

# Uncomment the following line if there is not CFX in your account
# w3.cfx.contract(name="Faucet").claimCfx().transact().executed()

w3.cfx.send_transaction({
'to': w3.address.zero_address(),
'value': 10**18,
}).executed()
```

Or you can also use API as you do in `web3.py`:
Or you can also use API as you do in `web3.py` before v6.20:

``` python
# modified from https://web3py.readthedocs.io/en/stable/middleware.html#signing
# modified from https://web3py.readthedocs.io/en/v6.20.2/transactions.html#chapter-1-w3-eth-send-transaction-signer-middleware
from conflux_web3 import Web3
w3 = Web3("https://test.confluxrpc.com")
from conflux_web3.middleware import construct_sign_and_send_raw_middleware
w3 = Web3(Web3.HTTPProvider("https://test.confluxrpc.com"))
from conflux_web3.middleware import SignAndSendRawMiddlewareBuilder
from cfx_account import Account
acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530', network_id=w3.cfx.chain_id)
w3.middleware_onion.inject(SignAndSendRawMiddlewareBuilder.build(acct), layer=0)
w3.cfx.default_account = acct.address

transaction = {
Expand Down
22 changes: 21 additions & 1 deletion tests/middleware/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from conflux_web3 import Web3
from conflux_web3.middleware.wallet import (
Wallet,
construct_sign_and_send_raw_middleware
construct_sign_and_send_raw_middleware,
SignAndSendRawMiddlewareBuilder
)

@pytest.mark.xdist_group(name="account")
Expand All @@ -28,6 +29,25 @@ def test_wallet_middleware_single_init(w3:Web3, account: LocalAccount):
assert hash
w3.cfx.wait_for_transaction_receipt(hash)

@pytest.mark.xdist_group(name="account")
def test_wallet_middleware_single_init_by_builder(w3:Web3, account: LocalAccount):
wallet = SignAndSendRawMiddlewareBuilder.build(account, w3.cfx.chain_id)
w3.middleware_onion.inject(wallet, layer=0)
tx = {
'from': account.address,
# 'nonce': w3.cfx.get_next_nonce(addr),
# 'gas': 21000,
'to': w3.cfx.account.create().address,
'value': 10**9,
# 'gasPrice': 10**9,
# 'chainId': w3.cfx.chain_id,
# 'storageLimit': 0,
# 'epochHeight': status['epochNumber']
}
hash = w3.cfx.send_transaction(tx)
assert hash
w3.cfx.wait_for_transaction_receipt(hash)

@pytest.mark.xdist_group(name="account")
def test_no_chain_id_wallet_middleware_single_init(w3:Web3, account: LocalAccount):
wallet = construct_sign_and_send_raw_middleware(account)
Expand Down

0 comments on commit 1c00cff

Please sign in to comment.