Đến đây, chúng ta đã sẵn sàng phát triển và triển khai các hợp đồng thông minh Klaytn!
Tạo thư mục dự án
Trước tiên, hãy tạo một thư mục có chứa mã nguồn.
$ mkdir klaytn-testboard
$ cd klaytn-testboard
Khởi chạy Truffle
Khởi chạy Truffle để triển khai hợp đồng.
$ truffle init
Soạn một hợp đồng thông minh đơn giản bằng Solidity
Tạo KlaytnGreeter.sol trong thư mục klaytn-testboard/contracts.
$ cd contracts
$ touch KlaytnGreeter.sol
$ vi KlaytnGreeter.sol
Viết mã sau trong 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;
}
}
Sửa đổi tập lệnh di chuyển
$ cd ..
$ cd migrations
$ vi 1_initial_migration.js