Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ETH): add gas_limit coins param to override default values #2137

Merged
merged 6 commits into from
Jul 18, 2024

Conversation

dimxy
Copy link
Collaborator

@dimxy dimxy commented Jun 7, 2024

In PR #2051 there were added several gas limit constants to allocate gas price more precisely for different swap operations and for sending coin and tokens, instead of the old single capped value. However there are also erc20 tokens which are called over proxy contracts, which require more gas.
This PR:

  • increases the default consts for erc20 ops (to the old value actually) to ensure proxied erc20 would have enough gas
  • adds gas_limit param to coins file to allow setting lower (or higher) gas limits for selected tokens

Fixes #2135

Sample of gas_limit (with all available limit values) in the coins file:

{
    "coin": "ETH",
    "gas_limit": {
        "eth_send_coins": 21000,               -- Gas limit for sending coins 
        "eth_send_erc20": 65000,              -- Gas limit for sending ERC20 tokens
        "eth_payment": 75000,                    -- Gas limit for swap payment tx with coins
        "erc20_payment": 120000,              -- Gas limit for swap payment tx with ERC20 tokens
        "eth_receiver_spend": 55000,         -- Gas limit for swap receiver spend tx with coins 
        "erc20_receiver_spend": 130000,    -- Gas limit for swap receiver spend tx with ERC20 tokens
        "eth_sender_refund": 110000,          -- Gas limit for swap refund tx with coins
        "erc20_sender_refund": 110000,      -- Gas limit for swap refund tx with with ERC20 tokens
        "eth_max_trade_gas": 150000.        -- Gas limit for other operations
    }
}

Both gas_limit and all of its params are optional.

@dimxy dimxy requested review from cipig and shamardy June 7, 2024 17:34
@dimxy dimxy changed the title feat(ETH): add gas_limit coins param to override default gas limit value feat(ETH): add gas_limit coins param to override default values Jun 7, 2024
@cipig
Copy link
Member

cipig commented Jun 7, 2024

do we need both eth_send_coins and eth_send_erc20? we will need to configure the values for each token in part anyway, so i guess we need only one variant.

Copy link
Member

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! I have some suggestions and question

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
mm2src/coins/eth/v2_activation.rs Outdated Show resolved Hide resolved
mm2src/coins/eth.rs Outdated Show resolved Hide resolved
@dimxy
Copy link
Collaborator Author

dimxy commented Jun 9, 2024

do we need both eth_send_coins and eth_send_erc20? we will need to configure the values for each token in part anyway, so i guess we need only one variant.

I have a set of the same gas limit constants in MM2 and this param allows to override any of them (just for sake of completeness). You may override only one or few or no one at all

@cipig
Copy link
Member

cipig commented Jun 10, 2024

first of all, code works fine, i can set the different limits in coins file
i am already using some lower limits: KomodoPlatform/coins#1074

i collected some gas values for couple different tokens:

======== PLG20 ========

USDT transfer       40,182
USDT erc20Payment   78,553
USDT receiverSpend  55,671
USDT senderRefund   53,641

DAI transfer        40,186
DAI erc20Payment    78,689
DAI receiverSpend   55,675
DAI senderRefund    48,821

PAXG transfer       40,208
PAXG erc20Payment   78,577
PAXG receiverSpend  55,673

USDC transfer       45,059 
USDC erc20Payment   98,788 
USDC receiverSpend  55,724 
USDC senderRefund   53,706 

TBTC transfer       41,696
TBTC erc20Payment   91,965
TBTC receiverSpend  69,461

XIDR erc20Payment   85,751
XIDR receiverSpend  64,606
XIDR senderRefund   62,588

RNDR erc20Payment   80,107 
RNDR senderRefund   50,484

GLM erc20Payment    78,565 
GLM senderRefund    53,643 

EURE transfer       77,410
EURE erc20Payment   134,763
EURE receiverSpend  88,099 

GRT transfer        40,232
GRT erc20Payment    78,613 
GRT receiverSpend   55,709

GMT transfer        34,312
GMT erc20Payment    84,693
GMT receiverSpend   62,101

GNS transfer        34,785
GNS erc20Payment    90,174
GNS receiverSpend   62,550

NEXO transfer       40,220
NEXO erc20Payment   95,701
NEXO receiverSpend  67,997

ETH erc20Payment    73,233
ETH receiverSpend   67,435

TRYB erc20Payment   78,553
TRYB receiverSpend  55,649

AGEUR transfer      41,879
AGEUR erc20Payment  79,989
AGEUR receiverSpend 57,332

CRV transfer        40,232
CRV erc20Payment    95,701
CRV receiverSpend   50,909

======== BEP20 ========

BTC transfer        34,491
BTC erc20Payment    72,794
BTC receiverSpend   49,980
BTC senderRefund    36,750

======== main coins ========

BNB ethPayment      49,406
BNB receiverSpend   40,793
BNB senderRefund    42,887

MATIC ethPayment    48,730
MATIC receiverSpend 40,817
MATIC senderRefund  38,787

