# solidity 基础 **Published by:** [leefbiant.eth](https://paragraph.com/@leefbiant/) **Published on:** 2022-05-08 **URL:** https://paragraph.com/@leefbiant/solidity ## Content 数据类型: solidity 像其他语言支持大部分基础数据 基础类型 int uint string bool , 不支持浮点类型。 1 address 地址类型, 普通地址和 payable 类型的地址,payable类型支持接收主币 2 bytes~bytes32 ,32字节以内的建议使用 bytes 3 Arrays数据类型,支持基础数据和自定义数据。特殊之处 new memory 数组需指定大小,不能push。 4 mapping(key => value), key 必须时是类型,不支持结构体,mapping。 数据存储: 1 storage 键值形式存储,永久存储 2 memory 执行合约时用到的临时存储. 3 calldata 合约调用时传入的数据,不可修改。外部调用的参数需要calldata 注: 在storage和 memory之间 复制将创建一个副本。 storage 和 storage 直接复制只创建一个引用。 memory 和 memory 直接复制只创建一个引用。 immutable 不可变量, 合约构造的时候初始化 constant常量,编译阶段确定 全局变量: block.blockhash(uint blockNumber) returns (bytes32):指定区块的区块哈希——仅可用于最新的 256 个区块且不包括当前区块;而 blocks 从 0.4.22 版本开始已经不推荐使用,由 blockhash(uint blockNumber) 代替 block.coinbase (address): 挖出当前区块的矿工地址 block.difficulty (uint): 当前区块难度 block.gaslimit (uint): 当前区块 gas 限额 block.number (uint): 当前区块号 block.timestamp (uint): 自 unix epoch 起始当前区块以秒计的时间戳 msg.data (bytes): 完整的 calldata msg.gas (uint): 剩余 gas - 自 0.4.21 版本开始已经不推荐使用,由 gesleft() 代替 msg.sender (address): 消息发送者(当前调用) msg.sig (bytes4): calldata 的前 4 字节(也就是函数标识符) msg.value (uint): 随消息发送的 wei 的数量 tx.gasprice (uint): 交易的 gas 价格 tx.origin (address): 交易发起者(完全的调用链) now (uint): 目前区块时间戳(block.timestamp) gasleft() returns (uint256):剩余的 gas 函数可见效: Private(私有):限制性最强,函数只能在所定义的智能合约内部调用。 Internal(内部):可以在所定义智能合约内部调用该函数,也可以从继承合约中调用该函数。 External(外部):只能从智能合约外部调用。 (如果要从智能合约中调用它,则必须使用 this。) Public(公开):可以从任何地方调用。 函数状态可变性: view:用view声明的函数只能读取状态,而不能修改状态。 pure:用pure声明的函数既不能读取也不能修改状态。 payable:用payable声明的函数可以接受发送给合约的主币,如果未指定,该函数将自动拒绝所有发送给它的主币。 函数修饰器: 当你要在执行函数之前检查某些条件时,可以使用修饰器 onwer检查 modifier onlyOwner(){ require(msg.sender == owner, "must be owner); _; } 原子锁 modifier tylock() { require(!lock); lock = true; _; lock = false; } ## Publication Information - [leefbiant.eth](https://paragraph.com/@leefbiant/): Publication homepage - [All Posts](https://paragraph.com/@leefbiant/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@leefbiant): Subscribe to updates - [Twitter](https://twitter.com/leef_biant): Follow on Twitter