Skip to main content

Truffle

In this tutorial, we'll walk through creating a basic Truffle project and deploying a token contract.

Here's a video walkthrough:

Prerequisites​

Before you begin, ensure you've:

  1. Set up your wallet
  2. Funded your wallet with goerli ETH
  3. Bridged Goerli ETH to Linea
  4. Installed Truffle using the recommended installation procedure.

Create a Truffle project​

To create an empty Truffle project, run:

truffle init linea-tutorial

Change into the new directory:

cd linea-tutorial

Write the smart contract​

Create your smart contract in the contracts directory by either creating a new file Token.sol or calling truffle create contract Token. Then, add the following code:

pragma solidity 0.8.17;

// SPDX-License-Identifier: MIT

contract Token {
string public name = "My Token";
string public symbol = "MTK";
uint8 public decimals = 18;
uint256 public totalSupply = 100000000;

mapping (address => uint256) public balances;
address public owner;

constructor() {
owner = msg.sender;
balances[owner] = totalSupply;
}

function transfer(address recipient, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance.");
balances[msg.sender] -= amount;
balances[recipient] += amount;
}
}
Do not use this contract code in production

The above contract is for testing purposes and has not been audited.

You can check if it compiles by running truffle compile from the root folder.

Write the migration script​

To tell Truffle how, and in what order we want to deploy our smart contracts, we need to write a migration script.

Create 1_deploy_token.js in the migrations directory, and add the following code:

const Token = artifacts.require("Token");

module.exports = function (deployer) {
deployer.deploy(Token);
};

Deploy your contract​

Truffle allows you to deploy through the Truffle Dashboard using your MetaMask wallet!

Truffle Dashboard​

Truffle Dashboard allows you to forgo saving your private keys locally, instead connecting to your MetaMask wallet for deployments. To deploy with Truffle Dashboard, you need to:

  1. Run truffle dashboard in your terminal, which will open a window on port 24012.

  2. Navigate to localhost:24012 in your browser. Please ensure that Dashboard is connected to the Linea testnet by connecting your MetaMask wallet to Linea. For reference, the Linea testnet network ID is 59140.

    confirm network

  3. Run truffle migrate --network dashboard in a separate terminal.

  4. Navigate back to localhost:24012. You should see a prompt asking your to confirm the deployment. Click Confirm.

    confirm deployment

Next, you can optionally verify your contract on the network.

note

You may encounter rate limiting if you are connecting via the public endpoint. If your dapp needs full Infura node access, open a support ticket here.