Oh, Go on, Write Your Own Smart Contract

Too many people just talk about blockchain, and actually know very little about what actually happens on the ledger, and in the…

Photo by Shubham Dhage on Unsplash

Oh, Go on, Write Your Own Smart Contract

Too many people just talk about blockchain, and actually know very little about what actually happens on the ledger, and in the opportunities that it brings. Imagine a world, where I could just open up an application, and have access to every mathematical function ever created, and to use it in a trustworthy way, and without requiring centralised servers, their operating systems, a cloud environment, sandboxes, and all the other things that come with software? Well, smart contracts can give us that functionality. So, it’s important to understand how they work.

Ethereum

Ethereum opened up the world to writing dependable and trusted smart contracts. These contracts could be viewed by anyone, and a proven to operate in a dependable manner. Within them, we can keep a state, such as in an election where would remember each voter, and whether they have been contacted, then when they have registered, and then whether they have voted. Our smart contracts can thus have a life and can sustain things without requiring servers and services. To do this, we need to pay the miners some gas to mine (or process) the contract.

Creating a simple stateless smart contract

But we don’t always have to do this, and we can also write a smart contract which is just a call to the smart contract, and where a state does not have to be recorded. This will be free of cost. So, let’s write a bit of code that does some simple maths. In the following we will implement sqrt(), sqr(), mul(), sub(), and add():

pragma solidity ^0.8.0;
contract mymath {
function sqrt(uint x) public view returns (uint y) {
uint z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
function sqr(uint a) public view returns (uint) {
uint c = a * a;
return c;
}
function mul(uint a, uint b) public view returns (uint) {
uint c = a * b;
return c;
}
function sub(uint a, uint b) public view returns (uint) {
return a - b;
}
function add(uint a, uint b) public view returns (uint) {
uint c = a + b;
return c;
}
}

In this case, the “public ” part makes sure we can see the output of the function, and the “view” part allows it to be stateless (and where we just have to receiver the value without the smart contact remember the state). On Ethereum we normally use the Solidity language to create a smart contract and then compile it into the byte code required for the ledger. First, can we start by entering the Solidity code into Remix [here]:

Once entered, we can then compile it with the Solidity compiler. It is important to take a note of the compiler version at this stage, as we will need this later:

Once compiled we can then deploy the smart contract to a test network (Ropsten). For this , we need to connect our Metamask wallet:

Once it has been deployed, we can see our wallet identifies the deployed contract:

Onto the ledger

And clicking through gives us the address of the contract, and then viewing it on the explorer, we can see the transaction:

The address here is “0x0895..”, so we can view the smart contract from:

https://ropsten.etherscan.io/address/0x0895a540cff8e7829284f1d9c55daf624d6e2df9

We now need to verify and publish the contact, with click on “Verify and Publish”:

After this, we can define the Compiler Version and the licence:

We then need to add your code for it to be checked:

It takes around 30 seconds, but, eventually, we should have our code accepted:

We now have the contract published to the Ropsten test network:

Next, by selected the Contract tab, and can view the read parameters. The exposed functions are add(), mul(), sqr(), sqrt() and sub():

To test, we can just enter the variables for a given function, and get a result:

Conclusions

There you go. It’s as simple as that! The only cost here was the deployment of the smart contract to the ledger, and where access to it is then free. Only if we need the state recorded is there a cost for the mining process.