🌎 Hello World Smart Contract

Step-by-step walkthrough for creating, deploying, and interacting with a simple Hello World smart contract using Alchemy.

Estimated time to complete this guide: ~30 minutes

If you are new to blockchain development and don't know where to start, or if you just want to understand how to deploy and interact with smart contracts using Alchemy, Truffle, and web3, this guide is for you. We will walk through creating and deploying a simple smart contract on the ropsten test network using a Metamask account, Solidity, Truffle, and Alchemy (don't worry if you don't understand what any of this means yet, we will explain it!). We'll also go through how we can interact with our smart contract once it's deployed!

Step 1: Create your Alchemy account

You will need an Alchemy account in order to deploy and make requests to your smart contract. If you don't already have one, you can sign up for free here: https://dashboard.alchemyapi.io/signup/.

Step 2: Create your app

Once you've created an Alchemy account, you can generate an API key by creating an app. This will allow us to make requests to the ropsten test network, to learn more about testnets check out this guide.

  1. Navigate to the "Create App" page in your Alchemy Dashboard by hovering over "Apps" in the nav bar and clicking "Create App"

  2. Name your app "Hello World", offer a short description, select "Staging" for the Environment, and choose "Ropsten" for your network

  3. Click "Create app" and thats it! Your app should appear in the table below

Step 3: Create a Metamask Account

Metamask is used to manage your Ethereum account address. You can download and create a Metamask account here. When you are creating an account, or if you already have an account, make sure to switch over to the "Ropsten Test Network" in the upper right.

Step 4: Add ether from a Faucet

In order to deploy our smart contract to the test network, we'll need some fake ether. To get ether we can go to the Ropsten faucet and enter your Ropsten account address, then click "Send Ropsten Eth". You should see Eth in your Metamask account soon afterπŸ€‘

Step 5: Check your Balance

To double check our balance is there, we can use our Alchemy API key and account address to make a call to eth_getBalance. You can do this through the command line or by using Alchemy's composer feature.

curl https://eth-ropsten.alchemyapi.io/v2/your-api-key \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_getBalance","params":["your-account-address", "latest"],"id":0}'

The result you get back should be:

{"jsonrpc": "2.0", "id": 0, "result": "0x2B5E3AF16B1880000"}

NOTE: One thing to note is that the result is in wei not eth. Wei is used as the smallest denomination of ether. The conversion from wei to eth is: 1 eth = 10^18 wei. So if we convert 0x2B5E3AF16B1880000 to decimal we get 5*10^18 which equals 5 eth.

Phew! Our fake money is all thereπŸ€‘ .

Step 6: Download Truffle

​Truffle is a development environment, testing network, and asset pipeline for Ethereum that we will use to build, compile, and deploy our smart contract. To download Truffle, you can install NodeJS and paste the following command in your terminal:

npm install -g truffle

Step 7: Create a Truffle Project

Next we have to create a Truffle project to store our files.

  1. Create a new directory for your Truffle project:

mkdir Hello-World
cd Hello-World
  1. Get boilerplate files for creating and deploying smart contracts by "unboxing":

truffle unbox

Once this operation is completed, you'll now have a project structure with the following items:

Step 8: Install HDWalletProvider

​Truffle HDWallet provider is an easy way to configure network connection to ethereum through a provider like Alchemy. You can install it using the following command:

npm install @truffle/hdwallet-provider

Step 9: Write our Contract

Open up the Hello-World project in your favorite editor. Smart contracts are written in a language called Solidity which is what we will use to write our HelloWorld.sol smart contract.

  1. Navigate to the "contracts" folder and create a new file called HelloWorld.sol

  2. ​Ethereum Studio has a great sample Hello World smart contract which we will be using for this tutorial. Copy and paste in the contents below into your HelloWorld.sol file, and be sure to read the comments to understand what this contract does:

