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: wallet address invoke contract input validation #62

Merged
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
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

## Unreleased

### Added

- Wallet address contract invocation input validation for payable contracts.

### [0.12.0] - 2024-12-06

### Added

- Use Poetry as the dependency manager
- Relax dependency version constraints

### [0.11.0] - 2024-11-27

### Added

- Add support for funding wallets (Alpha feature release)
- Must reach out to CDP SDK Discord channel to be considered for this feature.
- Must reach out to CDP SDK Discord channel to be considered for this feature.
- Added create and update feature for `SmartContractEventActivity` webhook and its related event type filter.

### Fixed

- Fix bug in `Asset.from_model` where passed in asset ID was not used when creating a gwei or wei asset.

## [0.10.4] - 2024-11-25
Expand Down Expand Up @@ -57,7 +64,7 @@
### Changed

- Make faucet transactions async i.e. using `faucet_tx.wait()` to wait for the transaction to be confirmed.
- This will make the SDK more consistent and make faucet transactions more reliable.
- This will make the SDK more consistent and make faucet transactions more reliable.

## [0.0.9] - 2024-10-29

Expand Down
6 changes: 6 additions & 0 deletions cdp/wallet_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,15 @@ def invoke_contract(
Returns:
ContractInvocation: The contract invocation object.
Raises:
ValueError: If an amount is provided and an asset_id does not exist
"""
normalized_amount = Decimal(amount) if amount else Decimal("0")

if normalized_amount > 0.0 and not asset_id:
raise ValueError("Asset ID is required for contract invocation if an amount is provided")

if amount and asset_id:
self._ensure_sufficient_balance(normalized_amount, asset_id)

Expand Down
21 changes: 21 additions & 0 deletions tests/test_wallet_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,29 @@ def test_invoke_contract(
@patch("cdp.wallet_address.ContractInvocation")
@patch("cdp.Cdp.api_clients")
@patch("cdp.Cdp.use_server_signer", False)
def test_invoke_contract_with_invalid_input(
mock_api_clients, mock_contract_invocation, wallet_address_factory, balance_model_factory
):
"""Test the invoke_contract method raises an error with invalid input."""
wallet_address_with_key = wallet_address_factory(key=True)

with pytest.raises(Exception, match="Asset ID is required for contract invocation if an amount is provided"):
wallet_address_with_key.invoke_contract(
contract_address="0xcontractaddress",
method="testMethod",
abi=[{"abi": "data"}],
args={"arg1": "value1"},
amount=Decimal("1"),
)


@ patch("cdp.wallet_address.ContractInvocation")
@ patch("cdp.Cdp.api_clients")
@ patch("cdp.Cdp.use_server_signer", False)
def test_invoke_contract_api_error(
mock_api_clients, mock_contract_invocation, wallet_address_factory, balance_model_factory


):
"""Test the invoke_contract method raises an error when the create API call fails."""
wallet_address_with_key = wallet_address_factory(key=True)
Expand Down
Loading