KomodoPlatform / komodo-wallet

Komodo Wallet
https://app.komodoplatform.com
GNU General Public License v3.0
240 stars 208 forks source link

[BUG]: not enough balance error when starting swaps #2210

Closed cipig closed 1 week ago

cipig commented 1 year ago

Sometimes i get the following error when i want to start swaps: image

I can't reliably reproduce it though... sometimes, after the error shows up, switching pair and then switching back to the initial pair "fixes" the problem. atm i can't get around it, no matter what i do... even entering a different volume shows the same error, which makes no sense, since the resulting amount of JJPY-PLG20 should be totally different... see this second screenshot image trying to buy 444 RTM (instead of 3927 shows the exact same error

the error shows up after clicking on "start swap"

cipig commented 1 year ago

i get this on KMD too

i click on an order in the orderbook to buy a coin (smart chain) with KMD and log shows

[20:14:41] [info] [qt.trading.page.cpp:1207] [3852795]: [determine_fees] trade_preimage answer received: [{"error":"rpc:211] dispatcher_legacy:141] dispatcher_legacy:158] taker_swap:2366] check_balance:75] Not enough KMD for swap: available 10617.45189773, required at least 10617.45189790213127413127413127413127413127413127413127413127413127413127413127413127413127413127413, locked by swaps Some(BigDecimal(\"0\"))"}]

if i then click on "Start Swap" in GUI, i get the same error in GUI and swap does not start, but log shows

[20:17:53] [debug] [main.prerequisites.hpp:87] [3852794]: Getting fees info... 1/50
[20:17:53] [info] [qt.trading.page.cpp:1207] [3852807]: [determine_fees] trade_preimage answer received: [{"result":{"base_coin_fee":{"coin":"DEX","amount":"0.00001","amount_fraction":{"numer":"1","denom":"100000"},"amount_rat":[[1,[1]],[1,[100000]]],"paid_from_trading_vol":true},"rel_coin_fee":{"coin":"KMD","amount":"0.00001","amount_fraction":{"numer":"1","denom":"100000"},"amount_rat":[[1,[1]],[1,[100000]]],"paid_from_trading_vol":false},"taker_fee":{"coin":"KMD","amount":"0.2629343629343629343629343629343629343629343629343629343629343629343629343629343629343629343629343629","amount_fraction":{"numer":"681","denom":"2590"},"amount_rat":[[1,[681]],[1,[2590]]],"paid_from_trading_vol":false},"fee_to_send_taker_fee":{"coin":"KMD","amount":"0.00001","amount_fraction":{"numer":"1","denom":"100000"},"amount_rat":[[1,[1]],[1,[100000]]],"paid_from_trading_vol":false},"total_fees":[{"coin":"DEX","amount":"0.00001","amount_fraction":{"numer":"1","denom":"100000"},"amount_rat":[[1,[1]],[1,[100000]]],"required_balance":"0","required_balance_fraction":{"numer":"0","denom":"1"},"required_balance_rat":[[0,[]],[1,[1]]]},{"coin":"KMD","amount":"0.2629543629343629343629343629343629343629343629343629343629343629343629343629343629343629343629343629","amount_fraction":{"numer":"3405259","denom":"12950000"},"amount_rat":[[1,[3405259]],[1,[12950000]]],"required_balance":"0.2629543629343629343629343629343629343629343629343629343629343629343629343629343629343629343629343629","required_balance_fraction":{"numer":"3405259","denom":"12950000"},"required_balance_rat":[[1,[3405259]],[1,[12950000]]]}]}}]

(i changed the amount to 1 before clicking on start swap, the max amount would have eaten all my balance)

so swap would work fine, but GUI does not let me start it because of the previous determine_fees error with the max amount

the initial error on trade_preimage is when the order would use the entire available balance and likely because GUI works with decimals in max_taker_vol: https://github.com/KomodoPlatform/atomicDEX-Desktop/blob/dev/src/core/atomicdex/pages/qt.trading.page.cpp#L761 but GUI should at least let me start the swap with a lower volume, for which the second determine_fees does not show an error... atm it is stuck with the error when i would buy by using my entire balance, which is not my intention

why is trade_preimage even called when i click on an order in orderbook? is that needed? can we disable it? i see no infos that are shown before clicking on "start swap" and then a new trade_preimage is called anyway it was added here: https://github.com/KomodoPlatform/atomicDEX-Desktop/commit/4b5f594fc23420fd03fb2622bbf5331f65b319f3

smk762 commented 1 year ago

why is trade_preimage even called when i click on an order in orderbook? is that needed? can we disable it? i see no infos that are shown before clicking on "start swap" and then a new trade_preimage is called anyway

@cipig some of the trade_preimage response is used to determine other values like max tradable volume after fees. As it is a slow method to respond, I suspect its race conditions that may cause a problem. There may be some newer API methods like max_maker_vol.

Related discussion at https://github.com/KomodoPlatform/AtomicDEX-mobile-private/pull/1726

During 0.5.8 RC testing we will test this more intensively to determine if issue still present and how it can be replicated.

cipig commented 1 year ago

image trying to sell 345 IRIS out of 360 for LTC and getting this error same error when i try to sell 50% (180 IRIS)

cipig commented 1 year ago

image it happens when you are about to buy a coin that needs spending your entire balance of the other coin (or close to it) error shown in Desktop log: [15:47:55] [info] [qt.trading.page.cpp:1207] [1054554]: [determine_fees] trade_preimage answer received: [{"error":"rpc:211] dispatcher_legacy:141] dispatcher_legacy:158] taker_swap:2398] check_balance:75] Not enough BCH for swap: available 0.01670641, required at least 0.016706817338158, locked by swaps Some(BigDecimal(\"0\"))"}]

it does not always happen, i assume a rounding error and that's also what the error message indicates i assume it happens in 50% of the cases based on price of order

after price changed, i could start the swap image uuid 2101f27e-8f7b-44f8-965a-b445e44f30c6

output in logs: [15:51:06] [info] [qt.trading.page.cpp:1207] [1054552]: [determine_fees] trade_preimage answer received: [{"result":{"base_coin_fee":{"coin":"LBC","amount":"0.0001","amount_fraction":{"numer":"1","denom":"10000"},"amount_rat":[[1,[1]],[1,[10000]]],"paid_from_trading_vol":true},"rel_coin_fee":{"coin":"BCH","amount":"0.00001","amount_fraction":{"numer":"1","denom":"100000"},"amount_rat":[[1,[1]],[1,[100000]]],"paid_from_trading_vol":false},"taker_fee":{"coin":"BCH","amount":"0.0001","amount_fraction":{"numer":"1","denom":"10000"},"amount_rat":[[1,[1]],[1,[10000]]],"paid_from_trading_vol":false},"fee_to_send_taker_fee":{"coin":"BCH","amount":"0.00001","amount_fraction":{"numer":"1","denom":"100000"},"amount_rat":[[1,[1]],[1,[100000]]],"paid_from_trading_vol":false},"total_fees":[{"coin":"BCH","amount":"0.00012","amount_fraction":{"numer":"3","denom":"25000"},"amount_rat":[[1,[3]],[1,[25000]]],"required_balance":"0.00012","required_balance_fraction":{"numer":"3","denom":"25000"},"required_balance_rat":[[1,[3]],[1,[25000]]]},{"coin":"LBC","amount":"0.0001","amount_fraction":{"numer":"1","denom":"10000"},"amount_rat":[[1,[1]],[1,[10000]]],"required_balance":"0","required_balance_fraction":{"numer":"0","denom":"1"},"required_balance_rat":[[0,[]],[1,[1]]]}]}}]

=========================

same error when using USDT-PLG20 i some other try: [16:03:11] [info] [qt.trading.page.cpp:1207] [1054558]: [determine_fees] trade_preimage answer received: [{"error":"rpc:211] dispatcher_legacy:141] dispatcher_legacy:158] taker_swap:2398] check_balance:75] Not enough USDT-PLG20 for swap: available 19.038574, required at least 19.03858033776136888262548262548262548262548262548262548262548262548262548262548262548262548262548263, locked by swaps Some(BigDecimal(\"0\"))"}]

========================

TUSD-PLG20 in a third try (idk why the error message pops up 2 times in a row)

[16:15:58] [info] [qt.trading.page.cpp:1207] [1070075]: [determine_fees] trade_preimage answer received: [{"error":"rpc:211] dispatcher_legacy:141] dispatcher_legacy:158] taker_swap:2398] check_balance:75] Not enough TUSD-PLG20 for swap: available 7.8938062860956194, required at least 7.893806354751101206177606177606177606177606177606177606177606177606177606177606177606177606177606178, locked by swaps Some(BigDecimal(\"0\"))"}]
[16:16:00] [info] [qt.trading.page.cpp:1207] [1070072]: [determine_fees] trade_preimage answer received: [{"error":"rpc:211] dispatcher_legacy:141] dispatcher_legacy:158] taker_swap:2398] check_balance:75] Not enough TUSD-PLG20 for swap: available 7.8938062860956194, required at least 7.893806354751101206177606177606177606177606177606177606177606177606177606177606177606177606177606178, locked by swaps Some(BigDecimal(\"0\"))"}]

with the corresponding error on GUI image

kaks25 commented 11 months ago

I still have this issue running on v0.6.1 image

cipig commented 2 months ago

the problem seems to come from calling eth_estimateGas (likely called by trade_preimage) with values that are higher then your balance and which then lead to errors from RPCs transfer amount exceeds balance

example from PLG20:

[12:27:05] [debug] [qt.trading.page.cpp:1707] [951583]: reset_fees
30 12:27:06, mm2_main::rpc::dispatcher:121] ERROR RPC error response: taker_swap:2500] eth:5701] Transport error: request MethodCall(MethodCall { jsonrpc: Some(V2), method: "eth_estimateGas", params: Array([Object({"from": String("0x15c577f4b9fbe66735a9bff9b0b5d3ea2854bfaa"), "to": String("0x236aa50979d5f3de3bd1eeb40e81137f22ab794b"), "gasPrice": String("0x7c9f04109"), "value": String("0x0"), "data": String("0xa9059cbb000000000000000000000000d8997941dd1346e9231118d5685d866294f59e5b000000000000000000000000000000000000000000000000000002aceffa3967")})]), id: Num(6) }) failed: Invalid response: Server: 'https://block-proxy.komodo.earth/rpc/matic', error: RPC error: Error { code: ServerError(3), message: "execution reverted: ERC20: transfer amount exceeds balance", data: Some(String("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002645524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000")) }

