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:
- Set up your wallet
- Funded your wallet with goerli ETH
- Bridged Goerli ETH to Linea
- 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;
}
}
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:
Run
truffle dashboard
in your terminal, which will open a window on port24012
.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 is59140
.Run
truffle migrate --network dashboard
in a separate terminal.Navigate back to
localhost:24012
. You should see a prompt asking your to confirm the deployment. Click Confirm.
Next, you can optionally verify your contract on the network.