Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Usage: uv run python evm/scoped_account.send_transaction_custom.py
# Note: Must set CUSTOM_RPC_URL in .env

import asyncio
import os
from cdp import CdpClient
from cdp.evm_transaction_types import TransactionRequestEIP1559
from dotenv import load_dotenv
from web3 import Web3

load_dotenv()

async def main():
async with CdpClient() as cdp:
account = await cdp.evm.get_or_create_account(name="Playground-Account")
print(f"Account: {account.address}")
# Create a network-scoped account using custom RPC
polygon_account = await account.__experimental_use_network__(
network="https://polygon-rpc.com"
)

# Test send_transaction with TransactionRequestEIP1559
print("Sending transaction...")
transaction_hash = await polygon_account.send_transaction(
transaction=TransactionRequestEIP1559(
to="0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
value=Web3.to_wei(0.000001, "ether"),
)
)
print(f"Transaction hash: {transaction_hash}")

print("Waiting for transaction confirmation...")
receipt = await polygon_account.wait_for_transaction_receipt(
transaction_hash=transaction_hash
)
print(f"Transaction receipt: {receipt}")

if __name__ == "__main__":
asyncio.run(main())
2 changes: 1 addition & 1 deletion examples/python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion python/cdp/actions/evm/swap/send_swap_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __init__(self, address: str, owners: list[BaseAccount]):
api_clients,
SignAndWrapTypedDataForSmartAccountOptions(
smart_account=smart_account_interface,
chain_id=1 if options.network == "ethereum" else 8453, # Base chain ID
chain_id=1 if options.network == "ethereum" else 8453,
typed_data=swap_data.permit2_data.eip712,
owner_index=0,
idempotency_key=permit2_idempotency_key,
Expand Down
6 changes: 2 additions & 4 deletions python/cdp/evm_server_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,7 @@ async def wait_for_fund_operation_receipt(
interval_seconds=interval_seconds,
)

async def __experimental_use_network__(
self, network: str | None = None, rpc_url: str | None = None
):
async def __experimental_use_network__(self, network: str | None = None):
"""Create a network-scoped version of this account.

Args:
Expand All @@ -737,7 +735,7 @@ async def __experimental_use_network__(
"""
from cdp.network_scoped_evm_server_account import NetworkScopedEvmServerAccount

return NetworkScopedEvmServerAccount(self, network, rpc_url)
return NetworkScopedEvmServerAccount(self, network)

async def use_spend_permission(
self,
Expand Down
2 changes: 1 addition & 1 deletion python/cdp/evm_smart_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ async def use_spend_permission(
paymaster_url=paymaster_url,
)

async def __experimental_use_network__(self, network: str):
async def __experimental_use_network__(self, network: str | None = None):
"""Create a network-scoped version of this smart account.

Args:
Expand Down
28 changes: 28 additions & 0 deletions python/cdp/network_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Network configuration for EVM chains."""

from web3 import Web3

# Network to chain ID mapping
NETWORK_TO_CHAIN_ID: dict[str, int] = {
# Ethereum networks
Expand Down Expand Up @@ -56,6 +58,32 @@ def get_network_name(chain_id: int) -> str | None:
return CHAIN_ID_TO_NETWORK.get(chain_id)


def resolve_chain_id_from_rpc_url(rpc_url: str) -> int:
"""Resolve chain ID from an RPC URL by making a call to the endpoint.

Args:
rpc_url: The RPC URL to resolve

Returns:
Chain ID for the network

Raises:
ValueError: If the RPC URL is invalid or the chain ID cannot be resolved
Exception: If there's an error connecting to the RPC endpoint

"""
try:
# Create a temporary web3 instance to get the chain ID
w3 = Web3(Web3.HTTPProvider(rpc_url))

# Get the chain ID
chain_id = w3.eth.chain_id

return chain_id
except Exception as e:
raise ValueError(f"Failed to resolve chain ID from RPC URL {rpc_url}: {e!s}") from e


def is_supported_network(network: str) -> bool:
"""Check if a network is supported.

Expand Down
Loading
Loading