Most people have probably heard about blockchains and even fewer people probably understand how they actually work. But even so, what makes blockchain technology itself so appealing is not about what you can do with the blockchain itself, rather what you can do with Smart Contracts. Smart Contracts, or “distributed apps” or “DApps” as they are sometimes called extend blockchains by allowing developers to apply programming logic to blockchains. This opens up countless possibilities how one can use Blockchains.
In this tutorial, we’ll deploy a simple smart contract and interact with it. It doesn’t do much, but we have to start somewhere!
Ethereum was one of the first implementation of Smart Contracts. While Ethereum does not prescribe a particular language for writing smart contracts, the most popular language by far is Solidity.
Smart contracts have the ability to be called by way of RPC or remote procedure calls. A contract will typically have a set of functions that are setup in such a way that they can be invoked from outside the contract, even remotely. This, in effect, creates an API that can be used to interact with the contract.
In addition to exposing some functions, smart contracts also maintain a state. A contracts state is all the data that is managed by the contract that is stored on a blockchain. The state of a contract is modified by the functions that are called and the new state is recorded on the blockchain until the next time a function on the contract is called. Unlike a database though, the contract state does not overwrite the old state, rather it creates new blocks on the blockchain. This means that every change to the contract is visible on the blockchain.
Consider the following code:
pragma solidity ^0.4.21;
contract HelloWorld {
uint myStorageData;
function HelloWorld(uint v) public {
myStorageData = v;
}
function setStorageData(uint newValue) public {
myStorageData = newValue;
}
function getStorageData() public constant returns (uint) {
return myStorageData;
}
}
The variable myStorageData
is a state variable for this smart contract. It can be modified by calling setStorageData
by way of a remote procedure call. getStorageData
is a “getter” function that retrieves a value from the contract, and HelloWorld
is the constructor for the contract.
There are several tools available for developing smart contracts on Ethereum. Two of the most accessible tools are Ganache and Truffle. Ganache is a GUI based tool that sets up a personal blockchain that can be used for development. Download and run Ganache to get started.
Truffle is a framework and set of utilities that automate building, testing, and deploying smart contracts to Ethereum networks. Truffle runs at the command line because its intent is more than just development. It’s intended to be scriptable so it can be used for automation processes for DevOps.
To get Truffle:
windows-build-tools
package for npm.
Windows:
npm install -g windows-build-tools
`
Mac, Linux, and Windows:
npm install -g truffle
mkdir hello-world
cd hello-world
truffle init
truffle create contract HelloWorld
HelloWorld.sol
in the contracts
folder in the hello-world
. Open this file in your favorite text editor and paste the code for the HelloWorld contract above over the default code in the contract and save it. truffle create migration helloworld
migrations
folder, which is used to deploy contracts to an Ethereum network. The file will be look something like 1524597135_helloworld.js
. Open this file and paste the following code over the default code, then save it.
var HelloWorld = artifacts.require("HelloWorld");
module.exports = function(deployer) {
deployer.deploy(HelloWorld, 5);
};
truffle.js
in the root of the hello-world
folder. This file contains configuration information for Truffle. Paste in the following code over the defaults and save it. What this is doing is configuring Truffle to talk to Ganache.
module.exports = {
networks: {
ganache: {
host: "127.0.0.1",
port: 7545,
network_id: "*" // Match any network id
}
}
};
--network
flag tells Truffle what network to use from the truffle.js
file.
truffle console --network ganache
--reset
flag forces Truffle to recompile and redeploy the contract regardless of what state it might be in.
deploy --reset
HelloWorld: 0xe4e47451aad6c89a6d9e4ad104a7b77ffe1d3b36
<YOUR ADDRESS HERE>
with your address.
HelloWorld.at("<YOUR ADDRESS HERE>").getStorageData()
This will retrieve the data stored in the state variable.
HelloWorld.at("<YOUR ADDRESS HERE>").setStorageData()
This will change the value. Retrieve the value again to see that it was change.
Ctrl + C
twice.That’s it! You setup a blockchain and deployed a contract to it. In our next post we’ll explorer how to interact with a contract using JavaScript. Stay tuned for part 3!
Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…
Cloud management is difficult to do manually, especially if you work with multiple cloud…
Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…
https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…
FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…
Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…