diff --git a/cdp/transaction.py b/cdp/transaction.py index e314838..816ad1a 100644 --- a/cdp/transaction.py +++ b/cdp/transaction.py @@ -130,7 +130,7 @@ def raw(self) -> DynamicFeeTransaction: "type": "0x2", # EIP-1559 transaction type } - # Handle the 'to' field separately + # Handle 'to' field separately since smart contract deployments have an empty 'to' field if parsed_payload["to"]: transaction_dict["to"] = Web3.to_bytes(hexstr=parsed_payload["to"]) else: diff --git a/tests/test_wallet.py b/tests/test_wallet.py index 3b29ab1..d7b4133 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -615,7 +615,6 @@ def test_wallet_deploy_token_no_default_address(wallet_factory): wallet.deploy_token(name="TestToken", symbol="TT", total_supply="1000000") -# Add similar tests for deploy_nft and deploy_multi_token with no default address @patch("cdp.Cdp.use_server_signer", True) def test_wallet_deploy_nft_no_default_address(wallet_factory): """Test the deploy_nft method of a Wallet with no default address.""" diff --git a/tests/test_wallet_address.py b/tests/test_wallet_address.py index 47758b7..ff0fd36 100644 --- a/tests/test_wallet_address.py +++ b/tests/test_wallet_address.py @@ -753,3 +753,105 @@ def test_wallet_address_deploy_multi_token_with_server_signer(mock_api_clients, # Verify that sign and broadcast methods are not called when using server signer mock_smart_contract.sign.assert_not_called() mock_smart_contract.broadcast.assert_not_called() + + +@patch("cdp.wallet_address.SmartContract") +@patch("cdp.Cdp.api_clients") +@patch("cdp.Cdp.use_server_signer", False) +def test_deploy_token_api_error(mock_api_clients, mock_smart_contract, wallet_address_with_key): + """Test the deploy_token method raises an error when the create API call fails.""" + mock_smart_contract.create.side_effect = Exception("API Error") + + with pytest.raises(Exception, match="API Error"): + wallet_address_with_key.deploy_token(name="TestToken", symbol="TT", total_supply="1000000") + + mock_smart_contract.create.assert_called_once() + + +@patch("cdp.wallet_address.SmartContract") +@patch("cdp.Cdp.api_clients") +@patch("cdp.Cdp.use_server_signer", False) +def test_deploy_token_broadcast_api_error( + mock_api_clients, mock_smart_contract, wallet_address_with_key +): + """Test the deploy_token method raises an error when the broadcast API call fails.""" + mock_smart_contract_instance = Mock(spec=SmartContract) + mock_smart_contract.create.return_value = mock_smart_contract_instance + mock_smart_contract_instance.broadcast.side_effect = Exception("API Error") + + with pytest.raises(Exception, match="API Error"): + wallet_address_with_key.deploy_token(name="TestToken", symbol="TT", total_supply="1000000") + + mock_smart_contract.create.assert_called_once() + mock_smart_contract_instance.sign.assert_called_once_with(wallet_address_with_key.key) + mock_smart_contract_instance.broadcast.assert_called_once() + + +@patch("cdp.wallet_address.SmartContract") +@patch("cdp.Cdp.api_clients") +@patch("cdp.Cdp.use_server_signer", False) +def test_deploy_nft_api_error(mock_api_clients, mock_smart_contract, wallet_address_with_key): + """Test the deploy_nft method raises an error when the create API call fails.""" + mock_smart_contract.create.side_effect = Exception("API Error") + + with pytest.raises(Exception, match="API Error"): + wallet_address_with_key.deploy_nft( + name="TestNFT", symbol="TNFT", base_uri="https://example.com/nft/" + ) + + mock_smart_contract.create.assert_called_once() + + +@patch("cdp.wallet_address.SmartContract") +@patch("cdp.Cdp.api_clients") +@patch("cdp.Cdp.use_server_signer", False) +def test_deploy_nft_broadcast_api_error( + mock_api_clients, mock_smart_contract, wallet_address_with_key +): + """Test the deploy_nft method raises an error when the broadcast API call fails.""" + mock_smart_contract_instance = Mock(spec=SmartContract) + mock_smart_contract.create.return_value = mock_smart_contract_instance + mock_smart_contract_instance.broadcast.side_effect = Exception("API Error") + + with pytest.raises(Exception, match="API Error"): + wallet_address_with_key.deploy_nft( + name="TestNFT", symbol="TNFT", base_uri="https://example.com/nft/" + ) + + mock_smart_contract.create.assert_called_once() + mock_smart_contract_instance.sign.assert_called_once_with(wallet_address_with_key.key) + mock_smart_contract_instance.broadcast.assert_called_once() + + +@patch("cdp.wallet_address.SmartContract") +@patch("cdp.Cdp.api_clients") +@patch("cdp.Cdp.use_server_signer", False) +def test_deploy_multi_token_api_error( + mock_api_clients, mock_smart_contract, wallet_address_with_key +): + """Test the deploy_multi_token method raises an error when the create API call fails.""" + mock_smart_contract.create.side_effect = Exception("API Error") + + with pytest.raises(Exception, match="API Error"): + wallet_address_with_key.deploy_multi_token(uri="https://example.com/multi-token/{id}.json") + + mock_smart_contract.create.assert_called_once() + + +@patch("cdp.wallet_address.SmartContract") +@patch("cdp.Cdp.api_clients") +@patch("cdp.Cdp.use_server_signer", False) +def test_deploy_multi_token_broadcast_api_error( + mock_api_clients, mock_smart_contract, wallet_address_with_key +): + """Test the deploy_multi_token method raises an error when the broadcast API call fails.""" + mock_smart_contract_instance = Mock(spec=SmartContract) + mock_smart_contract.create.return_value = mock_smart_contract_instance + mock_smart_contract_instance.broadcast.side_effect = Exception("API Error") + + with pytest.raises(Exception, match="API Error"): + wallet_address_with_key.deploy_multi_token(uri="https://example.com/multi-token/{id}.json") + + mock_smart_contract.create.assert_called_once() + mock_smart_contract_instance.sign.assert_called_once_with(wallet_address_with_key.key) + mock_smart_contract_instance.broadcast.assert_called_once()