example from BEP20 with SOL-BEP20:

[12:33:49] [warning] [qt.trading.page.cpp:1058] [951583]: last_trading_error is BalanceIsLessThanTheMinimalTradingAmount
30 12:33:50, mm2_main::rpc::dispatcher:121] ERROR RPC error response: taker_swap:2500] eth:5701] Transport error: request MethodCall(MethodCall { jsonrpc: Some(V2), method: "eth_estimateGas", params: Array([Object({"from": String("0x15c577f4b9fbe66735a9bff9b0b5d3ea2854bfaa"), "to": String("0x570a5d26f7765ecb712c0924e4de545b89fd43df"), "gasPrice": String("0x77359400"), "value": String("0x0"), "data": String("0xa9059cbb000000000000000000000000d8997941dd1346e9231118d5685d866294f59e5b0000000000000000000000000000000000000000000000000004717cbf467411")})]), id: Num(1) }) failed: Invalid response: Server: 'https://bsc1.cipig.net:18655/', error: RPC error: Error { code: ServerError(3), message: "execution reverted: BEP20: transfer amount exceeds balance", data: Some(String("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002642455032303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000")) }

my balance of SOL-BEP20: image

i wonder why we need to pass a volume to eth_estimateGas at all, since the volume has nothing to do with txfees/gas... i pay the same txfee if i send 1000 TBTC-PLG20 or 0.00001 TBTC-PLG20 looks like we could omit value from eth_estimateGas calls: https://ethereum.stackexchange.com/questions/156455/eth-estimategas-throws-execution-reverted-error looks like this issue is actually from mm2 and trade_preimage

