Hashgraph Tutorial — Deploying Smart Contracts

Hashing Systems
Hashing Systems
Published in
6 min readJun 26, 2019

While cryptocurrency and file services provide essential functions to Hedera users, smart contracts are the prized cherry on top. With smart contracts, intermediaries can be replaced by trustless, automated programs that are tamper-proof and can charge far less.

Source from https://www.hedera.com/smart-contracts

First made possible by Ethereum, smart contracts are now capable of far higher throughput when combined with Hashgraph’s new consensus model. You can read more about how Hedera smart contracts work in one of our previous articles. For this tutorial, we’re getting straight into the code.

Registration & Account Setup

To deploy smart contracts on Hedera, you must first create an account on the Hedera Portal for access to the mainnet. Once you’ve created your portal account, follow these steps to complete your account creation on the Hedera mainnet. If you’d prefer to use the testnet instead, be sure to specify in the Hedera Portal and use the correct network setting when we’re setting up our environment.

Environment Setup

Next, let’s set up the Hedera SDK. You have the choice of using Eclipse or IntelliJ, the latter of which we’ll use for this tutorial. The easiest way to get started is to download the Hedera Java SDK, which has some smart contract examples already set up for us. You can quickly clone this repository by going to File > New > Project from Version Control > Git and then pasting the Github URL. If you’d like to save your following changes, you can fork the SDK repo and commit your work there.

Once IntelliJ has finished cloning the repo, you’ll need to configure your network settings by selecting the .env.sample file and filling out the required information which you’ll find on your portal account. You’ll also need to provide your account’s private key which you can generate yourself by following these instructions. Once complete, rename the file from .env.sample -> .env.

Next, you’ll need to navigate to the Maven window via View > Tool Windows > Maven and double click on compile located in sdk > Lifecycle. Once that’s complete, double click on the examples folder in the Project window, right click on pom.xml inside and select Add as Maven Project from the menu.

Finally, go to examples > src > main > java > com.hedera.hashgraph.sdk.examples > simple to double click on CreateAccount and run CreateAccount.main(). If you see the example output below, then you’re ready to start deploying smart contracts! Otherwise, if something has gone wrong, refer to the setup documentation to see if you’ve missed a step.

private key = 302e020100300506032b6570042204205cc0824ddd5a38e9ffa9d942dcbdf801b146ad6b1599969116c0f6f1d4e4ea86public key = 302a300506032b657003210001b7239580fed95d34a7d377b7ae5e9b8fa99435642c0a7afc8e0ab451896f1daccount = 0.0.1827

Deploying a Smart Contract

Now that your environment is set up let’s take a look at your first smart contract. Navigate to examples/src/main/resources/hello_world.sol and double click to open the file. This smart contract is as simple as it gets. It provides a greet() function which returns “Hello, world!” along with a kill() function that allows the owner to destroy it.

contract HelloWorld {
// the contract's owner, set in the constructor
address owner;
constructor() public {
// set the owner of the contract for `kill()`
owner = msg.sender;
}
// return a string
function greet() public pure returns (string memory) {
return "Hello, world!";
}
// recover the funds of the contract
function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); }
}

Did you notice the .sol file extension? That tells us this is a Solidity file. Solidity is the most common smart contract language in use today and so was adopted by Hedera for their platform. You can read more about Solidity and its syntax in the docs.

To deploy this contract, navigate to examples/src/main/java/com/ hedera/hashgraph/sdk/examples/advanced and open CreateSimpleContract.java. This file has been written to deploy the hello_world contract with the contract’s bytecode already compiled into a JSON file for us. If you read through the code, you can see that the contract bytecode is deployed to the Hedera file service at lines 42–47 after which the contract’s greet() function is called at lines 66–70 with a value returned.

// Line 42: CreateSimpleContract.java
var fileTx = new FileCreateTransaction(client).setExpirationTime(
Instant.now()
.plus(Duration.ofSeconds(3600)))
// Use the same key as the operator to "own" this file
.addKey(operatorKey.getPublicKey())
.setContents(byteCodeHex.getBytes());
// Line 66: CreateSimpleContract.java
var contractCallResult = new ContractCallQuery(client).setGas(30000)
.setContractId(newContractId)
.setFunctionParameters(CallParams.function("greet"))
.execute();

To see it work for yourself, right click on the file and select Run ‘CreateSimpleContract.main()’. You should soon see an output that returns the contract message.

Congratulations! You’ve just deployed your first smart contract on the Hedera Hashgraph.

Calling State-Changing Functions

“Hello, World!” is always a good start, but let’s go further than that. To make full use of smart contracts, you’ll also need to change their state by calling functions.

Luckily, Hedera’s developers have left us another example. In the same folder where we found hello_world, you can see another contract called stateful.sol. Notice this contract has a new function called setMessage() which allows the contract owner to change the message after deployment.

function set_message(string memory message_) public {
// only allow the owner to update the message
if (msg.sender != owner) return;
message = message_;
}

In the same folder as before, we can find a deployment script called CreateStatefulContract.java. This time, the message is passed in as the contract gets deployed on line 61. After getting the message returned, the script then calls setMessage() to pass in a new message on line 84.

// Line 84: CreateStatefulContract.java
new ContractExecuteTransaction(client).setContractId(newContractId)
.setGas(100_000_000)
.setFunctionParameters(CallParams.function("set_message")
.add("hello from hashing systems again!"))
.execute();

Go ahead and add your own message this time to see how it works. Once again, right click on the file and select Run for the output below. If you see the second message has changed, then you know setMessage() did its job.

Creating Smart Contracts

While these example contracts have been a big help to us, you might now be wondering how you can write and deploy your own smart contracts. Never fear; you can start writing smart contracts right now without even installing a compiler. Simply open up a new tab and go to Remix, a fully-functional browser-based IDE complete with a compiler and testing environment.

To see how this works for yourself, recreate the original hello_world contract by pasting the source code into Remix and then change the message that gets returned by the greet() function at line 14. Remix will then automatically recompile this contract and provide us with fresh new bytecode. Navigate to the Compile tab, and click Bytecode to copy it to your clipboard.

Go to remix.ethereum.org

Once you’ve copied your bytecode, go back to the resources folder where the smart contracts are located and open hello_world.json. Replace everything in the JSON file with the new bytecode and then save your changes. Now when you run CreateSimpleContract, you should see your new message appear.

Wrapping up

Now you know everything needed to deploy smart contracts on Hedera. If you’re looking to create more advanced smart contracts, don’t forget that you’ll need to create custom deployment scripts so be sure to refer to the Hedera documentation for how to use the SDK.

In the meantime, stay tuned for upcoming tutorials on Hedera’s File System and more!

--

--

Hashing Systems
Hashing Systems

DLTs for a Web 3.0 world. Built on Hedera Hashgraph.