什么是智能合约
从技术角度来说,智能合约可以看作一种计算机程序,这种程序可以自主地执行全部或部分和合约相关的操作,并产生相应的可以被验证的证据,来说明执行合约操作的有效性。
Web3.0的底层技术是分布式账本技术和分布式数据库技术,这就好比操作系统里的文档系统(Filing)和I/O(输出入系统),也像是区块链里的Layer-1数据处理结构。分布式存储就像是操作系统里的文档系统,分布式计算就像是操作系统里的CPU(中央处理器),分布式数据传输(分布式通信)也就好比I/O。CPU、文档系统和I/O都是操作系统的基本要素,类比到Web3.0的底层技术亦是如此。
What is a smart contract
From a technical point of view, a smart contract can be seen as a computer program that can independently perform all or part of contract-related operations, and generate corresponding verifiable evidence to illustrate the effectiveness of contract operations.
pragma solidity ^0.8.0;
contract SimpleStorage {
uint public storedData;
address public owner=
uint256 public money;
address public thisAddress=address(this);
address public thisAddress2=msg.sender;
uint public etherTest=1 gwei;
mapping(address => uint256) public balances;
event Set(uint x);
enum State { Created, Locked, InValid }
State public state;
modifier onlyOwner(){
require(owner == msg.sender,"only owner can call this function");
_;
}
struct Voter { // 结构体
uint weight;
bool voted;
}
Voter[] public voter;
function set(uint x) public onlyOwner {
storedData = x; //
emit Set(storedData);
}
function setState(State _state) public{
state=_state;
}
function f(uint len) public pure {
uint[] memory a = new uint[](7);
bytes memory b = new bytes(len);
assert(a.length == 7);
assert(b.length == len);
a[6] = 8;
}
function setVoter( uint _weight,bool _voted) public {
voter.push(Voter(_weight,_voted));
}
function bal() public view returns(uint){//返回余额
return msg.sender.balance;
}
function testPure(uint x) public pure returns(uint){//纯
return x*2;
}
function buy() public payable{
assert(msg.value>0);//必须大于0,不然不执行下面的
money+=msg.value;
}
function mint(address account,uint256 amount) public onlyOwner{//铸币,当前账号才有权限
balances[account] += amount;
}
}