Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Truffle Tutorial
  • Conclusion
  1. TIPS

Tips: Truffle Tutorial

PreviousTips: Solidity by "Immunable"NextTips: Microblog Dapp

Last updated 2 years ago

Truffle Tutorial

This tutorial will walk you through the basics of developing a smart contract in Solidity using Truffle. The goal of this tutorial is to help you understand the basics of Solidity and Truffle, and how they can work together to create a powerful, decentralized application.

Prerequisites

Before we start, you will need to install the following:

Getting Started

To get started, create a new folder for your project and navigate to it in your terminal. Then, use truffle init to create the project structure:

mkdir my-projectcd my-projecttruffle init This will create the folder structure for your project, which should look like this:

my-project├── contracts├── migrations├── test└── truffle-config.js

Creating a Smart Contract

Now that we have our project set up, let's create a simple smart contract. In your project directory, create a new file called MyContract.sol in the contracts directory.

Open the file in your preferred text editor, and paste the following code:

pragma solidity ^0.8; 
contract MyContract {    
    uint256 public myNumber; 
    function setNumber(uint256 _number) public {        
        myNumber = _number;    
    }
}

This is a simple contract that stores a number in the myNumber variable, and allows anyone to change the number using the setNumber function.

Compiling the Contract

Now that we have our contract written, we need to compile it. We can do this using the Truffle compile command:

truffle compile This will compile our contract and generate a MyContract.json file in the build/contracts directory, which contains the ABI (Application Binary Interface) of our contract.

Deploying the Contract

Now that we have our contract compiled, we can deploy it. To do this, we will use the Truffle migrate command:

truffle migrate This will deploy our contract to the blockchain, and generate a new address for it. We can use this address to interact with our contract.

Interacting with the Contract

Now that our contract is deployed, we can start interacting with it. We can do this using the Truffle console command:

truffle console This will open a console where we can interact with our contract. First, we need to get an instance of our contract:

let instance = await MyContract.deployed() Now that we have an instance of our contract, we can call our setNumber function:

await instance.setNumber(42) Finally, we can check the value of myNumber:

let myNumber = await instance.myNumber()console.log(myNumber.toNumber())// Output: 42\

Conclusion

In this tutorial, we have gone through the basics of developing a smart contract in Solidity using Truffle. We have seen how to create a contract, compile it, deploy it, and interact with it.

Now that you have the basics down, you can start building more complex applications using Solidity and Truffle. Good luck!

Node.js
Truffle