前言
上篇文章我们搭建了以太坊网络私有链,实现了多节点间的转账交易以及了解和使用了多重签名钱包。本篇 我们将继续深挖,共同探讨智能合约部分。首先我们要编写以太坊的只能合约,那就得先了解下Solitidy 语言。戳官网 http://solidity.readthedocs.io/en/develop/index.html
Solitidy简单介绍:
●它是一个面向合约的高级语言,其语法类似于JavaScript。是运行在以太坊虚拟机中的代码。
●它是静态类型的编程语言,编译期间会检查其数据类型。支持继承、类和复杂的用户定义类型。
●在线体验:https://remix.ethereum.org
一个面向数字加密货币最简单的合约代码示例如下:
< style="outline: 0px;color: rgb(45, 174, 191);background-image: url('http://cn-sec.com/wp-content/uploads/2021/08/4-1629080794.png');background-position: left top;background-size: initial;background-attachment: initial;background-origin: initial;background-clip: initial;background-repeat: no-repeat;display: inline-block;width: 16px;height: 16px; right: 19px;top: 19px;opacity: 0.3;background-color: rgb(245, 245, 245) !important;border-width: initial !important;border-style: none !important;border-color: initial !important;" title="复制代码">
pragma solidity ^0.4.21;
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() public {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
if (msg.sender != minter) return;
balances[receiver] += amount;
}
function send(address receiver, uint amount) public {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
< style="outline: 0px;color: rgb(45, 174, 191);background-image: url('http://cn-sec.com/wp-content/uploads/2021/08/4-1629080794.png');background-position: left top;background-size: initial;background-attachment: initial;background-origin: initial;background-clip: initial;background-repeat: no-repeat;display: inline-block;width: 16px;height: 16px; right: 19px;top: 19px;opacity: 0.3;background-color: rgb(245, 245, 245) !important;border-width: initial !important;border-style: none !important;border-color: initial !important;" title="复制代码">
部署合约
① 创建一个新合约,具体步骤如下:
点击部署
确认后,主页面将显示合约创建成功
回到合约界面,此时合约已经创建成功
我们执行后返回主界面,已经有2个矿工确认该项交易 合约执行成功
我们复制下主账户地址,然后到合约里面查询下合约币数是否已到账
从合约中发送交易
我们现在从合约中给a节点子账户发送一笔交易(此时ACCOUNT2账户余额是为0的)
执行成功后,我们复制下Account2地址,在合约中查询下Account账户情况:
以上操作都是再同节点操作,下面我们尝试向不同节点发送合约交易
交易后我们查询下 主账户合约余额是否正确
至此,智能合约部署 和使用已经全部讲完。接下来 我们将深入探讨学习和使用Solidity 编写智能合约以及其他应用。
本文始发于微信公众号(网络侦查研究院):区块链踩坑之基础扫盲及搭建以太坊网络私有链(单节点)
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论