Now we are ready to develop and deploy Klaytn smart contracts!
First of all, create a directory where the source code locates.
$ mkdir klaytn-testboard$ cd klaytn-testboard
Initialize Truffle for contract deployment.
$ truffle init
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;}}
$ cd ..$ cd migrations$ vi 1_initial_migration.js
Modify 1_initial_migration.js
as the following.
const Migrations = artifacts.require("./Migrations.sol");const KlaytnGreeter = artifacts.require("./KlaytnGreeter.sol");module.exports = function(deployer) {deployer.deploy(Migrations);deployer.deploy(KlaytnGreeter, 'Hello, Klaytn');};
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).
$ cd ..$ vi truffle-config.js
Modify configuration as below
// truffle-config.jsmodule.exports = {networks: {klaytn: {host: '127.0.0.1',port: 8551,from: '0x75a59b94889a05c03c66c3c84e9d2f8308ca4abd', // enter your account addressnetwork_id: '1001', // Baobab network idgas: 20000000, // transaction gas limitgasPrice: 25000000000, // gasPrice of Baobab is 25 Gpeb},},compilers: {solc: {version: "0.5.6" // Specify compiler's version to 0.5.6}}};
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:
$ truffle deploy --network klaytn --resetUsing network 'klaytn'.Running migration: 1_initial_migration.jsDeploying Migrations...... 0x0f5108bd9e51fe6bf71dfc472577e3f55519e0b5d140a99bf65faf26830acfcaMigrations: 0x97b1b3735c8f2326a262dbbe6c574a8ea1ba0b7dDeploying KlaytnGreeter...... 0xcba53b6090cb4a118359b27293ba95116a8f35f66ae50fbd23ae1081ce9ffb9eKlaytnGreeter: [SAVE THIS ADDRESS!!] # this is your smart contract addressSaving successful migration to network...... 0x14eb68727ca5a0ac767441c9b7ab077336f9311f71e9854d42c617aebceeec72Saving artifacts...
WARNING
: It returns an error when your account is locked.
Running migration: 1_initial_migration.jsReplacing Migrations...... undefinedError encountered, bailing. Network state unknown. Review successful transactions manually.Error: authentication needed: password or unlock
This is how you unlock your account.
> personal.unlockAccount('0x775a59b94889a05c03c66c3c84e9d2f8308ca4abd')Unlock account 0x75a59b94889a05c03c66c3c84e9d2f8308ca4abdPassphrase:true
And then you are ready to go. Try deploy again.