Berachain Contract Deploy (MacOS)

Deploy a contract (in this case a token) to the Berachain testnet for possible airdrops in the future.

If this is too hard or daunting, try the easy version. Who knows which ways will work.

Download Foundry

Foundry is toolkit for Ethereum contract development and testing.

Enter installation line into Terminal:

curl -L https://foundry.paradigm.xyz | bash
foundryup

Create Token Contract

1. Create directory to store contract

cd path/to/directory

2. Create a new Forge (Foundry) project

forge init my_token --vscode

This creates a directory with a basic forge project structure

Delete template files:

  • src/Counter.sol

  • test/Counter.t.sol

  • script/Counter.s.sol

3. Install OpenZeppelin ERC20 implementations

OpenZeppelin is the industry standard for certain implementations of commonly used contracts (tokens, NFTs, etc.). All contracts are audited and open-sourced so developers don't have to recreate the same contracts.

forge install openzeppelin/openzeppelin-contracts

Now open remappings.txt within the root of your project directory and add the last line.

ds-test/=lib/forge-std/lib/ds-test/src/ 
forge-std/=lib/forge-std/src/ 
@openzeppelin/=lib/openzeppelin-contracts/        // Add this line

This will make it easier to import OpenZeppelin's files.

4. Implement Token Contract

cd src
touch token.sol

Open VSCode and install Solidity by Nomic Foundation.

Then open the token directory in VSCode. Opening just token.sol may give false compile error messages (ex: File not found).

Paste this basic contract code into token.sol and change "TOKEN_NAME" and "TOKEN_TICKER" to your liking.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract token is ERC20 {
    uint256 public constant INITIAL_SUPPLY = 1_000_000_000 * 1 ether;
    uint256 public constant BURN_PERCENTAGE = 1; // 1% 
    address public constant BURN_ADDRESS = 0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF; 

    constructor() ERC20("TOKEN_NAME", "TOKEN_TICKER"){
        _mint(msg.sender, INITIAL_SUPPLY);
    }

    function transfer(address recipient, uint256 amount) public override returns(bool) { 
        uint256 burnAmount = (amount * BURN_PERCENTAGE) / 100;
        super._transfer(msg.sender, recipient, amount - burnAmount);
        super._transfer(msg.sender, BURN_ADDRESS, burnAmount);
        return true;
    }
}

Deploy Token Contract to Berachain Testnet

1. Get testnet funds

Add Berachain network to MetaMask here.

Use a test wallet to be safe and paste your address into this faucet. Use multiple wallets to get more testnet funds but it may take several tries.

2. Deploy token to testnet

Obtain your private key from MetaMask of the wallet with BERA testnet funds and replace it in the given code:

forge create src/token.sol:token --rpc-url https://artio.rpc.berachain.com/ --private-key (PRIVATE_KEY)

This code will deploy your contract to Berachain testnet.

You may get errors such as:

  • Contract was not deployed

  • (code: -32000, message: already known, data: None)

  • (code: -32000, message: replacement transaction underpriced, data: None)

Continue to deploy until it works:

View your contract on Berachain testnet explorer and pasting the Transaction Hash.


Resources:

Last updated