flowfi icon indicating copy to clipboard operation
flowfi copied to clipboard

[Smart Contracts] Atomic Cross-Asset Stream Routing via Stellar DEX & Path Payments

Open blurbeast opened this issue 2 months ago • 16 comments

Overview & Background

A core superpower of the Stellar network is its native decentralized exchange (SDEX) and Soroban AMM liquidity pools. In global payroll, employers usually hold treasury assets in USD stablecoins (e.g. USDC), while international employees and contractors need to receive compensation in their domestic currencies (e.g. EURC, BRL, or XLM). Currently, FlowFi streams require the deposited token to match the withdrawn token identically.


Detailed Problem Statement

  1. Currency Mismatch Friction:
    • Recipients receiving USDC who require EURC must manually withdraw, navigate to an exchange or DEX interface, execute a swap, pay slippage/fees, and transfer to their off-ramp.
  2. Double Transaction Overhead:
    • Multiple manual swaps incur additional latency, multiple transaction fees, and poor user experience.
  3. No Direct Path Payment Settlement on Withdrawal:
    • FlowFi cannot tap directly into Soroban DEX swap protocols or Stellar Classic Path Payments during the stream claim phase.

Technical Specification & Architecture

1. Cross-Asset Swap Integration Interface

Integrate an adapter for Stellar Soroban DEX / AMM swap router contracts:

#[contractclient(name = "DexRouterClient")]
pub trait DexRouter {
    fn swap_exact_tokens_for_tokens(
        env: Env,
        amount_in: i128,
        amount_out_min: i128,
        path: Vec<Address>,
        to: Address,
        deadline: u64,
    ) -> Vec<i128>;
}

2. Auto-Swap Withdrawal Entrypoint

Implement withdraw_and_swap in contracts/stream_contract/src/lib.rs:

pub fn withdraw_and_swap(
    env: Env,
    recipient: Address,
    stream_id: u64,
    target_token: Address,
    min_target_amount: i128,
    dex_router: Address,
    deadline: u64,
) -> Result<i128, StreamError>

3. Execution Flow

  • Enforce recipient.require_auth().
  • Compute claimable token balance in stream principal asset.
  • If claimable amount > 0:
    • Withdraw principal amount from stream accounting.
    • Approve or transfer stream tokens to dex_router.
    • Execute swap from stream asset to target_token with min_target_amount slippage guard and deadline verification.
    • Ensure target tokens are delivered directly to recipient.
    • Emit CrossAssetWithdrawalExecuted event with input token, output token, rate, and slippage metrics.

Target Files

  • contracts/stream_contract/src/lib.rs
  • contracts/stream_contract/src/types.rs
  • contracts/stream_contract/src/events.rs
  • contracts/stream_contract/src/test.rs

Acceptance Criteria

  • [ ] Recipient can withdraw vested balance in an alternative token via a single atomic call.
  • [ ] Slippage check reverts transaction if output amount is lower than min_target_amount.
  • [ ] Expired deadline reverts with StreamError::DeadlineExpired.
  • [ ] Stream accounting correctly updates withdrawn_amount in the base deposit token.
  • [ ] Comprehensive mock DEX tests simulate price impact, slippage failures, and successful multi-hop swaps.

blurbeast avatar Sep 27 '26 05:09 blurbeast

@divysam has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

Hello I would to work on this issue please

ℹ️ Repo Maintainers: To accept this application, review their application or assign @divysam to this issue.

divysam avatar Sep 27 '26 15:09 divysam

@Osifowora has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

​Hi maintainer, I would love to work on this issue. I can get started on it right away.

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Osifowora to this issue.

Osifowora avatar Sep 27 '26 15:09 Osifowora

Congratulations, @Osifowora! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

🧑‍💻 @Osifowora: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Sep 27 '26 15:09 drips-wave[bot]

This issue has been marked as completed by a Drips Wave moderator for @Osifowora as part of the Stellar Wave Program's 9th Wave 🥳

Moderator's reason: [Auto-assessed] The PR implements the requested atomic cross-asset stream routing feature via Stellar DEX and path payments (#1466), along with position receipts, dynamic fees, and the GraphQL gateway. It includes full acceptance test coverage, and there are no maintainer objections across any channel.

😎 @Osifowora: You earned 150 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here.

🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

ℹ️ Note: This issue was marked complete via moderation. Points were issued even though the GitHub issue remains open.

drips-wave[bot] avatar Oct 04 '26 14:10 drips-wave[bot]

@laxjovial has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hello maintainer, i would like to work on this issue

ℹ️ Repo Maintainers: To accept this application, review their application or assign @laxjovial to this issue.

laxjovial avatar Oct 16 '26 22:10 laxjovial

@kvngsulex has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

I've reviewed the issue and I'm confident I can implement a solid solution while following the projects convention. Happy to get started right away if assigned looking forward to contributing.

ℹ️ Repo Maintainers: To accept this application, review their application or assign @kvngsulex to this issue.

kvngsulex avatar Oct 16 '26 22:10 kvngsulex

@Gentle842 has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hi maintenaner, I can work on this, Pls kindly assign it to me

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Gentle842 to this issue.

Gentle842 avatar Oct 16 '26 22:10 Gentle842

@CollinsKRO has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hi Maintainer, I can fix this pls assign. ETA 5hrs

ℹ️ Repo Maintainers: To accept this application, review their application or assign @CollinsKRO to this issue.

CollinsKRO avatar Oct 16 '26 22:10 CollinsKRO

@sheyi3131 has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

I can fix this with immediate effect Please assign

ℹ️ Repo Maintainers: To accept this application, review their application or assign @sheyi3131 to this issue.

sheyi3131 avatar Oct 16 '26 22:10 sheyi3131

@anasisgay16 has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

Hello maintainer, i would like to work on this issue

ℹ️ Repo Maintainers: To accept this application, review their application or assign @anasisgay16 to this issue.

SantanFeli avatar Oct 16 '26 22:10 SantanFeli

@harmondconsultant-png has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

hy, i can work on it pleas assign

ℹ️ Repo Maintainers: To accept this application, review their application or assign @harmondconsultant-png to this issue.

harmondconsultant-png avatar Oct 16 '26 22:10 harmondconsultant-png

@Wittig18 has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

hey maintainer, i would love to work on this

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Wittig18 to this issue.

Wittig18 avatar Oct 16 '26 22:10 Wittig18

@Nuruddeen61 has applied to work on this issue as part of the Stellar Wave Program's 10th wave.

I love to work on this issue please assign me 🙏🙏🙏

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Nuruddeen61 to this issue.

Nuruddeen61 avatar Oct 16 '26 22:10 Nuruddeen61

Congratulations, @laxjovial! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on October 22, 2026.

🧑‍💻 @laxjovial: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Oct 16 '26 22:10 drips-wave[bot]

Congratulations, @anasisgay16! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on October 22, 2026.

🧑‍💻 @anasisgay16: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Oct 16 '26 22:10 drips-wave[bot]