# 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:&#x20;

* **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:&#x20;

* **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:&#x20;

* **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:&#x20;

* **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.**&#x200D;

## **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:&#x20;

```
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])`**.**<br>

This should give you a broad overview of the key components of a smart contract\![ You can find more detail in the Solidity Documentation linked here.](https://docs.soliditylang.org/en/v0.8.0/contracts.html)

## Further

Now, you are ready to write your own contract. Please visit next section with [Your first smart contract](/for-developers/smart-contract-development/your-first-smart-contract.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.u2u.xyz/for-developers/smart-contract-development/structure-of-a-smart-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