dimxy commented 2 months ago

it happens when you are about to buy a coin that needs spending your entire balance of the other coin (or close to it) error shown in Desktop log: ... Not enough BCH for swap: available 0.01670641, required at least 0.016706817338158

I used the balances for failed swaps IRIS/LTC and LBC/BCH from screenshots above in our docker swap tests. Tests worked correctly and also final balances after the swaps became 0. I assume when a user clicks to spend max volume the "max_taker_vol" rpc is called to get the max amount and later when the user starts a swap, the "trade_preimage" rpc is called with that max amount. (Those failed swaps actually threw errors in the "trade_preimage" rpc.)

My guess: the transaction fee increased between the max_taker_vol and trade_preimage calls (max_taker_vol trade_preimage rpc both get txfee) @cipig @shamardy

(BTW for the IRIS/LTC swap it is even more unclear: the error showed that the max value around 360 was used but in the swap edit box the value was 345. Looks like a GUI issue)

cipig commented 2 months ago

My guess: the transaction fee increased between the max_taker_vol and trade_preimage calls (max_taker_vol trade_preimage rpc both get txfee) @cipig @shamardy

LBC and BCH have a fixed fee https://github.com/KomodoPlatform/coins/blob/master/coins#L1580, like most of the UTXO coins. So at least for those it's not the fee. I also guess that the error will not come if you use a certain amount to swap, but your balance is higher then that.

dimxy commented 2 months ago

I also took a look into the Komodo Wallet desktop code: it converts the max_taker_vol rpc result (coming as fractional) into decimal: https://github.com/KomodoPlatform/komodo-wallet-desktop/blob/070cf55c688bb9facd4cce42da361bdd2f8e14b7/src/core/atomicdex/pages/qt.trading.page.cpp#L618 Maybe it is here when we may collect some number error as in the trade_preimage rpc the input volume is fractional.

BTW mm2 logs both max_taker_vol and trade_preimage amounts as debug level. Could we search for those logs (or if debug is off, switch it on for future)?

(I managed to get same error in docker swap test by converting the max_taker_vol value to big decimal and using this value in trade_preimage rpc: Not enough MYCOIN1 for swap: available 0.01658682, required at least 0.01658682000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)

dimxy commented 1 week ago

BTW our doc for the trade_preimage_rpc advises not to use the max volume directly for set_price rpc. I think this makes sense. Maybe the trade_preimage_rpc should return the max volume with some tolerance (lower bound)