Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(py): configurable eth account wallet provider rpc_url #474

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions python/coinbase-agentkit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- Added `rpc_url` to `EthAccountWalletProvider` configurable options.

### Fixed

- Fixed erc20 `get_balance` action to format erc20 balance with correct number of decimals.
Expand Down
95 changes: 66 additions & 29 deletions python/coinbase-agentkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@ AgentKit is a framework for easily enabling AI agents to take actions onchain. I
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Usage](#usage)
- [Create an AgentKit instance](#create-an-agentkit-instance)
- [Create an AgentKit instance with a specified wallet provider](#create-an-agentkit-instance-with-a-specified-wallet-provider)
- [Create an AgentKit instance with specified action providers](#create-an-agentkit-instance-with-specified-action-providers)
- [Use with a framework extension (e.g., LangChain + OpenAI)](#use-with-a-framework-extension)
- [Create an AgentKit instance](#create-an-agentkit-instance)
- [Create an AgentKit instance with a specified wallet provider](#create-an-agentkit-instance-with-a-specified-wallet-provider)
- [Create an AgentKit instance with specified action providers](#create-an-agentkit-instance-with-specified-action-providers)
- [Use with a framework extension (e.g., LangChain + OpenAI)](#use-with-a-framework-extension)
- [Creating an Action Provider](#creating-an-action-provider)
- [Adding Actions to your Action Provider](#adding-actions-to-your-action-provider)
- [Adding Actions that use a Wallet Provider](#adding-actions-that-use-a-wallet-provider)
- [Adding an Action Provider to your AgentKit instance](#adding-an-action-provider-to-your-agentkit-instance)
- [Adding Actions to your Action Provider](#adding-actions-to-your-action-provider)
- [Adding Actions that use a Wallet Provider](#adding-actions-that-use-a-wallet-provider)
- [Adding an Action Provider to your AgentKit instance](#adding-an-action-provider-to-your-agentkit-instance)
- [Wallet Providers](#wallet-providers)
- [CdpWalletProvider](#cdpwalletprovider)
- [Network Configuration](#network-configuration)
- [Configuring from an existing CDP API Wallet](#configuring-from-an-existing-cdp-api-wallet)
- [Configuring from a mnemonic phrase](#configuring-from-a-mnemonic-phrase)
- [Exporting a wallet](#exporting-a-wallet)
- [Importing a wallet from WalletData JSON string](#importing-a-wallet-from-walletdata-json-string)
- [Configuring gas parameters](#configuring-cdpwalletprovider-gas-parameters)
- [EthAccountWalletProvider](#ethaccountwalletprovider)
- [Configuring gas parameters](#configuring-ethaccountwalletprovider-gas-parameters)
- [CdpWalletProvider](#cdpwalletprovider)
- [Network Configuration](#network-configuration)
- [Configuring from an existing CDP API Wallet](#configuring-from-an-existing-cdp-api-wallet)
- [Configuring from a mnemonic phrase](#configuring-from-a-mnemonic-phrase)
- [Exporting a wallet](#exporting-a-wallet)
- [Importing a wallet from WalletData JSON string](#importing-a-wallet-from-walletdata-json-string)
- [Configuring gas parameters](#configuring-cdpwalletprovider-gas-parameters)
- [EthAccountWalletProvider](#ethaccountwalletprovider)
- [Configuring gas parameters](#configuring-ethaccountwalletprovider-gas-parameters)
- [Contributing](#contributing)

## Getting Started

*Prerequisites*:
_Prerequisites_:

- [Python 3.10+](https://www.python.org/downloads/)
- [CDP Secret API Key](https://docs.cdp.coinbase.com/get-started/docs/cdp-api-keys#creating-secret-api-keys)

Expand All @@ -54,9 +56,9 @@ agent_kit = AgentKit()

```python
from coinbase_agentkit import (
AgentKit,
AgentKitConfig,
CdpWalletProvider,
AgentKit,
AgentKitConfig,
CdpWalletProvider,
CdpWalletProviderConfig
)

Expand Down Expand Up @@ -97,7 +99,8 @@ agent_kit = AgentKit(AgentKitConfig(

Example using LangChain + OpenAI:

*Prerequisites*:
_Prerequisites_:

- [OpenAI API Key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key)
- Set `OPENAI_API_KEY` environment variable

Expand Down Expand Up @@ -131,7 +134,7 @@ from coinbase_agentkit.network import Network
class MyActionProvider(ActionProvider[WalletProvider]):
def __init__(self):
super().__init__("my-action-provider", [])

# Define if the action provider supports the given network
def supports_network(self, network: Network) -> bool:
return True
Expand Down Expand Up @@ -207,6 +210,7 @@ agent_kit = AgentKit(AgentKitConfig(
AgentKit supports the following wallet providers:

EVM:

- [CdpWalletProvider](https://github.com/coinbase/agentkit/blob/master/python/coinbase_agentkit/wallet_providers/cdp_wallet_provider.py) - Uses the Coinbase Developer Platform (CDP) API Wallet
- [EthAccountWalletProvider](https://github.com/coinbase/agentkit/blob/master/python/coinbase_agentkit/wallet_providers/eth_account_wallet_provider.py) - Uses a local private key for any EVM-compatible chain

Expand Down Expand Up @@ -247,7 +251,6 @@ wallet_provider = CdpWalletProvider(CdpWalletProviderConfig(

The `CdpWalletProvider` can be configured from a mnemonic phrase by passing the `mnemonic_phrase` and `network_id` parameters to the `CdpWalletProviderConfig`. If `network_id` is not defined, the `CdpWalletProvider` will fall back to the env var `NETWORK_ID`, and if that is not defined, it will default to `base-sepolia`.


```python
from coinbase_agentkit import CdpWalletProvider, CdpWalletProviderConfig

Expand Down Expand Up @@ -315,9 +318,9 @@ import os
from eth_account import Account

from coinbase_agentkit import (
AgentKit,
AgentKitConfig,
EthAccountWalletProvider,
AgentKit,
AgentKitConfig,
EthAccountWalletProvider,
EthAccountWalletProviderConfig
)

Expand Down Expand Up @@ -350,9 +353,9 @@ import os
from eth_account import Account

from coinbase_agentkit import (
AgentKit,
AgentKitConfig,
EthAccountWalletProvider,
AgentKit,
AgentKitConfig,
EthAccountWalletProvider,
EthAccountWalletProviderConfig
)

Expand All @@ -378,6 +381,40 @@ agent_kit = AgentKit(AgentKitConfig(
))
```

#### Configuring `EthAccountWalletProvider` rpc url

The `EthAccountWalletProvider` also exposes parameters for defining the rpc url manually.

```python
import os
from eth_account import Account

from coinbase_agentkit import (
AgentKit,
AgentKitConfig,
EthAccountWalletProvider,
EthAccountWalletProviderConfig
)

private_key = os.environ.get("PRIVATE_KEY")
assert private_key is not None, "You must set PRIVATE_KEY environment variable"
assert private_key.startswith("0x"), "Private key must start with 0x hex prefix"

account = Account.from_key(private_key)

wallet_provider = EthAccountWalletProvider(
config=EthAccountWalletProviderConfig(
account=account,
chain_id="84532",
rpc_url="https://sepolia.base.org",
)
)

agent_kit = AgentKit(AgentKitConfig(
wallet_provider=wallet_provider
))
```

## Contributing

See [CONTRIBUTING.md](https://github.com/coinbase/agentkit/blob/master/CONTRIBUTING.md) for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class EthAccountWalletProviderConfig(BaseModel):
account: LocalAccount
chain_id: str
gas: EvmGasConfig | None = Field(None, description="Gas configuration settings")
rpc_url: str | None = Field(None, description="Optional RPC URL to override default chain RPC")

class Config:
"""Configuration for EthAccountWalletProvider."""
Expand All @@ -42,7 +43,7 @@ def __init__(self, config: EthAccountWalletProviderConfig):
self.account = config.account

chain = NETWORK_ID_TO_CHAIN[CHAIN_ID_TO_NETWORK_ID[config.chain_id]]
rpc_url = chain.rpc_urls["default"].http[0]
rpc_url = config.rpc_url or chain.rpc_urls["default"].http[0]

self.web3 = Web3(Web3.HTTPProvider(rpc_url))
self.web3.middleware_onion.inject(
Expand Down