A smart contract built on the Soroban platform that enables trustless atomic swaps of tokens between two parties.
This contract facilitates atomic (indivisible) token exchanges between two different parties. The key feature is that parties don't need to know each other, and their signatures can be matched off-chain. The swap either completes entirely or not at all, eliminating counterparty risk.
- Trustless Exchange: No need for parties to know or trust each other
- Atomic Execution: The entire transaction either succeeds completely or fails completely
- Minimum Price Protection: Both parties can specify minimum acceptable amounts
- Refund Mechanism: Automatically refunds excess tokens to the sender
- Signature Flexibility: Signatures can be matched off-chain for greater flexibility
pub fn swap(
env: Env,
a: Address,
b: Address,
token_a: Address,
token_b: Address,
amount_a: i128,
min_b_for_a: i128,
amount_b: i128,
min_a_for_b: i128,
)This function handles the atomic swap between two parties with the following parameters:
env: Soroban environmentaandb: Addresses of the two parties involved in the swaptoken_aandtoken_b: Addresses of the tokens to be exchangedamount_a: Amount of token_a that user A is willing to givemin_b_for_a: Minimum amount of token_b that user A expects in returnamount_b: Amount of token_b that user B is willing to givemin_a_for_b: Minimum amount of token_a that user B expects in return
fn move_token(
env: &Env,
token: &Address,
from: &Address,
to: &Address,
max_spend_amount: i128,
transfer_amount: i128,
)This internal function manages the token transfer process:
- Transfers tokens from the sender to the contract
- Transfers the agreed amount to the recipient
- Refunds any excess tokens back to the sender
-
Precondition Verification:
- Checks if amount_b ≥ min_b_for_a (ensuring user A's minimum price expectation is met)
- Checks if amount_a ≥ min_a_for_b (ensuring user B's minimum price expectation is met)
-
Authorization:
- Requires authorization from both parties
- Uses symmetric arguments, allowing signatures to be used interchangeably
-
Token Exchange:
- Transfers token_a from user A to user B
- Transfers token_b from user B to user A
- Automatically refunds any excess tokens
- Built using the Soroban SDK for Stellar's smart contract platform
- Implemented with
#![no_std]for efficient operation in blockchain environments - Uses Rust's type system for safety and reliability
- Symmetric design allows for flexible signature matching
- The contract ensures atomic execution, preventing partial swaps
- Authorization is required from both parties
- Minimum price protection prevents unfavorable trades
- The contract acts as a trustless intermediary
Contributions are welcome! Please feel free to submit a Pull Request.