Using Truffle
Overview
This guide walks you through using Truffle migrations to deploy an NFT smart contract to the Palm network.
🏁 Prerequisites
- Get PALM Tokens to pay the deployment transaction fees.
- Connect to Palm network
- MetaMask Wallet Setup to get your private key.
- Install Node.js.
Tutorial
1. Initialize Your NPM Project
npm init
2. Install Truffle on Your Project Folder
npm install --save truffle
3. Initialize Truffle
npx truffle init
4. Install Truffle’s HDWalletProvider
npm install --save @truffle/hdwallet-provider
5. Add your Contract
We’ll use a contract based on the OpenZeppelin library’s ERC-721 implementation.
a) First, install the Open Zeppelin library in order to inherit its classes:
npm install --save @openzeppelin/contracts
b) Next, create a file named NFT.sol
under the contracts
folder.
c) Lastly, add the following smart contract to NFT.sol
file. Note that we have named our contract NFT
, this can be any name.
// SPDX-License-Identifier: MITpragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract NFT is ERC721 { constructor() ERC721("MyERC721", "M721") {}}
6. Create Your Migration File
Under the migrations
folder, create your filename: 1_example_migration.js
// At the beginning of the migration, we tell Truffle // which contracts we'd like to interact with via the artifacts.require() method. var MyContract = artifacts.require("NFT");
//The deployer object is your main interface for staging deployment tasks, // and its API is described at the bottom of this page. module.exports = function(deployer) { // deployment steps deployer.deploy(MyContract); };
7. Set environment variables with dotenv
- Install
dotenv
npm install --save dotenv
- Create a
.gitignore
file
A great example of a Solidity .gitignore file can be found here
- Create an
.env
file n your project’s root folder, and set environment variables in the file as follows
PRIVATE_KEY = #The private key of the account you intend to use on the Palm network INFURA_API_KEY = #Your Infura API key
8. Edit truffle-config.js
with the following text
const HDWalletProvider = require("@truffle/hdwallet-provider");
require('dotenv').config() // store environment variables from '.env' to process.env
module.exports = { compilers: { solc: { version: "^0.8.0" } }, networks: { palm_testnet: { provider: () => new HDWalletProvider({ providerOrUrl: `https://palm-testnet.infura.io/v3/${process.env.INFURA_API_KEY}`, privateKeys: [ process.env.PRIVATE_KEY ] }), network_id: 11297108099, // chain ID gasPrice: 1000 // gas price in gwei }, palm_mainnet: { provider: () => new HDWalletProvider({ providerOrUrl: `https://palm-mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`, privateKeys: [ process.env.PRIVATE_KEY ] }), network_id: 11297108109, gasPrice: 1000 } }}
9. Deploy to the Target Palm Network Environment
npx truffle migrate --network palm_testnet
npx truffle migrate --network palm_mainnet
Compiling your contracts...===========================> Everything is up to date, there is nothing to compile.⠏ Fetching solc version list from solc-bin. Attempt #1⠦ Fetching solc version list from solc-bin. Attempt #1Starting migrations...======================> Network name: 'palm_testnet'> Network id: 11297108099> Block gas limit: 18800000 (0x11edd80)
1_example_migration.js======================⠹ Fetching solc version list from solc-bin. Attempt #1 Deploying 'NFT' --------------- > transaction hash: 0xb65c36a771336168ca01db091d7132209baca3a576cfa563bc19b157449d3de8 > Blocks: 1 Seconds: 4lc-bin. Attempt #1 > contract address: 0x3DB7Da7f66a416B1865b699E426fE9e413285BB9 > block number: 8197697 > block timestamp: 1666119909 > account: 0xF92d8201C320cB952fE551b1Ec9e7AD063a3649d > balance: 0.499999997349611 > gas used: 2650389 (0x287115) > gas price: 0.000001 gwei > value sent: 0 ETH > total cost: 0.000000002650389 ETH
> Saving artifacts ------------------------------------- > Total cost: 0.000000002650389 ETH
Summary=======> Total deployments: 1> Final cost: 0.000000002650389 ETH
10. Look Up Your Deployment on the Palm Network Block Explorer
Paste your `contract address:` from the log of the previous step into the search bar. It may take 10-20 minutes to appear.
Testnet | Mainnet | |
---|---|---|
Explorer URL | https://testnet.palm.chainlens.com/ | https://palm.chainlens.com/ |
If successful, you will see something like this screenshot:
11. Install Truffle-Plugin-Verify
This plugin will allow you to automatically verify your smart contracts’s source code straight from the CLI.
npm install -D truffle-plugin-verify
12. Add the plugin to your truffle-config.js
file
const HDWalletProvider = require("@truffle/hdwallet-provider");
require('dotenv').config() // store environment variables from '.env' to process.env
module.exports = { plugins: ['truffle-plugin-verify'], //<== added line compilers: { solc: { version: "^0.8.0" } }, networks: { palm_testnet: { provider: () => new HDWalletProvider({ providerOrUrl: `https://palm-testnet.infura.io/v3/${process.env.INFURA_API_KEY}`, privateKeys: [ process.env.PRIVATE_KEY ] }), network_id: 11297108099, // chain ID gasPrice: 1000, // gas price in gwei }, palm_mainnet: { provider: () => new HDWalletProvider({ providerOrUrl: `https://palm-mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`, privateKeys: [ process.env.PRIVATE_KEY ] }), network_id: 11297108109, gasPrice: 1000, } }}
13. Verify Contract
Use the name of your contract to verify on the desired network.
npx truffle run verify NFT --network palm_testnet
npx truffle run verify NFT --network palm_mainnet
Verifying contracts on etherscan Etherscan has no support for network palm_mainnet with chain id 11297108109Verifying contracts on sourcify Verifying NFT Pass - Verified: https://sourcify.dev/#/lookup/0x9A08eF6efeBEa9834201Ac673C555653e0E11564 Successfully verified 1 contract(s).