全局变量(Global Variables)
abi.encode(...) returns (bytes):对给定的参数进行ABI编码。abi.encodePacked(...) returns (bytes): Performes packed encoding of the given argumentsabi.encodeWithSelector(bytes4 selector, ...) returns (bytes)::对给定的参数进行ABI编码——从第二个预置给定的四字节选择器开始abi.encodeWithSignature(string signature, ...) returns (bytes):相当于abi.encodeWithSelector(bytes4(keccak256(signature), ...)block.blockhash(uint blockNumber) returns (bytes32): 给定的块的hash值, 只有最近工作的256个块的hash值—— 在 0.4.22 后请使用blockhash(uint blockNumber).block.coinbase(address): 当前块的矿工的地址block.difficulty(uint): 当前块的难度block.gaslimit(uint): 当前块的gaslimitblock.number(uint):当前块的数量block.timestamp(uint):当前块的时间戳gasleft() returns (uint256): 剩余 gasmsg.data(bytes): 完整的calldatamsg.gas(uint): 剩余 gas - 0.4.21后请使用gasleft()msg.sender(address): 消息的发送者(当前调用)msg.value(uint): 和消息一起发送的wei的数量now(uint): 当前块的时间戳(block.timestamp的别名)tx.gasprice(uint):交易的gas价格tx.origin(address):交易的发送者(全调用链)assert(bool condition): abort execution and revert state changes if condition isfalse(用于内部错误)require(bool condition): abort execution and revert state changes if condition isfalse(用于输入错误或外部组件的错误)require(bool condition, string message): abort execution and revert state changes if condition isfalse(用于输入错误或外部组件的错误). 并提供错误信息.revert(): 中止执行并还原状态更改revert(string message):中止执行并还原状态更改,提供解释字符串blockhash(uint blockNumber) returns (bytes32): : 给定的块的hash值, 只有最近工作的256个块的hash值keccak256(...) returns (bytes32):计算(紧凑排列的)参数的 Ethereum-SHA3 hash值sha3(...) returns (bytes32): an alias tokeccak256sha256(...) returns (bytes32): 计算(紧凑排列的)参数的SHA256 hash值ripemd160(...) returns (bytes20):计算 256个(紧凑排列的)参数的RIPEMDecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): 椭圆曲线签名公钥恢复,错误时返回0addmod(uint x, uint y, uint k) returns (uint): compute(x + y) % kwhere the addition is performed with arbitrary precision and does not wrap around at2**256. Assert thatk != 0starting from version 0.5.0.mulmod(uint x, uint y, uint k) returns (uint): compute(x * y) % kwhere the multiplication is performed with arbitrary precision and does not wrap around at2**256. Assert thatk != 0starting from version 0.5.0.this(current contract’s type): 当前合约,在地址上显式转换super: 在层次关系上一层的合约selfdestruct(address recipient): 销毁当前的合约,将其资金发送到指定addresssuicide(address recipient): a deprecated alias toselfdestruct<address>.balance(uint256): address地址中的账户余额(以wei为单位)<address>.send(uint256 amount) returns (bool): 将一定量wei发送给address地址,若失败返回false。<address>.transfer(uint256 amount): 将一定量wei发送给address地址,若失败抛出异常。
| 注解 |
|---|
不要依赖于block.timestamp,now和blockhash用作随机性的来源,除非你知道你在做什么。 |
| 时间戳和块哈希在一定程度上受矿工的影响。例如,挖掘社区中的坏角色可以在选定的散列上运行赌场支付函数,如果他们没有收到任何钱,只需重试一个不同的散列。 |
| 当前块时间戳必须严格大于最后一个块的时间戳,但唯一的保证是它将位于规范链中的两个连续块的时间戳之间的某处。 |
| 注解 |
|---|
| 由于区块链是变化的,所以块哈希对于全部区块块不可用。只能访问最近256个块的散列值,所有其他区块哈希将为0。 |

本文详细介绍了Solidity语言中用于智能合约开发的关键函数,包括全局变量的使用方法、消息与事务的操作、数学运算及安全性相关的函数。此外,还探讨了如何正确使用这些函数以避免常见的安全陷阱。
6752

被折叠的 条评论
为什么被折叠?



