...
/Compile and Deploy Solidity Contracts
Compile and Deploy Solidity Contracts
We'll cover the following...
Truffle makes compiling & deploying smart contracts markedly easier, while still giving you visibility and control.
Set up Truffle
Make an empty repository, cd into it, then:
Install HDWalletProvider
Create your contract
In ./contracts create a new contract called HelloWorld.sol with the following code:
Compile your contract
Compile “HelloWorld” with the following command.
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:
Modify truffle.js file (or truffle-config.js), like below:
Run the following command:
Note: We have already setuped everything for you. Press the
RUNbutton 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:
Finally, invoke the contract function and say hello. To do so, run the following command in the truffle console: