Skip to main content

Setting Up the Template for Your OP_NET Contract

This guide explains how to set up a custom OP_NET contract by forking the OP_20 template as a base for your smart contract development. The OP_20 template already includes a sample implementation of a token contract, making it an ideal starting point for customization.


Step 1: Forking the OP_20 Template

  1. Visit the Repository:
    Navigate to the official OP_20 GitHub repository: https://github.com/btc-vision/OP_20

  2. Fork the Repository:
    Click the "Fork" button on GitHub to create a copy of the OP_20 repository in your account.

  3. Clone Your Fork:
    Clone your forked repository to your local machine:

    git clone https://github.com/your-username/OP_20.git
    cd OP_20
  4. Install Dependencies:
    Run the following command to install all necessary dependencies:

    npm install

Step 2: Locate the Sample Contract

After setting up the repository, you can find the sample token contract in the following file:

src/contracts/MyToken.ts

This contract provides a solid base with the following functionalities:

  • Token deployment with customizable name, symbol, decimals, and maximum supply.
  • Built-in minting and airdrop methods.
  • Ready-to-use OP_20 compatibility.

Step 3: Customize Your Contract

Modify the onDeployment method to set your token's name, symbol, decimals, and max supply. Add or remove methods in the execute function to customize your token's functionality. For example, you could implement a burn method or advanced token economics.

Example: Adding a Burn Method

private burn(callData: Calldata): BytesWriter {
const amount: u256 = callData.readU256(); // Read the amount to burn from the calldata

const resp = this._burn(amount, false); // true means that only the deployer can burn

const writer: BytesWriter = new BytesWriter(1);
writer.writeBoolean(resp); // Write the response to the writer

return writer;
}

Step 4: Build and Test Your Contract

  1. Compile Your Contract:
    Run the following command to compile your contract into WebAssembly (WASM):

    npm run build
  2. Test Your Contract:

    Learn more about testing your contract using the unit test framework.