Skip to content

Commit

Permalink
Update package to use web3.py >=6.0.0, <7 (#87)
Browse files Browse the repository at this point in the history
* update and fix dependencies

- bump web3.py version to >=6, <7

- chore: bump package version to 2.0.0

- chore: lock packages

- chore: update vscode settings.json

- update .vscode/settings.json to properly use black formatter

* fix: get private key using v6 LocalAccount private key property

* feat: use v6 method and property naming convention

- use Pythonic snake_case where applicable
- `web3.module.Module uses` `w3` naming instead of `web3`(e.g. `self.web3.eth.block_number` -> `self.w3.eth.block_number`)
- sha3 method has been renamed to keccak

* chore: update PKG-INFO web3 package version

* Update simple.py for web3.py v6

* Update pyproject.toml and poetry.lock to the latest

---------

Co-authored-by: George Zhang <[email protected]>
  • Loading branch information
vile and odysseus0 authored Jul 23, 2024
1 parent c00711a commit f0062a5
Show file tree
Hide file tree
Showing 8 changed files with 1,776 additions and 1,100 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"python.formatting.provider": "black"
}
"editor.formatOnSave": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
2 changes: 1 addition & 1 deletion PKG-INFO
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Author-email: [email protected]
Requires-Python: >=3.9,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: web3 (>=5.22.0,<6)
Requires-Dist: web3 (>=6,<7)
Description-Content-Type: text/markdown

This library works by injecting a new module in the Web3.py instance, which allows
Expand Down
26 changes: 13 additions & 13 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def main() -> None:
print(f"Sender address: {sender.address}")
print(f"Receiver address: {receiverAddress}")
print(
f"Sender account balance: {Web3.fromWei(w3.eth.get_balance(sender.address), 'ether')} ETH"
f"Sender account balance: {Web3.from_wei(w3.eth.get_balance(sender.address), 'ether')} ETH"
)
print(
f"Receiver account balance: {Web3.fromWei(w3.eth.get_balance(receiverAddress), 'ether')} ETH"
f"Receiver account balance: {Web3.from_wei(w3.eth.get_balance(receiverAddress), 'ether')} ETH"
)

# bundle two EIP-1559 (type 2) transactions, pre-sign one of them
Expand All @@ -106,10 +106,10 @@ def main() -> None:
nonce = w3.eth.get_transaction_count(sender.address)
tx1: TxParams = {
"to": receiverAddress,
"value": Web3.toWei(0.001, "ether"),
"value": Web3.to_wei(0.001, "ether"),
"gas": 21000,
"maxFeePerGas": Web3.toWei(200, "gwei"),
"maxPriorityFeePerGas": Web3.toWei(50, "gwei"),
"maxFeePerGas": Web3.to_wei(200, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(50, "gwei"),
"nonce": nonce,
"chainId": NETWORK_CONFIG[NETWORK]["chain_id"],
"type": 2,
Expand All @@ -118,10 +118,10 @@ def main() -> None:

tx2: TxParams = {
"to": receiverAddress,
"value": Web3.toWei(0.001, "ether"),
"value": Web3.to_wei(0.001, "ether"),
"gas": 21000,
"maxFeePerGas": Web3.toWei(200, "gwei"),
"maxPriorityFeePerGas": Web3.toWei(50, "gwei"),
"maxFeePerGas": Web3.to_wei(200, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(50, "gwei"),
"nonce": nonce + 1,
"chainId": NETWORK_CONFIG[NETWORK]["chain_id"],
"type": 2,
Expand Down Expand Up @@ -158,15 +158,15 @@ def main() -> None:
target_block_number=block + 1,
opts={"replacementUuid": replacement_uuid},
)
print("bundleHash", w3.toHex(send_result.bundle_hash()))
print("bundleHash", w3.to_hex(send_result.bundle_hash()))

stats_v1 = w3.flashbots.get_bundle_stats(
w3.toHex(send_result.bundle_hash()), block
w3.to_hex(send_result.bundle_hash()), block
)
print("bundleStats v1", stats_v1)

stats_v2 = w3.flashbots.get_bundle_stats_v2(
w3.toHex(send_result.bundle_hash()), block
w3.to_hex(send_result.bundle_hash()), block
)
print("bundleStats v2", stats_v2)

Expand All @@ -182,10 +182,10 @@ def main() -> None:
print(f"canceled {cancel_res}")

print(
f"Sender account balance: {Web3.fromWei(w3.eth.get_balance(sender.address), 'ether')} ETH"
f"Sender account balance: {Web3.from_wei(w3.eth.get_balance(sender.address), 'ether')} ETH"
)
print(
f"Receiver account balance: {Web3.fromWei(w3.eth.get_balance(receiverAddress), 'ether')} ETH"
f"Receiver account balance: {Web3.from_wei(w3.eth.get_balance(receiverAddress), 'ether')} ETH"
)


Expand Down
28 changes: 14 additions & 14 deletions flashbots/flashbots.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self, w3: Web3, signed_tx: HexBytes, max_block_number: int):
self.max_block_number = max_block_number
self.tx = {
"signed_transaction": signed_tx,
"hash": self.w3.sha3(signed_tx),
"hash": self.w3.keccak(signed_tx),
}

def wait(self) -> bool:
Expand Down Expand Up @@ -147,12 +147,12 @@ def sign_bundle(
if tx.get("nonce") is None:
tx["nonce"] = nonces.get(
signer.address,
self.web3.eth.get_transaction_count(signer.address),
self.w3.eth.get_transaction_count(signer.address),
)
nonces[signer.address] = tx["nonce"] + 1

if "gas" not in tx:
tx["gas"] = self.web3.eth.estimateGas(tx)
tx["gas"] = self.w3.eth.estimate_gas(tx)

signed_tx = signer.sign_transaction(tx)
signed_transactions.append(signed_tx.rawTransaction)
Expand Down Expand Up @@ -192,7 +192,7 @@ def sign_bundle(

unsigned_tx = serializable_unsigned_transaction_from_dict(tx_dict)
raw = encode_transaction(unsigned_tx, vrs=(v, r, s))
assert self.web3.keccak(raw) == tx["hash"]
assert self.w3.keccak(raw) == tx["hash"]
signed_transactions.append(raw)

return signed_transactions
Expand Down Expand Up @@ -243,7 +243,7 @@ def send_bundle_munger(
) -> List[Any]:
signed_txs = self.sign_bundle(bundled_transactions)
self.response = FlashbotsBundleResponse(
self.web3, signed_txs, target_block_number
self.w3, signed_txs, target_block_number
)
return self.send_raw_bundle_munger(signed_txs, target_block_number, opts)

Expand Down Expand Up @@ -286,22 +286,22 @@ def simulate(
):
# interpret block number from tag
block_number = (
self.web3.eth.block_number
self.w3.eth.block_number
if block_tag is None or block_tag == "latest"
else block_tag
)

# sets evm params
evm_block_number = self.web3.toHex(block_number)
evm_block_number = self.w3.to_hex(block_number)
evm_block_state_number = (
self.web3.toHex(state_block_tag)
self.w3.to_hex(state_block_tag)
if state_block_tag is not None
else self.web3.toHex(block_number - 1)
else self.w3.to_hex(block_number - 1)
)
evm_timestamp = (
block_timestamp
if block_timestamp is not None
else self.extrapolate_timestamp(block_number, self.web3.eth.block_number)
else self.extrapolate_timestamp(block_number, self.w3.eth.block_number)
)

signed_bundled_transactions = self.sign_bundle(bundled_transactions)
Expand All @@ -327,7 +327,7 @@ def extrapolate_timestamp(self, block_tag: int, latest_block_number: int):
block_delta = block_tag - latest_block_number
if block_delta < 0:
raise Exception("block extrapolation negative")
return self.web3.eth.get_block(latest_block_number)["timestamp"] + (
return self.w3.eth.get_block(latest_block_number)["timestamp"] + (
block_delta * SECONDS_PER_BLOCK
)

Expand Down Expand Up @@ -357,7 +357,7 @@ def call_bundle_munger(
)

def get_user_stats_munger(self) -> List:
return [{"blockNumber": hex(self.web3.eth.blockNumber)}]
return [{"blockNumber": hex(self.w3.eth.block_number)}]

getUserStats: Method[Callable[[Any], Any]] = Method(
json_rpc_method=FlashbotsRPC.flashbots_getUserStats,
Expand Down Expand Up @@ -414,14 +414,14 @@ def send_private_transaction_munger(
)
if max_block_number is None:
# get current block num, add 25
current_block = self.web3.eth.block_number
current_block = self.w3.eth.block_number
max_block_number = current_block + 25
params = {
"tx": self.to_hex(signed_transaction),
"maxBlockNumber": max_block_number,
}
self.response = FlashbotsPrivateTransactionResponse(
self.web3, signed_transaction, max_block_number
self.w3, signed_transaction, max_block_number
)
return [params]

Expand Down
2 changes: 1 addition & 1 deletion flashbots/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
text=Web3.keccak(text=request_data.decode("utf-8")).hex()
)
signed_message = Account.sign_message(
message, private_key=self.signature_account.privateKey.hex()
message, private_key=self.signature_account._private_key
)

headers = self.get_request_headers() | {
Expand Down
Loading

0 comments on commit f0062a5

Please sign in to comment.