1. Blockchain and Kaia Overview
1.1. What is Blockchain?
Blockchain is a distributed and immutable digital ledger that records all transactions that occur on a network. Each block in the chain contains an encrypted number of transactions, and when a new block is created, it is intimately linked to the previous block, forming a continuous chain that cannot be altered without detection. . This ensures high transparency and security, as information cannot be easily altered or deleted without network consensus, making blockchain a foundational technology for applications such as money. electronics and smart contracts.
1.2. Kaia Consensus
Consensus mechanism: Kaia uses the Istanbul BFT algorithm, an improved version of PBFT (Practical Byzantine Fault Tolerance) optimized for blockchain networks.
Target:
- Ability to process 4,000 transactions per second.
- Ability to complete transactions instantly.
- Block creation time 1 second.
- Supports more than 50 consensus nodes participating in the process.
Node type:
- CN (Consensus Node): Managed by CCO (Core Cell Operator) and responsible for creating blocks.
- PN (Proxy Node): Provides interface to the network.
- EN (Endpoint Node): Provides services to end users.
Consensus process:
When a transaction is sent to the CN (Council
) from the PNs, the VRF (Verifiable Random Function)
function is used to randomly select 1 Committee
consisting of 4 CNs. 1 CN will continue to be randomly selected to create a block
along with the observations of 3 other CNs. When a block
is created, 2/3 of the CNs in the Council
will have to sign the block
to reach consensus.
1.3. KLVM
KLVM
, short for Kaia Virtual Machine
, is a decentralized virtual machine environment running on the Kaia blockchain network. KLVM
enables the execution of smart contracts, which are programs that automatically execute transactions or specific actions based on pre-coded conditions. Smart contracts are written in programming languages such as Solidity, then compiled into bytecode
that KLVM
can understand and execute. KLVM
is compatible with Ethereum's EVM
, so it fully supports current development tools, opcode
, and source code.
1.4. What is smart contract?
A smart contract
is a type of computer program designed to automatically execute, control, or confirm events and actions according to pre-programmed terms. They exist and operate on Kaia
, ensuring high transparency and security as they cannot be modified once deployed. Smart contracts
help automate processes, reduce the need for intermediaries and minimize the risk of fraud or errors. They are versatile and can be used in a variety of fields, from finance and insurance to supply chain management and real estate. The development of smart contracts
is ushering in a new era in the way we interact and conduct digital transactions, bringing greater efficiency, transparency and autonomy to users.
1.5. Transaction and how to sign a Transaction
In the Kaia network, a "transaction" or transaction is an action performed by a user to transfer KLAY (Kaia native token) or Kaia-based tokens from one address to another, or to interact with contracts. smart contract. Each transaction includes information such as source address, destination address, amount of money transferred, gas (transaction fee), and optional data if the transaction interacts with a smart contract.
When making transactions on Kaia, signing the transaction using a crypto wallet like MetaMask is an important step to ensure safety and security. Specifically, this process takes place as follows:
- Create Transaction: User enters necessary information for the transaction such as receiving address, amount of
KLAY
or tokens to transfer, and gas. In MetaMask, for example, users can adjust gas levels so transactions are processed faster. - Transaction Signing: Once the transaction information is entered, the wallet generates a digital transaction signed with the user's private key. Signing this transaction proves that the user has the right to use the address from which the transaction was sent without revealing their private key.
- Sending Transaction: The signed transaction is then sent to the Kaia network via a wallet such as Kaikas or MetaMask. The network will confirm the transaction and execute it, transfer funds or interact with the smart contract as requested.
- Transaction Confirmation: Finally, the transaction will be confirmed by the network, and information about it will be recorded on the blockchain. Users can track the status of transactions through online tools such as Etherscan.
This process not only helps ensure transactions are carried out securely, but also helps prevent tampering or unauthorized alteration of transactions, thanks to the transparency and immutability of the blockchain.
2. Solidity File Structure
2.1. SPDX License Identifier
All solidity contracts should have License declaration in the first line.
// SPDX-License-Identifier: MIT
List of licenses from SPDX repository: https://spdx.org/licenses/
2.2. Pragmas
pragma
is the keyword used to declare the compiler version of Solidity. pragma
only applies to the current local file so you must add pragma
to all files in the project directory.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
You can use the ^
sign or the comparison operators <
, <=
, >
, >=
in conjunction with the compiler declaration.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20; // use compiler version 0.8.20 and above
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0; // use compiler version bigger or equal to 0.4.22 and less than 0.9.0
2.3. Import file from other sources
Import the whole file
import "FileName"
Import all and assign alias
import * as symbolName from "FileName";
Name import
Name import
means you will specify the name of the import object from another file. The reason you should use this option is that it makes your code clearer.
import {ContractOne as alias, ContractTwo} from "FileName";
2.4. Comment
To comment, you can use //
and /* */
// comment for 1 line.
/*
Comment for
multiple lines
*/
There is also NatSpec comment with ///
or /** **/
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
/// @author The Project Team
/// @title A simple storage example
contract SimpleStorage {
uint storedData;
/// Store `x`.
/// @param x the new value to store
/// @dev stores the number in the state variable `storedData`
function set(uint x) public {
storedData = x;
}
/// Return the stored value.
/// @dev retrieves the value of the state variable `storedData`
/// @return the stored value
function get() public view returns (uint) {
return storedData;
}
}
3. Contract structure
3.1. State variables
State variables
are variables declared at the beginning of the contract, outside the scope of local variables
declared in function
.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;
contract SimpleStorage {
uint storedData; // State variable
// ...
}
3.2. Functions
Function
are functions declared to perform calculations, change the value of variables, etc. A sample function
is given below.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract SimpleAuction {
function bid() public payable { // Function
// ...
}
}
// Helper function defined outside of a contract
function helper(uint x) pure returns (uint) {
return x * 2;
}
3.3. Function modifiers
Function modifier
are declarations for function
to create conditions for running actions of that function
.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
contract Purchase {
address public seller;
modifier onlySeller() { // Modifier
require(
msg.sender == seller,
"Only seller can call this."
);
_;
}
function abort() public view onlySeller { // Modifier usage
// ...
}
}
3.4. Events
event
is a feature for recording smart contract activities. event
is often used in building interactive UI with smart contracts.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.22;
event HighestBidIncreased(address bidder, uint amount); // Event
contract SimpleAuction {
function bid() public payable {
// ...
emit HighestBidIncreased(msg.sender, msg.value); // Triggering event
}
}
3.5. Errors
error
is used to inform the user why the action failed, and error
has a lower gas
cost than returning string
.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
/// Not enough funds for transfer. Requested `requested`,
/// but only `available` available.
error NotEnoughFunds(uint requested, uint available);
contract Token {
mapping(address => uint) balances;
function transfer(address to, uint amount) public {
uint balance = balances[msg.sender];
if (balance < amount)
revert NotEnoughFunds(amount, balance);
balances[msg.sender] -= amount;
balances[to] += amount;
// ...
}
}
3.6. Struct types
struct is used to declare a type
of object
.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;
contract Ballot {
struct Voter { // Struct
uint weight;
bool voted;
address delegate;
uint vote;
}
}
3.7. Enum types
enum
is used to declare a type
whose values are constant
.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;
contract Purchase {
enum State { Created, Locked, Inactive } // Enum
}