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.
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)
// 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>
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]
truffle migrate
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
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.