Contracts

More information about Contract in Solidity

constructor

Constructor is a function that runs immediately when the smart contract is initialized

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

// Base contract X
contract X {
    string public name;

    constructor(string memory _name) {
        name = _name;
    }
}

State variable visibility

  • public - Public variables are similar to internal variables (allowing the current contract and inherited contracts to access) but will automatically create a getter function so that external contracts can also access it.

  • internal - The variable can only be accessed by the current contract and inherited contracts. This is also the default visibility for state variable.

  • private - The variable can only be accessed by the current contract.

Note: The internal and private variables only restrict access to other contracts. The value of the variable remains visible to everyone.

Function visibility

  • external - function that can only be called from outside.

  • public - function can both be called by another function in contract, and can also be called from outside.

  • internal - function can only be called by an existing contract or an inherited contract.

  • private - function can only be called by the current contract.

Getter function

function is used to call the public variable that the compiler automatically creates. Also used to refer to the concept of function used to query variables to view.

Constants and immutable state variables

  • constant - variables whose values ​​are fixed immediately upon compilation (put into contract bytecode).

  • immutable - variables whose values ​​can be assigned during construct.

Pure function

function does not read or change the state of the blockchain. Or used as a calculation function.

Payable functions and addresses

Receive Ether and Fallback function

A contract can have at most one receive function, declared using receive() external payable { ... } (without the function keyword). This function must have no arguments, cannot return anything and must have external visibility as well as payable state mutability. It can be virtual, it can be override and it can have modifiers.

Oracle

Oracle for smart contracts is a bridge between blockchain and the outside world. It provides data to smart contracts from sources outside the blockchain, such as APIs, market data, weather data, etc.

Here are some examples of how to use oracle for smart contracts:

  • Providing price data for decentralized markets (DeFi): Oracle can provide price data for crypto assets, allowing traders to make trades on decentralized exchanges.

  • Activate insurance contracts: Oracle can provide data about insurance events, such as accidents or natural disasters, to trigger insurance payments.

  • Automate processes: Oracle can be used to automate processes, such as bill payment or supply chain management.

List of Oracles on Klaytn: https://klaytn.foundation/ecosystem/?search=&cate=oracles-bridges&sort=abc

Last updated