---
# System prepended metadata

title: Solidity(待整理)

---

# Solidity(待整理)

## Define version

- 指定版本介於0.6.0-0.9.0
`pragma solidity >=0.6.0 <0.9.0;`
- 指定版本為 0.6.0
`pragma solidity 0.6.0;`
- 指定版本為0.6.X (<0.7)
`pragma solidity ^0.6.0;`

## Define contract
similar to `class` in OOP language
```solidity
contract contractname{
    ...
}
```
## Type & Declaring Variables
[ref](https://docs.soliditylang.org/en/v0.8.20/types.html)



| Type| content | 
|---|---|
|bool    | true, false| 
|int8-256/uint8-256    | 2**(8-256) -1 ~ 0(initialized to 0 if not assign)| 
|address|wallet address|
|bytes1-32|**fixed** size, sequence of bytes from 1 to up to 32.|
|bytes|**Dynamically**-sized byte array|
|string|**Dynamically**-sized UTF-8-encoded string|
struct
mapping(dict)


## Functions & Variables Visibility

#### `External`
**can't** be called by the same contract
#### `Public`
can be called by anyone
#### `Internal` (default)
can only be called by the other functions inside of the same contract, or in its derrived contract.
#### `Private`
only visible for the contract they are define in and not contracts

## View & Pure
Don't have to make transaction

### View
Read some state of blockchain
### Pure
Do some type of math

```solidity
function funcname() visibility view/pure returns(){
    
}
```

## Memory & Storage

### Memory
Data will only be stored during the execution of the function or if the contract call.
> After execution, delete this data.
### Storage
Data will persist even after the function executes.
> Keep it forever

## Payable (red button)
This kind of function can be used to pay for things.

## require(condition)
    stop contracts from executing unless some certain parameters are met.

## transfer()

    msg.sender.transfer(address(this).balance)
    transfer eth to mag.sender
    
## Modifier
    Is used to change the behavior of  a func in a declarative way.
    ex:
    modifier onlyOwner{
        require(msg.sender == owner)
        _;
    }
    Before you run a func, run require first.
    '_' means run rest of the code.
    
## for loop
for(;;)

## Deploy contract
1. Build the contract deploy transaction
2. Sign the transaction
3. Send the transaction 