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.
- 🤝 Peer-to-peer token swaps
- 🔒 Optional counterparty specification
- ⚡ Gas-efficient implementation
- 🛡️ Comprehensive security measures
- 🧪 Extensive test coverage
- 📋 Batch order querying
- ✅ OpenZeppelin standard compliance
- Reentrancy protection
- SafeERC20 implementation
- Input validation
- Access control
- Arithmetic overflow protection
- Node.js >= 14.0.0
- npm >= 6.0.0
- Hardhat
- Clone the repository:
git clone https://github.com/yourusername/otc-swap-contract.git
cd otc-swap-contract- Install dependencies:
npm install- Compile contracts:
npx hardhat compile- Run tests:
npx hardhat testThe OTC Swap contract enables users to:
-
Create swap orders specifying:
- Sell token and amount
- Buy token and amount
- Optional specific counterparty
-
Fill existing orders
-
Cancel orders
-
Query order details and status
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
)// 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);The contract includes a comprehensive test suite covering:
- Basic functionality
- Edge cases
- Security scenarios
- Malicious token attacks
Run tests with coverage:
npx hardhat coverage- Set up your environment variables:
cp .env.example .env
# Edit .env with your configuration- Deploy to network:
npx hardhat run scripts/deploy.js --network <network-name>struct Order {
address maker;
address partner; // Optional specific counterparty
address sellToken;
uint256 sellAmount;
address buyToken;
uint256 buyAmount;
uint256 createdAt;
bool active;
}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
);- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
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.
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenZeppelin for their secure contract implementations
- Hardhat for the development environment
- The Ethereum community for best practices and standards