Skip to content

btc-vision/motochef-contract

Repository files navigation

MotoChef Contract

MotoChef is a decentralized smart contract that facilitates staking and reward distribution for LP tokens, including a dedicated BTC pool (pool ID 0). Inspired by the SushiSwap MasterChef design, MotoChef enables users to stake LP tokens and BTC to earn MOTO rewards while ensuring robust UTXO selection and efficient pool management.


🚀 Table of Contents


📌 Overview

MotoChef allows users to stake LP tokens and BTC to earn MOTO token rewards. Each pool represents a staking opportunity for a specific token, with Pool ID 0 dedicated exclusively to BTC. The contract efficiently calculates rewards based on user stakes, pool allocation points, and staking duration, while securely managing treasury fees through a UTXO selection mechanism.


✨ Features

Decentralized Staking – Stake LP tokens and BTC to earn MOTO rewards.
Dynamic Reward Distribution – Rewards are calculated based on pool allocation points and user stakes.
Secure Fee Management – Implements precise UTXO selection to ensure fees go to the treasury.
Flexible Pool Management – Supports multiple pools with custom allocation points.
Transparent Event Logging – Emits events for staking, unstaking, deposits, withdrawals, and reward harvesting.
Robust Security – Features reentrancy protection and safe arithmetic to mitigate vulnerabilities.


🏛 Architecture

MotoChef is implemented using AssemblyScript, drawing inspiration from the SushiSwap MasterChef contract. Key components include:

  • 🔹 PoolInfo & StoredPoolInfoArray – Manages LP token addresses, allocation points, reward calculations, and storage efficiency.
  • 🔹 UserInfoMemoryMap – Tracks individual user stakes and reward debts for accurate reward distribution.
  • 🔹 UTXO Utilities – Functions like findFirstUTXOOfValue() and spendUTXO() help manage fee transactions correctly.
  • 🔹 ReentrancyGuard – Protects critical functions from malicious reentrant calls.
  • 🔹 Event System – Emits events to enable transparent off-chain monitoring.

🛠 Getting Started

📋 Prerequisites

  • Node.js v22.x
  • npm (Node Package Manager)
  • Development Environment – Ensure you meet the OP_NET prerequisites

🔧 Installation / Quickstart

Clone the repository and run the setup script to install everything:

git clone https://github.com/btc-vision/motochef-contract.git
cd motochef-contract
npm run setup

This will:

  1. Initialize git submodules
  2. Install root and submodule dependencies
  3. Build the project
  4. Copy necessary files
  5. Run all unit tests

🧪 Testing

MotoChef includes a comprehensive test suite covering:

  • UTXO Selection – Ensures correct identification of UTXOs based on value and recipient.
  • Staking & Unstaking – Verifies deposits, withdrawals, and reward distribution.
  • Pool Management – Tests reward calculations and allocation adjustments.
  • Security Measures – Confirms ReentrancyGuard prevents attacks.

Run tests using:

npm run test

🚀 Deployment

MotoChef can be deployed on OP_NET using two methods:

📜 1. OP_NET Deployment Guide

Follow the detailed guide: Deploying a Contract on OP_NET.

💼 2. OP_WALLET Deployment

Use OP_WALLET for a manual deployment:

  • Open OP_WALLET.
  • Click "Deploy" and upload the compiled contract.

🔒 Static Template Code Addresses (contractPubKey) for MotoChefFactory

MotoChefFactory clones from published template contracts using their code addresses (contractPubKey), not bytecode hashes.

  1. Build templates and publish them to your target network:
npm run build:mocks && npm --prefix testnet-scripts run deploy:templates
  1. Grab template contractPubKey values:
  • Open testnet-scripts/deployments/templates-latest.json
  • Copy contractPubKey for TemplateOP20 and TemplateMotoChef
  1. Convert each 0x-hex pubkey to a uint8[] and paste into the factory:
npm --prefix testnet-scripts run utils:hex-to-uint8 -- --hex 0x<YOUR_PUBKEY>

Update in src/core/MotoChefFactory.ts:

  • MotoChefFactory.TOKEN_TEMPLATE_BYTECODE_ADDRESS
  • MotoChefFactory.MOTOCHEF_TEMPLATE_BYTECODE_ADDRESS

Example:

private static readonly TOKEN_TEMPLATE_BYTECODE_ADDRESS: Address = new Address([
  /* uint8[] from utils:hex-to-uint8 */
]);
private static readonly MOTOCHEF_TEMPLATE_BYTECODE_ADDRESS: Address = new Address([
  /* uint8[] from utils:hex-to-uint8 */
]);
  1. Rebuild and deploy the factory, then initialize it:
npm --prefix testnet-scripts run deploy:factory
npm --prefix testnet-scripts run initialize:factory
  1. Deploy a token and verify:
npm --prefix testnet-scripts run testnet:01 -- \
  --name "MyToken" --symbol "MTK" --decimals 18 \
  --initial-mint-amount "1000000000000000" \
  --max-supply "1000000000000000000000000" \
  --free-mint-supply "100000" --free-mint-per-tx "1000"

Troubleshooting:

  • If deployment fails, ensure the factory static arrays match the published templates’ contractPubKey values.

🤝 Contributing

Contributions are welcome! Follow these steps to contribute:

1️⃣ Fork the repository on GitHub.
2️⃣ Create a new branch:

git checkout -b feature/YourFeature

3️⃣ Commit your changes with clear messages.
4️⃣ Push the branch and open a pull request.

📌 Ensure your code adheres to the project’s coding standards and includes relevant tests.


📜 License

This project is licensed under the MIT License.

About

MotoChef Contract

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors