区块链学堂(3):Solidity
生活随笔
收集整理的這篇文章主要介紹了
区块链学堂(3):Solidity
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Solidity
在上一篇文章中,我們可以看到?pragma solidity 0.4.9;,
這里的Solidity,就是以太坊智能合約的核心語言Solidity,也是本教程的重點。
Solidity是什么?
Solidity是以太坊智能合約的編程語言,通過編譯&部署智能合約,可以實現智能合約的Create、執行和查看,從而實現某些商業應用。
幾個簡單的Solidity例子
通過以下幾個智能合約,我們可以將一些商業應用很好的區塊鏈化,從而實現去中介、去信任、高度透明的商業模型。
在之后的整個教程中,我們會逐步解析Solidity編程,幫助大家快速掌握Solidity這門語言,并且將區塊鏈落地到前端Web頁面上
I 實現1+2+3+..+n的求和功能
pragma solidity 0.4.9; contract Demo1 {/*計算從1到N的求和*/function f(uint n) returns (uint sum) {if (n == 0) throw; uint result = 0;for (uint i=0; i<=n; i++) {result +=i;}return result;} }II 實現一個代幣功能,并自帶挖礦和轉移代幣的功能。
pragma solidity ^0.4.0;contract Coin {// The keyword "public" makes those variables// readable from outside.address public minter;mapping (address => uint) public balances;// Events allow light clients to react on// changes efficiently.event Sent(address from, address to, uint amount);// This is the constructor whose code is// run only when the contract is created.function Coin() {minter = msg.sender;}function mint(address receiver, uint amount) {if (msg.sender != minter) return;balances[receiver] += amount;}function send(address receiver, uint amount) {if (balances[msg.sender] < amount) return;balances[msg.sender] -= amount;balances[receiver] += amount;Sent(msg.sender, receiver, amount);} }III 實現一個眾籌的智能合約,各個用戶可以籌款、籌款成功可以將所得轉讓給受益人,每個參與眾籌者可以獲得代幣。
pragma solidity ^0.4.2; contract token { function transfer(address receiver, uint amount){ } }contract Crowdsale4 {address public beneficiary;uint public fundingGoal;uint public amountRaised;uint public deadline;uint public price;token public tokenReward;mapping(address => uint256) public balanceOf;bool public fundingGoalReached = false;event GoalReached(address beneficiary, uint amountRaised);event FundTransfer(address backer, uint amount, bool isContribution);bool public crowdsaleClosed = false;/* data structure to hold information about campaign contributors *//* at initialization, setup the owner */function Crowdsale4 (address ifSuccessfulSendTo,uint fundingGoalInEthers,uint durationInMinutes,uint etherCostOfEachToken,token addressOfTokenUsedAsReward) {beneficiary = ifSuccessfulSendTo;fundingGoal = fundingGoalInEthers * 1 ether;deadline = now + durationInMinutes * 1 minutes;price = etherCostOfEachToken * 1 ether;tokenReward = token(addressOfTokenUsedAsReward);}/* The function without name is the default function that is called whenever anyone sends funds to a contract */function () payable {if (crowdsaleClosed) throw;uint amount = msg.value;balanceOf[msg.sender] += amount;amountRaised += amount;tokenReward.transfer(msg.sender, amount / price);FundTransfer(msg.sender, amount, true);}modifier afterDeadline() { if (now >= deadline) _; }/* checks if the goal or time limit has been reached and ends the campaign */function checkGoalReached() afterDeadline {if (amountRaised >= fundingGoal){fundingGoalReached = true;GoalReached(beneficiary, amountRaised);}crowdsaleClosed = true;}function safeWithdrawal() afterDeadline {if (!fundingGoalReached) {uint amount = balanceOf[msg.sender];balanceOf[msg.sender] = 0;if (amount > 0) {if (msg.sender.send(amount)) {FundTransfer(msg.sender, amount, false);} else {balanceOf[msg.sender] = amount;}}}if (fundingGoalReached && beneficiary == msg.sender) {if (beneficiary.send(amountRaised)) {FundTransfer(beneficiary, amountRaised, false);} else {//If we fail to send the funds to beneficiary, unlock funders balancefundingGoalReached = false;}}} }Solidity的簡介就到此為止了,后面我們會具體解析這些合約的奧秘
前面說過,智能合約是部署在以太坊的網絡上的,那么如何搭建一個以太坊網絡呢,就需要官方提供的工具Geth了。下一章會詳細說明。
原文地址:?http://www.ethchinese.com/?p=630
總結
以上是生活随笔為你收集整理的区块链学堂(3):Solidity的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 区块链学堂(2):最简单的智能合约
- 下一篇: 区块链学堂(4):以太坊基本概念及工具G