区块链学堂(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相关推荐
- 区块链学堂:区块链引子
链客,专为开发者而生,有问必答! 此文章来自链客区块链技术问答社区,未经允许拒绝转载. 区块链学堂(1):区块链引子 相信正在看这篇文章的读者一定已经听说过区块链了,并且也相信区块链是一项前景美妙,甚 ...
- 区块链学堂(6):以太坊生产网络/测试网络/私有网络
区块链学堂(6):以太坊生产网络/测试网络/私有网络 要理解以太坊 PrivateNetwork 先要理解以太坊的两种官方网络 目前以太坊官方提供了两种网络 生产环境网络 测试网络 TestNet 下 ...
- 调用wasm_PDX Utopia区块链协议栈使用Solidity调用wasm智能合约
在这个瞬息万变的世界中,智能合约已成为所有平台中强有力的服务.Solidity是一种趋势,PDX Utopia区块链协议栈使用Solidity调用wasm智能合约. ▼ 什么是Solidity? So ...
- java玩转区块链-基础篇-solidity语法-基础类型
java玩转区块链-基础篇-solidity语法-基础类型 java环境配置 代码准备 maven 完整solidity 执行步骤 基础类型 布尔类型 类型标识: 字面常量值: 运算符: 短路规则: ...
- 区块链智能合约solidity入门
想知道更多关于区块链技术知识,请百度[链客区块链技术问答社区] 链客,有问必答!! 使用ubuntu系统安装ethereum开发环境 安装 Nodejs sudo apt-get update sud ...
- 区块链学堂(4):以太坊基本概念及工具Geth、Browser-solidity、Mist
在我研究以太坊技术的过程中,由于相关资料较少,因此走了较多的弯路.所以这里我们不会先介绍Solidity的语法结构,而会在接下来的第一阶段,从实战出发,陆续向大家介绍Geth.Browser-soli ...
- 区块链学堂(2):最简单的智能合约
最简单的智能合约 最简单的一个智能合约 pragma solidity 0.4.9; contract DemoTypes {function f(uint a) returns (uint b) { ...
- 区块链开发之Solidity编程基础(三)合约数据存储
Solidity编程基础三 概要 以太坊虚拟机 EVM 数据位置 storage/存储 memory/内存 calldata/调用数据 Stack 栈 数据赋值成本 概要 本章将进行太坊虚拟机EVM的 ...
- 区块链智能合约solidity入门 1
使用ubuntu系统安装ethereum开发环境 安装 Nodejs sudo apt-get updatesudo apt install curlcurl -sL https://deb.node ...
最新文章
- CSS中的超链接和超链接分类
- SM50强制终结后台JOB进程
- git图文工具_全网最详细的Windows里Git client客户端管理工具SourceTree的下载与安装(图文详解)...
- Linux下装完mysql后默认密码是多少
- 一步步优化JVM六:优化吞吐量[转]
- docker php nginx,docker 构建nginx + php
- Java基础——TreeSet
- ios uiwindow弹窗_iOS-UIWindow详解
- QT下使用MapX控件的问题以及Activex技术
- Appium自动化测试原理
- linux深度商店 apt,Deepin系统安装软件总结:通过商店、二进制包、deb包、终端命令安装...
- 解锁前端面试体系核心攻略.pdf
- Solaris10上修改hostid
- Vista和Win2008中如何卸载IE8
- connect to address IP: No route to host
- 电脑连上Jink后打开Altium Designer弹出Failed to measure TotalIRLen(6)
- 王艺瑞浙江大学计算机学院,关于公示2010年(秋)同等学力申请进入论文阶段学员名单的通知...
- 解决IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter
- 摄影测量实习-解析空中三角测量-C#代码
- UEFI下Windows引导过程