After following we the previous steps you should now be setup to compile and deploy smart contracts. This section will detail briefly the process for compiling and deploying smart contracts with both Truffle and Remix IDE.

Truffle Smart Contract Deployment

Below is an example smart contract for creating an Ethereum faucet (Example code is from the book Mastering Ethereum and pulled from here: https://github.com/ethereumbook/ethereumbook/blob/develop/code/Solidity/Faucet.sol)

  1. Copy the following code into a file called Faucet.sol in the "contracts" directory within your Truffle project directory
// SPDX-License-Identifier: CC-BY-SA-4.0

// Version of Solidity compiler this program was written for
pragma solidity 0.6.4;

// Our first contract is a faucet!
contract Faucet {
    // Accept any incoming amount
    receive() external payable {}

    // Give out ether to anyone who asks
    function withdraw(uint withdraw_amount) public {
        // Limit withdrawal amount
        require(withdraw_amount <= 100000000000000000);

        // Send the amount to the address that requested it
        msg.sender.transfer(withdraw_amount);
    }
}

<aside> 💡 Note: The Solidity version in this contract is 0.6.4 so that will need match your truffle-config.js file solc version

</aside>

  1. Compile the contract:
truffle compile

<aside> 💡 If the Solidity file imports libraries such as OpenZeppelin from NPM modules you may need to install the older version of that particular module for it to work. You can list out NPM modules, and install an older version with the commands below.

</aside>

npm view @openzeppelin/contracts versions --json
npm install @openzeppelin/[email protected]
  1. After successfully compiling the contract we will use the "Migrate" functionality to deploy the smart contract to our local Ganache blockchain
truffle migrate

Untitled

Now that the contract has been deployed, one way to interact with it would be to leverage the Truffle console. More info on that can be found here:

Truffle | Using Truffle Develop and the Console | Documentation | Truffle Suite

Remix IDE Smart Contract Deployment

You can also deploy a smart contract directly from the Remix IDE. One of the nice features about Remix is there is functionality built-in to directly interact with functions of a deployed contract. Let's explore this now.

  1. Navigate to the Remix IDE

Remix - Ethereum IDE

  1. Click the "File Explorer" tab and click the "+" button to create a new workspace, name it Faucet