Introduction to P2P Swap Contract
The P2P Swap Contract is a decentralized peer-to-peer token exchange system that enables users to create, manage, and fulfill token swap orders in a trustless marketplace environment. Built on the EVVM ecosystem, it provides a comprehensive order book system with flexible fee structures and integrated staking rewards.
Core Contract Functions
Order Management System
The P2P Swap Contract implements a sophisticated order book system:
- Order Creation (
makeOrder): Users can create swap orders offering one token in exchange for another - Order Cancellation (
cancelOrder): Order creators can cancel their unfilled orders and reclaim their tokens - Order Fulfillment: Two dispatch methods with different fee structures for order execution
- Market Discovery: Automatic market creation for new token pairs
Fee Structure Models
- Proportional Fee Model: Percentage-based fees calculated from order amounts
- Fixed Fee Model: Capped fee structure with maximum limits to protect users
- Dynamic Fee Distribution: Fees distributed between sellers, service treasury, and MATE stakers
EVVM Ecosystem Integration
- Payment Processing: Full integration with EVVM's payment system for secure token transfers
- Staking Rewards: Executors who are MATE stakers receive additional rewards
- Signature Authorization: EIP-191 signature verification for all operations
- Service Staking: Contract can stake MATE tokens to participate in the staking ecosystem
Market Architecture
Dynamic Market Creation
- Automatic Markets: New markets created automatically when users create orders for new token pairs
- Market Identification: Each token pair (tokenA/tokenB) gets a unique market ID
- Order Storage: Orders organized by market ID and sequential order IDs within each market
- Efficient Lookup: Fast market discovery and order retrieval through optimized mappings
Order Book Structure
- Sequential Order IDs: Orders assigned sequential IDs within each market for easy tracking
- Slot Management: Efficient slot reuse when orders are cancelled or filled
- Order Metadata: Complete order information including seller, amounts, and status
- Market Statistics: Real-time tracking of available orders and market capacity
Economic Model
Fee Distribution System
The contract implements a three-way fee split:
- Seller Rewards: Portion of fees goes to order creators as incentive
- Service Treasury: Contract accumulates fees for operational sustainability
- MATE Staker Rewards: Stakers who execute transactions receive fee portions
Staker Incentive Structure
- Base Rewards: All staker executors receive MATE token rewards
- Priority Fee Bonuses: Additional rewards for processing priority transactions
- Execution Multipliers: Higher rewards for complex operations (3x-5x base rewards)
- Fee Sharing: Stakers receive portions of transaction fees as additional compensation
Treasury Management
- Fee Accumulation: Service fees automatically accumulate in contract balances
- Withdrawal System: Time-delayed withdrawal proposals for treasury management
- Balance Tracking: Real-time tracking of contract token balances
- Staking Integration: Contract can stake accumulated MATE tokens for additional rewards
Security Architecture
Signature-Based Authorization
All operations require cryptographic signatures using EIP-191 standard:
- User Authorization: Each action must be signed by the relevant user
- Replay Protection: Nonce-based system prevents transaction replay attacks
- Parameter Integrity: Signatures cover all critical transaction parameters
- Message Verification: Dedicated SignatureUtils library for consistent verification
Time-Delayed Governance
Administrative functions use time-delayed execution:
- Proposal Period: 1-day waiting period for all administrative changes
- Transparency: All changes visible before execution
- Emergency Controls: Proposals can be cancelled during waiting period
- Owner Management: Secure owner transfer process with proposal/acceptance pattern
Integration Capabilities
EVVM Core Integration
- Payment Functions: Direct integration with EVVM's
pay,caPay, anddisperseCaPayfunctions - Token Abstractions: Works with EVVM's internal token representation system
- Staker Detection: Automatic detection of staker status for reward distribution
- Priority Processing: Support for priority fees and expedited transaction processing
Staking Service Integration
- Service Staking: Contract can stake MATE tokens as a service participant
- Reward Accumulation: Automatic accumulation of staking rewards
- Unstaking Process: Controlled unstaking with proper authorization
- Balance Management: Integration with staking balance tracking
Execution Methods
The P2P Swap Contract supports multiple execution approaches:
Direct Execution
- User-Initiated: Users interact directly with the P2P Swap contract
- Immediate Processing: Transactions submitted directly to the blockchain
- Full Control: Complete autonomy over transaction timing and parameters
Fisher Execution
- Fishing Spot Integration: Users submit transactions through the fishing spot system
- Fisher Processing: Specialized fisher nodes capture and execute transactions
- Staker Benefits: Fisher executors who are stakers receive enhanced rewards
- Optimized Routing: Fishers handle transaction optimization and gas management
Order Lifecycle
Order Creation Process
- Signature Generation: User signs order parameters with their private key
- Token Transfer: User's tokens transferred to contract via EVVM payment system
- Market Assignment: Order assigned to appropriate market (created if necessary)
- Order Storage: Order details stored in contract with unique ID
- Reward Distribution: Staker executors receive MATE token rewards
Order Fulfillment Process
- Order Selection: Buyer selects order from available market orders
- Signature Verification: Buyer's signature verified for dispatch authorization
- Token Exchange: Automatic token swap between buyer and seller
- Fee Processing: Fees calculated and distributed to all parties
- Order Cleanup: Completed order removed from active order book
Order Cancellation Process
- Authorization Check: Verify cancellation request from order creator
- Token Return: Original tokens returned to order creator
- Order Removal: Order removed from market order book
- Market Update: Market statistics updated to reflect cancellation
Fee Calculation Models
Proportional Fee Model
- Percentage-Based: Fees calculated as percentage of order amount
- Configurable Rate: Admin-adjustable percentage rate (default 5%)
- Predictable Costs: Users know exact fee percentage in advance
- Scalable Structure: Fees scale proportionally with order size
Fixed Fee Model
- Capped Fees: Maximum fee limit protects users from excessive charges
- Hybrid Approach: Uses proportional calculation up to maximum limit
- User Protection: Prevents fee exploitation on large orders
- Flexible Implementation: Supports both models simultaneously
Administrative Features
Governance System
- Owner Management: Secure owner transfer with time-delayed acceptance
- Fee Configuration: Adjustable fee percentages and distribution ratios
- Treasury Operations: Controlled withdrawal system for accumulated fees
- Staking Management: Contract-level staking operations for service participation
Monitoring and Analytics
- Order Tracking: Complete order history and status monitoring
- Market Analytics: Real-time market statistics and order book depth
- Fee Analytics: Detailed fee collection and distribution tracking
- Balance Monitoring: Real-time contract balance tracking across all tokens
The P2P Swap Contract provides a comprehensive decentralized exchange infrastructure that seamlessly integrates with the EVVM ecosystem while offering flexible fee structures, robust security, and attractive incentives for both users and stakers.