BlockIo / block_io-php

PHP library for Block.io
MIT License
56 stars 139 forks source link

Exceptions #18

Closed dwjorgeb closed 3 years ago

dwjorgeb commented 7 years ago

Type: Exception

Message: Failed: Cannot withdraw funds without Network Fee of xxxxxxxxxx BTC. Maximum withdrawable balance is xxxxxxxxx BTC.

Filename: /vendor/block_io-php/block_io-php/lib/block_io.php

Line Number: 121

So, I want to merge all my balance from all addresses into my main address.

For that, I made a call to get_address_balance and it gives me the available_balance, but I have to deduct network fees.

So, I needed to make a call to get_network_fee_estimate, with the available balance, so it fails and returns me the max_withdrawal_available, so, then, I withdraw that amount from all those addresses into the main one.

That's all fine, except that, instead of failing silently (like $op->status == 'fail') and returning the max_withdrawal_available, the get_network_fee_estimate call throws an exception, which crashes the script.

I expected that when a call failed, it would give a "fail" status but not for it to throw an exception and crash the script. Or, at the very least, add a max_withdrawable_balance call, which would automatically deduct the network fees and give a withdrawable balance.

My question, is it ok for me to get rid of the line 121 ? if ($json_result->status != 'success') { throw new Exception('Failed: ' . $json_result->data->error_message); } or will it break something else?

Thanks!

doersf commented 7 years ago

Removing that will break the script in the sense that you wouldn't get other failure errors thrown as exceptions. If you are able to handle those yourself, this is fine.

The way to handle this with Line 121 intact would be to catch the exception and parse the text showing the available balance for withdrawal. The amount should be usable without any further calls.

The new amount, if used in the same withdrawal API call (swapping out of the old amount you used), will execute the withdrawal of the total balance to the specified destination(s).

We will add an "amounts=max" option to handle this internally in the future, so you will be able to just use "amounts=max" and sign the transaction, and execute the withdrawal without any need for parsing errors like this.

dwjorgeb commented 7 years ago

Thank you for your quick response. I have commented out line 121, since I'm already checking every operation with if ($op->status == 'success') That is somewhat similar to what I'm doing, I get the addresses' available balance, do an estimate_network_fee call, which fails and gives me the max withdrawable amount, then I swap the withdraw amount with that value and call the withdraw method.

The amount=max would be greatly appreciated, it would streamline a lot of code and would require less API calls to your backend, instead of 3 calls (available balance > network fee estimate > withdraw) it would require only one (withdraw amounts=max).

Thanks!

stringsko commented 4 years ago

+1

aflorea4 commented 3 years ago

+1

doersf commented 3 years ago

Additional data will be passed for API exceptions. See 2.0-dev/examples/basic.php.