美文网首页
solidity(二)

solidity(二)

作者: OpenCoder | 来源:发表于2018-10-11 16:10 被阅读0次

【传智播客.黑马程序员训练营成都中心】

solidity(二)

目标

Solidity是一种面向对象的智能合约高级语言,运行在以太坊区块链平台的虚拟机(EVM)上。我们现在就使用它去编写一个hello world 入门程序。

第一步?

在命令行环境中输入以下命令, 连接到以太坊测试网络

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n8" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> geth --testnet --fast --cache=512 console</pre>

第二步

在 Geth 提示符下输入以下代码, 创建一个用户并设置密码

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n12" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
personal.newAccount()</pre>

第三步

在 Geth 提示符下输入以下代码, 确认新用户的账户余额为 0, 并开始”挖矿”(mine)

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n17" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
eth.getBalance(eth.accounts[0])
miner.start()</pre>

第四步

新开一个命令行窗口并输入以下命令, 将这个窗口连接到正在挖矿的窗口

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n20" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
geth attach</pre>

第五步

在 Geth 提示符下输入以下代码, 确认新用户的账户余额有所增长

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n26" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
eth.getBalance(eth.accounts[0])</pre>

第六步

使用智能合约的在线编译器 https://ethereum.github.io/browser-solidity/编译以下代码

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
contract HelloWorld
{
address creator;
string greeting;
function HelloWorld(string _greeting) public
{
creator = msg.sender;
greeting = _greeting;
}
function greet() constant returns (string)
{
return greeting;
}
function setGreeting(string _newgreeting)
{
greeting = _newgreeting;
}
/**********
Standard kill() function to recover funds
**********/
function kill()
{
if (msg.sender == creator)
suicide(creator); // kills this contract and sends remaining
funds back to creator
}
}</pre>

第七步

编译器生成的代码如下

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
var _greeting = /* var of type string here */ ;
var helloworldContract =
web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs
":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"n
ame":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[]
,"payable":false,"type":"function"},{"constant":true,"inputs":[],"name"
:"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type"
:"function"},{"inputs":[{"name":"_greeting","type":"string"}],"type":"c
onstructor"}]);
var helloworld = helloworldContract.new(
_greeting,
{
from: web3.eth.accounts[0],
data:
'606060405260405161044e38038061044e833981016040528080518201919060200150
505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff0
21916908302179055508060016000509080519060200190828054600181600116156101
000203166002900490600052602060002090601f016020900481019282601f1061009e5
7805160ff19168380011785556100cf565b828001600101855582156100cf579182015b
828111156100ce5782518260005055916020019190600101906100b0565b5b509050610
0fa91906100dc565b808211156100f657600081815060009055506001016100dc565b50
90565b50505b506103428061010c6000396000f360606040526000357c0100000000000
0000000000000000000000000000000000000000000009004806341c0e1b51461005257
8063a413686214610066578063cfae3217146100c15761004d565b610002565b3461000
2576100646004805050610141565b005b34610002576100bf6004808035906020019082
018035906020019191908080601f0160208091040260200160405190810160405280939
291908181526020018383808284378201915050505050509090919050506101d5565b00
5b34610002576100d36004805050610286565b604051808060200182810382528381815
18152602001915080519060200190808383829060006004602084601f0104600302600f
01f150905090810190601f1680156101335780820380516001836020036101000a03191
6815260200191505b509250505060405180910390f35b600060009054906101000a9004
73ffffffffffffffffffffffffffffffffffffffff1673fffffffffffffffffffffffff
fffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101
d257600060009054906101000a900473fffffffffffffffffffffffffffffffffffffff
f1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b80600160005090
80519060200190828054600181600116156101000203166002900490600052602060002
090601f016020900481019282601f1061022457805160ff191683800117855561025556
5b82800160010185558215610255579182015b828111156102545782518260005055916
02001919060010190610236565b5b5090506102809190610262565b8082111561027c57
60008181506000905550600101610262565b5090565b50505b50565b602060405190810
16040528060008152602001506001600050805460018160011615610100020316600290
0480601f016020809104026020016040519081016040528092919081815260200182805
4600181600116156101000203166002900480156103335780601f106103085761010080
8354040283529160200191610333565b820191906000526020600020905b81548152906
001019060200180831161031657829003601f168201915b5050505050905061033f565b
9056',
gas: 4700000
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + '
transactionHash: ' + contract.transactionHash);
}
})
设置显示的字符串以及减少费用(gas)
var _greeting = "Hello World" ;
gas: 300000</pre>

第九步

将修改完的代码复制到第四步的窗口中, 如果出现

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
Error: account is locked undefined
错误的话, 则使用
personal.unlockAccount(eth.accounts[0], 'password')
命令将用户解锁.</pre>

第十步

等候一段时间之后, geth 窗口就会出现

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
Contract mined! address...,</pre>

表明合约代码发布成功

第十一步

使用 helloworld.greet()命令来运行该合约

<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n51" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; text-align: left; break-inside: avoid; display: block; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
控制台将会显示“Hello World”</pre>


以上是入门demo,接着了解下基本概念。

以太坊:

以太坊是一个开源的,分布式的,基于区块链技术的公共平台,无需审查及第三方干扰即可运行应用程序。

智能合约:

智能合约仅仅是电脑程序。我们基于智能合约创建以太坊应用。请记住,虽然这个概念现在随着以太坊出现,但是它实际上是由 Nick Szabo 于 1996 年提出。

EVM:

EVM 是以太坊智能合约的沙盒运行时以及完全独立的环境。这意味着每个在 EVM 中运行的智能合约无法连接网络、文件系统或者其他进程。简称以太坊虚拟机。

Gas:

鉴于以太坊是一个分布式平台,所以必须有一种方式来限定给定智能合约的可用资源,否则可能会使得整个网络的算力瘫痪。Gas 通过为 EVM 中执行的每个指令确定它的执行成本来解决这个问题。一个重要的事情是,每笔发送到网络中的交易都有一个“Gas 预算”。如果预算的 Gas 用完了,交易将以失败告终,但是这笔交易仍然会被开采到区块链中。

Ether(ETH):

这是以太坊的加密数字货币。Gas/Ether 的动态兑换价格用来衡量一个操作将花费多少ETH。执行交易所需支付的费用通过 Gas 用量与 Gas 价格相乘得出(计算所得的费用将使用ETH 支付)。你可以将交易的 Gas 价格设置为任何值。但是,如果你设置的 Gas 价格太低的话,没有人会执行你的代码。

账户:

每个账户都是由一个地址标识的。同一地址空间由两种账户共享。一种是由公私钥对控制的外部账户,该账户通常由人拥有,用来存储 ETH。另一种是由它存储的代码控制的合约账户。这两种账户之间存在一些差异,其中非常重要的一点,只有外部账户才能启动交易。

交易:

交易是从一个账户发送到另一个账户的消息。你可以发送一笔交易给另一个账户以转移ETH。如果目的账户是合约账户,其代码也将被执行。请注意,涉及代码执行的每笔交易都将在网络中的所有节点上执行。此外,所有的代码运行以及交易执行都将被记录在以太坊区块链上。

结束语

入门的小demo,和基本的相关概念,就是今天所有的内容啦。下次内容将介绍搭环境,和开发一个基于以太坊的代币。谢谢观看。

相关文章

网友评论

      本文标题:solidity(二)

      本文链接:https://www.haomeiwen.com/subject/peibaftx.html