// Specifies the version of Solidity, using semantic versioning.
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/layout-of-source-files.html#pragma
pragma solidity ^0.5.10;
​
// Defines a contract named `HelloWorld`.
// A contract is a collection of functions and data (its state).
// Once deployed, a contract resides at a specific address on the Ethereum blockchain.
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html
contract HelloWorld {
​
// Declares a state variable `message` of type `string`.
// State variables are variables whose values are permanently stored in contract storage.
// The keyword `public` makes variables accessible from outside a contract
// and creates a function that other contracts or clients can call to access the value.
string public message;
​
// Similar to many class-based object-oriented languages, a constructor is
// a special function that is only executed upon contract creation.
// Constructors are used to initialize the contract's data.
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#constructors
constructor(string memory initMessage) public {
// Accepts a string argument `initMessage` and sets the value
// into the contract's `message` storage variable).
message = initMessage;
}
​
// A public function that accepts a string argument
// and updates the `message` storage variable.
function update(string memory newMessage) public {
message = newMessage;
}
}

This smart contract is very simple, it stores a message that is passed in when the contract is initialized and can be updated by calling the update function.

Step 10: Configure our Project

The next step is to edit your truffle-config.js file to use HDWalletProvider and provide all the necessary configuration for deploying to ropsten.

1. First, define the HDWalletProvider object in your configuration file. Add this line at the top of your truffle-config.js file:

const HDWalletProvider = require("@truffle/hdwallet-provider");

2. Next, provide a reference to your mnemonic or seed phrase from Metamask. to get your seed reference from Metamask follow these instructions. Once you have your mnemonic, we recommend storing it safely in an environment file (we will also add our API key here).

First, install the dotenv package.

npm install dotenv --save

Next add the following line to your truffle-config.js file.

require('dotenv').config()

Then create a .env file at the root directory of your application and add your mnmeonic and Alchemy API URL to it:

API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"
MNEMONIC = "your-metamask-seed-reference"

Finally add the new environment variables to our truffle-config.js file:

const { API_URL, MNEMONIC } = process.env;

3. Add the ropsten network to the module.exports in truffle-config.js:

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*", // Match any network id
gas: 5000000
},
ropsten: {
provider: function() {
return new HDWalletProvider(MNEMONIC, API_URL)
},
network_id: 3
}
​
},
compilers: {
solc: {
settings: {
optimizer: {
enabled: true, // Default: false
runs: 200 // Default: 200
},
}
}
}
};

Step 11: Compile our Smart Contract

To compile a Truffle project, navigate to the root of the directory where the project is located and then type the following command:

truffle compile

You should now see a build/contracts/ directory in your project. These artifacts are integral to the inner workings of Truffle, and are important for deploying your smart contract. This includes your contract's ABI, which is something you will encounter down the line. You should not edit these files.

Step 12: Add HelloWorld to 2_deploy_contracts.js file

For purposes of speed, we will also comment out the deployment of the other smart contracts. Since our smart contract has a constructor that takes in a parameter, we have to include the parameter in our deploy().

//const ConvertLib = artifacts.require("ConvertLib");
//const MetaCoin = artifacts.require("MetaCoin");
​
const HelloWorld = artifacts.require("HelloWorld");
const initMessage = "Hello world!"
​
module.exports = function(deployer) {
//deployer.deploy(ConvertLib);
//deployer.link(ConvertLib, MetaCoin);
//deployer.deploy(MetaCoin);
deployer.deploy(HelloWorld, initMessage);
};

Step 13: Deploy our Smart Contract

In order to deploy our smart contract to the Ethereum network, we will use truffle's migrations which are JavaScript files that help you deploy contracts to the Ethereum network.

To run your migrations, run the following:

$ truffle migrate --network ropsten

You should then see a response that looks similar to the following:

