blockchain - !!! solidity基础语法 basic 生成abi 看这个就够了,

访问量: 259

文档在这里:https://www.tutorialspoint.com/solidity/solidity_overview.htm

安装

npm install -g solc

查看 版本

$ solcjs --version
0.8.11+commit.d7f03943.Emscripten.clang

在本地构建 hardhat  , 使用方法看这里: https://siwei.me/blog/posts/blockchain-hardhat-eth

编译第一个sol 文件

pragma solidity ^0.8.0;

contract SayHi {
  uint256 number;
  function store(uint256 num) public {
    number = num;
  }

  function get() public view returns (uint256){
    return number;
  }
}

具体用法见hardhat 的编译和运行方式 

Types

Boolean, Integer,  Fixed Point Numbers

这里要记得:solidity会对变量做个区分:

int  // 占用地址确定

int[]  // 可变数组,占用的地址或者空间是可变的

byte  "asdfsadf" 长度是变化的

上面 变化的内容,在 生成abi data 并进行 eth_sendRawTransaction时,会作为data参数调用,这些变化的内容是需要加以指定的

Variable

state variable

local variable

global variable

Scope: 

public , interval, private

字符串,数组, Mapping

string data = "test";

uint balance[10];

mapping(address => uint) public balances;

特殊变量

https://www.tutorialspoint.com/solidity/solidity_special_variables.htm

特殊方法

receive() 该方法被定义的时候前面不能增加function

全局变量(特别重要) 可以拿来就用

refer to: https://docs.soliditylang.org/en/latest/units-and-global-variables.html#special-variables-and-functions

全局变量表示了某次转账中的典型信息,特别重要


    blockhash(uint blockNumber) returns (bytes32): hash of the given block when blocknumber is one of the 256 most recent blocks; otherwise returns zero

    block.basefee (uint): current block’s base fee (EIP-3198 and EIP-1559)

    block.chainid (uint): current chain id

    block.coinbase (address payable): 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 as seconds since unix epoch

    gasleft() returns (uint256): remaining gas

    msg.data (bytes calldata): complete calldata   重要。 转账的附言

    msg.sender (address): sender of the message (current call)   发送者地址

    msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)   

    msg.value (uint): number of wei sent with the message   转账的数量 (主链币)

    tx.gasprice (uint): gas price of the transaction   

    tx.origin (address): sender of the transaction (full call chain)

functions

modifier : 某个function的 before hook (个人认为)

view : 用来读取状态的函数  (读取链上信息)  对应abi的stateMutability 也是view

pure: 不read, 也不write state  (不读写链上信息)  在 abi的stateMutability   也是pure

在stateMutability 还有 nonPayable 和 payable, 对应的就是修改链上数据的方法了。

payable  表示该函数负责收款

nonPayable

Contract

https://www.tutorialspoint.com/solidity/solidity_contracts.htm

对于一些方法的scope

public - all can access
external - Cannot be accessed internally, only externally
internal - only this contract and contracts deriving from it can access
private - can be accessed only from this contract

virtual: 允许被继承后实现的

override: 跟java中的概念一样, 覆盖.

生成ABI

订阅/RSS Feed

Subscribe