知乎专栏 | 多维度架构 |
address public minter; string name; int num; uint constant x = 32**22 + 8; string constant text = "abc"; bytes32 constant myHash = keccak256("abc"); uint256 ticket = 1 ether;
变量赋值
pragma solidity ^0.4.25; contract C { uint[] data; function f() public view returns (uint, bool, uint) { return (7, true, 2); } function g() public { // 声明和分配变量。 明确指定类型是不可能的。 var (x, b, y) = f(); // 分配给一个预先存在的变量。 (x, y) = (2, 7); // 互换值的常用技巧对于非价值存储类型不起作用。 (x, y) = (y, x); // 组件可以省略(也可以用于变量声明)。 // 如果元组以空组件结束,其余的值将被丢弃。 (data.length,) = f(); // 设置长度为 7 // 在左边也可以做同样的事情。 (,data[3]) = f(); // Sets data[3] to 2 // 组件只能在作业的左侧排除,但有一个例外: (x,) = (1,); // (1,) 是指定1元组元的唯一方法,因为(1)等于1。 } }
block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most recent blocks block.coinbase (address): current block miner’s address block.difficulty (uint): current block difficulty block.gaslimit (uint): current block gaslimit block.number (uint): current block number block.timestamp (uint): current block timestamp msg.data (bytes): complete calldata msg.gas (uint): remaining gas msg.sender (address): sender of the message (current call) msg.value (uint): number of wei sent with the message now (uint): current block timestamp (alias for block.timestamp) tx.gasprice (uint): gas price of the transaction 6.4. Solidity in Depth 99Solidity Documentation, 0.4.10 tx.origin (address): sender of the transaction (full call chain) revert(): abort execution and revert state changes keccak256(...) returns (bytes32): compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) arguments sha3(...) returns (bytes32): an alias to keccak256() sha256(...) returns (bytes32): compute the SHA-256 hash of the (tightly packed) arguments ripemd160(...) returns (bytes20): compute the RIPEMD-160 hash of the (tightly packed) arguments ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): recover address associated with the public key from elliptic curve signature, return zero on error addmod(uint x, uint y, uint k) returns (uint): compute (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2**256 mulmod(uint x, uint y, uint k) returns (uint): compute (x * y) % k where the multiplication is performed with arbitrary precision and does not wrap around at 2**256 this (current contract’s type): the current contract, explicitly convertible to address super: the contract one level higher in the inheritance hierarchy selfdestruct(address recipient): destroy the current contract, sending its funds to the given address .balance (uint256): balance of the Address in Wei .send(uint256 amount) returns (bool): send given amount of Wei to Address, returns false on failure .transfer(uint256 amount): send given amount of Wei to Address, throws on failure
block.blockhash(uint blockNumber) returns (bytes32): 某个区块的区块链hash值
block.coinbase (address): 当前区块的挖矿地址
block.difficulty (uint): 当前区块的难度
block.gaslimit (uint): 当前区块的gaslimit
block.number (uint): 当前区块编号
block.timestamp (uint): 当前区块时间戳
msg.data (bytes): 参数数据
msg.gas (uint): 剩余的gas
msg.sender (address): 当前发送消息的地址,执行合约的地址。
msg.sig (bytes4): 方法ID
msg.value (uint): 执行合约时,转账的eth数量,以wei为单位。
now (uint): 时间戳,等价于block.timestamp (uint)
tx.gasprice (uint): 交易的gas单价
tx.origin (address):交易发送地址