-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Wrap ETH to WETH on Base Action
- Loading branch information
1 parent
1335967
commit 81d6b11
Showing
16 changed files
with
210 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from collections.abc import Callable | ||
|
||
from cdp import Wallet | ||
from pydantic import BaseModel, Field | ||
|
||
from cdp_agentkit_core.actions import CdpAction | ||
|
||
WETH_ADDRESS = "0x4200000000000000000000000000000000000006" | ||
|
||
WETH_ABI = [ | ||
{ | ||
"inputs": [], | ||
"name": "deposit", | ||
"outputs": [], | ||
"stateMutability": "payable", | ||
"type": "function", | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"name": "account", | ||
"type": "address", | ||
}, | ||
], | ||
"name": "balanceOf", | ||
"outputs": [ | ||
{ | ||
"type": "uint256", | ||
}, | ||
], | ||
"stateMutability": "view", | ||
"type": "function", | ||
}, | ||
] | ||
|
||
WRAP_ETH_PROMPT = """ | ||
This tool can only be used to wrap ETH to WETH. | ||
Do not use this tool for any other purpose, or trading other assets. | ||
Inputs: | ||
- Amount of ETH to wrap. | ||
Important notes: | ||
- The amount is a string and cannot have any decimal points, since the unit of measurement is wei. | ||
- Make sure to use the exact amount provided, and if there's any doubt, check by getting more information before continuing with the action. | ||
- 1 wei = 0.000000000000000001 WETH | ||
- Minimum purchase amount is 100000000000000 wei (0.0000001 WETH) | ||
- Only supported on the following networks: | ||
- Base Sepolia (ie, 'base-sepolia') | ||
- Base Mainnet (ie, 'base', 'base-mainnnet') | ||
""" | ||
|
||
|
||
class WrapEthInput(BaseModel): | ||
"""Input argument schema for wrapping ETH to WETH.""" | ||
|
||
amount_to_wrap: str = Field( | ||
..., | ||
description="Amount of ETH to wrap in wei", | ||
) | ||
|
||
|
||
def wrap_eth(wallet: Wallet, amount_to_wrap: str) -> str: | ||
"""Wrap ETH to WETH. | ||
Args: | ||
wallet (Wallet): The wallet to wrap ETH from. | ||
amount_to_wrap (str): The amount of ETH to wrap in wei. | ||
Returns: | ||
str: A message containing the wrapped ETH details. | ||
""" | ||
try: | ||
invocation = wallet.invoke_contract( | ||
contract_address=WETH_ADDRESS, | ||
method="deposit", | ||
abi=WETH_ABI, | ||
args={}, | ||
amount=amount_to_wrap, | ||
asset_id="wei", | ||
) | ||
result = invocation.wait() | ||
return f"Wrapped ETH with transaction hash: {result.transaction.transaction_hash}" | ||
except Exception as e: | ||
return f"Unexpected error wrapping ETH: {e!s}" | ||
|
||
|
||
class WrapEthAction(CdpAction): | ||
"""Wrap ETH to WETH action.""" | ||
|
||
name: str = "wrap_eth" | ||
description: str = WRAP_ETH_PROMPT | ||
args_schema: type[BaseModel] | None = WrapEthInput | ||
func: Callable[..., str] = wrap_eth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from cdp_agentkit_core.actions.wrap_eth import ( | ||
WETH_ABI, | ||
WETH_ADDRESS, | ||
WrapEthAction, | ||
WrapEthInput, | ||
wrap_eth, | ||
) | ||
|
||
|
||
def test_wrap_eth_success(wallet_factory, contract_invocation_factory): | ||
"""Test successful ETH wrapping.""" | ||
mock_wallet = wallet_factory() | ||
mock_invocation = contract_invocation_factory() | ||
|
||
mock_wallet.invoke_contract.return_value = mock_invocation | ||
mock_invocation.wait.return_value = mock_invocation | ||
|
||
amount = "1000000000000000000" # 1 ETH in wei | ||
|
||
with ( | ||
patch.object( | ||
mock_wallet, "invoke_contract", return_value=mock_invocation | ||
) as mock_invoke_contract, | ||
patch.object(mock_invocation, "wait", return_value=mock_invocation) as mock_invocation_wait, | ||
): | ||
result = wrap_eth(mock_wallet, amount) | ||
|
||
mock_invoke_contract.assert_called_once_with( | ||
contract_address=WETH_ADDRESS, | ||
method="deposit", | ||
abi=WETH_ABI, | ||
args={}, | ||
amount=amount, | ||
asset_id="wei", | ||
) | ||
mock_invocation_wait.assert_called_once_with() | ||
|
||
assert result == f"Wrapped ETH with transaction hash: {mock_invocation.transaction_hash}" | ||
|
||
|
||
def test_wrap_eth_failure(wallet_factory): | ||
"""Test ETH wrapping failure.""" | ||
mock_wallet = wallet_factory() | ||
mock_wallet.invoke_contract.side_effect = Exception("Test error") | ||
|
||
amount = "1000000000000000000" | ||
result = wrap_eth(mock_wallet, amount) | ||
|
||
assert result == "Unexpected error wrapping ETH: Test error" | ||
|
||
|
||
def test_wrap_eth_action_initialization(): | ||
"""Test WrapEthAction initialization and attributes.""" | ||
action = WrapEthAction() | ||
|
||
assert action.name == "wrap_eth" | ||
assert action.args_schema == WrapEthInput | ||
assert callable(action.func) | ||
|
||
|
||
def test_wrap_eth_input_model_valid(): | ||
"""Test WrapEthInput accepts valid parameters.""" | ||
valid_input = WrapEthInput(amount_to_wrap="1000000000000000000") | ||
assert valid_input.amount_to_wrap == "1000000000000000000" | ||
|
||
|
||
def test_wrap_eth_input_model_missing_params(): | ||
"""Test WrapEthInput raises error when params are missing.""" | ||
with pytest.raises(ValueError): | ||
WrapEthInput() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters