Alchemy: Create a Hello World Smart Contract
Learn how to create and deploy a simple hello world smart contract.
Smart contracts are pieces of computer code that are built to run on blockchain networks to facilitate, verify, or negotiate a contract agreement. Smart contracts can only be initiated under certain conditions that users agree on. Currently, smart contracts are mainly used for transactions such as NFT minting, but they can be used for any piece of code, such as a simple tutorial program known as ‘Hello World’ that prints a return string to the screen.
Read below to learn how to create and deploy a simple hello world smart contract.
This tool is available on a Linux or macOS system.
Set up a credentials file for S3FS at
${HOME}/.passwd-s3fs
. You will need to save your Filebase Access and Secret keys to this file and give it owner permissions. You can do so with the following commands:echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > ${HOME}/.passwd-s3fs
chmod 600 ${HOME}/.passwd-s3fs
ACCESS_KEY_ID is your Filebase Access key, and SECRET_ACCESS_KEY is your Filebase Secret key. For more information on Filebase access keys, see here.
You can mount a Filebase IPFS bucket with the command:
s3fs mybucket /path/to/mountpoint -o passwd_file=${HOME}/.passwd-s3fs -o url=https://s3.filebase.com
- mybucket: name of your Filebase bucket
- /path/to/mountpoint
cd /path/to/mounted/bucket
mkdir helloWorld
cd helloWorld
Select the ‘Create App’ button to get started.

For our example, we called our app ‘hello world’. Set the chain to ‘Ethereum’, and the network to ‘Göerli’.




Start by initiating our project. You should still be in the directory
/path/to/mounted/bucket/helloWorld
.npm init
Here’s how we answered the installation questions:

npm install –save-dev hardhat
npx hardhat
At the prompt, select the ‘Create an empty hardhat.config.js’ option.

mkdir contracts
mkdir scripts
The
contracts
folder will be used to store our smart contract, and the scripts
folder will be used to store scripts used to deploy and interact with the smart contract.Create a new file in the contracts directory and open the file in your preferred IDE such as VsCode. Name this file
HelloWorld.sol
. Enter the following content into the file:// 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.7.3;
// 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 {
//Emitted when update function is called
//Smart contract events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen.
event UpdatedMessages(string oldStr, string newStr);
// 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) {
// 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 {
string memory oldMsg = message;
message = newMessage;
emit UpdatedMessages(oldMsg, newMessage);
}
}
npm install dotenv –save
API_URL = "ALCHEMY_HTTP_KEY"
PRIVATE_KEY = "METAMASK_PRIVATE_KEY"
Replace ALCHEMY_HTTP_KEY with your Alchemy API key, and replace METAMASK_PRIVATE_KEY with your Metamask private key. For instructions on how to get your Metamask private key, check out this guide.
npm install --save-dev @nomiclabs/hardhat-ethers "[email protected]^5.0.0"
Enter the following content in this file:
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.7.3",
defaultNetwork: "ropsten",
networks: {
hardhat: {},
ropsten: {
url: API_URL,
accounts: [`0x${PRIVATE_KEY}`]
}
},
}
Save this file.
npx hardhat compile
To do this, we need to write a script called
deploy.js
. Create a new file called deploy.js
in your scripts
directory, then enter the following content in the new file:async function main() {
const HelloWorld = await ethers.getContractFactory("HelloWorld");
// Start deployment, returning a promise that resolves to a contract object
const hello_world = await HelloWorld.deploy("Hello World!");
console.log("Contract deployed to address:", hello_world.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Save this file.
npx hardhat run scripts/deploy.js --network ropsten
This command will return an output similar to:
Contract deployed to address: 0x9137566f9D8C3722066c3eD2372843b07A6688ae
Save this address value.
Enter the contract address from the previous step in the search bar. The contract address will bring up something like this:

You can click on the transaction details to view more information, such as the transaction’s To and From addresses, the transaction fee, and the gas fee:


If you have any questions, please join our Discord server, or send us an email at [email protected]