atomicDEX-API icon indicating copy to clipboard operation
atomicDEX-API copied to clipboard

Swaps with Polygon tokens may fail due to payment transaction stuck not mined

Open dimxy opened this issue 11 months ago • 6 comments

Sometimes POL transactions may be stuck in mempool for hours (but eventually get mined). This occurs due to low tx fee. Apparently the reason for that is that gas price is very changeable in the POL chain. For a sample tx KDF set the gas price of 39 gwei but for the next block gas price sharply increased to 44 gwei and continued growing in next blocks, leaving our tx unmined for hours.

Currently for gas price we use max of two: eth_gasPrice and the latest block gas price (from eth_feeHistory) multiplied by 11. Looks like this is not enough for Polygon where gas price may change in wide intervals (due to low POL coin value).

It is noticed that at the same time base fee for POL may be very low (<1 gwei) and priority fee could be high (>50 gwei). This allows for total gas fee to be changed very widely. (In fact this is not how the eip1559 standard proposed to set base and priority fee but this may occur for POL). For POL there is also a recommendation to set the priority fee of min 30 gwei.

The proposed solution to fix is for selected coins (like POL) allow new params in the coins file:

  • set default gas policy as eip1559 for swaps and withdraw
  • set custom priority predictions multipliers (currently fixed in the code), to allow wide priority fee changes.
  • set min priority fee value in gwei
  • for legacy gas policy set a custom gas price multiplier (although I think it's better to switch to the eip1559 gas policy for such coins as some gas fee prediction mechanism is already implemented for eip1559 txns)

@cipig

dimxy avatar Feb 03 '25 08:02 dimxy

@cipig if you this is urgent (requires a fix asap) please change priority

shamardy avatar Feb 03 '25 12:02 shamardy

I vote for using eip1559 and letting user to choose which SwapTxFeePolicy to use (Low, Medium, High) for each swap or skip it and use one 1559 lvl as default.

for legacy gas policy set a custom gas price multiplier

As an alternative if user not sure that eip1559 will work.

Actually why dont provide all these variants and let user to choose gas policy at runtime without re-enabling coin?

laruh avatar Feb 05 '25 04:02 laruh

we still should fix legacy, since i fear that it will take a long time for all GUIs to implement eip1559 and the good thing is that it's very easy... just add the x% to the gas price we got from the RPCs we could also save a call completely, the fee_history can be spared completely... just ask for gas_price, add x% to it and that's it, nothing else needed

cipig avatar Feb 05 '25 04:02 cipig

I agree that for the hotfix we should do this

just ask for gas_price, add x% to it and that's it, nothing else needed

In a long term it worth providing eip1559 support

laruh avatar Feb 05 '25 04:02 laruh

Can we please add the set_swap_transaction_fee_policy param to the enable calls, so i can set it to eg "Medium" for all tokens that i enable? atm i would have to call set_swap_transaction_fee_policy for each enabled token separately every time i restart mm2. I use eg this to enable all PLG20 tokens that i use: curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"enable_eth_with_tokens\",\"mmrpc\":\"2.0\",\"params\":{\"get_balances\":false,\"ticker\":\"MATIC\",\"mm2\":1,\"priv_key_policy\":\"ContextPrivKey\",\"swap_contract_address\":\"0x9130b257D37A52E52F21054c4DA3450c72f595CE\",\"fallback_swap_contract\":\"0x9130b257D37A52E52F21054c4DA3450c72f595CE\",\"nodes\":[{\"url\":\"https://electrum3.cipig.net:18755\"},{\"url\":\"https://polygon-bor-rpc.publicnode.com\"}],\"tx_history\":false,\"erc20_tokens_requests\":[{\"ticker\":\"1INCH-PLG20\"},{\"ticker\":\"AAVE-PLG20\"},{\"ticker\":\"AGEUR-PLG20\"},{\"ticker\":\"ANKR-PLG20\"},{\"ticker\":\"APE-PLG20\"},{\"ticker\":\"ARPA-PLG20\"},{\"ticker\":\"ATOM-PLG20\"},{\"ticker\":\"BAL-PLG20\"},{\"ticker\":\"BRZ-PLG20\"},{\"ticker\":\"CADC-PLG20\"},{\"ticker\":\"COMP-PLG20\"},{\"ticker\":\"CRV-PLG20\"},{\"ticker\":\"DAI-PLG20\"},{\"ticker\":\"ETH-PLG20\"},{\"ticker\":\"EURE-PLG20\"},{\"ticker\":\"EUROE-PLG20\"},{\"ticker\":\"EURS-PLG20\"},{\"ticker\":\"FXS-PLG20\"},{\"ticker\":\"GLM-PLG20\"},{\"ticker\":\"GMT-PLG20\"},{\"ticker\":\"GNS-PLG20\"},{\"ticker\":\"GRT-PLG20\"},{\"ticker\":\"HEX-PLG20\"},{\"ticker\":\"IDRT-PLG20\"},{\"ticker\":\"JAUD-PLG20\"},{\"ticker\":\"JBRL-PLG20\"},{\"ticker\":\"JCAD-PLG20\"},{\"ticker\":\"JCHF-PLG20\"},{\"ticker\":\"JCNY-PLG20\"},{\"ticker\":\"JEUR-PLG20\"},{\"ticker\":\"JGBP-PLG20\"},{\"ticker\":\"JGOLD-PLG20\"},{\"ticker\":\"JJPY-PLG20\"},{\"ticker\":\"JKRW-PLG20\"},{\"ticker\":\"JMXN-PLG20\"},{\"ticker\":\"JNZD-PLG20\"},{\"ticker\":\"JPLN-PLG20\"},{\"ticker\":\"SYN-PLG20\"},{\"ticker\":\"JSGD-PLG20\"},{\"ticker\":\"JTRY-PLG20\"},{\"ticker\":\"SPACE-PLG20\"},{\"ticker\":\"JPYC-PLG20\"},{\"ticker\":\"KNC-PLG20\"},{\"ticker\":\"LDO-PLG20\"},{\"ticker\":\"LINK-PLG20\"},{\"ticker\":\"MANA-PLG20\"},{\"ticker\":\"MASK-PLG20\"},{\"ticker\":\"MKR-PLG20\"},{\"ticker\":\"NEXO-PLG20\"},{\"ticker\":\"PGX-PLG20\"},{\"ticker\":\"PAXG-PLG20\"},{\"ticker\":\"POT-PLG20\"},{\"ticker\":\"PYR-PLG20\"},{\"ticker\":\"REQ-PLG20\"},{\"ticker\":\"RNDR-PLG20\"},{\"ticker\":\"SAND-PLG20\"},{\"ticker\":\"SHIB-PLG20\"},{\"ticker\":\"SNX-PLG20\"},{\"ticker\":\"SOL-PLG20\"},{\"ticker\":\"SUSHI-PLG20\"},{\"ticker\":\"TBTC-PLG20\"},{\"ticker\":\"TEL-PLG20\"},{\"ticker\":\"DFX-PLG20\"},{\"ticker\":\"TRYB-PLG20\"},{\"ticker\":\"UNI-PLG20\"},{\"ticker\":\"OM-PLG20\"},{\"ticker\":\"USDT-PLG20\"},{\"ticker\":\"WBTC-PLG20\"},{\"ticker\":\"WOO-PLG20\"},{\"ticker\":\"YFI-PLG20\"},{\"ticker\":\"XIDR-PLG20\"},{\"ticker\":\"XSGD-PLG20\"}]}}"

cipig avatar Mar 14 '25 16:03 cipig

To fix this issue I suggest adding the following params, both into the coins file and enable tokens request:

  • swap_fee_priority - legacy, low, medium or high - eip1559 priority used in swaps (also set by set_swap_transaction_fee_policy rpc)

Minimal fee per gas or gas price:

  • base_fee_min (in gwei) - according to POLYGON requirements should be set to min 30 gwei or higher
  • priority_fee_mult (in gwei) - same as above if legacy gas price is used (instead of eip1559) it will be checked for >= min_base_fee + min_priority_fee

Correction multiplier (to adjust base and priority fee):

  • base_fee_mult (float) - to multiply the base fee obtained from the provider
  • priority_fee_mult (float) - to multiply the priority fee from the provider if legacy gas price is used it will be multiplied by base_fee_mult

In the coins file the above params should be set in the platform coin. The coin activation request should allow to set/override those params too

dimxy avatar Mar 14 '25 18:03 dimxy