This tutorial aims to guide you through the process of deploying and interacting with smart contracts on the Ethereum network.
By the end of this tutorial, you should be able to:
- Understand what smart contracts are
- Learn how to write and deploy smart contracts
- Interact with deployed smart contracts
Before starting with this tutorial, you should have:
- Basic understanding of blockchain technology
- Basic knowledge of Solidity, the programming language for Ethereum smart contracts
- Installed Node.js and npm
- Installed the Truffle Suite, which includes Truffle, Ganache, and Drizzle
A smart contract is a self-executing contract with the terms of the agreement directly written into code.
We will start by writing a simple smart contract in Solidity. Save the following code as SimpleStorage.sol
:
pragma solidity ^0.5.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
This contract has a single variable storedData
and two functions to set and get its value.
We will use Truffle to compile and deploy our contract. First, let's compile it with truffle compile
.
Next, we need to create a migration script for deployment. In the migrations
directory, create a new file 2_deploy_contracts.js
:
var SimpleStorage = artifacts.require("./SimpleStorage.sol");
module.exports = function(deployer) {
deployer.deploy(SimpleStorage);
};
Now we can deploy our contract to Ganache (a local Ethereum blockchain) with truffle migrate
.
With our contract deployed, we can interact with it using Truffle Console. Start the console with truffle console
and get an instance of our deployed contract:
let instance = await SimpleStorage.deployed()
Now you can call the contract's methods:
await instance.set(100)
await instance.get()
The get()
function should return 100
.
First, write the contract in Solidity:
pragma solidity ^0.5.0;
contract HelloWorld {
string public message;
constructor(string memory initMessage) public {
message = initMessage;
}
function update(string memory newMessage) public {
message = newMessage;
}
}
This contract stores a message and allows it to be updated. Save it as HelloWorld.sol
.
Next, create a migration script:
var HelloWorld = artifacts.require("./HelloWorld.sol");
module.exports = function(deployer) {
deployer.deploy(HelloWorld, "Hello, World!");
};
This script will deploy the HelloWorld contract and initialize the message to "Hello, World!".
Get an instance of the deployed HelloWorld contract:
let instance = await HelloWorld.deployed()
Now you can call the contract's methods:
console.log(await instance.message())
await instance.update("Hello, Ethereum!")
console.log(await instance.message())
The first console.log()
should print "Hello, World!", and the second should print "Hello, Ethereum!".
In this tutorial, you've learned what smart contracts are, how to write, deploy, and interact with them on the Ethereum network.
Tip: This is similar to the SimpleStorage contract, but with a different variable type.
Tip: Use a Solidity array to store the items.
Tip: Use a Solidity mapping to store the messages.
To continue learning about smart contracts and Ethereum development, consider exploring more complex contract interactions, integrating with front-end applications, and deploying to a testnet or mainnet.