通過學習,自己模擬仿寫了一個眾籌的智能合約代碼。內容還不夠完善,只完成了投資和取款操作。
//SPDX-License-Identifier: SimPL-2.0pragma solidity >=0.7.0 <0.8.9;contract zhongchou{ //投資者投資記錄:投資目標,投資金額 struct toMoney{ address payable addressReceiptor; uint money; } //投資者基本信息:地址,是否被激活,總投資金額,投資次數,映射記錄投資記錄 struct funder{ address payable addressfunder; bool isActive; uint totalMoney; uint numberGive; mapping(uint=>toMoney)expMap; } //眾籌合約:合約創建者,是否被激活,金額總需求,已投資金額,投資人數量,映射記錄投資人 struct needMoneyContract{ address payable addressNeeder; // payable address addressContract; bool isActive; uint totalMoney; uint giveMoney; uint amountFunder; mapping (uint=>funder)mapFunder; } //眾籌發起者:地址,激活狀態,需求總金額,已經被投資的金額,發起的眾籌的數量,映射記錄投資合約 struct needer{ address addressNeeder; bool isActive; uint amountMoneyNeed; uint amountHasFunded; uint numberContract; mapping(uint=>needMoneyContract)expMap; } //記錄眾籌合約總數,合約地址(資金池地址) uint amountContract; address payable public addressFinance; //三方數組 mapping(address=>funder)funderMap; mapping(uint=>needMoneyContract)contractMap; mapping(address=>needer)neederMap; constructor(){ addressFinance=payable(msg.sender); } //創建一個眾籌發起者 function createNeeder()public returns(bool){ //需要判定是否已經被激活 if(neederMap[msg.sender].isActive){ return false; } else{ address _addressNeeder=msg.sender; //0.8.0后不允許直接創建一個包含映射的結構體。需要通過引用的方式,先創建一個storage類型的結構體(與目標是引用關系),再對新變量進行操作即可。 needer storage tmp1=neederMap[_addressNeeder]; tmp1.addressNeeder=_addressNeeder; tmp1.isActive=true; tmp1.amountMoneyNeed=0; tmp1.amountHasFunded=0; tmp1.numberContract=0; return true; } } function createContract( uint _amountMoneyNeed )public returns(bool){ address _addressNeeder=msg.sender; uint tmpNum=amountContract++; needMoneyContract storage tmp2=contractMap[tmpNum]; tmp2.addressNeeder=payable(_addressNeeder); tmp2.isActive=true; tmp2.totalMoney=_amountMoneyNeed; tmp2.giveMoney=0; tmp2.amountFunder=0; uint tmpContract=neederMap[_addressNeeder].numberContract++; neederMap[_addressNeeder].amountMoneyNeed+=_amountMoneyNeed; needMoneyContract storage tmp3=neederMap[_addressNeeder].expMap[tmpContract]; needMoneyContract storage tmp4=contractMap[tmpNum]; tmp3=tmp4; return true; } function createFunder()public returns(bool){ if(funderMap[msg.sender].isActive){ return false; } else{ address _address=msg.sender; funder storage tmpfund=funderMap[_address]; tmpfund.addressfunder=payable(_address); tmpfund.isActive=true; tmpfund.totalMoney=0; tmpfund.numberGive=0; return true; } } function donateMoney( uint money, uint idContract, address addressNeeder ) public payable returns(bool){ require(contractMap[idContract].isActive==true); require(money==msg.value); require(contractMap[idContract].addressNeeder==addressNeeder); // payable address adressDonate=msg.sender; address tmpfunder=msg.sender; funderMap[tmpfunder].totalMoney+=money; toMoney storage tmpMoney=funderMap[tmpfunder].expMap[funderMap[tmpfunder].numberGive]; tmpMoney.addressReceiptor=payable(addressNeeder); tmpMoney.money=money; funderMap[tmpfunder].numberGive++; contractMap[idContract].giveMoney+=money; funder storage tmpfund1=contractMap[idContract].mapFunder[contractMap[idContract].amountFunder++]; funder storage tmpfund2=funderMap[tmpfunder]; tmpfund1=tmpfund2; return true; } function isComplete(uint idContract)public payable returns(bool){ require(contractMap[idContract].isActive==true); require(contractMap[idContract].addressNeeder==msg.sender); require(contractMap[idContract].totalMoney<=contractMap[idContract].giveMoney); needMoneyContract storage tmptrans=contractMap[idContract]; tmptrans.isActive=false; address tmpaddr=msg.sender; uint getMoney=tmptrans.giveMoney; neederMap[tmpaddr].amountHasFunded+=getMoney; tmptrans.addressNeeder.transfer(getMoney); return true; }}
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
k8指的是什么意思?所謂K8就是KB,是漢語拼音的縮寫。全拼是昆邦,意思是中文裝訂。在許多論壇中,綁定通常是一個敏感詞,因此使用K8代替。k8和k9區別?一般來說,K8的管子比K9的細。2. K8、K9為管道壁厚等級,壁厚e=K(0.5+0.001dn)。式中:DN為管道的公稱直徑,單位為mm;K為壁厚等級,如K=8、9、10…]~公用.dll下載;2。點擊網站下載K8公用.dll壓縮包文件;3。...
安正男裝什么價位 安正男裝屬于什么檔次?安正男裝屬于什么檔次? 安正是安正時尚集團推出的高端男裝品牌,致力于滿足都市精英男士的時尚商務需求。該品牌成立于2010年,總部位于上海,其品牌包括尹默、九子、MOISSAC摩薩克和FIONACHEN斐娜晨。安正男裝詮釋了城市現代時尚紳士的形象,以堅持、真實、挑戰的生活理念,通過時尚精神的塑造和精湛的技術表達,使外部形象和精神狀態充滿活力和青春感。通過富...
索尼各個機型發布時間?1.索尼愛立信T682001年10月推出這是索尼愛立信在2001年推出的第一款手機。雖然在當時是一款功能手機,但是憑借著不錯的外形和內置的一些娛樂功能,在市場上引起了極大的關注。然后在2002年6月,索尼推出了升級版T68i。這也成為索尼愛立信未來的一個策略。特別是索尼愛立信T68是一款外接數碼相機的手機,支持彩信。2.索尼愛立信P8002003年1月推出一年后,索尼愛立信推...