Skip to content

Fix TON STON.fi quote after reserve and jetton swap#1110

Merged
0xh3rman merged 1 commit into
mainfrom
h/ton-stonfi-swap-fixes
May 7, 2026
Merged

Fix TON STON.fi quote after reserve and jetton swap#1110
0xh3rman merged 1 commit into
mainfrom
h/ton-stonfi-swap-fixes

Conversation

@0xh3rman
Copy link
Copy Markdown
Collaborator

@0xh3rman 0xh3rman commented May 6, 2026

Summary

  • Fix STON.fi Jetton-origin swaps by resolving the sender jetton wallet during quote-data construction and passing it into the STON.fi transaction builders.
  • Fix TON swap preload fee handling by using the quote data type: contract swaps reserve the native TON attached by STON.fi, while transfer-style swaps keep normal TON transfer fee behavior.
  • Fix TON Jetton -> cross-chain transfer swaps, including Near Intents USDT -> ETH/BNB, by looking up the recipient jetton wallet against the actual TON deposit address instead of the receive-side EVM address.

Issues Fixed

  1. STON.fi Jetton -> TON could fail after moving from the core-ts proxy swapper to the native Rust provider.

    • Root cause: Jetton-origin STON.fi transactions need the outer TON message sent to the user's sender jetton wallet, but the Rust path did not resolve that wallet before building quote data.
    • Fix: the STON.fi provider fetches the sender jetton wallet from TON RPC and passes it into the v1/v2 transaction builders.
  2. STON.fi TON swap confirmation could under-report the required native TON balance.

    • Root cause: TON preload treated swap fees like a generic token transfer reserve, but STON.fi v2 contract swap messages attach extra native TON (0.30 TON for Jetton-origin swaps and quote.from_value + 0.31 TON for TON-origin swaps).
    • Fix: contract quote data now gets a local 0.31 TON native reserve in gem_ton preload. Transfer quote data continues through the normal TON transfer fee path, so Near Intents is not over-reserved as a STON.fi contract swap.
  3. Near Intents TON Jetton -> EVM swaps could fail during preload with TON API decode errors.

    • Root cause: for a transfer-style swap, wallet code passes the receive-side wallet as destination_address. For USDT -> ETH/BNB that is an EVM 0x... address, and TON preload tried to call /jetton/wallets?owner_address=0x....
    • Fix: TON preload now uses swap_data.data.to as the recipient jetton owner for transfer-style swaps, which is the actual TON deposit address. Contract swaps skip recipient jetton lookup because that metadata is not used by the signer.

Follow-up

Source

0.3 TON: https://github.com/ston-fi/sdk/blob/main/packages/sdk/src/contracts/dex/v2_1/router/BaseRouterV2_1.ts#L34-L45
0.01 pTON: https://github.com/ston-fi/sdk/blob/main/packages/sdk/src/contracts/pTON/v2_1/PtonV2_1.ts#L24-L27

Validation

  • cargo fmt --all --check
  • cargo test -p gem_ton --features rpc provider::preload::tests
  • cargo test -p primitives swap_mock
  • cargo test -p swapper stonfi::provider::tests
  • cargo test -p swapper near_intents::provider::tests
  • cargo clippy -p gem_ton -p primitives --features gem_ton/rpc --all-targets -- -D warnings
  • cargo clippy -p swapper -p gem_ton --features gem_ton/rpc --all-targets -- -D warnings

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors fee calculation and quote value resolution across the gem_ton and swapper crates. In gem_ton, the calculate_transaction_fee function now returns a Result and correctly handles swap-attached values, while the signer was updated to ensure token swaps use the sender's Jetton wallet as the destination. In the swapper crate, resolve_max_quote_value was refactored into quote_value_after_reserve and quote_value_after_reserve_by_chain to allow for provider-specific reserved fees, such as the new constant introduced for Stonfi native swaps. All swapper providers and the swapper checklist were updated to reflect these changes. I have no feedback to provide.

@0xh3rman 0xh3rman changed the title [codex] Fix TON STON.fi swap accounting and signing Fix TON STON.fi swap accounting and signing May 6, 2026
@0xh3rman 0xh3rman marked this pull request as ready for review May 6, 2026 22:56
@0xh3rman 0xh3rman changed the title Fix TON STON.fi swap accounting and signing Fix TON STON.fi quote after reserve and jetton swap May 6, 2026
Comment thread crates/gem_ton/src/signer/transaction/sign.rs Outdated
@0xh3rman 0xh3rman force-pushed the h/ton-stonfi-swap-fixes branch 2 times, most recently from d65c09a to 2ae8ef5 Compare May 7, 2026 06:58
Direct Jetton-origin STON.fi swaps to the sender jetton wallet and keep TON preload fee checks aligned with contract swap message reserves. Transfer-style TON swaps reuse normal TON transfer fee handling.
@0xh3rman 0xh3rman force-pushed the h/ton-stonfi-swap-fixes branch from 2ae8ef5 to d0f63f6 Compare May 7, 2026 07:04
@0xh3rman 0xh3rman merged commit 988d6ae into main May 7, 2026
4 checks passed
@0xh3rman 0xh3rman deleted the h/ton-stonfi-swap-fixes branch May 7, 2026 10:57
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.

2 participants