function (param types) {
internal|external} [pure|constant|view|payable][returns (return types)] varName;
function f(<parameter types>) {
internal|external} [pure|constant|view|payable][returns (<return types>)]{
// function body}
示例:
contract SimpleFunc {
function hello(uint i) {
// todo
}
}
属性介绍:
Solidity的异常处理:
assert/require:
contract OwnedToken{
// TokenCreator是一个合约类型
// 未初始化前,为一个引用
TokenCreator creator;
address owner; // 状态变量
bytes32 name; // 状态变量
// 构造函数
function OwnedToken(bytes32 _name) public{
owner = msg.sender;
creator = TokenCreator(msg.sender);// 另外一个合约
name = _name;
}
}
contact TokenCreator{
function createToken(bytes32 name) public returns (OwnedToken){
// 创建一个新的合约,name为构造函数所需变量
OwnedToken tokenAddress = new OwnedToken(name);
return tokenAddress;
}
}
contract Owned {
function owned() {
owner = msg.sender; }
address owner;
}
// 使用`is`来继承另外一个合约
// 子合约可以使用所有的非私有变量,包括内部函数和状态变量
contract Mortal is Owned {
function kill() {
if (msg.sender == owner) selfdestruct(owner);
}
}
contact Feline{
// 函数没有函数实现,为抽象函数,对应的合约为抽象合约
function utterance() returns (bytes32);
function getContractName() returns (string) {
return "Feline";
}
}
contract Cat is Feline{
// 继承抽象合约,实现函数功能
function utterance() returns (bytes32) {
return "miaow";}
}
// 接口
interface Token {
function transfer(address recipient, uint amount);
}
// 接口可以被
contract MyToken is Token {
function transfer(address recipient, uint amount){
// 函数发现
}
}
library Set{
struct Data {
mapping(uint => bool) flags; }
// 第一个参数的类型为“storage reference”, 仅存储地址,而不是这个库的特别特征
// 按照一般语言的惯例,`self`代表第一个参数
function insert(Data storage self, uint value)
public
return (bool)
{
if (self.flags[value])
return false; //already there
self.flags[value] = true;
return true;
}
}
contract C{
Set.Data knownValues;
function register(uint value) public{
// 库可以直接调用,而无需使用this
requires(Set.insert(knownValues, value));
}
}
pragma solidity >=0.6.4;
contract MyCoin{
//数据结构
//1.用户
address[] userList;
mapping(address=>uint8) userDict;
//2.用户的币
mapping(address=>uint) balances;
//3.用户交易,第一个address为交易发起方,第二个为接收方,uint[]为交易量记录
mapping(address => mapping(address=>uint[])) public trans;
//用户初次可领取的数量
uint iniCount = 100;
function Mycoin() public{
//合约本身拥有的币
balances[address(this)] = 100000;
}
//领取货币,只能一次
function getCoin() public returns (bool sufficientAndFirstGet) {
//判断合约是否钱足够
if(balances[address(this)]<iniCount) return false;
//判断是否从合约领取过币
if(0!=trans[address(this)][msg.sender].length) return false;
//领取币
balances[address(this)] -= iniCount;
balances[msg.sender] += iniCount;
//记录交易
trans[address(this)][msg.sender].push(iniCount);
//加入用户列表
if(0 == userDict[msg.sender]){
userList.push(msg.sender);
userDict[msg.sender] = 1;
}
return true;
}
//发送货币
function sendCoin(address receiver, uint amount) public returns(bool sufficient){
//判断是否还有足够的币
if(balances[address(this)]<amount) return false;
//发生交易
balances[msg.sender] -= amount;
balances[receiver] += amount;
//记录交易
trans[msg.sender][receiver].push(amount);
//加入用户列表
if (0 == userDict[receiver]) {
userList.push(receiver);
userDict[receiver] = 1;
}
return true;
}
//获得某个用户的货币量
function getBalances(address addr) public returns(uint){
return balances[addr];
}
//获得领取的进度
function getPercent() public returns(uint){
uint sum = 0;
for(uint i = 0;i < userList.length; i++){
address userAddress = userList[i]; //用户的地址
sum = sum + balances[userAddress];
}
return (100*sum)/100000;
}
//获得币最多的用户地址
function getBest() public returns (address){
address maxAdd;
uint maxCoin = 0;
//获得币最多的用户地址
for(uint i = 0; i < userList.length; i++){
address userAddress = userList[i]; //用户地址
uint userCoin = balances[userAddress]; //用户积分
if(userCoin > maxCoin){
maxAdd = userAddress;
maxCoin = userCoin;
}
}
return maxAdd;
}
}
pragma solidity >=0.6.4;
import "./MyCoin.sol";
contract MyBid is MyCoin{
//数据结构
string private ticket;//电商兑换码
address owner;//合约创建者
//时间是unix的绝对时间戳
uint public auctionEnd;
// 拍卖的当前状态
address public highestBidder; //最高出价用户的地址
uint public highestBid; //最高出价
//出价列表
mapping (address => uint) bids;
// 拍卖结束后设为 true,将禁止所有的变更
bool ended;
//事件
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
//dev 创建一个简单的拍卖
//_biddingTime,拍卖时间;_ticket,兑换码
constructor (uint _biddingTime, string memory _ticket) public{
ticket = _ticket;
auctionEnd = block.timestamp + _biddingTime;
owner = msg.sender;
}
// 竞价,value为加价,和之前的出价叠加
function bid(uint value) public {
// 如果拍卖已结束,撤销函数的调用。
require(block.timestamp < auctionEnd);
// 如果出价不够高,不继续执行
uint actualValue = bids[msg.sender] + value;
require(actualValue > highestBid);
//把积分发送到合约地址,已经检查额度
sendCoin(address(this),value);
//更新出价和最高出价
bids[msg.sender] = actualValue;
highestBidder = msg.sender;
highestBid = actualValue;
emit HighestBidIncreased(msg.sender, actualValue);
}
// 结束拍卖,并把最高的出价发送给受益人
function auctionEnded() public payable returns (string memory){
// 1. 条件
require(now >= auctionEnd,"Not End!"); // 拍卖尚未结束
require(!ended); // 该函数已被调用
//必须是最高出价用户调用,或者管理员强制终止
require(msg.sender == highestBidder || msg.sender == owner);
// 2. 生效
ended = true;
emit AuctionEnded(highestBidder, highestBid);
// 3. 返回ticket,只能查看一次
return ticket;
}
// `_;` 表示修饰符,可代表被修饰函数位置
modifier onlyOwner {
require(msg.sender == owner);
_;
}
//开始新的拍卖
function newBid(uint _biddingTime, string memory _ticket) public onlyOwner {
// 1. 条件
require(now >= auctionEnd);
require(ended);
//重置数据
ticket = _ticket;
auctionEnd = now + _biddingTime;
ended = false;
delete highestBidder; //重置最高出价用户的地址
delete highestBid; //重置最高出价
//delete bids[msg.sender];
}
}
注意:可能由于版本问题存在一些错误,如有参考,请及时更正!