Truffle makes compiling & deploying smart contracts markedly easier, while still giving you visibility and control.

Set up Truffle

Press + to interact
npm install -g truffle

Make an empty repository, cd into it, then:

Press + to interact
truffle init

Install HDWalletProvider

Press + to interact
npm install --save truffle-hdwallet-provider

Create your contract

In ./contracts create a new contract called HelloWorld.sol with the following code:

Press + to interact
pragma solidity 0.8.13;
contract HelloWorld {
function sayHelloWorld() public pure returns (string memory) {
return "Hello World";
}
}

Compile your contract

Compile “HelloWorld” with the following command.

Press + to interact
truffle compile

This compiles the original code into Ethereum bytecode. If everything goes well, it will create .json file under build/contracts folder.

Deploy your contract

In ./migrations, create a deployment script specifically named 2_deploy_contracts.js with the following code:

Press + to interact
var HelloWorld = artifacts.require("./HelloWorld.sol");
module.exports = function (deployer) {
deployer.deploy(HelloWorld);
}

Modify truffle.js file (or truffle-config.js), like below:

Press + to interact
module.exports = {
/**
* Networks define how you connect to your ethereum client and let you set the
* defaults web3 uses to send transactions. If you don't specify one truffle
* will spin up a development blockchain for you on port 9545 when you
* run `develop` or `test`. You can ask a truffle command to use a specific
* network from the command line, e.g
*
* $ truffle test --network <network-name>
*/
networks: {
// Useful for testing. The `development` name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
// You should run a client (like ganache, geth, or parity) in a separate terminal
// tab if you use this network and you must also set the `host`, `port` and `network_id`
// options below to some value.
//
development: {
host: "0.0.0.0", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
},
// Set default mocha options here, use special reporters, etc.
mocha: {
// timeout: 100000
},
// Configure your compilers
compilers: {
solc: {
version: "0.8.13", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
}
},
// Truffle DB is currently disabled by default; to enable it, change enabled:
// false to enabled: true. The default storage location can also be
// overridden by specifying the adapter settings, as shown in the commented code below.
//
// NOTE: It is not possible to migrate your contracts to truffle DB and you should
// make a backup of your artifacts to a safe location before enabling this feature.
//
// After you backed up your artifacts you can utilize db by running migrate as follows:
// $ truffle migrate --reset --compile-all
//
// db: {
// enabled: false,
// host: "127.0.0.1",
// adapter: {
// name: "sqlite",
// settings: {
// directory: ".db"
// }
// }
// }
};

Run the following command:

Press + to interact
truffle migrate

Note: We have already setuped everything for you. Press the RUN button below to compile and deploy the smart contract.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Migrations {
  address public owner = msg.sender;
  uint public last_completed_migration;

  modifier restricted() {
    require(
      msg.sender == owner,
      "This function is restricted to the contract's owner"
    );
    _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }
}

Congrats! The “HelloWorld” smart contract has been successfully deployed to Ganache.

Let’s see the deployed contract.

Access your deployed contract

Set up your Truffle console to development network. To do so, open a new terminal using Ctrl+b % and execute the following command:

Press + to interact
truffle console --network development

Finally, invoke the contract function and say hello. To do so, run the following command in the truffle console:

Press + to interact
HelloWorld.deployed().then(function(instance){return instance });