Starting migrations...
======================
> Network name: 'ropsten'
> Network id: 3
> Block gas limit: 8000000 (0x7a1200)
​
1_initial_migration.js
======================
​
Deploying 'Migrations'
----------------------
> transaction hash: 0x61a6c81aaf5be5329c8572ac8de8f9d27064d75f5184f2389f66212b91c9736e
> Blocks: 1 Seconds: 12
> contract address: 0x341662A4BD97bf8542bB0d815F99aff47dB2Fc42
> block number: 8903909
> block timestamp: 1603052580
> account: 0x610Ae88399fc1687FA7530Aac28eC2539c7d6d63
> balance: 4.98766424
> gas used: 168286 (0x2915e)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00336572 ETH
​
​
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00336572 ETH
​
2_deploy_contracts.js
=====================
​
Deploying 'HelloWorld'
----------------------
> transaction hash: 0x1312f26f70bd444a25790215c56aa4d87a56bc40d141f216df0661ddc3df42bb
> Blocks: 2 Seconds: 32
> contract address: 0x70c86b8d660eBd0adef24E9ACcb389BFb6611B2b
> block number: 8903912
> block timestamp: 1603052602
> account: 0x610Ae88399fc1687FA7530Aac28eC2539c7d6d63
> balance: 4.98206016
> gas used: 237925 (0x3a165)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0047585 ETH
​
​
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0047585 ETH
​
Summary
=======
> Total deployments: 2
> Final cost: 0.00812422 ETH

Once this is finished without errors you will have deployed the contract, check it out on https://ropsten.etherscan.io/ by searching for your transaction hash or contract address!! πŸŽ‰

If you head over to your Alchemy dashboard and click on the app details for your Hello World app you can see that there are requests flowing through. These requests are made in order to fulfill the deployment of your smart contract.

Interacting with your Smart Contract

Now that we've successfully deployed a smart contract to the Ropsten network, let's test out our web3 skills and interact with it!

Step 1: Install web3

We need install a web3 package in order to interact with the Ethereum blockchain. We'll be using Alchemy web3 in this example, however, there are a handful of other web3 providers you can choose from.

In your project directory run:

npm install @alch/alchemy-web3

Step 2: Create a contract-interact.js file

In your project directory create a contract-interact.js file and add the following lines of code:

require('dotenv').config();
const API_URL = process.env.API_URL;
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(API_URL);

Step 3: Grab your contract ABI

Our contract ABI (Application Binary Interface) is the interface to interact with our smart contract. Learn more about Contract ABIs here. Truffle automatically generates an ABI for us and saves it in the HelloWorld.json file. In order to use this we'll need to parse out the contents by adding the following lines of code to our contract-interact.js file:

const contract = require("./build/contracts/HelloWorld.json");

If you want to see the ABI you can print it to your console:

console.log(JSON.stringify(contract.abi));

To run contract-interact.js, navigate to your terminal and run

node contract-interact.js

Step 4: Create an instance of your contract

In order to interact with our contract we need to create an instance of it in our code. To do so we'll need our contract address which we can get from the deployment or Etherscan by looking up the address you used to deploy the contract. In the above example our contract address is 0x70c86b8d660eBd0adef24E9ACcb389BFb6611B2b .

Next we will use the web3 contract method to create our contract using the ABI and address:

const contractAddress = "0x70c86b8d660eBd0adef24E9ACcb389BFb6611B2b";
const helloWorldContract = new web3.eth.Contract(contract.abi, contractAddress);

Step 5: Read the init message

Remember when we deployed our contract with theinitMessage = "Hello world!"? We are now going to read that message stored in our smart contract and print it to the console.

In JavaScript we use asynchronous functions while interacting with networks. Check out this article to learn more about this.

Use the below code to call the message function in our smart contract and read the init message:

async function main() {
const message = await helloWorldContract.methods.message.call();
console.log("The message is: " + message);
}
main();

After running the file using node contract-interact.js in the terminal we should see this response:

The message is: Hello world!

Congrats! You've just successfully read smart contract data from the Ethereum blockchain, way to go!

Step 6: Update the message

Now instead of just reading the message, we'll update the messaged saved in our smart contract using the update function. β€Œ

In order to do so we'll need to call another async function with the new message we want:

async function main() {
const message = await helloWorldContract.methods.message.call();
console.log("The message is: " + message);
const newMessage = "Hello Drupe!";
await helloWorldContract.methods.update(newMessage).call();
console.log("The new message is: " + newMessage);
}
main();

You should see a response that looks like:

The message is: Hello world!
The new message is: Hello Drupe!

Woooo congrats! You've now deployed and interacted with an ethereum smart contract πŸŽ‰ !