Skip to content

Commit

Permalink
feat: wallet address invoke contract input validation (#62)
Browse files Browse the repository at this point in the history
* wallet address invoke contract input validation
* implementing feedback
  • Loading branch information
stat authored Dec 10, 2024
1 parent 018313a commit 115dbcd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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

0 comments on commit 115dbcd

Please sign in to comment.