Bug: Multi-hop swap gas limit too tight — 2-hop exceeds 1,200,000 #39

Closed
opened 2026-03-23 02:17:49 +00:00 by Brouie · 8 comments
Brouie commented 2026-03-23 02:17:49 +00:00 (Migrated from gitlab.com)

Steps:\n1. Swap OPAL → ONYX → CORAL (2-hop via router)\n2. Amount: 10 OPAL, slippage 5%\n\nExpected: Tx succeeds within gas limit\nGot: out of gas; gasWanted: 1200000, gasUsed: 1227713\n\nCurrent: SWAP_GAS_PER_HOP = 600k, so 2-hop = 1,200,000. Actual usage is 1,227,713 — exceeds by ~28k.\n\nSuggested fix: Add 10% buffer to gas calculation, e.g. (hops * 600k * 1.1) or bump SWAP_GAS_PER_HOP to 650k.

**Steps:**\n1. Swap OPAL → ONYX → CORAL (2-hop via router)\n2. Amount: 10 OPAL, slippage 5%\n\n**Expected:** Tx succeeds within gas limit\n**Got:** out of gas; gasWanted: 1200000, gasUsed: 1227713\n\nCurrent: SWAP_GAS_PER_HOP = 600k, so 2-hop = 1,200,000. Actual usage is 1,227,713 — exceeds by ~28k.\n\n**Suggested fix:** Add 10% buffer to gas calculation, e.g. (hops * 600k * 1.1) or bump SWAP_GAS_PER_HOP to 650k.
PlasticDigits commented 2026-03-23 03:34:43 +00:00 (Migrated from gitlab.com)

mentioned in commit 7c1e63e88b

mentioned in commit 7c1e63e88b07658b7dbccdc6fe2210bd7d843320
PlasticDigits commented 2026-03-23 03:34:52 +00:00 (Migrated from gitlab.com)

Fix pushed in commit 7c1e63e.

Added a SWAP_GAS_BUFFER = 1.1 constant and applied it to multi-hop gas calculations so the gas limit is now Math.round(SWAP_GAS_PER_HOP × hops × 1.1).

Before → After for the reported 2-hop case:

  • Gas limit: 1,200,000 → 1,320,000
  • Observed usage was 1,227,713, so the new limit provides ~92k of headroom.

Changes:

  • frontend-dapp/src/utils/constants.ts — new SWAP_GAS_BUFFER constant
  • frontend-dapp/src/services/terraclassic/transactions.ts — buffer applied to both direct and send-wrapped execute_swap_operations
  • Tests updated and passing
Fix pushed in commit 7c1e63e. Added a `SWAP_GAS_BUFFER = 1.1` constant and applied it to multi-hop gas calculations so the gas limit is now `Math.round(SWAP_GAS_PER_HOP × hops × 1.1)`. **Before → After for the reported 2-hop case:** - Gas limit: 1,200,000 → 1,320,000 - Observed usage was 1,227,713, so the new limit provides ~92k of headroom. Changes: - `frontend-dapp/src/utils/constants.ts` — new `SWAP_GAS_BUFFER` constant - `frontend-dapp/src/services/terraclassic/transactions.ts` — buffer applied to both direct and send-wrapped `execute_swap_operations` - Tests updated and passing
PlasticDigits commented 2026-03-23 03:35:56 +00:00 (Migrated from gitlab.com)

@Brouie Please verify ix.

@Brouie Please verify ix.
Brouie commented 2026-03-23 04:31:43 +00:00 (Migrated from gitlab.com)

mentioned in issue #18

mentioned in issue #18
Brouie commented 2026-03-23 06:51:37 +00:00 (Migrated from gitlab.com)

Verified #38 fix — custom slippage now passed to router correctly. However #39 still fails: TOPAZ->ONYX->CORAL 2-hop swap gives gasWanted: 1,320,000 vs gasUsed: 1,320,097. The 1.1x buffer (SWAP_GAS_BUFFER in constants.ts) is too tight — off by 97 gas. Suggest bumping to 1.15 or 1.2.

Verified #38 fix — custom slippage now passed to router correctly. However #39 still fails: TOPAZ->ONYX->CORAL 2-hop swap gives gasWanted: 1,320,000 vs gasUsed: 1,320,097. The 1.1x buffer (SWAP_GAS_BUFFER in constants.ts) is too tight — off by 97 gas. Suggest bumping to 1.15 or 1.2.
PlasticDigits commented 2026-03-23 09:01:09 +00:00 (Migrated from gitlab.com)

mentioned in commit d3da36159e

mentioned in commit d3da36159e503c735b9750841d5f6a2c045cc4bc
PlasticDigits commented 2026-03-23 09:01:16 +00:00 (Migrated from gitlab.com)

Follow-up pushed in d3da361 (still leaving this open until verified on-chain).

Change: execute_swap_operations gas is now max(buffered base + (hops × padding), hops × min per hop):

  • SWAP_GAS_BUFFER (1.1) unchanged on the (SWAP_GAS_PER_HOP × hops) term
  • SWAP_MULTIHOP_GAS_PADDING_PER_HOP = 50,000 extra per hop (covers the ~97 gas miss on 1.32M with margin)
  • EXECUTE_SWAP_OPS_MIN_GAS_PER_HOP = 661,000 floor per hop so totals never sit just above bare 600k × hops × 1.1

Example 2-hop: ~1,420,000 gas wanted (was 1,320,000 after first fix).

@Brouie please re-run the TOPAZ → ONYX → CORAL (or similar) 2-hop on your env and confirm the tx no longer runs out of gas; then we can close #39.

Follow-up pushed in `d3da361` (still leaving this open until verified on-chain). **Change:** `execute_swap_operations` gas is now `max(buffered base + (hops × padding), hops × min per hop)`: - `SWAP_GAS_BUFFER` (1.1) unchanged on the `(SWAP_GAS_PER_HOP × hops)` term - `SWAP_MULTIHOP_GAS_PADDING_PER_HOP` = **50,000** extra per hop (covers the ~97 gas miss on 1.32M with margin) - `EXECUTE_SWAP_OPS_MIN_GAS_PER_HOP` = **661,000** floor per hop so totals never sit just above bare `600k × hops × 1.1` **Example 2-hop:** ~**1,420,000** gas wanted (was 1,320,000 after first fix). @Brouie please re-run the TOPAZ → ONYX → CORAL (or similar) 2-hop on your env and confirm the tx no longer runs out of gas; then we can close #39.
Brouie commented 2026-03-24 05:06:24 +00:00 (Migrated from gitlab.com)

Verified — TOPAZ->ONYX->CORAL 2-hop swap succeeds. Gas no longer runs out. Closing.

Verified — TOPAZ->ONYX->CORAL 2-hop swap succeeds. Gas no longer runs out. Closing.
Brouie (Migrated from gitlab.com) closed this issue 2026-03-24 05:06:26 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
code/cl8y-dex-terraclassic#39
No description provided.