Solidity 学习笔记
通过学习Solidity
,然后输出文章检验自己的学习成果Github仓库
欢迎大家关注我的X
Solidity
中有3
种类型的变量
blockhash(uint blockNumber) returns (bytes32)
:指定区块的哈希值–仅可用于最新的256
个区块且不包括当前区块,否则返回0。block.basefee (uint)
:当前区块的基础费用(EIP-3198and EIP-1559)block.chainid (uint)
:当前链id
block.coinbase (address payable)
:挖出当前区块矿工的地址block.difficulty (uint)
:当前区块难度,在evm paris
升级后,该值不再准确,使用block.prevrandao
代替。(EIP-4399)block.prevrandao (uint)
:信标链提供的随机数,在evm paris
升级后,使用该值代替block.difficulty
block.gaslimit (uint)
:当前区块的gas限制block.number (uint)
:当前区块号block.timestamp (uint)
:当前区块的时间戳,自unix纪元以来的秒数gasleft() returns (uint256)
:剩余的gasmsg.data (bytes)
:完整的calldata
msg.sender (address)
:消息发送者 (当前调用者)msg.sig (bytes4)
:calldata
的前四个字节 (也就是函数标识符)msg.value (uint)
:当前交易发送的wei
的数量tx.gasprice (uint)
:交易的gas价格tx.origin (address)
:交易发起者(完全的调用链)该例子是局部变量、状态变量和全局变量使用的例子
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Variables {
string public text = "hello,fyy";
uint public num = 1118;
uint public gasLeft;
address public origin;
uint public timestamp;
function doSomething() public {
uint i = 1114;
text = "hello,xdp";
gasLeft = gasleft();
origin = tx.origin;
timestamp = block.timestamp;
}
function blockHash() view public returns (bytes32) {
return blockhash(block.number - 1);
}
function getBaseFee() view public returns (uint) {
return block.basefee;
}
function getChainID() view public returns (uint) {
return block.chainid;
}
function getCoinBase() view public returns (address) {
return block.coinbase;
}
function getDifficulty() view public returns (uint) {
return block.difficulty;
}
function getPrevRandao() view public returns (uint) {
return block.prevrandao;
}
function getGasLimit() view public returns (uint) {
return block.gaslimit;
}
}
string public text = "hello,fyy";
uint public num = 1118;
uint public gasLeft;
address public origin;
uint public timestamp;
gas
uint i = 1114;
gas
text = "hello,xdp";
gasLeft = gasleft();
origin = tx.origin;
timestamp = block.timestamp;
gasLeft = gasleft();
origin = tx.origin;
timestamp = block.timestamp;
function blockHash() view public returns (bytes32) {
return blockhash(block.number - 1);
}
function getBaseFee() view public returns (uint) {
return block.basefee;
}
function getChainID() view public returns (uint) {
return block.chainid;
}
function getCoinBase() view public returns (address) {
return block.coinbase;
}
function getDifficulty() view public returns (uint) {
return block.difficulty;
}
function getPrevRandao() view public returns (uint) {
return block.prevrandao;
}
function getGasLimit() view public returns (uint) {
return block.gaslimit;
}