Intermediate

 

Sample Contracts – KlaytnGreeter

Welcome to our tutorial on KlaytnGreeter, a sample smart contract on the Klaytn blockchain network. Sample contracts are pre-written contracts that can be used as a starting point for your own contract development. The KlaytnGreeter contract is a simple example of a smart contract that can be used to demonstrate the basics of writing, testing, and deploying smart contracts on the Klaytn network. In this tutorial, we will explore the KlaytnGreeter contract and its functionalities, walk through the process of deploying the contract on the Klaytn network, and provide best practices for using sample contracts in your own development. Whether you are a developer, researcher, or simply a blockchain enthusiast, this tutorial will provide valuable insights and hands-on experience in working with sample contracts on the Klaytn network.

The KlaytnGreeter contract is a simple example of a smart contract on the Klaytn blockchain network. It is a greeting contract that allows users to set a custom greeting message and retrieve it. The contract contains a single state variable “greeting” which is a string that stores the greeting message, and two functions “setGreeting” and “greet” that allows the user to set and retrieve the greeting message respectively. The “setGreeting” function takes a string input and sets the “greeting” variable to that string. The “greet” function returns the current value of the “greeting” variable.

When the contract is deployed on the Klaytn network, it is assigned a unique contract address. This address can be used to interact with the contract by calling its functions. To set the greeting message, a user would call the “setGreeting” function and provide the desired message as input. To retrieve the current greeting message, a user would call the “greet” function.

It’s important to note that interacting with the contract on the Klaytn network requires KLAY, the native token of the network. Additionally, executing a function of the contract on the Klaytn network costs a certain amount of gas, a unit of measurement for the amount of computational work required to execute a transaction or contract.

KlaytnGreeter is a simple contract that returns a greeting message. A greeting message is set when the contract is deployed.

Writing KlaytnGreeter

pragma solidity 0.5.6;
contract Mortal {
    /* Define variable owner of the type address */
    address payable owner;
    /* This function is executed at initialization and sets the owner of the contract */
    constructor () public { owner = msg.sender; }
    /* Function to recover the funds on the contract */
    function kill() public { if (msg.sender == owner) selfdestruct(owner); }
}

contract KlaytnGreeter is Mortal {
    /* Define variable greeting of the type string */
    string greeting;
    /* This runs once when the contract is created */
    constructor (string memory _greeting) public {
        greeting = _greeting;
    }
    /* Main function */
    function greet() public view returns (string memory) {
        return greeting;
    }
}

Deploying KlaytnGreeter using Remix Online IDE

  • Please visit Klaytn Plugin for Remix and create a KlaytnGreeter contract. The complete source code was given above.

  • Prepare your account which will be used to deploy the contract.

  • Deploy the contract with the initial parameter, a greeting message.

  • After deploying, you can invoke greet from the IDE.

With this, you complete this workshop successfully!!