From f091d68759cdbc17c075640c3763f154a3bf9fd0 Mon Sep 17 00:00:00 2001 From: rifle Date: Tue, 29 Oct 2024 11:26:35 +0800 Subject: [PATCH 1/3] fix: transfer amount less than or equal to the balance. --- cdp/wallet_address.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdp/wallet_address.py b/cdp/wallet_address.py index 389012f..98f0a8a 100644 --- a/cdp/wallet_address.py +++ b/cdp/wallet_address.py @@ -336,7 +336,7 @@ def _ensure_sufficient_balance(self, amount: Decimal, asset_id: str) -> None: """ current_balance = self.balance(asset_id) - if amount < current_balance: + if amount <= current_balance: return raise InsufficientFundsError(expected=amount, exact=current_balance) From d9d7a444a435e13adf0a262a7a77b9abfcd1e290 Mon Sep 17 00:00:00 2001 From: riflecode Date: Tue, 29 Oct 2024 20:28:10 +0800 Subject: [PATCH 2/3] feat: perform a test to ensure the balance is sufficient for the full amount. --- tests/test_wallet_address.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_wallet_address.py b/tests/test_wallet_address.py index 7e522f8..9c86fac 100644 --- a/tests/test_wallet_address.py +++ b/tests/test_wallet_address.py @@ -999,3 +999,23 @@ def test_deploy_multi_token_broadcast_api_error(mock_smart_contract, wallet_addr 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.Cdp.api_clients") +def test_ensure_sufficient_balance_sufficient_full_amount( + mock_api_clients, wallet_address_factory, balance_model_factory +): + """Test the ensure_sufficient_balance method with sufficient full amount balance.""" + wallet_address = wallet_address_factory() + balance_model = balance_model_factory( + amount="1500000000000000000", network_id="base-sepolia", asset_id="eth", decimals=18 + ) + + mock_get_balance = Mock() + mock_get_balance.return_value = balance_model + mock_api_clients.external_addresses.get_external_address_balance = mock_get_balance + + wallet_address._ensure_sufficient_balance(Decimal("1.5"), "eth") + + mock_get_balance.assert_called_once_with( + network_id=wallet_address.network_id, address_id=wallet_address.address_id, asset_id="eth" + ) From f03d4375b718839e0a0bb7a3dcbdf19d6cc7b56f Mon Sep 17 00:00:00 2001 From: riflecode Date: Tue, 29 Oct 2024 23:11:09 +0800 Subject: [PATCH 3/3] feat: fix code style --- tests/test_wallet_address.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_wallet_address.py b/tests/test_wallet_address.py index 9c86fac..a04d6f2 100644 --- a/tests/test_wallet_address.py +++ b/tests/test_wallet_address.py @@ -1000,6 +1000,7 @@ def test_deploy_multi_token_broadcast_api_error(mock_smart_contract, wallet_addr mock_smart_contract_instance.sign.assert_called_once_with(wallet_address_with_key.key) mock_smart_contract_instance.broadcast.assert_called_once() + @patch("cdp.Cdp.api_clients") def test_ensure_sufficient_balance_sufficient_full_amount( mock_api_clients, wallet_address_factory, balance_model_factory