Automated Market Maker — OLIGHFT SMART COIN Mastercard Tier Smart Contract
#![no_std] use soroban_sdk::{ contract, contractimpl, contracttype, Address, Env, token, }; #[contracttype] pub struct Pool { pub token_a: Address, pub token_b: Address, pub reserve_a: i128, pub reserve_b: i128, pub total_shares: i128, pub fee_bps: u32, } #[contract] pub struct McSwapDex; #[contractimpl] impl McSwapDex { /// Create a new liquidity pool pub fn create_pool( env: Env, admin: Address, token_a: Address, token_b: Address, fee_bps: u32, ) -> Pool { admin.require_auth(); let pool = Pool { token_a, token_b, reserve_a: 0, reserve_b: 0, total_shares: 0, fee_bps, }; // Store pool in persistent storage pool } /// Add liquidity and receive LP shares pub fn add_liquidity( env: Env, user: Address, amount_a: i128, amount_b: i128, ) -> i128 { user.require_auth(); // Transfer tokens to pool // Calculate LP shares: sqrt(amount_a * amount_b) // Mint LP tokens to user 0 // returns shares minted } /// Swap token_a for token_b (or vice versa) pub fn swap( env: Env, user: Address, token_in: Address, amount_in: i128, min_out: i128, // slippage protection ) -> i128 { user.require_auth(); // x * y = k constant product formula // fee = amount_in * fee_bps / 10000 // amount_out = reserve_b - (k / (reserve_a + net_in)) assert!(amount_out >= min_out, "slippage exceeded"); amount_out } /// Remove liquidity by burning LP shares pub fn remove_liquidity( env: Env, user: Address, shares: i128, ) -> (i128, i128) { user.require_auth(); // Proportional withdrawal (0, 0) } /// Get swap quote without executing pub fn get_quote( env: Env, token_in: Address, amount_in: i128, ) -> i128 { 0 } }
| Function | Description | Auth | Gas (XLM) |
|---|---|---|---|
| create_pool | Initialize a new token pair pool | Admin | ~0.018 |
| add_liquidity | Deposit tokens, receive LP shares | User | ~0.014 |
| remove_liquidity | Burn LP shares, withdraw tokens | User | ~0.012 |
| swap | Exchange one token for another | User | ~0.010 |
| get_quote | Preview swap output (read-only) | None | ~0.001 |
| get_reserves | Query pool reserves | None | ~0.001 |
| get_price | Current spot price of pair | None | ~0.001 |
| collect_fees | Withdraw accrued protocol fees | Admin | ~0.008 |
Constant product market maker for trustless swaps
COREEarn swap fees proportional to your liquidity
YIELDmin_out parameter prevents sandwich attacks
SAFETYAll-or-nothing execution in single transaction
ATOMIC0.05% stablecoins, 0.3% standard, 1% exotic
FLEXIBLEQuery swap output before executing
PREVIEWStake $50 to activate your Mastercard tier and earn $3 daily rewards. Choose your contract period:
When a user activates with $50, the fee is distributed across the 8-level upline binary tree:
| Generation | Role | Commission | Amount |
|---|---|---|---|
| — | Main Wallet (Platform) | — | $30 |
| Gen 1 | Direct Inviter | 10% | $5 |
| Gen 2 | Inviter’s Inviter | 6% | $3 |
| Gen 3 | Upline Level 3 | 4% | $2 |
| Gen 4 | Upline Level 4 | 4% | $2 |
| Gen 5 | Upline Level 5 | 4% | $2 |
| Gen 6 | Upline Level 6 | 4% | $2 |
| Gen 7 | Upline Level 7 | 4% | $2 |
| Gen 8 | Upline Level 8 | 4% | $2 |
| TOTAL | $50 | ||
#[contracttype] pub struct McStake { pub staker: Address, pub stake_amount: i128, // $50 activation pub daily_reward: i128, // $3 per day pub duration_days: u32, // 90 | 180 | 365 days pub service_fee: i128, // $20 platform fee pub sponsor: Address, // direct inviter (Gen 1) pub start_time: u64, pub claimed_days: u32, } // 8-generation binary tree commission rates (in $) const GEN_COMMISSIONS: [i128; 8] = [ 5_0000000, // Gen 1: $5 (direct inviter) 3_0000000, // Gen 2: $3 2_0000000, // Gen 3: $2 2_0000000, // Gen 4: $2 2_0000000, // Gen 5: $2 2_0000000, // Gen 6: $2 2_0000000, // Gen 7: $2 2_0000000, // Gen 8: $2 ]; // Total referral: $20 | Main wallet: $30 | Grand total: $50 #[contractimpl] impl McSwapDex { /// Activate Mastercard staking — $50 deposit + $20 service fee /// duration: 90 (3 mo) | 180 (6 mo) | 365 (1 yr) /// $50 split: $30 main wallet + $20 across 8-gen binary tree pub fn activate_stake( env: Env, staker: Address, sponsor: Address, duration: u32, ) { staker.require_auth(); assert!(duration == 90 || duration == 180 || duration == 365, "invalid period"); let token = get_payment_token(&env); let tok = token::Client::new(&env, &token); let activation = 50_0000000; // $50 let fee = 20_0000000; // $20 service fee // Transfer $50 + $20 fee from user to contract tok.transfer(&staker, &env.current_contract_address(), &(activation + fee)); // ── Distribute $50 activation across binary tree ── let main_wallet = get_main_wallet(&env); let mut remaining = activation; // $50 let mut current = sponsor.clone(); for gen in 0..8 { let commission = GEN_COMMISSIONS[gen]; if let Some(upline) = get_sponsor(&env, ¤t) { tok.transfer( &env.current_contract_address(), ¤t, &commission, ); remaining -= commission; current = upline; } else { // No upline — remaining goes to main wallet break; } } // Send remainder ($30 or more) to main wallet tok.transfer( &env.current_contract_address(), &main_wallet, &remaining, ); let stake = McStake { staker: staker.clone(), stake_amount: activation, daily_reward: 3_0000000, // $3/day duration_days: duration, service_fee: fee, sponsor: sponsor.clone(), start_time: env.ledger().timestamp(), claimed_days: 0, }; env.storage().persistent().set(&staker, &stake); } /// Claim accrued daily rewards pub fn claim_rewards(env: Env, staker: Address) -> i128 { staker.require_auth(); let mut stake: McStake = env.storage().persistent().get(&staker).unwrap(); let elapsed = (env.ledger().timestamp() - stake.start_time) / 86400; let claimable = elapsed.min(stake.duration_days as u64) as u32 - stake.claimed_days; let payout = (claimable as i128) * stake.daily_reward; stake.claimed_days += claimable; env.storage().persistent().set(&staker, &stake); // Transfer rewards to staker token::Client::new(&env, &get_payment_token(&env)) .transfer(&env.current_contract_address(), &staker, &payout); payout // 90d→$270 | 180d→$540 | 365d→$1,095 } }
| Function | Description | Auth | Cost |
|---|---|---|---|
| activate_stake | Deposit $50 + $20 fee, choose period (90/180/365 days) | User | $70 total |
| claim_rewards | Claim accrued $3/day rewards | User | ~0.01 XLM |
| get_sponsor | Get upline address for a given user | None | ~0.001 XLM |
#[test] fn test_swap_constant_product() { let env = Env::default(); env.mock_all_auths(); let client = McSwapDexClient::new(&env, &env.register_contract(None, McSwapDex)); // Add liquidity: 10000 XLM + 5000 USDC client.add_liquidity(&lp, &10000_0000000, &5000_0000000); // Swap 100 XLM for USDC let out = client.swap(&user, &xlm, &100_0000000, &40_0000000); assert!(out >= 40_0000000, "min_out not met"); // Verify k is preserved (with fees) let (r_a, r_b) = client.get_reserves(); assert!(r_a * r_b >= 10000_0000000 * 5000_0000000); }
OLIGHFT SMART COIN MC DEX Contract v2.1.0 • Stellar Testnet • Rust/WASM
Built with soroban-sdk 21.0.0 • April 2026