您好!欢迎光临这里是中欧体育官方app下载,我们竭诚为您服务!
定制咨询热线+86 0000 88888
联系我们

中欧体育(中国)官方APP下载·最新IOS/安卓/手机APP下载

邮 箱:admin@bdl-expo.com
手 机:11576674694
电 话:+86 0000 88888
地 址:江西省赣州市顺昌县视央大楼3932号

如何在Loom的BaseChain创建和部署智能合约:中欧体育官方app下载

发布时间:2023-05-27 23:36人气:
本文摘要:Loom Network为开发人员获取了建构高性能面向用户的dapp所需的可扩展性和可用性。

Loom Network为开发人员获取了建构高性能面向用户的dapp所需的可扩展性和可用性。Loom Network为以太坊获取了一个第二层解决方案,具备以下主要优点:1. 1-3秒证实时间。

2. 用户需要缴纳气体酬劳。3. 防止主网交易冗余。4. 与MetaMask和其他主要以太坊钱包几乎相容。创立智能合约如果您是创立以Solidity撰写的智能合约的初学者,我强烈建议您用于Remix IDE来研发和用于智能合约。

它可以精彩地测试某些功能。Remix还不会为任何智能合约创立合约ABI和bytecode-您以后必须将它们构建到游戏客户端中。对于高级编码人员或专家,我建议用于Truffle。

您还可以遵循Pranshu Rastogi的本指南,网卓新闻网,用于Truffle为Loom创立和部署智能合约。我们将创立的智能合约是针对用于Game Engine Unity创立的小型单人原型游戏,玩家可以在其中搜集硬币。

这些硬币实质上是免费创立并留存在Loom区块链上的!pragma solidity =0.5.0 0.6.0;/// @author Julian Sakowski/// @title A prototype game contractcontract CoinCollector {/// @dev The amount of coins required to win the game.uint256 private winCondition;/// @dev Maps the subscriber index to an address.mapping (address = uint256) private coinCount;/** @dev Emits on game start. * @param _player The address of the player starting the game. */event onStartGame(address _player);/** @dev Emits on a coin being collected. * @param _player The address of the player collecting the coin. * @param _coinCount The current collected coin count of the player. */event onCoinCollected(address _player, uint256 _coinCount);/** @dev Emits on game end. * @param _player The address of the player ending the game. */event onEndGame(address _player);/** @notice Creates the smart contract and initializes the win condition. * @dev The constructor, which initializes the win condition. * @param _winCondition The amount of coins required to win a game. */constructor(uint256 _winCondition) public {winCondition = _winCondition;}/** @notice Any player can start a game for him or herself. * @dev Resets the coinCount for msg.sender. */function startGame() external {coinCount[msg.sender] = 0;emit onStartGame(msg.sender);}/** @notice Any player can collect coins. * @dev Increases the coin count for the msg.sender and checks for the valid win condition. */function collectCoin() external {coinCount[msg.sender] += 1;emit onCoinCollected(msg.sender, coinCount[msg.sender]);if (coinCount[msg.sender] == winCondition){emit onEndGame(msg.sender);}}/** @notice Shows the count of collected coins for the active player. * @dev Returns the coin count collected by the msg.sender. * @return The count of collected coins. */function getCoinCount() external view returns (uint256) {return coinCount[msg.sender];}}一步一步来让我们再行创立此原型游戏智能合约。首先让我们创立一个标准化的智能合约pragma solidity =0.5.0 0.6.0;contract CoinCollector {}留意,我们定义了编译器版本的范围。它必需在版本5.0和6.0之间。

引荐这样做到是因为较低的版本有可能包括安全性问题,而较高的版本可能会毁坏合约代码。我们的合约称作CoinCollector。让我们还包括一个获得胜利条件,即玩家夺得游戏必须搜集的硬币数量。

pragma solidity =0.5.0 0.6.0;contract CoinCollector {uint256 private winCondition;constructor(uint256 _winCondition) public {winCondition = _winCondition;}}留意,我们创立了一个全局变量“ winCondition”,其类型为uint256(于是以自然数)。此外,我们还包括了构造函数。一旦部署了智能合约,构造函数就是一个类似功能。在这里,我们在参数中定义实际的获得胜利条件。

让我们开始吧。10.为了更佳的阐述,让参数一直以下划线结尾,以便您可以区分参数和全局变量。我们的功能:pragma solidity =0.5.0 0.6.0;contract CoinCollector {uint256 private winCondition;mapping (address = uint256) private coinCount;constructor(uint256 _winCondition) public {winCondition = _winCondition;}function startGame() external {coinCount[msg.sender] = 0;}function collectCoin() external {coinCount[msg.sender] += 1;}function getCoinCount() external view returns (uint256) {return coinCount[msg.sender];}}留意,除了构造函数,我们现在还创立了三个函数。我们期望需要开始游戏,搜集硬币并一直接管有关早已搜集了多少硬币的信息。

我们为每个函数获取了可见性修饰符。有可能的修饰符是public,private,internal和external。

1. Public意味任何人都可以调用公共函数。2. Private意味著不能从契约内部调用函数。3. Internal容许从父合约承继的合约用于该函数。4. External 函数是合约模块的一部分,这意味著它们可以从其他合约和通过事务调用。

也就是说,除非必须展开外部交互,否则请将函数划为Private或Internal。此外我们的getCoinCount函数具备“ view”关键字,该关键字是附加的,但登录我们不变更此函数内的任何存储变量。

我们仅有“查阅”某些数据,例如我们的硬币。为了追踪搜集了多少硬币,我们加到了同构coinCount。

游戏开始时,将重置玩家的值,并且在玩游戏时,每个硬币搜集将使该值减少1。我们的同构将提供一个地址(当前玩家)并将其“指向”该值,即所搜集硬币的计数。因此,我们需要在coinCount中留存每个玩家的当前状态。让我们加到最后的代码段:Events。

Events对我们的客户而言最重要。它使我们需要注意到再次发生了什么。例如我们想要告诉玩家何时开始并夺得比赛或何时搜集硬币。

pragma solidity =0.5.0 0.6.0;contract CoinCollector {uint256 private winCondition;mapping (address = uint256) private coinCount;event onStartGame(address _player);event onCoinCollected(address _player, uint256 _coinCount);event onEndGame(address _player);constructor(uint256 _winCondition) public {winCondition = _winCondition;}function startGame() external {coinCount[msg.sender] = 0;emit onStartGame(msg.sender);}function collectCoin() external {coinCount[msg.sender] += 1;emit onCoinCollected(msg.sender, coinCount[msg.sender]);if (coinCount[msg.sender] == winCondition){emit onEndGame(msg.sender);} }function getCoinCount() external view returns (uint256) {return coinCount[msg.sender];}}请注意,我们在存储变量所在的顶部加到了有可能的事件。每个事件定义了所需的信息。

在collectCoin函数中,我们加到了一个if语句,以一直检查仅有搜集硬币的玩家否符合获得胜利条件。如果是这样,将收到完结游戏事件,并且客户端不会注意到该事件。每次我们在代码中提到“ msg.sender”时,它都指向起源于交易的地址,即播放器。

智能合约基本已完成。随便嬉戏并加到其他功能,例如可以搜集的全部硬币,搜集有所不同的硬币类型,或者构建机制以使其沦为多人游戏。

是时候充分发挥你的创造力!部署智能合约首先,我们必须用于以下命令加装Loom SDK:curl https://raw.githubusercontent.com/loomnetwork/loom-sdk-documentation/master/scripts/get_loom.sh | sh接下来,用于以下命令创立部署智能合约所需的私钥:./loom genkey -k priv_key -a pub_key现在,我们正在为智能合约创立一个二进制文件。solc --bin --overwrite -o . CoinCollector.sol并用于此二进制文件将我们的智能合约部署到Loom Testnet(完全免费!)。我们登录私钥“-k”,二进制文件“-b”,链URL“-u”和链ID“-chain”。./loom deploy -k priv_key -b CoinCollector.bin -u:80 --chain “extdev-plasma-us1”结果不应如下右图,其中最上方是合约地址,后跟我们的bytecode。

New contract deployed with address: extdev-plasma-us1:0xfC4ce0E01e8fdd309af77564bF5bE8b7dE79E3e9Runtime bytecode: [96 128 96 64 82 52 128 21 97 0 16 87 96 0 128 253 9180 96 4 54 16 97 0 94 87 96 0 53 124 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 144 4 128 99 171 126 56 160 20 97 0 99 87 12899 202 83 16 172 20 97 0 129 87 128 99 214 90 181 242 20 97 0 139 87 9196 0 128 253 91 97 0 107 97 0 149 86 91 96 64 81 128 130 129 82 96 32 1145 80 80 96 64 81 128 145 3 144 243 91 97 0 137 97 0 220 86 91 0 91 970 147 97 2 130 86 91 0 91 96 0 96 1 96 0 51 115 255 255 255 255 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 22 115 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 25522 129 82 96 32 1 144 129 82 96 32 1 96 0 32 84 144 80 144 86 91 96 1128 96 0 51 115 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 255 255 255 255 255 22 115 255 255 255 255 255 255 255 255 255 255255 255 255 255 255 255 255 255 255 255 22 129 82 96 32 1 144 129 82 9632 1 96 0 32 96 0 130 130 84 1 146 80 80 129 144 85 80 127 191 239 136214 251 22 149 67 110 98 57 38 222 62 243 239 224 63 35 239 35 206 3619 14 22 231 220 54 231 86 87 51 96 1 96 0 51 115 255 255 255 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 22 115 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 22 129 82 96 32 1 144 129 82 96 32 1 96 0 32 84 96 64 81 128 131 115255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 255 22 115 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 255 255 255 255 255 22 129 82 96 32 1 130 129 82 96 32 1 146 80 8080 96 64 81 128 145 3 144 161 96 0 84 96 1 96 0 51 115 255 255 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 22 115255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 255 22 129 82 96 32 1 144 129 82 96 32 1 96 0 32 84 20 21 97 2 12887 127 241 146 45 227 151 135 44 67 217 208 185 165 167 164 56 126 82138 167 86 234 251 122 48 149 77 42 177 197 150 123 193 51 96 64 81 128130 115 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 255 255 255 22 115 255 255 255 255 255 255 255 255 255 255 255 255255 255 255 255 255 255 255 255 22 129 82 96 32 1 145 80 80 96 64 81 128145 3 144 161 91 86 91 96 0 96 1 96 0 51 115 255 255 255 255 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 22 115 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 25522 129 82 96 32 1 144 129 82 96 32 1 96 0 32 129 144 85 80 127 164 24168 129 11 81 212 86 200 190 242 122 103 50 143 121 1 217 23 209 155 38140 190 174 131 119 90 81 239 236 96 51 96 64 81 128 130 115 255 255255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 25522 115 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255255 255 255 255 22 129 82 96 32 1 145 80 80 96 64 81 128 145 3 144 16186 254 161 101 98 122 122 114 48 88 32 37 59 125 14 80 36 176 248 22094 188 101 217 194 206 222 90 41 16 251 207 104 252 40 65 194 155 70208 254 117 97 0 41]Transaction receipt: e9810a90a1cecebce9c5d2b19f152ea52029d574f9ae1cd08cce30d4b3af65f4请注意,已部署的智能合约地址为“ 0xfC4ce0E01e8fdd309af77564bF5bE8b7dE79E3e9”。现在您可以在Loom Block Explorer中寻找它。

必须将智能合约地址加到到您的游戏客户端。十分好!智能合约早已研发完备,去在Unity中创立迷你游戏吧,立刻用于您的智能合约!。


本文关键词:中欧体育官方app下载,中欧体育app下载安装

本文来源:中欧体育官方app下载-www.bdl-expo.com

+86 0000 88888