Skip to content

Liberdus/otc-swap-contract

Repository files navigation

OTC Swap Smart Contract

A secure and efficient smart contract for peer-to-peer token swaps on Ethereum, enabling users to create, fill, and manage OTC (Over-The-Counter) token swap orders with optional counterparty specification.

Features

  • 🤝 Peer-to-peer token swaps
  • 🔒 Optional counterparty specification
  • ⚡ Gas-efficient implementation
  • 🛡️ Comprehensive security measures
  • 🧪 Extensive test coverage
  • 📋 Batch order querying
  • ✅ OpenZeppelin standard compliance

Security Features

  • Reentrancy protection
  • SafeERC20 implementation
  • Input validation
  • Access control
  • Arithmetic overflow protection

Prerequisites

  • Node.js >= 14.0.0
  • npm >= 6.0.0
  • Hardhat

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/otc-swap-contract.git
cd otc-swap-contract
  1. Install dependencies:
npm install
  1. Compile contracts:
npx hardhat compile
  1. Run tests:
npx hardhat test

Contract Overview

The OTC Swap contract enables users to:

  1. Create swap orders specifying:

    • Sell token and amount
    • Buy token and amount
    • Optional specific counterparty
  2. Fill existing orders

  3. Cancel orders

  4. Query order details and status

Core Functions

function createOrder(
    address partner,
    address sellToken,
    uint256 sellAmount,
    address buyToken,
    uint256 buyAmount
) external returns (uint256)

function fillOrder(uint256 orderId) external

function cancelOrder(uint256 orderId) external

function getOrder(uint256 orderId) external view returns (
    address maker,
    address partner,
    address sellToken,
    uint256 sellAmount,
    address buyToken,
    uint256 buyAmount,
    uint256 createdAt,
    bool active
)

Usage Example

// Create an order
const sellAmount = ethers.parseEther('100');
const buyAmount = ethers.parseEther('200');
await tokenA.approve(otcSwap.address, sellAmount);
const orderId = await otcSwap.createOrder(
    ethers.ZeroAddress,  // Allow any counterparty
    tokenA.address,      // Sell token
    sellAmount,          // Sell amount
    tokenB.address,      // Buy token
    buyAmount           // Buy amount
);

// Fill an order
await tokenB.approve(otcSwap.address, buyAmount);
await otcSwap.fillOrder(orderId);

Testing

The contract includes a comprehensive test suite covering:

  • Basic functionality
  • Edge cases
  • Security scenarios
  • Malicious token attacks

Run tests with coverage:

npx hardhat coverage

Deployment

  1. Set up your environment variables:
cp .env.example .env
# Edit .env with your configuration
  1. Deploy to network:
npx hardhat run scripts/deploy.js --network <network-name>

Architecture

Order Structure

struct Order {
    address maker;
    address partner;     // Optional specific counterparty
    address sellToken;
    uint256 sellAmount;
    address buyToken;
    uint256 buyAmount;
    uint256 createdAt;
    bool active;
}

Events

event OrderCreated(
    uint256 indexed orderId,
    address indexed maker,
    address indexed partner,
    address sellToken,
    uint256 sellAmount,
    address buyToken,
    uint256 buyAmount,
    uint256 createdAt
);

event OrderFilled(
    uint256 indexed orderId,
    address indexed taker,
    address sellToken,
    uint256 sellAmount,
    address buyToken,
    uint256 buyAmount
);

event OrderCancelled(
    uint256 indexed orderId,
    address indexed maker
);

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Security

This contract has not been audited. Use at your own risk.

If you discover any security issues, please contact us instead of using the issue tracker.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • OpenZeppelin for their secure contract implementations
  • Hardhat for the development environment
  • The Ethereum community for best practices and standards

About

A secure, decentralized peer-to-peer OTC token swap smart contract with optional counterparty specification and comprehensive testing suite.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors