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

fix: Arb gas estimate brotli compression calldata byte estimate and estimateGasUsed adding arb L2 gas cost #468

Merged

Conversation

jsy1218
Copy link
Member

@jsy1218 jsy1218 commented Jan 6, 2024

  • What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
    Bug fixes

  • What is the current behavior? (You can also link to an open issue here)
    There are 3 bugs:

  1. Arbitrum calldata gas cost is based on the original calldata bytes, not brotli compressed bytesize.
  2. non-simulated estimatedGasUsed does not add the Arbitrum L1 calldata posting fee in terms of the L2 gas price.
  3. Arb and OP gas price oracle does not pass the block number to the block tag for historical gas price.
  • What is the new behavior (if this is a feature change)?
    There are 3 fixes:
  1. Use Brotli-One compression as an approximate to get the compressed Arbitrum L2->L1 calldata bytesizes.
  2. In non-simulated estimatedGasUsed calculation, add back the Arbitrum L2->L1 gas cost in terms of L2 Arb gas price.
  3. Pass block number to the block tag, so that the historical quote with gas adjusted will be more accurate on Arb and OP network.
$./bin/cli quote --tokenIn 0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9 --tokenOut 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 --amount 20000 --exactIn --minSplits 1 --router alpha --chainId 42161 --recipient 0xb38e8c17e38363af6ebdcb3dae12e0243582891d --simulate --requestBlockNumber 167333306
Best Route:
[V3] 80.00% = USDT -- 0.05% [0x641C00A822e8b671738d32a431a4Fb6074E5c79d] --> WETH, [V3] 20.00% = USDT -- 0.01% [0x8c9D230D45d6CfeE39a6680Fb7CB7E8DE7Ea8E71] --> USDC -- 0.05% [0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443] --> WETH
	Raw Quote Exact In:
		8.91
	Gas Adjusted Quote In:
		8.91

Gas Used Quote Token: 0.000137
Gas Used USD: 0.308263
Calldata: 0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000100000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d00000000000000000000000000000000000000000000000000000003b9aca0000000000000000000000000000000000000000000000000005e426f91ab1eef8800000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002bfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb90001f482af49447d8a07e3bd95bd0d56f35241523fbab10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d00000000000000000000000000000000000000000000000000000000ee6b2800000000000000000000000000000000000000000000000000178ff9163bd1095f00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000042fd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9000064ff970a61a04b1ca14834a43f5de4533ebddb5cc80001f482af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000000000000000000000
Value: 0x00

  blockNumber: "167333306"
  estimatedGasUsed: "1374404"
  gasPriceWei: "100000000"
  simulationStatus: 2
Total ticks crossed: 3

$ ./bin/cli quote --tokenIn 0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9 --tokenOut 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 --amount 20000 --exactIn --minSplits 1 --router alpha --chainId 42161 --recipient 0xb38e8c17e38363af6ebdcb3dae12e0243582891d --requestBlockNumber 167333164
Best Route:
[V3] 55.00% = USDT -- 0.01% [0x8c9D230D45d6CfeE39a6680Fb7CB7E8DE7Ea8E71] --> USDC -- 0.05% [0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443] --> WETH, [V3] 45.00% = USDT -- 0.05% [0x641C00A822e8b671738d32a431a4Fb6074E5c79d] --> WETH
	Raw Quote Exact In:
		8.91
	Gas Adjusted Quote In:
		8.91

Gas Used Quote Token: 0.000132
Gas Used USD: 0.297242
Calldata: 0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000120000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d000000000000000000000000000000000000000000000000000000028fa6ae0000000000000000000000000000000000000000000000000040c8910b2f1a665d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000042fd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9000064ff970a61a04b1ca14834a43f5de4533ebddb5cc80001f482af49447d8a07e3bd95bd0d56f35241523fbab10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d0000000000000000000000000000000000000000000000000000000218711a00000000000000000000000000000000000000000000000000350162788d03f0a700000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002bfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb90001f482af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000
Value: 0x00

  blockNumber: "167333164"
  estimatedGasUsed: "1325266"
  gasPriceWei: "100000000"
Total ticks crossed: 3