i know, some values are missing, but i guess that the following things are true:

  • receiverSpend is always lower then erc20Payment
  • senderRefund is always lower then receiverSpend (or similar, for BNB itself it's actually slightly higher)
  • transfer is always lower then senderRefund

so maybe we should change the default limits a bit

  • ETH_SENDER_REFUND can be set to same as ETH_RECEIVER_SPEND, since those values are nearly the same in reality (38k-42k)
  • lower ETH_SEND_ERC20 from 210k... the real values for transfer are 35k-45k and 77k for EURE proxy token
  • if ERC20_PAYMENT of 150k works with the EURE proxy token, then we can lower all other ERC20 limits (ERC20_RECEIVER_SPEND, ERC20_SENDER_REFUND, ETH_SEND_ERC20) since the real values are always lower then ERC20_PAYMENT
  • or we set ERC20_PAYMENT higher then 150k ... i also observed that when you set the limits to close to the real values, the tx is reverted too... is there a "rule" about this? to me it looks like you can't go past 90% usage of the gas limit
    this is a erc20payment of EURE: https://polygonscan.com/tx/0xd81e5434ee5c9388c4a26f66004903953af963477e892091567edc02f671115c (uses 89% gas, so could be close to being reverted)
  • or maybe we should think about using lower values altogether and define higher ones for the tokens that need it... seems that most tokens are not proxy tokens (and EURE will move to a new contract anyway to save fees, see https://github.com/monerium/smart-contracts/blob/feat/v2/docs/version2.md#1-gas-cost-efficiency)... drawback is that some tokens could stop working if we don't find and set the higher limits

@dimxy
Copy link
Collaborator Author

dimxy commented Jun 10, 2024

is there a "rule" about this? to me it looks like you can't go past 90% usage of the gas limit

I found in metamask that they multiply calculated gas limit by 1,5 for contract calls.

Copy link
Collaborator

@mariocynicys mariocynicys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have the same nit about the unwrap_ors, LGTM otherwise :)

mm2src/coins/eth.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator

shamardy commented Jun 20, 2024

or maybe we should think about using lower values altogether and define higher ones for the tokens that need it... seems that most tokens are not proxy tokens (and EURE will move to a new contract anyway to save fees, see https://github.com/monerium/smart-contracts/blob/feat/v2/docs/version2.md#1-gas-cost-efficiency)... drawback is that some tokens could stop working if we don't find and set the higher limits

I am for this approach as I think it's the best one but it will not work for custom tokens. Maybe for custom tokens I can add higher const values when implementing them. If we follow this approach, for any new token added to config, it should be tested with the default low limits for transactions and swaps before adding the gas_limit configs if transactions or swaps need more gas. What do you think @dimxy @cipig , do you envision any drawbacks if we used lower limits as consts?

P.S. for custom tokens they will not be used for cross-chain swaps most probably but will be used to be traded on same chain using the upcoming liquidity routing feature (1inch or uniswap integration) so these will have different gas limits calculations.

@dimxy
Copy link
Collaborator Author

dimxy commented Jun 20, 2024

What do you think @dimxy @cipig , do you envision any drawbacks if we used lower limits as consts?

P.S. for custom tokens they will not be used for cross-chain swaps most probably but will be used to be traded on same chain using the upcoming liquidity routing feature (1inch or uniswap integration) so these will have different gas limits calculations.

Maybe yet we could add some token categories in the coins file and create different gas limit defaults for them (like proxied/non-proxied).

@cipig
Copy link
Member

cipig commented Jun 24, 2024

Maybe yet we could add some token categories in the coins file and create different gas limit defaults for them (like proxied/non-proxied).

was thinking about that too, but i guess it only makes sense if proxied tokens are detected automatically by mm2 and then higher limits are applied... like default * 1.5 (this would work for EURE-PLG20, but it's the only such token i found so far, so idk if other proxied tokens need other limits... and if the detection is not done automatically, we can simply define higher limits for each of such tokens manually in the coins file with the existing "gas_limit" structure

some differences in gas consumption are still a bit of a mystery to me, eg USDT-BEP20 receiverSpend 67,104 49,968... same call (receiverSpend) used a different amount of gas, one time 67k, the other time 50k ... first i thought the difference depends on if the call was done by maker or taker, but it's not that, so idk where such differences come from... will continue to collect data

from the tokens i tested so far (30 or so), this values would work (incl. EURE-PLG20, the proxied token)

    pub const ETH_PAYMENT: u64 = 65_000; // same value atm
    pub const ETH_RECEIVER_SPEND: u64 = 65_000; // same value atm
    pub const ETH_SENDER_REFUND: u64 = 65_000; // is 100_000 atm

    pub const ETH_SEND_ERC20: u64 = 90_000; // is 210_000 atm
    pub const ERC20_PAYMENT: u64 = 150_000; // same value atm
    pub const ERC20_RECEIVER_SPEND: u64 = 120_000; // is 150_000 atm
    pub const ERC20_SENDER_REFUND: u64 = 120_000; // is 150_000 atm

and excluding EURE-PLG20 this values are fine

    pub const ETH_SEND_ERC20: u64 = 60_000; // is 210_000 atm
    pub const ERC20_PAYMENT: u64 = 120_000; // same value atm
    pub const ERC20_RECEIVER_SPEND: u64 = 90_000; // is 150_000 atm
    pub const ERC20_SENDER_REFUND: u64 = 90_000; // is 150_000 atm

but i have only tested PLG20, AVX20 and BEP20 tokens... from the main coins i also tested FTM and KCS, but not the tokens on those chains

Comment on lines +7062 to +7063
if coin_conf["gas_limit"].is_null() {
Ok(Default::default())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already handled by serde(default)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if "gas_limit" is null serde returns invalid type: null error. I wanted to handle this differently (to return the default) than any bad values (to fail)

Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@shamardy
Copy link
Collaborator

@KomodoPlatform/qa I guess @cipig already added some gas limits to some coins here KomodoPlatform/coins#1074 , KomodoPlatform/coins#1075 but the new gas_limit parameter described here #2137 (comment) needs to be documented.

@shamardy
Copy link
Collaborator

shamardy commented Jul 18, 2024

I guess I can merge this since docs PR is already open and under review plus @cipig already uses it and it works.

@shamardy shamardy merged commit e356bb8 into dev Jul 18, 2024
25 of 29 checks passed
@shamardy shamardy deleted the gas-limit-override branch July 18, 2024 00:24
shamardy added a commit that referenced this pull request Jul 18, 2024
dimxy added a commit that referenced this pull request Jul 21, 2024
* dev:
  feat(nft-swap): add standalone maker contract and proxy support (#2100)
  feat(ETH): add `gas_limit` coins param to override default values (#2137)
  feat(tendermint): implement better sequence resolving logic (#2164)
  ci(artifact): add target for macos on apple silicon (#2163)
  fix(helpers): extend http to ws address conversion (#2166)
  fix(makerbot): add "testcoin" to provider options (#2161)
  fix(hd_wallet): make extended pubkey of hd wallet generic (#2159)
  fix(docker-tests): implement containers runtime directories (#2162)
  feat(tendermint): improve the `max` handling for tendermint withdraw (#2155)
  revert #2158 (comment) (#2160)
  ci(artifacts): upload build artifacts with in-tree script (#2158)
  test(tendermint): migrate to local/offline containerized testnets (#2128)
  use easingthemes/ssh-deploy@v5.0.3 for all builds except windows (#2157)
  chore(bin): rename mm2 binaries to kdf (#2126)
Alrighttt pushed a commit that referenced this pull request Jul 25, 2024
)

This commit does the following:
- Increases the default consts for erc20 ops (to the old value actually) to ensure proxied erc20 would have enough gas.
- adds `gas_limit` param that can be set in coins config to allow setting lower (or higher) gas limits for selected tokens.
Alrighttt added a commit that referenced this pull request Jul 25, 2024
Alrighttt pushed a commit that referenced this pull request Aug 9, 2024
)

This commit does the following:
- Increases the default consts for erc20 ops (to the old value actually) to ensure proxied erc20 would have enough gas.
- adds `gas_limit` param that can be set in coins config to allow setting lower (or higher) gas limits for selected tokens.
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Aug 12, 2024
* dev: (22 commits)
  chore(release): bump mm2 version to 2.2.0-beta (KomodoPlatform#2188)
  ci(docker-tests): ignore tendermint IBC tests for now (KomodoPlatform#2185)
  feat(nft-swap): complete refund methods (KomodoPlatform#2129)
  chore(release): add changelog entries for v2.1.0-beta (KomodoPlatform#2165)
  fix(zcoin): don't force low r signing to generate htlc pubkey for zcoin (KomodoPlatform#2184)
  chore(rust-analyzer): add rust-analyzer into the workspace toolchain (KomodoPlatform#2179)
  chore: migrate .cargo/config to .cargo/config.toml to avoid deprecation warning (KomodoPlatform#2177)
  fix(swaps): ensure taker payment spend confirmations (KomodoPlatform#2176)
  feat(nft-swap): add standalone maker contract and proxy support (KomodoPlatform#2100)
  feat(ETH): add `gas_limit` coins param to override default values (KomodoPlatform#2137)
  feat(tendermint): implement better sequence resolving logic (KomodoPlatform#2164)
  ci(artifact): add target for macos on apple silicon (KomodoPlatform#2163)
  fix(helpers): extend http to ws address conversion (KomodoPlatform#2166)
  fix(makerbot): add "testcoin" to provider options (KomodoPlatform#2161)
  fix(hd_wallet): make extended pubkey of hd wallet generic (KomodoPlatform#2159)
  fix(docker-tests): implement containers runtime directories (KomodoPlatform#2162)
  feat(tendermint): improve the `max` handling for tendermint withdraw (KomodoPlatform#2155)
  revert KomodoPlatform#2158 (comment) (KomodoPlatform#2160)
  ci(artifacts): upload build artifacts with in-tree script (KomodoPlatform#2158)
  test(tendermint): migrate to local/offline containerized testnets (KomodoPlatform#2128)
  ...
@smk762 smk762 removed the docs label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants