Deploy a Smart Contract

Now we are ready to develop and deploy Klaytn smart contracts!

Creating a Project Directory

First of all, create a directory where the source code locates.

$ mkdir klaytn-testboard
$ cd klaytn-testboard

Initializing Truffle

Initialize Truffle for contract deployment.

$ truffle init

Writing a Simple Smart Contract in Solidity

Create KlaytnGreeter.sol at klaytn-testboard/contracts directory.

$ cd contracts
$ touch KlaytnGreeter.sol
$ vi KlaytnGreeter.sol

Write the following code in KlaytnGreeter.sol.

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 payable { if (msg.sender == owner) selfdestruct(owner); }
}

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

Modifying the Migration Script

Modify 1_initial_migration.js as the following.

Deploying a Smart Contract using Truffle

Enter Klaytn's network information into truffle.js.

WARNING: Currently Klaytn Baobab network's gasPrice is fixed to 25 Gpeb (It returns an error if you attempt to use any other number).

Modify configuration as below

Deploy the contract using the following command.

NOTE: Use --network to select which network to deploy and --reset to overwrite.

NOTE: Make sure that your Klaytn node is running.

Your contract address is displayed followed `KlaytnGreeter:

WARNING: It returns an error when your account is locked.

This is how you unlock your account.

And then you are ready to go. Try deploy again.

Last updated