Also if I run the same CLI from the main branch (with getGasData refactoring, because I'm requesting historical block number), then I can see main estimatedGasUsed is still way off, if I don't request simulate:

$ ./bin/cli quote --tokenIn 0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9 --tokenOut 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 --amount 20000 --exactIn --minSplits 1 --router alpha --chainId 42161 --recipient 0xb38e8c17e38363af6ebdcb3dae12e0243582891d --requestBlockNumber 167333164
Best Route:
[V3] 55.00% = USDT -- 0.01% [0x8c9D230D45d6CfeE39a6680Fb7CB7E8DE7Ea8E71] --> USDC -- 0.05% [0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443] --> WETH, [V3] 45.00% = USDT -- 0.05% [0x641C00A822e8b671738d32a431a4Fb6074E5c79d] --> WETH
	Raw Quote Exact In:
		8.91
	Gas Adjusted Quote In:
		8.91

Gas Used Quote Token: 0.000157
Gas Used USD: 0.353507
Calldata: 0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000120000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d000000000000000000000000000000000000000000000000000000028fa6ae0000000000000000000000000000000000000000000000000040c8910b2f1a665d00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000042fd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9000064ff970a61a04b1ca14834a43f5de4533ebddb5cc80001f482af49447d8a07e3bd95bd0d56f35241523fbab10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d0000000000000000000000000000000000000000000000000000000218711a00000000000000000000000000000000000000000000000000350162788d03f0a700000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002bfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb90001f482af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000
Value: 0x00

  blockNumber: "167333164"
  estimatedGasUsed: "358000"
  gasPriceWei: "100000000"
Total ticks crossed: 3

$ ./bin/cli quote --tokenIn 0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9 --tokenOut 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 --amount 20000 --exactIn --minSplits 1 --router alpha --chainId 42161 --recipient 0xb38e8c17e38363af6ebdcb3dae12e0243582891d --simulate --requestBlockNumber 167333306
Best Route:
[V3] 100.00% = USDT -- 0.05% [0x641C00A822e8b671738d32a431a4Fb6074E5c79d] --> WETH
	Raw Quote Exact In:
		8.91
	Gas Adjusted Quote In:
		8.91

Gas Used Quote Token: 0.000108
Gas Used USD: 0.242368
Calldata: 0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000009184e72a00000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000100000000000000000000000000b38e8c17e38363af6ebdcb3dae12e0243582891d00000000000000000000000000000000000000000000000000000004a817c80000000000000000000000000000000000000000000000000075d2114da31e751600000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002bfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb90001f482af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000000000000000000000
Value: 0x00

  blockNumber: "167333306"
  estimatedGasUsed: "1080608"
  gasPriceWei: "100000000"
  simulationStatus: 2
Total ticks crossed: 1

@jsy1218 jsy1218 self-assigned this Jan 6, 2024
@jsy1218 jsy1218 requested a review from a team as a code owner January 6, 2024 01:39
@gzeoneth
Copy link
Contributor

gzeoneth commented Jan 8, 2024

Good to see these getting fixed, let me know if you have further question.

Copy link
Contributor

@cgkol cgkol left a comment

Choose a reason for hiding this comment

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

Changes look good generally, but dont we need some test coverage here?

@jsy1218
Copy link
Member Author

jsy1218 commented Jan 11, 2024

Changes look good generally, but dont we need some test coverage here?

I spent some time thinking about how to add testing coverages for this PR. It's a bit tricky. We could assert that the Arbitrum Sequencer posting calldata L2 -> L1 cost as unit test, but it's hard to find both non-compressed and compressed calldata online. Instead I added the unit test to cover that the compressed bytes are used for gas estimate, as well as 16 gas units per calldata byte.

The most tricky part is to cover the change in best-swap-route. There's no existing unit test coverage against this giant function, and it's too time consuming to write one at this point. But this is the crux that we are missing, which is causing inflated network cost on web app. So what I did is to write a A/B test between non-simulated gas vs simulated gas, to make sure they are not like 90% away from each other anymore. 50% away from each other is still a lot, but it's already a great improvement from before this PR.

@jsy1218 jsy1218 requested a review from cgkol January 11, 2024 17:07
Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@types/brotli 1.3.4 None +0 3.31 kB types
brotli 1.3.3 eval +0 1.5 MB devongovett

@jsy1218
Copy link
Member Author

jsy1218 commented Jan 11, 2024

Base-network integ-test failing with simulation ones. I debugged on my local and saw it was due to Tenderly returning 500. Don't see routing-api quote endpoint success rate down for base network (we use different Tenderly project for this github action vs prod env), so I assume this is a transient issue.

@jsy1218 jsy1218 requested a review from mikeki January 12, 2024 16:37
Copy link
Contributor

@cgkol cgkol left a comment

Choose a reason for hiding this comment

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

Nice!

@jsy1218 jsy1218 merged commit 293b21e into main Jan 12, 2024
22 checks passed
@jsy1218 jsy1218 deleted the jsy1218/fix-arb-gasEstimateUsed-compression-and-best-swap-route branch January 12, 2024 18:29
@jsy1218 jsy1218 mentioned this pull request Jan 12, 2024
@trevorbidlack
Copy link

Small note @jsy1218. The brotli.compress usage in getArbitrumBytes wont work in browser environments.

Error:
`../../node_modules/brotli/build/encode.js:2:141
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/brotli/compress.js
../../node_modules/brotli/index.js
../../node_modules/@uniswap/smart-order-router/build/main/util/gas-factory-helpers.js
../../node_modules/@uniswap/smart-order-router/build/main/routers/alpha-router/alpha-router.js
../../node_modules/@uniswap/smart-order-router/build/main/routers/alpha-router/config.js`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants