Creating a smart contract on the HPB Blockchain

Jeff P
10 min readFeb 2, 2021

--

I recently decided to give away some abstract art in digital form to members of the HPB Telegram community, and all they needed to do was make a simple HPB transaction on the HPB main-net and then post the transaction hash.

One lucky winner asked me how we actually “prove” the ownership of the asset had been transferred, so I decided to create a very quick, very basic smart contract in solidity, which would do the following:

  1. Store the SHA256 checksum of the digital image in a smart contract
  2. Transfer the ownership of the smart contract to the new owner
  3. share the digital artwork image, so that the new owner can also generate the SHA256 checksum for the same image, and when the resulting checksum is shown, it can be compared to the SHA256 checksum stored in the smart contract, effectively proving ownership.

So here was the digital artwork image I gave away (Artwork now officially owned by Telegram user Omega (Telegram Omega_001)

Now if you were to right-click on this image and save it to your computer as “Omega.png” (remember the capital “O”) and then you were to visit http://onlinemd5.com/ and click the “choose file” button an point to the Omega.png file you just saved, (remember to switch the conversion type to SHA256), you will see the following checksum generated:

9da12f7a3cb707e11ef8980cfbf50e39121d1cce118c644c098e866e2b16b733

This checksum is what I want to store in a smart contract that my HPB wallet has generated, so I can then I transfer ownership of the smart contract to Omega.

Ok, hope you’re still with me! :-)

So let’s create a smart contract!

The beauty of HPB is that it runs the Ethereum Virtual Machine, so we can easily compile smart contracts written in Solidity, which makes life a lot easier!

The first thing we want to do is switch across to the HPB IDE to write the smart contract: This can be found at https://remix.hpb.io

It will look something like this when you first arrive:

If you’ve never coded before, don’t worry! It might look a little frightening for a new person, but it’s actually quite straightforward.

The first column on the left of the page will show browser and config — These are both drop-down menus

The browser drop-down will show you your solidity program files — basically the smart contracts that you plan to compile for the HPB blockchain. When Remix first loads, it populates the main central browser window with a basic mock-up smart contract called ballot.sol

There’s also another hidden smart contract called ballot_test.sol which you can see if you click on the browser drop down menu in the left column.

Now we don’t need any of these example smart contracts, so we’ll delete them both by right-clicking on both smart contracts in the browser drop-down menu on the left, and selecting delete. You should then be left with an empty browser window.

Lets create a new contract. To do this, click on the teeny tiny “+” icon in the top-left corner of the page, where you will be prompted to give your new contract a name. For this example, I will call this contract.sol

(NOTE: All solidity smart contracts should end with the .sol extension)

Ok so the first thing we want to do is prepare the new smart contract.

We start with typing in the following lines of code in bold in the contract window:

pragma solidity ^0.5.7;

(all solidity smart contracts begin with “pragma solidity, and then you specify the compiler version you wish to use)

contract Transfer_Asset_Ownership { }

(we specify the smart contract with a name, which cannot include spaces, and then provide some opening and closing curly braces.

Everything the smart contract actually does will now be written as code inside these two curly braces, so we’ll make some space between them. At this stage, the smart contract window should look like this….

As you can see, we have a nice gap between the curly braces to type in our code :-)

To begin with, we want to declare three variables:

  • address public owner;
  • address public contractAddress;
  • bytes public sha256hash;

The first variable will be the smart contract “owner”, stored as a HPB wallet address.

The second variable will be the actual address of the smart contract itself, also stored as an address. Smart contract addresses look exactly the same as wallet addresses, and begin with 0x

The third variable will be the actual sha256 hash checksum of the digital art stored in bytes.

Each variable uses the “public” keyword which means that anyone can view the values stored in these variables.

(NOTE: All variables need to end with a semi-colon)

We now want to add something important known as a “constructor” function. This is a special solidity function for when smart contracts are actually written to the blockchain for the first time. They are used to assign values to variables and perform tasks.

This will be our constructor function:

constructor() public payable {

owner = msg.sender;

contractAddress = address(this);

}

In this case, the function is basically saying that when the smart contract is first written to the blockchain, it should assign the value of the message sender (msg.sender) which in this case will be me, and assign this value to the “owner” address variable. Basically, once the smart contract is made, I will be the owner. Secondly, I want to assign the address of the newly-created smart contract to the “contractAddress” variable.

You should also notice the constructor includes the “payable” keyword. Just like any transaction on the blockchain requires “gas” to make a transaction, you also need to pay to write to a smart contract, so you declare the constructor payable to write to it.

Next we want to add a function allow me to transfer ownership of the smart contract to someone else, once it exists on the blockchain. With smart contracts, you can read information from the contract, or write information to it. This will be a “write” function, as I will want to overwrite the “owner” variable, with the address of the new owner, who will be Omega.

function changeOwner(address payable _owner) public{

require(msg.sender == owner);

owner = _owner;

}

So to break this function down a bit, we’ve given the function a name (changeOwner)

We are stating that we will be eventually writing a new value when we call this function (_owner will be the new value)

For security reasons, we are stating that only the current owner has the rights to call this function to change the ownership to a new owner — require(msg.sender == owner)

And finally, we are saying that whatever new value we offer as _owner, it will overwrite the value currently stored as owner, which in this instance was msg.sender (me!)

Ok, after adding in the constructor and the first function, the smart contract should look like this:

The final function will be to write the sha256 checksum to the file. Now we could actually just write this value to the sha256hah variable in the smart contract when we first generate it as part of the constructor, but I’ll show you how we can write to the smart contract once it’s already deployed on the blockchain instead.

We add in the following function:

function setsha256hash(bytes memory _sha256hash) public

{
sha256hash = _sha256hash;
}

so once again, we’ve given the function a name (setsha256hash) and we are saying that we’re going to add in a new value (_sha256hash) and then assign this new value to the variable sha256hash

That’s it, the smart contract is complete and ready to be deployed on the HPB blockchain! :-)

you can also add comments anywhere in your code which is good to act as a reminder of what you are doing. Any comments should begin with // which tells the remix compiler to ignore the text after the // as it’s just a comment.

Your final complete smart contract code should now look something like this:

pragma solidity ^0.5.7;
contract Transfer_Asset_Ownership {

address public owner;
address public contractAddress;
bytes public sha256hash;
constructor() public payable {
owner = msg.sender;
contractAddress = address(this);
}

function changeOwner(address payable _owner) public{
require(msg.sender == owner);
owner = _owner;
}

function setsha256hash(bytes memory _sha256hash) public{
sha256hash = _sha256hash;
}


}

you should tick the “Auto Compile” tick-box on the right side if it isn’t already ticked, and if everything works correctly, you’ll see a green box on the right column, stating that the code is valid, and will compile without any errors. If there are yellow boxes here, they will be warnings, and red boxes mean that you have a code error. The blue box above the green box is there just to offer some suggestions on potentially improving your code, but can usually be ignored.

Ok so we have a valid smart contract, and no compile errors, so we’re ready to write this contract to the HPB blockchain!

You’ll need some HPB coin to issue a smart contract, and you’ll need Metamask installed. If you don’t already have Metamask, you can install it for Chrome or Edge browsers as an extension.

Once you have Metamask installed, you’ll need to setup Metamask for the HPB network. Fill in the steps below.

Once you have Metamask setup to the correct network, and you have some HPB in the wallet, you are ready to switch to the “Run” tab of Remix to actually deploy your smart contract.

Check that the Environment tab is set to Web3 and that your account (i.e. you HPB wallet address is showing), to indicate that Metamask is connected to Remix, ready for you to deploy the smart contract.

You can now click on the red “Deploy” button, and Metamask will pop-open asking you to confirm that you wish to write the smart contract to the HPB blockchain for the amount shown…

As you can see, Metamask is telling me that it’s going to cost me 0.005877 HBP to create this smart contract…..that’s barely a fraction of $0.01 :-)

(compare that to writing to Ethereum, and you’ll see why HPB is a far superior environment to deploy your smart contracts and DApps on!)

So if we confirm that, and wait a few seconds, you’ll see in the transaction was approved, and the smart contract now officially exists on the HPB blockchain :-)

Now lets actually interact with our smart contract….

On the right column, you’ll see you’re newly created smart contract….click on the little drop down arrow to open up so see the variables and functions we added to it…

Red buttons are opportunities to “write” to the smart contract, and requre a fee. Blue buttons are “read” variables that we simply read information from the smart contract. If I click on the blue buttons, you can see what is stored in them….

We have a smart contract address and we also have an owner address, both written the smart contract as it was deployed, using the special constructor function.

But the sha256 hash value is empty….we haven’t written the value to this variable yet.

To do this, we use the red buttons, which are the “write” functions.

I need to type in the value I want to store in the field, and then click the red button, so in this case I want to store the checksum that we retrieved earlier.

9da12f7a3cb707e11ef8980cfbf50e39121d1cce118c644c098e866e2b16b733

Important NOTE: we need to prepend this with 0x , just as all HPB smart contracts, addresses and transactions have, or else the write function will error.

Once we click on the button, Metamask will once again pop up, informing us that the function is payable to write to the smart contract. Once we confirm, and the transaction completes after a few seconds, we can now go and re-try the blue button to see if the sha256 hash has been stored to the smart contract.

We can easily do the same thing for transferring ownership of the smart contract, by adding in the HPB wallet address of the new owner in the field next to the red “changeOwnership” button. Again, this is a “write” function, so is payable, and once completed you can click the blue “owner” button to see the updated address.

Hope that makes sense guys. Feel free to join the HPB Global telegram group for lots more information on HPB and writing smart contracts and DApps for the HPB blockchain!

--

--

Jeff P
Jeff P

Written by Jeff P

I tend to write about anything I find interesting. There’s not much more to it than that really :-)