U2U Network Documentations
  • Introduction
    • Our Vision
    • Litepaper
      • Overview
        • U2U Network Diagram
        • U2U Network with DEPIN and Web3 Eco System
      • Consensus
  • Services
    • RPC
      • RPC Endpoints
      • Ethereum API Methods
        • eth_blockNumber
        • eth_call
        • eth_chainId
        • eth_estimateGas
        • eth_feeHistory
        • eth_gasPrice
        • eth_getBalance
        • eth_getBlockByHash
        • eth_getBlockByNumber
        • eth_getBlockTransactionCountByHash
        • eth_getBlockTransactionCountByNumber
        • eth_getCode
        • eth_getLogs
        • eth_getProof
        • eth_getStorageAt
        • eth_getTransactionByBlockHashAndIndex
        • eth_getTransactionByBlockNumberAndIndex
        • eth_getTransactionByHash
        • eth_getTransactionCount
        • eth_getTransactionReceipt
        • eth_getUncleByBlockHashAndIndex
        • eth_getUncleByBlockNumberAndIndex
        • eth_getUncleCountByBlockHash
        • eth_getUncleCountByBlockNumber
        • eth_maxPriorityFeePerGas
        • eth_sendRawTransaction
        • eth_subscribe
        • eth_unsubscribe
        • net_version
        • net_listening
        • net_peerCount
        • web3_clientVersion
        • web3_sha3
        • debug_traceTransaction
      • DAG API Methods
        • dag_getEvent
        • dag_getEventPayload
        • dag_getHeads
        • eth_currentEpoch
      • Trace API Methods
        • trace_block
        • trace_get
        • trace_transaction
        • trace_filter
    • Explorer
      • Explorer URLs
      • Explorer API
        • Account
        • Block
        • Contract
        • Logs
        • Stats
        • Token
        • Transaction
      • GraphQL
      • Explorer RPC API
      • Smart Contract Verification
        • Via explorer
        • Via hardhat plugin
      • Testnet Faucet
    • Wallets
      • Metamask
      • Wallet Connect
      • Coinbase Wallet
      • U2U Super App
        • Policy
    • Staking
      • How To Stake?
      • FAQ
    • The Graph
      • Deploy a Graph Node
      • Deploy a Subgraph
    • Bridge
      • Smart Contracts & EOA
    • Oracle
      • Smart Contracts & EOA
  • Network
    • Node Types
    • Requirements
    • Build Dependencies
    • Run Normal Node
      • Mainnet Normal Node
      • Testnet Normal Node
      • Run via Docker
    • Run Validator Node
      • Mainnet Validator Node
      • Testnet Validator Node
    • Run A Local Test Network
    • Troubleshooting
  • SUBNET
    • Overview
    • Subnet Node Type
    • Requirements
    • NFT Digital License
    • Subnet Node Setup
      • Master Node
      • Verifier Node
      • Edge Node CLI
  • For Developers
    • SDK
    • Smart Contract Development
      • What are smart contracts?
      • What is gas?
      • Structure of a smart contract
      • Your first smart contract
      • Test your smart contract
      • Deploy your smart contract
      • Submit your contract to explorer
      • Interact With Your Smart Contract
      • Integrate Your Smart Contract With a Frontend
      • Additional Resources
    • Smart Contract Deployment
      • Deploy with Remix
      • Deploy with Hardhat
Powered by GitBook
On this page
  • Types
  • Data Storage
  • Function Modifiers
  • Special Functions and Variables
  • Events
  • Further
  1. For Developers
  2. Smart Contract Development

Structure of a smart contract

There's a number of key concepts defining U2U smart contracts that differ from a web2 development paradigm.

Types

The contract type in Solidity is a struct that groups a number of connected functions for a single objective.

Address types are 20-byte-long Ethereum addresses that start with the prefix 0x. They are written in hexadecimal format.

The majority of other kinds, such as booleans, integers, fixed-point numbers, byte arrays, and literals, will be recognisable to you as a developer.

Data Storage

In Solidity, reference data values can be stored as storage, memory or calldata depending on the role of the data stored. In particular:

  • storage keeps the data permanently on the blockchain, and is extremely expensive.

  • memory values are stored only for the lifetime of the smart contract's execution, and are inexpensive to use (costing only small amounts of gas).

  • calldata is a special data location that contains the function arguments, and is only available for external function call parameters.

Function Modifiers

Functions exist to get / set information based on calls initiated by external transactions. In particular, a smart contract can never run unless initiated by an external transaction - they don't execute silently in the background.

Access modifiers include:

  • public: Can be accessed by all functions or callers

  • external: Can be accessed only by external callers, not internal functions

  • internal: Can be accessed only by this contract, or contracts deriving from it

  • private: Can be accessed only from this contract itself

Other modifiers include:

  • view: This guarantees that the function will not modify the state of the contract's data (or data in storage).

  • pure: This guarantees that the function with neither read nor modify the state of the contract's data.

  • payable: Functions and addresses declared payable can receive ether into their contracts.

Special Functions and Variables

There are a number of global variables and functions that will be helpful to remember you have access to! Some special variables are:

  • block.number (uint256): The number of the most recent block.

  • block.timestamp (uint256): The UNIX timestamp of the most recent block.

  • block.gaslimit (uint256): The gas limit of the current block.

  • msg.sender (address payable): The sender of the transaction triggering the contract.

  • msg.value (uint256): The number of wei transferred with the message.

Special functions include:

  • receive(): Contracts may only have one of these functions declared. It serves as the default destination when a contract is sent Ether. It cannot have arguments, return anything, and must be external and payable.

  • fallback(): Contracts may only have one of these functions declared. It serves as the fallback if a call to the contract does not match any function, or if no data was supplied and the receive() function was not declared. It cannot have arguments, return anything, and must be external.‍

Events

Solidity events are roughly equivalent to logging in other programming paradigms. They're emitted when a contract is executed, stored permanently on the blockchain, but aren't accessible to be modified / read by smart contracts.

You can declare and emit an event like this:

event TestEvent (
	uint256 date,
  string value
);

emit TestEvent(block.timestamp,’My first event!”);

Events can be accessed in a variety of different ways:

  1. Events are stored in the receipts of transactions, and can be accessed there.

  2. You can subscribe to an event with myContract.events.TestEvent([options][, callback])

  3. You can request past events using a request like myContract.getPastEvents(event[, options][, callback]).

Further

PreviousWhat is gas?NextYour first smart contract

Last updated 11 months ago

This should give you a broad overview of the key components of a smart contract!

Now, you are ready to write your own contract. Please visit next section with .

You can find more detail in the Solidity Documentation linked here.
Your first smart contract