Solidity-Learning

Solidity 学习笔记

View the Project on GitHub XdpCs/Solidity-Learning

004-变量

背景

通过学习Solidity,然后输出文章检验自己的学习成果Github仓库

欢迎大家关注我的X

基础知识

局部变量

状态变量

全局变量

例子

例子

该例子是局部变量、状态变量和全局变量使用的例子

// 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;
uint i = 1114; 
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;
}

链接