Skip to content

mooncitydev/perpetual-trading-smart-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

425 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Gambit Protocol Contracts

License: MIT Solidity Hardhat

Decentralized perpetual trading platform with low fees, zero price impact, and up to 50x leverage.

πŸ“‹ Table of Contents

🎯 Overview

Gambit Protocol is a decentralized perpetual trading platform that enables users to trade perpetual futures with leverage up to 50x. The protocol features a unique AMM-based liquidity model that provides zero price impact trades and low fees.

Key Components

  • Vault: Core contract managing positions, collateral, and liquidity
  • Position Manager: Handles position creation, modification, and liquidation
  • Order Book: Manages limit orders and stop-loss orders
  • Oracle System: Fast price feeds with Chainlink integration
  • GMT Token: Native governance and utility token
  • Staking & Rewards: Staking mechanisms for GMT and GLP tokens
  • GLP Manager: Manages GLP (Gambit Liquidity Provider) token minting and burning

✨ Features

  • πŸš€ High Leverage: Trade with up to 50x leverage
  • πŸ’° Low Fees: Competitive trading and funding fees
  • πŸ”’ Zero Price Impact: AMM-based liquidity model
  • πŸ“Š Multiple Markets: Support for various trading pairs
  • 🎯 Limit Orders: Advanced order types including stop-loss
  • πŸ’Ž Staking Rewards: Earn rewards by staking GMT and GLP
  • 🌐 Multi-Chain: Deployable on multiple EVM-compatible chains
  • πŸ” Security: Audited smart contracts with comprehensive test coverage

πŸ—οΈ Architecture

contracts/
β”œβ”€β”€ core/              # Core trading logic
β”‚   β”œβ”€β”€ Vault.sol      # Main vault contract
β”‚   β”œβ”€β”€ PositionManager.sol
β”‚   β”œβ”€β”€ PositionRouter.sol
β”‚   β”œβ”€β”€ OrderBook.sol
β”‚   └── Router.sol
β”œβ”€β”€ gambit-token/      # GMT token implementation
β”‚   β”œβ”€β”€ GMT.sol
β”‚   └── Treasury.sol
β”œβ”€β”€ staking/           # Staking and rewards
β”‚   β”œβ”€β”€ RewardTracker.sol
β”‚   β”œβ”€β”€ RewardRouter.sol
β”‚   └── Vester.sol
β”œβ”€β”€ oracle/            # Price feed system
β”‚   β”œβ”€β”€ PriceFeed.sol
β”‚   └── FastPriceFeed.sol
β”œβ”€β”€ amm/               # AMM integrations
β”‚   β”œβ”€β”€ PancakeRouter.sol
β”‚   └── UniRouter.sol
└── libraries/         # Shared libraries
    β”œβ”€β”€ math/
    └── token/

πŸš€ Getting Started

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/gambit-protocol/contracts.git
    cd contracts
  2. Install dependencies

    npm install
  3. Set up environment variables

    Create an env.json file in the root directory (see env.json.example for reference):

    {
      "BSC_URL": "https://bsc-dataseed.binance.org/",
      "BSC_DEPLOY_KEY": "your-private-key",
      "BSCSCAN_API_KEY": "your-api-key"
    }

πŸ“– Usage

Compile Contracts

npm run compile

Run Tests

npm test

Generate TypeScript Types

npm run typechain

Check Contract Sizes

npm run size

Clean Build Artifacts

npm run clean

πŸ§ͺ Testing

The project includes comprehensive test suites covering:

  • Vault operations (deposit, withdraw, swap)
  • Position management (open, close, liquidate)
  • Order book functionality
  • Staking and rewards
  • Token operations

Run all tests:

npm test

Run specific test file:

npx hardhat test test/core/Vault/depositCollateral.js

🚒 Deployment

Local Development

  1. Start a local Hardhat node:

    npx hardhat node
  2. Deploy contracts to local network:

    npx hardhat run scripts/core/deployVault.js --network localhost

Testnet Deployment

Deploy to BSC Testnet:

npx hardhat run scripts/core/deployVault.js --network bscTestnet

Mainnet Deployment

⚠️ Warning: Always verify contracts and test thoroughly before mainnet deployment.

npx hardhat run scripts/core/deployVault.js --network bsc

πŸ”’ Security

Audits

This codebase has been audited by:

  • ABDK Consulting (see audits/ directory)

Best Practices

  • Always verify contracts on block explorers after deployment
  • Use timelock contracts for critical parameter changes
  • Implement multi-sig wallets for governance
  • Regular security reviews and updates

Known Considerations

  • Solidity version 0.6.12 is used for compatibility
  • Contracts use SafeMath for arithmetic operations
  • ReentrancyGuard is implemented where necessary

πŸ“ Scripts

The scripts/ directory contains deployment and management scripts:

  • Core Contracts: scripts/core/
  • Token Deployment: scripts/gambit-token/
  • Staking Setup: scripts/staking/
  • Peripherals: scripts/peripherals/

Example usage:

npx hardhat run scripts/core/deployVault.js --network bscTestnet

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a 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

Development Guidelines

  • Follow Solidity style guide
  • Write comprehensive tests for new features
  • Update documentation as needed
  • Ensure all tests pass before submitting PR

πŸ“„ License

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

πŸ“ž Contact

πŸ™ Acknowledgments

  • Built on the foundation of decentralized finance principles
  • Inspired by innovative DeFi protocols in the ecosystem
  • Community-driven development and feedback

⚠️ Disclaimer: This software is provided "as is" without warranty. Use at your own risk. Always conduct thorough testing and audits before deploying to mainnet.

Packages

 
 
 

Contributors