智能合約編程(一)感受智能合約代碼
- 報(bào)價(jià)
- 請(qǐng)來(lái)電詢價(jià)
- 全國(guó)熱線
- 18638029017
- 微信號(hào)
- 18638029017
- 漫云科技
- app開發(fā)
- 漫云網(wǎng)絡(luò)
- 源碼交付
- app定制
- 售后一對(duì)一
這個(gè)系列是筆者自己學(xué)習(xí)***時(shí)的筆記,基于**新版的Solidity技術(shù)文檔(0.8.7)。
參考:
Solidity-Solidity0.8.7documentation
docs.soliditylang.org/en/latest/index.html
Solidity*新(0.8.0)中文文檔
learnblockchain.cn/docs/solidity/index.html
另外還有許多其他medium的技術(shù)博客以及相關(guān)書籍
《深入以太坊智能合約**》、《精通以太坊》、《智能合約安全分析和審計(jì)指南》
感受代碼
鑄幣代碼
pragmasolidity^0.4;
contractCoin{
https://setthe"address"typevariableminter
addresspublicminter;
/*convert"address"(forstoringaddressorkey)
tothetypeof"uint"whichisassubscripofobjectbalances*/
mapping(address=>uint)publicbalances;
https://setaneventsoastobeseenpublicly
eventSent(addressfrom,addressto,uintamount);
https://constructoronlyrunoncewhencreatingcontract,unabletoinvoke
https://"msg"istheaddressofcreator."msg.sender"is
constructor()public{
minter=msg.sender;
}
https://鑄幣
https://canonlybecalledbycreator
functionmint(addressreceiver,uintamount)public{
require(msg.sender==minter);
balances[receiver]+=amount;
}
https://轉(zhuǎn)賬
functionsend(addressreceiver,uintamount)public{
require(balances[msg.sender]>=amount);
balances[msg.sender]-=amount;
balances[receiver]+=amount;
emitSent(msg.sender,receiver,amount);
}
}
源文件結(jié)構(gòu)
版本標(biāo)識(shí)
pragma
版本標(biāo)識(shí),是pragmaticinformation的簡(jiǎn)稱,用于啟動(dòng)編譯器檢查,避免因?yàn)閟olidity更新后造成的不兼容和語(yǔ)法變動(dòng)的錯(cuò)誤。只對(duì)本文件有效,如果導(dǎo)入其他文件,版本標(biāo)識(shí)不會(huì)被導(dǎo)入,而是采用工作的文件自身的版本標(biāo)識(shí)
pragmasolidity^0.5.2;
這里^表示從0.5.2到0.6(不含)的版本
導(dǎo)入其他文件
語(yǔ)法如下
import"filename";
這種導(dǎo)入方式會(huì)把導(dǎo)入文件的所有全局符號(hào)都導(dǎo)入到工作文件的全局作用域,會(huì)污染命名空間,不建議這么使用。
import*assymbolNamefrom"filename";
https://等價(jià)于
import"filename"assymbolName;
這樣所有的全局符號(hào)都以symbolName.symbol的格式提供。
我們還可以設(shè)置別名,別名和重定義的符號(hào)名,都可以表示導(dǎo)入的文件里的全局符號(hào)。
import{symbol1asalias,symbol2}from"filename";
路徑
路徑的形式和Linux下的完全一致,但是要避免使用..。我們可以引入指定路徑的文件,如import"./filename"assimbolName,是當(dāng)前目錄下的文件。引用的文件除了本地文件,git,http發(fā)現(xiàn)的其他資源。
實(shí)際solc編譯器使用的時(shí)候可以指定路徑的重映射,編譯器可以從重映射的位置讀取文件。尤其是使用網(wǎng)絡(luò)文件的時(shí)候例如,可以使github.com/ethereum/dapp-bin/library會(huì)被重映射到/usr/local/dapp-bin/library,格式如下。
solcgithub.com/ethereum/dapp-bin/=/usr/local/dapp-bin/source.sol
更具體地會(huì)在solc編譯器地部分說(shuō)明。而truffle框架和remix就相對(duì)智能,可以通過(guò)網(wǎng)絡(luò)獲取文件。
注釋
單行注釋https://,多行注釋/*......*/
一種natspec注釋,他是用https:///或者/**......*/,它里面可以使用Doxygen樣式來(lái)給出相關(guān)地信息。
Doxygen樣式地注釋可以使特殊地注釋形式變得可識(shí)別,方便讀取和自動(dòng)提取信息。主要有
https://SPDX-License-Identifier:GPL-3.0
pragmasolidity>=0.4.21<0.9.0;
/**@titleShapecalculator.
*@file(文件名)
*@authorJohnDoe
*@version1.0(版本)
*@details(細(xì)節(jié))
*@date(年-月-日)
*@license(版權(quán)協(xié)議)
*@brief(類的簡(jiǎn)單概述)
*@sectionLICENSE*/(這一段的主要內(nèi)容)
*@paramDescriptionofmethod'sorfunction'sinputparameter(形式參數(shù)說(shuō)明)
*@returnDescriptionofthereturnvalue(返回說(shuō)明)
*@retval(返回值說(shuō)明)
*@attention(注意)
*@warning(警告)
*@var(變量聲明)
*@bug(代碼缺陷)
*@exception(異常)
contractShapeCalculator{
https:///@devCalculatesarectangle'ssurfaceandperimeter.
https:///@paramwWidthoftherectangle.
https:///@paramhHeightoftherectangle.
https:///@returnsThecalculatedsurface.
https:///@returnpThecalculatedperimeter.
functionrectangle(uintw,uinth)publicpurereturns(uints,uintp){
s=w*h;
p=2*(w+h);
}
}
合約的結(jié)構(gòu)
狀態(tài)變量
狀態(tài)變量是**地存儲(chǔ)在合約存儲(chǔ)中的值,它具有數(shù)據(jù)的類型,也有可見性的屬性。在函數(shù)外的都是store狀態(tài)變量。
pragmasolidity>=0.4.0<0.9.0;
contractTinyStorage{
uintstoredXlbData;https://狀態(tài)變量
https://...
}
函數(shù)
函數(shù)是代碼的可執(zhí)行單元。函數(shù)通常在合約內(nèi)部定義,但也可以在合約外定義。
https://SPDX-License-Identifier:GPL-3.0
pragmasolidity>0.7.0<0.9.0;
contractTinyAuction{
functionMybid()publicpayable{https://定義函數(shù)
https://...
}
}
https://Helperfunctiondefinedoutsideofacontract
functionhelper(uintx)purereturns(uint){
returnx*2;
}
函數(shù)調(diào)用可發(fā)生在合約內(nèi)部或外部,且函數(shù)對(duì)其他合約有不同程度的可見性(可見性和getter函數(shù))。
函數(shù)可以接受參數(shù)和返回值。
函數(shù)修飾
函數(shù)修飾符用來(lái)修飾函數(shù),比如添加函數(shù)執(zhí)行前必須的先決條件,
contractOwner{
modifieronlyOwner{
require(msg.sender==owner);
_;
}
modifiercosts(uintprice){
if(msg.value>=price){
_;
}
}
}
函數(shù)體會(huì)插入在修飾函數(shù)的下劃線_的位置。所以只有當(dāng)修飾條件滿足之后才能執(zhí)行這個(gè)函數(shù),否則報(bào)錯(cuò)。
注意下面的用法。實(shí)際上常常會(huì)被繼承,作為模塊化處理。
https://SPDX-License-Identifier:MIT
pragmasolidity^0.8;
contractTest{
uintpublica;
uintpublicb;
functionset(uint_a,uint_b)public{
a=_a;
b=_b;
}
modifierFunc(uint_a)
{
require(a>_a,"error:aissosmall.");
_;
}
functionf(uint_a)publicviewFunc(_a)returns(uint){
return_a;
}
}
事件
事件是能方便地調(diào)用以太坊虛擬機(jī)日志功能的接口,分為設(shè)置事件和觸發(fā)事件
pragmasolidity>=0.4.21<0.9.0;
contractTinyAuction{
eventHighestBidIncreased(addressbidder,uintamount);https://事件
functionbid()publicpayable{
https://...
emitHighestBidIncreased(msg.sender,msg.value);https://觸發(fā)事件
}
}
結(jié)構(gòu)體
pragmasolidity>=0.4.0<0.9.0;
contractTinyBallot{
structVoter{https://結(jié)構(gòu)體
uintweight;
boolvoted;
addressdelegate;
uintvote;
}
}
枚舉類型
pragmasolidity>=0.4.0<0.9.0;
contractUpchain{
enumState{Created,Locked,InValid}https://枚舉
智能合約編程(一)感受智能合約代碼
鏈游系統(tǒng)開發(fā),app開發(fā),系統(tǒng)開發(fā),盲盒開發(fā),盲盒源碼
軟件開發(fā),技術(shù)咨詢,技術(shù)服務(wù),
深圳漫云網(wǎng)絡(luò)科技有限公司,是一家專注于高端APP定制開發(fā)服務(wù)和微信開發(fā)的服務(wù)機(jī)構(gòu),致力于為企業(yè)提供全面、系統(tǒng)的APP開發(fā)制作方案。在手機(jī)APP開發(fā)、做出來(lái)的系統(tǒng)從運(yùn)營(yíng)到推廣領(lǐng)域都擁有豐富經(jīng)驗(yàn),我們通過(guò)建立對(duì)目標(biāo)客戶和用戶行為的 分析,整合高質(zhì)量設(shè)計(jì)和超強(qiáng)的技術(shù),為您打造創(chuàng)意十足、有價(jià)值的企業(yè)品牌APP。 我們擁有10年以上行業(yè)經(jīng)驗(yàn)、百人的資深A(yù)PP開發(fā)技術(shù)團(tuán)隊(duì),我們已經(jīng)幫助眾多知名客戶提升他們的品牌和客戶關(guān)系,服務(wù)領(lǐng)域涉及企業(yè)集...