Skip to content

Using Truffle

Overview

Truffle is a popular development framework for building, testing, and deploying smart contracts on the Waterfall network. It provides a suite of tools and libraries that make it easier for developers to write, compile, and deploy smart contracts, as well as to interact with the Waterfall network.

Some of the key features of Truffle include:

  • A development environment that includes a console for interacting with the Waterfall network
  • A compiler that can convert Solidity code (the language used to write smart contracts on Waterfall) into bytecode that can be executed on the Ethereum Virtual Machine (EVM)
  • A test framework that allows developers to write automated tests for their smart contracts
  • A deployment tool that makes it easy to deploy smart contracts to the Waterfall network
  • A built-in smart contract management system that makes it easy to keep track of and manage multiple contracts

Truffle has become a popular tool among Waterfall developers because it simplifies the development process and makes it easier to write, test, and deploy smart contracts. It also provides a wide range of resources and documentation to help developers get started, and to learn more about smart contract development on the Waterfall network.

Installation

Refer to the installation instructions to install Truffle.
eg: npm install -g truffle

Create Project

  1. Create a new directory for your Truffle project:
    mkdir HelloWaterfall
    cd HelloWaterfall
    
  2. Create Truffle project
    truffle init
    
  3. Create HelloWaterfall.sol file in contracts directory and paste example contract from solidity documentation site
    // SPDX-License-Identifier: GPL-3.0
    pragma solidity ^0.8.4;
    
    contract Coin {
        // The keyword "public" makes variables
        // accessible from other contracts
        address public minter;
        mapping(address => uint) public balances;
    
        // Events allow clients to react to specific
        // contract changes you declare
        event Sent(address from, address to, uint amount);
    
        // Constructor code is only run when the contract
        // is created
        constructor() {
            minter = msg.sender;
        }
    
        // Sends an amount of newly created coins to an address
        // Can only be called by the contract creator
        function mint(address receiver, uint amount) public {
            require(msg.sender == minter);
            balances[receiver] += amount;
        }
    
        // Errors allow you to provide information about
        // why an operation failed. They are returned
        // to the caller of the function.
        error InsufficientBalance(uint requested, uint available);
    
        // Sends an amount of existing coins
        // from any caller to an address
        function send(address receiver, uint amount) public {
            if (amount > balances[msg.sender])
                revert InsufficientBalance({
                    requested: amount,
                    available: balances[msg.sender]
                });
    
            balances[msg.sender] -= amount;
            balances[receiver] += amount;
            emit Sent(msg.sender, receiver, amount);
        }
    }
    

Compile and migration

  1. Compile contracts
    truffle compile
    
  2. Create 1-deploy.js file in migrations directory and past next code:
    const Coin = artifacts.require("Coin");
    
    module.exports = function (deployer) {
        deployer.deploy(Coin);
    };
    

Check contracts in develop mode

Go to the develop console

truffle develop
1. Deploy contract
migrate
let instance = await Coin.deployed();
2. Check minter
> instance.minter()
'0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2'
3. Check balance
> let balance = await instance.balances('0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2')
undefined
> balance.toNumber()
0
4. Mint token
> instance.mint('0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2', 1000)
{
  tx: '0x5cf95ddb38ce1a148ce61c46f51f599b7e8929ad479c0a948cf6e8e74b4cfec5',
  receipt: {
   transactionHash: '0x5cf95ddb38ce1a148ce61c46f51f599b7e8929ad479c0a948cf6e8e74b4cfec5',
   transactionIndex: 0,
   blockNumber: 2,
   blockHash: '0xf05268bdd02a1e5a9cf200f3fcf7c614703ceedf95403117581b7ccd5053ce12',
   from: '0xf0fe891f97376ab4233c27ae8e2f13f51fa116f2',
   to: '0x90c43cee404a473304ff3a0d5b52040516731e80',
   cumulativeGasUsed: 46806,
   gasUsed: 46806,
   contractAddress: null,
   logs: [],
   logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
   status: true,
   effectiveGasPrice: 3278849404,
   type: '0x2',
   rawLogs: []
  },
  logs: []
}
5. Check balance
> balance = await instance.balances('0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2')
BN {
  negative: 0,
  words: [ 1000, <1 empty item> ],
  length: 1,
  red: null
}
> balance.toNumber()
1000

Deploy to Waterfall Network

  1. Install packages
    npm install @truffle/hdwallet-provider
    
    If you're having trouble installing, you can fix it npm config set legacy-peer-deps true
  2. Uncomment const HDWalletProvider = require('@truffle/hdwallet-provider'); line
  3. Go to Networks - RPC Endpoints page and check params
  4. Add Waterfall Network to truffle-config.js file
    networks: {
      waterfall: {
       from: 'a7062A2Bd7270740f1d15ab70B3Dee189A87b6DE', // Your Deployer Account
       provider: () => new HDWalletProvider({
         privateKeys: ['07201008f00b31de405a0af22ff04871044e027878929b7b47569ca76555ff09'], // Private key from your account
         providerOrUrl: `wss://rpc.testnet9.waterfall.network/ws`
       }),
       network_id: 1501869,
       confirmations: 5,
       websocket: true,
       skipDryRun: true,
      }
    }
    
  5. Deploy contract
    truffle migrate --network waterfall