Hedera
Search…
Deploy your first Hedera Smart Contract
Learn how to deploy a smart contract on Hedera

Summary

In this tutorial, you will learn how to create a simple smart contract on Hedera using solidity. This tutorial assumes you have completed the "Getting Started" section of the docs and understand the basics of creating and submitting a transaction to a Hedera network.

1. Create a "Hello Hedera" Smart Contract

Create a smart contract in solidity using the remix IDE. The "Hello Hedera" contract solidity file is sampled below along with the "Hello Hedera" JSON file that is produced after the contract has compiled. You can use remix to create and compile the contract yourself or you can copy the files below into your project. If you are not familiar with Solidity you can check out the docs here. Hedera supports the latest version of Solidity (v0.8.9) on preivewnet and testnet.
The contract stores two variables the owner and message. The constructor passes in the message parameter. The set_message function allows the owner to update the message variable and the get_message function allows you to return the message.
The HelloHedera.sol will serve as a reference to the contract that was compiled. The HelloHedera.json file contains the data.bytecode.object field that will be used to store the contract bytecode in a file on the Hedera network.
HelloHedera.sol
HelloHedera.json
1
pragma solidity >=0.7.0 <0.8.9;
2
3
contract HelloHedera {
4
// the contract's owner, set in the constructor
5
address owner;
6
7
// the message we're storing
8
string message;
9
10
constructor(string memory message_) public {
11
// set the owner of the contract for `kill()`
12
owner = msg.sender;
13
message = message_;
14
}
15
16
function set_message(string memory message_) public {
17
// only allow the owner to update the message
18
if (msg.sender != owner) return;
19
message = message_;
20
}
21
22
// return a string
23
function get_message() public view returns (string memory) {
24
return message;
25
}
26
27
// recover the funds of the contract
28
function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); }
29
}
Copied!
1
{
2
"deploy": {
3
"VM:-": {
4
"linkReferences": {},
5
"autoDeployLib": true
6
},
7
"main:1": {
8
"linkReferences": {},
9
"autoDeployLib": true
10
},
11
"ropsten:3": {
12
"linkReferences": {},
13
"autoDeployLib": true
14
},
15
"rinkeby:4": {
16
"linkReferences": {},
17
"autoDeployLib": true
18
},
19
"kovan:42": {
20
"linkReferences": {},
21
"autoDeployLib": true
22
},
23
"görli:5": {
24
"linkReferences": {},
25
"autoDeployLib": true
26
},
27
"Custom": {
28
"linkReferences": {},
29
"autoDeployLib": true
30
}
31
},
32
"data": {
33
"bytecode": {
34
"linkReferences": {},
35
"object": "608060405234801561001057600080fd5b506040516105583803806105588339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b8382019150602082018581111561006957600080fd5b825186600182028301116401000000008211171561008657600080fd5b8083526020830192505050908051906020019080838360005b838110156100ba57808201518184015260208101905061009f565b50505050905090810190601f1680156100e75780820380516001836020036101000a031916815260200191505b50604052505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550806001908051906020019061014492919061014b565b50506101e8565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061018c57805160ff19168380011785556101ba565b828001600101855582156101ba579182015b828111156101b957825182559160200191906001019061019e565b5b5090506101c791906101cb565b5090565b5b808211156101e45760008160009055506001016101cc565b5090565b610361806101f76000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e9826021461003b57806332af2edb146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe6101ec565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576101e9565b80600190805190602001906101e792919061028e565b505b50565b606060018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102845780601f1061025957610100808354040283529160200191610284565b820191906000526020600020905b81548152906001019060200180831161026757829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102cf57805160ff19168380011785556102fd565b828001600101855582156102fd579182015b828111156102fc5782518255916020019190600101906102e1565b5b50905061030a919061030e565b5090565b5b8082111561032757600081600090555060010161030f565b509056fea26469706673582212201644465f5f73dfd73a518b57770f5adb27f025842235980d7a0f4e15b1acb18e64736f6c63430007000033",
36
"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x40 MLOAD PUSH2 0x558 CODESIZE SUB DUP1 PUSH2 0x558 DUP4 CODECOPY DUP2 DUP2 ADD PUSH1 0x40 MSTORE PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x33 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 MLOAD PUSH1 0x40 MLOAD SWAP4 SWAP3 SWAP2 SWAP1 DUP5 PUSH5 0x100000000 DUP3 GT ISZERO PUSH2 0x53 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP4 DUP3 ADD SWAP2 POP PUSH1 0x20 DUP3 ADD DUP6 DUP2 GT ISZERO PUSH2 0x69 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP3 MLOAD DUP7 PUSH1 0x1 DUP3 MUL DUP4 ADD GT PUSH5 0x100000000 DUP3 GT OR ISZERO PUSH2 0x86 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP1 DUP4 MSTORE PUSH1 0x20 DUP4 ADD SWAP3 POP POP POP SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0xBA JUMPI DUP1 DUP3 ADD MLOAD DUP2 DUP5 ADD MSTORE PUSH1 0x20 DUP2 ADD SWAP1 POP PUSH2 0x9F JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0xE7 JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JUMPDEST POP PUSH1 0x40 MSTORE POP POP POP CALLER PUSH1 0x0 DUP1 PUSH2 0x100 EXP DUP2 SLOAD DUP2 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF MUL NOT AND SWAP1 DUP4 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND MUL OR SWAP1 SSTORE POP DUP1 PUSH1 0x1 SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH2 0x144 SWAP3 SWAP2 SWAP1 PUSH2 0x14B JUMP JUMPDEST POP POP PUSH2 0x1E8 JUMP JUMPDEST DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH1 0x1F LT PUSH2 0x18C JUMPI DUP1 MLOAD PUSH1 0xFF NOT AND DUP4 DUP1 ADD OR DUP6 SSTORE PUSH2 0x1BA JUMP JUMPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD DUP6 SSTORE DUP3 ISZERO PUSH2 0x1BA JUMPI SWAP2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0x1B9 JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x19E JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0x1C7 SWAP2 SWAP1 PUSH2 0x1CB JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x1E4 JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0x1CC JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH2 0x361 DUP1 PUSH2 0x1F7 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x36 JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x2E982602 EQ PUSH2 0x3B JUMPI DUP1 PUSH4 0x32AF2EDB EQ PUSH2 0xF6 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0xF4 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x51 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH5 0x100000000 DUP2 GT ISZERO PUSH2 0x6E JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP3 ADD DUP4 PUSH1 0x20 DUP3 ADD GT ISZERO PUSH2 0x80 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP2 DUP5 PUSH1 0x1 DUP4 MUL DUP5 ADD GT PUSH5 0x100000000 DUP4 GT OR ISZERO PUSH2 0xA2 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST SWAP2 SWAP1 DUP1 DUP1 PUSH1 0x1F ADD PUSH1 0x20 DUP1 SWAP2 DIV MUL PUSH1 0x20 ADD PUSH1 0x40 MLOAD SWAP1 DUP2 ADD PUSH1 0x40 MSTORE DUP1 SWAP4 SWAP3 SWAP2 SWAP1 DUP2 DUP2 MSTORE PUSH1 0x20 ADD DUP4 DUP4 DUP1 DUP3 DUP5 CALLDATACOPY PUSH1 0x0 DUP2 DUP5 ADD MSTORE PUSH1 0x1F NOT PUSH1 0x1F DUP3 ADD AND SWAP1 POP DUP1 DUP4 ADD SWAP3 POP POP POP POP POP POP POP SWAP2 SWAP3 SWAP2 SWAP3 SWAP1 POP POP POP PUSH2 0x179 JUMP JUMPDEST STOP JUMPDEST PUSH2 0xFE PUSH2 0x1EC JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP1 PUSH1 0x20 ADD DUP3 DUP2 SUB DUP3 MSTORE DUP4 DUP2 DUP2 MLOAD DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x13E JUMPI DUP1 DUP3 ADD MLOAD DUP2 DUP5 ADD MSTORE PUSH1 0x20 DUP2 ADD SWAP1 POP PUSH2 0x123 JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0x16B JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JUMPDEST POP SWAP3 POP POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 DUP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND CALLER PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND EQ PUSH2 0x1D1 JUMPI PUSH2 0x1E9 JUMP JUMPDEST DUP1 PUSH1 0x1 SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH2 0x1E7 SWAP3 SWAP2 SWAP1 PUSH2 0x28E JUMP JUMPDEST POP JUMPDEST POP JUMP JUMPDEST PUSH1 0x60 PUSH1 0x1 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV DUP1 PUSH1 0x1F ADD PUSH1 0x20 DUP1 SWAP2 DIV MUL PUSH1 0x20 ADD PUSH1 0x40 MLOAD SWAP1 DUP2 ADD PUSH1 0x40 MSTORE DUP1 SWAP3 SWAP2 SWAP1 DUP2 DUP2 MSTORE PUSH1 0x20 ADD DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV DUP1 ISZERO PUSH2 0x284 JUMPI DUP1 PUSH1 0x1F LT PUSH2 0x259 JUMPI PUSH2 0x100 DUP1 DUP4 SLOAD DIV MUL DUP4 MSTORE SWAP2 PUSH1 0x20 ADD SWAP2 PUSH2 0x284 JUMP JUMPDEST DUP3 ADD SWAP2 SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 JUMPDEST DUP2 SLOAD DUP2 MSTORE SWAP1 PUSH1 0x1 ADD SWAP1 PUSH1 0x20 ADD DUP1 DUP4 GT PUSH2 0x267 JUMPI DUP3 SWAP1 SUB PUSH1 0x1F AND DUP3 ADD SWAP2 JUMPDEST POP POP POP POP POP SWAP1 POP SWAP1 JUMP JUMPDEST DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH1 0x1F LT PUSH2 0x2CF JUMPI DUP1 MLOAD PUSH1 0xFF NOT AND DUP4 DUP1 ADD OR DUP6 SSTORE PUSH2 0x2FD JUMP JUMPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD DUP6 SSTORE DUP3 ISZERO PUSH2 0x2FD JUMPI SWAP2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0x2FC JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x2E1 JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0x30A SWAP2 SWAP1 PUSH2 0x30E JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x327 JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0x30F JUMP JUMPDEST POP SWAP1 JUMP INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 AND DIFFICULTY CHAINID 0x5F 0x5F PUSH20 0xDFD73A518B57770F5ADB27F025842235980D7A0F 0x4E ISZERO 0xB1 0xAC 0xB1 DUP15 PUSH5 0x736F6C6343 STOP SMOD STOP STOP CALLER ",
37
"sourceMap": "33:623:0:-:0;;;186:160;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;301:10;293:5;;:18;;;;;;;;;;;;;;;;;;331:8;321:7;:18;;;;;;;;;;;;:::i;:::-;;186:160;33:623;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;:::o;:::-;;;;;;;"
38
},
39
"deployedBytecode": {
40
"immutableReferences": {},
41
"linkReferences": {},
42
"object": "608060405234801561001057600080fd5b50600436106100365760003560e01c80632e9826021461003b57806332af2edb146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe6101ec565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576101e9565b80600190805190602001906101e792919061028e565b505b50565b606060018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102845780601f1061025957610100808354040283529160200191610284565b820191906000526020600020905b81548152906001019060200180831161026757829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102cf57805160ff19168380011785556102fd565b828001600101855582156102fd579182015b828111156102fc5782518255916020019190600101906102e1565b5b50905061030a919061030e565b5090565b5b8082111561032757600081600090555060010161030f565b509056fea26469706673582212201644465f5f73dfd73a518b57770f5adb27f025842235980d7a0f4e15b1acb18e64736f6c63430007000033",
43
"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x36 JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x2E982602 EQ PUSH2 0x3B JUMPI DUP1 PUSH4 0x32AF2EDB EQ PUSH2 0xF6 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0xF4 PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x51 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH5 0x100000000 DUP2 GT ISZERO PUSH2 0x6E JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP3 ADD DUP4 PUSH1 0x20 DUP3 ADD GT ISZERO PUSH2 0x80 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP2 DUP5 PUSH1 0x1 DUP4 MUL DUP5 ADD GT PUSH5 0x100000000 DUP4 GT OR ISZERO PUSH2 0xA2 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST SWAP2 SWAP1 DUP1 DUP1 PUSH1 0x1F ADD PUSH1 0x20 DUP1 SWAP2 DIV MUL PUSH1 0x20 ADD PUSH1 0x40 MLOAD SWAP1 DUP2 ADD PUSH1 0x40 MSTORE DUP1 SWAP4 SWAP3 SWAP2 SWAP1 DUP2 DUP2 MSTORE PUSH1 0x20 ADD DUP4 DUP4 DUP1 DUP3 DUP5 CALLDATACOPY PUSH1 0x0 DUP2 DUP5 ADD MSTORE PUSH1 0x1F NOT PUSH1 0x1F DUP3 ADD AND SWAP1 POP DUP1 DUP4 ADD SWAP3 POP POP POP POP POP POP POP SWAP2 SWAP3 SWAP2 SWAP3 SWAP1 POP POP POP PUSH2 0x179 JUMP JUMPDEST STOP JUMPDEST PUSH2 0xFE PUSH2 0x1EC JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP1 PUSH1 0x20 ADD DUP3 DUP2 SUB DUP3 MSTORE DUP4 DUP2 DUP2 MLOAD DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 DUP1 DUP4 DUP4 PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x13E JUMPI DUP1 DUP3 ADD MLOAD DUP2 DUP5 ADD MSTORE PUSH1 0x20 DUP2 ADD SWAP1 POP PUSH2 0x123 JUMP JUMPDEST POP POP POP POP SWAP1 POP SWAP1 DUP2 ADD SWAP1 PUSH1 0x1F AND DUP1 ISZERO PUSH2 0x16B JUMPI DUP1 DUP3 SUB DUP1 MLOAD PUSH1 0x1 DUP4 PUSH1 0x20 SUB PUSH2 0x100 EXP SUB NOT AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP JUMPDEST POP SWAP3 POP POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 DUP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND CALLER PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND EQ PUSH2 0x1D1 JUMPI PUSH2 0x1E9 JUMP JUMPDEST DUP1 PUSH1 0x1 SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH2 0x1E7 SWAP3 SWAP2 SWAP1 PUSH2 0x28E JUMP JUMPDEST POP JUMPDEST POP JUMP JUMPDEST PUSH1 0x60 PUSH1 0x1 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV DUP1 PUSH1 0x1F ADD PUSH1 0x20 DUP1 SWAP2 DIV MUL PUSH1 0x20 ADD PUSH1 0x40 MLOAD SWAP1 DUP2 ADD PUSH1 0x40 MSTORE DUP1 SWAP3 SWAP2 SWAP1 DUP2 DUP2 MSTORE PUSH1 0x20 ADD DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV DUP1 ISZERO PUSH2 0x284 JUMPI DUP1 PUSH1 0x1F LT PUSH2 0x259 JUMPI PUSH2 0x100 DUP1 DUP4 SLOAD DIV MUL DUP4 MSTORE SWAP2 PUSH1 0x20 ADD SWAP2 PUSH2 0x284 JUMP JUMPDEST DUP3 ADD SWAP2 SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 JUMPDEST DUP2 SLOAD DUP2 MSTORE SWAP1 PUSH1 0x1 ADD SWAP1 PUSH1 0x20 ADD DUP1 DUP4 GT PUSH2 0x267 JUMPI DUP3 SWAP1 SUB PUSH1 0x1F AND DUP3 ADD SWAP2 JUMPDEST POP POP POP POP POP SWAP1 POP SWAP1 JUMP JUMPDEST DUP3 DUP1 SLOAD PUSH1 0x1 DUP2 PUSH1 0x1 AND ISZERO PUSH2 0x100 MUL SUB AND PUSH1 0x2 SWAP1 DIV SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH1 0x1F LT PUSH2 0x2CF JUMPI DUP1 MLOAD PUSH1 0xFF NOT AND DUP4 DUP1 ADD OR DUP6 SSTORE PUSH2 0x2FD JUMP JUMPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD DUP6 SSTORE DUP3 ISZERO PUSH2 0x2FD JUMPI SWAP2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0x2FC JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0x2E1 JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0x30A SWAP2 SWAP1 PUSH2 0x30E JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x327 JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0x30F JUMP JUMPDEST POP SWAP1 JUMP INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 AND DIFFICULTY CHAINID 0x5F 0x5F PUSH20 0xDFD73A518B57770F5ADB27F025842235980D7A0F 0x4E ISZERO 0xB1 0xAC 0xB1 DUP15 PUSH5 0x736F6C6343 STOP SMOD STOP STOP CALLER ",
44
"sourceMap": "33:623:0:-:0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;352:182;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;563:90;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;352:182;486:5;;;;;;;;;;472:19;;:10;:19;;;468:32;;493:7;;468:32;519:8;509:7;:18;;;;;;;;;;;;:::i;:::-;;352:182;;:::o;563:90::-;607:13;639:7;632:14;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;563:90;:::o;-1:-1:-1:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;:::o"
45
},
46
"gasEstimates": {
47
"creation": {
48
"codeDepositCost": "173000",
49
"executionCost": "infinite",
50
"totalCost": "infinite"
51
},
52
"external": {
53
"get_message()": "infinite",
54
"set_message(string)": "infinite"
55
}
56
},
57
"methodIdentifiers": {
58
"get_message()": "32af2edb",
59
"set_message(string)": "2e982602"
60
}
61
},
62
"abi": [
63
{
64
"inputs": [
65
{
66
"internalType": "string",
67
"name": "message_",
68
"type": "string"
69
}
70
],
71
"stateMutability": "nonpayable",
72
"type": "constructor"
73
},
74
{
75
"inputs": [],
76
"name": "get_message",
77
"outputs": [
78
{
79
"internalType": "string",
80
"name": "",
81
"type": "string"
82
}
83
],
84
"stateMutability": "view",
85
"type": "function"
86
},
87
{
88
"inputs": [
89
{
90
"internalType": "string",
91
"name": "message_",
92
"type": "string"
93
}
94
],
95
"name": "set_message",
96
"outputs": [],
97
"stateMutability": "nonpayable",
98
"type": "function"
99
}
100
]
101
}
102
Copied!

2. Store the Smart Contract Bytecode on Hedera

Create a file using the FileCreateTransaction() API to store the hex-encoded byte code of the "Hello Hedera" contract. Once the file is created you can obtain the file ID from the receipt of the transaction.
Note: The bytecode is required to be hex encoded. It should not be the actual data the hex represents.
Java
JavaScript
Go
1
//Import the compiled contract from the HelloHedera.json file
2
Gson gson = new Gson();
3
JsonObject jsonObject;
4
5
InputStream jsonStream = HelloHederaSmartContract.class.getClassLoader().getResourceAsStream("HelloHedera.json");
6
jsonObject = gson.fromJson(new InputStreamReader(jsonStream, StandardCharsets.UTF_8), JsonObject.class);
7
8
//Store the "object" field from the HelloHedera.json file as hex-encoded bytecode
9
String object = jsonObject.getAsJsonObject("data").getAsJsonObject("bytecode").get("object").getAsString();
10
byte[] bytecode = object.getBytes(StandardCharsets.UTF_8);
11
12
//Create a file on Hedera and store the hex-encoded bytecode
13
FileCreateTransaction fileCreateTx = new FileCreateTransaction()
14
//Set the bytecode of the contract
15
.setContents(bytecode);
16
17
//Submit the file to the Hedera test network signing with the transaction fee payer key specified with the client
18
TransactionResponse submitTx = fileCreateTx.execute(client);
19
20
//Get the receipt of the file create transaction
21
TransactionReceipt fileReceipt = submitTx.getReceipt(client);
22
23
//Get the file ID from the receipt
24
FileId bytecodeFileId = fileReceipt.fileId;
25
26
//Log the file ID
27
System.out.println("The smart contract bytecode file ID is " +bytecodeFileId)
28
29
//v2 Hedera Java SDK
Copied!
1
//Import the compiled contract from the HelloHedera.json file
2
let helloHedera = require("./HelloHedera.json");
3
const bytecode = helloHedera.data.bytecode.object;
4
5
//Create a file on Hedera and store the hex-encoded bytecode
6
const fileCreateTx = new FileCreateTransaction()
7
//Set the bytecode of the contract
8
.setContents(bytecode);
9
10
//Submit the file to the Hedera test network signing with the transaction fee payer key specified with the client
11
const submitTx = await fileCreateTx.execute(client);
12
13
//Get the receipt of the file create transaction
14
const fileReceipt = await submitTx.getReceipt(client);
15
16
//Get the file ID from the receipt
17
const bytecodeFileId = fileReceipt.fileId;
18
19
//Log the file ID
20
console.log("The smart contract byte code file ID is " +bytecodeFileId)
Copied!
1
//Import and parse the compiled contract from the HelloHedera.json file
2
helloHedera, err := ioutil.ReadFile("./HelloHedera.json")
3
if err != nil {
4
println(err.Error(), ": error reading HelloHedera.json")
5
return
6
}
7
8
var contract contract = contract{}
9
10
err = json.Unmarshal([]byte(helloHedera), &contract)
11
if err != nil {
12
println(err.Error(), ": error unmarshaling the json file")
13
return
14
}
15
16
bytecode := []byte(contract.Object)
17
18
//Create a file on Hedera and store the hex-encoded bytecode
19
fileTx, err := hedera.NewFileCreateTransaction().
20
//Set the bytecode of the contract
21
SetContents([]byte(bytecode)).
22
//Submit the transaction to a Hedera network
23
Execute(client)
24
25
if err != nil {
26
println(err.Error(), ": error creating file")
27
return
28
}
29
30
//Get the receipt of the file create transaction
31
fileReceipt, err := fileTx.GetReceipt(client)
32
if err != nil {
33
println(err.Error(), ": error getting file create transaction receipt")
34
return
35
}
36
37
//Get the file ID
38
byteCodefileID := *fileReceipt.FileID
39
40
//Log the file ID
41
fmt.Printf("contract bytecode file: %v\n", bytecodeFileID)
Copied!

3. Deploy a Hedera Smart Contract

Create the contract and set the file ID to the file ID that stores the hex encoded byte code from the previous step. You will also need to set gas the value that will create the contract and pass the constructor parameters using ContractFunctionParameters() API. In this example, "hello from Hedera!" was passed to the constructor. After the transaction is successfully executed, you can get the contract ID from the receipt.
You will need to set the gas value high enough to deploy the contract. If you don't have enough gas you will receive an INSUFFICIENT_GAS response.
Java
JavaScript
Go
1
// Instantiate the contract instance
2
ContractCreateTransaction contractTx = new ContractCreateTransaction()
3
//Set the file ID of the Hedera file storing the bytecode
4
.setBytecodeFileId(newFileId)
5
//Set the gas to instantiate the contract
6
.setGas(100_000)
7
//Provide the constructor parameters for the contract
8
.setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera!"));
9
10
//Submit the transaction to the Hedera test network
11
TransactionResponse contractResponse = contractTx.execute(client);
12
13
//Get the receipt of the file create transaction
14
TransactionReceipt contractReceipt = contractResponse.getReceipt(client);
15
16
//Get the smart contract ID
17
ContractId newContractId = contractReceipt.contractId;
18
19
//Log the smart contract ID
20
System.out.println("The smart contract ID is " + newContractId);
21
22
//v2 Hedera Java SDK
Copied!
1
// Instantiate the contract instance
2
const contractTx = await new ContractCreateTransaction()
3
//Set the file ID of the Hedera file storing the bytecode
4
.setBytecodeFileId(bytecodeFileId)
5
//Set the gas to instantiate the contract
6
.setGas(100000)
7
//Provide the constructor parameters for the contract
8
.setConstructorParameters(new ContractFunctionParameters().addString("Hello from Hedera!"));
9
10
//Submit the transaction to the Hedera test network
11
const contractResponse = await contractTx.execute(client);
12
13
//Get the receipt of the file create transaction
14
const contractReceipt = await contractResponse.getReceipt(client);
15
16
//Get the smart contract ID
17
const newContractId = contractReceipt.contractId;
18
19
//Log the smart contract ID
20
console.log("The smart contract ID is " + newContractId);
21
22
//v2 JavaScript SDK
Copied!
1
2
// Instantiate the contract instance
3
contractTx, err := hedera.NewContractCreateTransaction().
4
//Set the file ID of the Hedera file storing the bytecode
5
SetBytecodeFileID(bytecodeFileID).
6
//Set the gas to instantiate the contract
7
SetGas(100000).
8
//Provide the constructor parameters for the contract
9
SetConstructorParameters(hedera.NewContractFunctionParameters().
10
AddString("Hello from hedera")).
11
Execute(client)
12
13
if err != nil {
14
println(err.Error(), ": error creating contract")
15
return
16
}
17
18
//Get the receipt of the contract create transaction
19
contractReceipt, err := contractTx.GetReceipt(client)
20
if err != nil {
21
println(err.Error(), ": error retrieving contract creation receipt")
22
return
23
}
24
25
//Get the contract ID from the receipt
26
newContractID := *contractReceipt.ContractID
27
28
//v2 Hedera Go SDK
Copied!

4. Call the get_message contract function

In the previous step, the contract message variable was set to "hello from Hedera!." You can return this message from the contract by submitting a query that will return the stored message string. The ContractCallQuery() similarly does not modify the state of the contract like other Hedera queries. It only reads stored values.
Java
JavaScript
Go
1
// Calls a function of the smart contract
2
ContractCallQuery contractQuery = new ContractCallQuery()
3
//Set the gas for the query
4
.setGas(100000)
5
//Set the contract ID to return the request for
6
.setContractId(newContractId)
7
//Set the function of the contract to call
8
.setFunction("get_message" )
9
//Set the query payment for the node returning the request
10
//This value must cover the cost of the request otherwise will fail
11
.setQueryPayment(new Hbar(2));
12
13
//Submit to a Hedera network
14
ContractFunctionResult getMessage = contractQuery.execute(client);
15
//Get the message
16
String message = getMessage.getString(0);
17
18
//Log the message
19
System.out.println("The contract message: " + message);
20
21
//v2 Hedera Java SDK
Copied!
1
// Calls a function of the smart contract
2
const contractQuery = await new ContractCallQuery()
3
//Set the gas for the query
4
.setGas(100000)
5
//Set the contract ID to return the request for
6
.setContractId(newContractId)
7
//Set the contract function to call
8
.setFunction("get_message" )
9
//Set the query payment for the node returning the request
10
//This value must cover the cost of the request otherwise will fail
11
.setQueryPayment(new Hbar(2));
12
13
//Submit to a Hedera network
14
const getMessage = await contractQuery.execute(client);
15
16
// Get a string from the result at index 0
17
const message = getMessage.getString(0);
18
19
//Log the message
20
console.log("The contract message: " + message);
21
22
//v2 Hedera JavaScript SDK
Copied!
1
// Calls a function of the smart contract
2
contractQuery, err := hedera.NewContractCallQuery().
3
//Set the contract ID to return the request for
4
SetContractID(newContractID).
5
//Set the gas for the query
6
SetGas(100000).
7
//Set the query payment for the node returning the request
8
//This value must cover the cost of the request otherwise will fail
9
SetQueryPayment(hedera.NewHbar(1)).
10
//Set the contract function to call
11
SetFunction("getMessage", nil). // nil -> no parameters
12
//Submit the query to a Hedera network
13
Execute(client)
14
15
if err != nil {
16
println(err.Error(), ": error executing contract call query")
17
return
18
}
19
20
// Get a string from the result at index 0
21
getMessage := contractQuery.GetString(0)
22
23
//Log the message
24
fmt.Printf("The contract message: ", getMessage)
25
26
//v2 Hedera Go SDK
Copied!

5. Call the set_message contract function

Call the set_message function of the contract. To do this you will need to use the ContractExecuteTransaction() API. This transaction will update the contract message. Once the transaction is successfully submitted you can verify the message was updated by requesting ContractCallQuery(). The message returned from the contract should now log "Hello from Hedera again!"
Java
JavaScript
Go
1
//Create the transaction to update the contract message
2
ContractExecuteTransaction contractExecTx = new ContractExecuteTransaction()
3
//Set the ID of the contract
4
.setContractId(newContractId)
5
//Set the gas for the call
6
.setGas(100_000)
7
//Set the function of the contract to call
8
.setFunction("set_message", new ContractFunctionParameters().addString("Hello from Hedera again!"));
9
10
//Submit the transaction to a Hedera network and store the response
11
TransactionResponse submitExecTx = contractExecTx.execute(client);
12
13
//Get the receipt of the transaction
14
TransactionReceipt receipt2 = submitExecTx.getReceipt(client);
15
16
//Confirm the transaction was executed successfully
17
System.out.println("The transaction status is" +receipt2.status);
18
19
//Query the contract for the contract message
20
ContractCallQuery contractCallQuery = new ContractCallQuery()
21
//Set ID of the contract to query
22
.setContractId(newContractId)
23
//Set the gas to execute the contract call
24
.setGas(100_000)
25
//Set the contract function
26
.setFunction("get_message")
27
//Set the query payment for the node returning the request
28
//This value must cover the cost of the request otherwise will fail
29
.setQueryPayment(new Hbar(2));
30
31
//Submit the query to a Hedera network
32
ContractFunctionResult contractUpdateResult = contractCallQuery.execute(client);
33
34
//Get the updated message
35
String message2 = contractUpdateResult.getString(0);
36
37
//Log the updated message
38
System.out.println("The contract updated message: " + message2);
Copied!
1
//Create the transaction to update the contract message
2
const contractExecTx = await new ContractExecuteTransaction()
3
//Set the ID of the contract
4
.setContractId(newContractId)
5
//Set the gas for the contract call
6
.setGas(100000)
7
//Set the contract function to call
8
.setFunction("set_message", new ContractFunctionParameters().addString("Hello from Hedera again!"));
9
10
//Submit the transaction to a Hedera network and store the response
11
const submitExecTx = await contractExecTx.execute(client);
12
13
//Get the receipt of the transaction
14
const receipt2 = await submitExecTx.getReceipt(client);
15
16
//Confirm the transaction was executed successfully
17
console.log("The transaction status is " +receipt2.status.toString());
18
19
//Query the contract for the contract message
20
const contractCallQuery = new ContractCallQuery()
21
//Set the ID of the contract to query
22
.setContractId(newContractId)
23
//Set the gas to execute the contract call
24
.setGas(100000)
25
//Set the contract function to call
26
.setFunction("get_message")
27
//Set the query payment for the node returning the request
28
//This value must cover the cost of the request otherwise will fail
29
.setQueryPayment(new Hbar(10));
30
31
//Submit the transaction to a Hedera network
32
const contractUpdateResult = await contractCallQuery.execute(client);
33
34
//Get the updated message at index 0
35
const message2 = contractUpdateResult.getString(0);
36
37
//Log the updated message to the console
38
console.log("The updated contract message: " + message2);
39
40
//v2 Hedera JavaScript SDK
Copied!
1
//Create the transaction to update the contract message
2
contractExecTx, err := hedera.NewContractExecuteTransaction().
3
//Set the ID of the contract
4
SetContractID(newContractID).
5
//Set the gas to execute the call
6
SetGas(100000).
7
//Set the contract function to call
8
SetFunction("setMessage", hedera.NewContractFunctionParameters().
9
AddString("Hello from Hedera again!")).
10
Execute(client)
11
12
if err != nil {
13
println(err.Error(), ": error executing contract")
14
return
15
}
16
17
//Get the receipt of the transaction
18
receipt2, err := contractExecTx.GetReceipt(client)
19
20
//Confirm the transaction was executed successfully
21
fmt.Printf("The transaction status is", receipt2.Status))
22
23
//Query the contract for the contract message
24
contractCallQuery, err := hedera.NewContractCallQuery().
25
//Set the contract ID to query
26
SetContractID(newContractID).
27
//Set the gas to execute the contract call
28
SetGas(100000).
29
//Set the query payment for the node returning the request
30
//This value must cover the cost of the request otherwise will fail
31
SetQueryPayment(hedera.NewHbar(2)).
32
//Set the contract function to call
33
SetFunction("getMessage", nil). // nil -> no parameters
34
//Submit the query to a Hedera network node
35
Execute(client)
36
37
if err != nil {
38
println(err.Error(), ": error executing contract call query")
39
return
40
}
41
42
// Get a string from the result at index 0
43
getMessage2 := contractCallQuery.GetString(0)
44
45
//Log the message
46
fmt.Printf("The updated contract message: ", getMessage2)
47
48
//v2 Hedera Go SDK
Copied!

Congratulations
🎉
! You have completed the following:

  • Created a simple smart contract on Hedera
  • Interacted with contract functions
Want to see more examples? Check out the following:
Last modified 30d ago