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
- Create a new directory for your Truffle project:
mkdir HelloWaterfall cd HelloWaterfall
- Create Truffle project
truffle init
- Create
HelloWaterfall.sol
file incontracts
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
- Compile contracts
truffle compile
- Create
1-deploy.js
file inmigrations
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
migrate
let instance = await Coin.deployed();
> instance.minter()
'0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2'
> let balance = await instance.balances('0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2')
undefined
> balance.toNumber()
0
> 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: []
}
> balance = await instance.balances('0xF0fE891F97376AB4233C27ae8E2F13f51Fa116F2')
BN {
negative: 0,
words: [ 1000, <1 empty item> ],
length: 1,
red: null
}
> balance.toNumber()
1000
Deploy to Waterfall Network
- Install packages
If you're having trouble installing, you can fix it
npm install @truffle/hdwallet-provider
npm config set legacy-peer-deps true
- Uncomment
const HDWalletProvider = require('@truffle/hdwallet-provider');
line - Go to Networks - RPC Endpoints page and check params
- Add Waterfall Network to
truffle-config.js
filenetworks: { 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, } }
- Deploy contract
truffle migrate --network waterfall