Solidity

Modifies clause error on a changed object

别等时光非礼了梦想. 提交于 2019-12-01 22:37:45
问题 How can I state (in Dafny) an " ensures " guarantee that the object returned by a method will be "new", i.e., will not be the same as an object used anywhere else (yet)? The following code shows a minimal example: method newArray(a:array<int>) returns (b:array<int>) requires a != null ensures b != null ensures a != b ensures b.Length == a.Length+1 { b := new int[a.Length+1]; } class Testing { var test : array<int>; method doesnotwork() requires this.test!=null requires this.test.Length > 10;

十一课堂|通过小游戏学习Ethereum DApps编程(3)

ぃ、小莉子 提交于 2019-12-01 22:20:21
1 solidity语言的知识点 Random Numbers 很多时候我们都需要随机数。 在solidity里面,我们可以通过 keccak256 来产生hash随机数。 // Generate a random number between 1 and 100: uint randNonce = 0; uint random = uint(keccak256(now, msg.sender, randNonce)) % 100; randNonce++; uint random2 = uint(keccak256(now, msg.sender, randNonce)) % 100; 在以太坊中,Dapp被调用时,被调用的这个操作将作为一个transaction被广播到网络上其他节点上。 网络上的节点收到了transaction后,都希望Proof of Work,所以都会尝试成为第一个解决这个transaction的节点。然后将这组交易与他们的工作证明(PoW)一起发布到网络的其他节点上。 但一个节点完成了transaction的处理,其他节点都将停止处理这个transaction,而且将尽快接受处理结果。 属于,一个节点可以决定是否广播一个transaction的结果。如果我们生产的随机数,不是我希望看到的,我们可以不广播这个结果,显然这样就不是公平的

十一课堂|通过小游戏学习Ethereum DApps编程(4)

北城以北 提交于 2019-12-01 22:20:03
在上篇完结的时候,我们制造出了这个独一无二可爱至极的角色: 这里我们继续总结一些关于solidity语言的知识点。并且开始了解一些比较高级的内容。 ERC20 tokens以及ERC721标准,和crypto-collectible。这些知识可以让我们可以和其他玩家交易自己的创造的角色。 1 Token 对于token的理解,众说纷纭。为了让你清醒的记忆token在这里的定义,我就不举例其他对token的解释了。 在这里,token就是一个Dapp,一个智能合约的意思。 重要的事情说三遍: token就是一个Dapp,一个智能合约的意思。 token就是一个Dapp,一个智能合约的意思。 这个智能合约可以追溯谁拥有多少"金币",然后有一些功能可以让"金币"拥有者进行交易。 So basically a token is just a contract that keeps track of who owns how much of that token, and some functions so those users can transfer their tokens to other addresses. 因为ERC20 tokens是一个已经被实现了的Dapp,就意味着,你可以直接在你的Dapp里面使用ERC20 tokens,不需要自己去定义自己的"金币"。 在ERC20

十一课堂|通过小游戏学习Ethereum DApps编程(1)

你。 提交于 2019-12-01 22:19:48
这篇文章,是通过制作一款可爱的游戏(DAPP,也可以称做智能合约),从而学习Solidity语言。和ETH网络的一些基础知识。 全程在线编程,无需搭建复杂的环境,只需要有任何其他语言的编程经验,即可马上学习。 网址: https://cryptozombies.io 这篇文章是一篇关于制作游戏的总结。 1 在线游戏简介 支持多语言界面: https://cryptozombies.io/ 课程: cryptozombies课程 编辑页面 在线编辑页面 学习总结 可视范围的 函数专用限制语法 整数 int是带符号整数 其他变量 2 solidity语言的知识点 modifier modifier 和 function有些相似。 主要用于提前检查function的参数是否符合function的要求。 这个就是经典检查调用智能合约的owner是否是此智能合约的开发者的modifier。 出自: https://github.com/OpenZeppelin/openzeppelin-solidity OpenZeppelin is a library for writing secure Smart Contracts on Ethereum. /** * @dev Throws if called by any account other than the owner. */

十一课堂|通过小游戏学习Ethereum DApps编程(5)

我与影子孤独终老i 提交于 2019-12-01 22:19:34
1 ERC721 tokens 在这个游戏里面,我们使用ERC721 tokens标准,通常的情况下,使用的是ERC20 tokens。 有兴趣的童学可以研究一下两个标准的不同。 ERC721 tokens有两种方式交易"金币"的方式。虽然在这里我用的"金币"一词,但是可以是如何东西,你的加密猫,你的无敌英雄角色。 下面的 transfer 和 approve + takeOwnership 是ERC721 tokens标准里面的两个交易接口。完成的功能相同。 function transfer(address _to, uint256 _tokenId) public; function approve(address _to, uint256 _tokenId) public; function takeOwnership(uint256 _tokenId) public; 2 Event Event的调用方法: 定义一个Event contract ERC721 { event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); event Approval(address indexed _owner, address indexed _approved, uint256

Modifies clause error on a changed object

为君一笑 提交于 2019-12-01 21:03:19
How can I state (in Dafny) an " ensures " guarantee that the object returned by a method will be "new", i.e., will not be the same as an object used anywhere else (yet)? The following code shows a minimal example: method newArray(a:array<int>) returns (b:array<int>) requires a != null ensures b != null ensures a != b ensures b.Length == a.Length+1 { b := new int[a.Length+1]; } class Testing { var test : array<int>; method doesnotwork() requires this.test!=null requires this.test.Length > 10; modifies this { this.test := newArray(this.test); //change array a with b this.test[3] := 9; //error

php程序员如何开发区块链、以太坊、智能合约的教程

喜欢而已 提交于 2019-11-30 23:50:09
以太坊是备受关注的区块链,它基于密码学技术和P2P通信技术 构建了一个去中心化的平台,所有的交易同步保存在每个节点中, 通过将区块单向级联成链,以太坊有效的保证了交易的不可篡改: 智能合约平台 以太坊是第一个实现了虚拟机的区块链,因此为智能合约 - Smart Contract - 的运行提供了良好的支持环境。也正因为这个原因,以太坊被称为区块链 2.0,以区别于比特币代表的以数字加密货币为核心特征的区块链1.0。 可以将智能合约理解为机器之间的合同约定,在满足一定条件时自动 执行约定好的逻辑,例如在保险理赔流程中,如果理赔条件满足就自动 将赔偿金释放给出险人,这个流程就可以使用智能合约来实现。 有多种语言可以开发以太坊智能合约,但目前最常用的是类似于JavaScript的 Solidity语言。本课程中将采用Solidity讲解智能合约的开发。 JSON-RPC 如果我们希望构造一个去中心化应用(DApp),除了智能合约的开发, 通常还需要使用其他开发语言为用户提供操作智能合约的用户接口,例如 开发一个网页、一个手机App或者一个桌面应用。这些代码都需要与以太坊 进行交互。 以太坊规定了每个节点需要实现的JSON RPC API 应用开发接口,该接口是传输无关的,应用程序可以通过HTTP、websocket或IPC等多种 通信机制来使用该接口协议操作以太坊节点:

十一课堂|通过小游戏学习Ethereum DApps编程(2)

拟墨画扇 提交于 2019-11-30 18:33:06
1 solidity语言的知识点 for ETH网络中,对于区块链的写入操作,是需要用户支付Gas的,所以我们很多时候选用 memory 而不是 storage。 memory用于临时存储,类似于RAM。 这样定义多个 memory。 uint[] memory evens = new uint[](5); for 语句和其他语言里面的语句很类似。 function getEvens() pure external returns(uint[]) { uint[] memory evens = new uint[](5); // Keep track of the index in the new array: uint counter = 0; // Iterate 1 through 10 with a for loop: for (uint i = 1; i <= 10; i++) { // If `i` is even... if (i % 2 == 0) { // Add it to our array evens[counter] = i; // Increment counter to the next empty index in `evens`: counter++; } } return evens; 可以得到: [2, 4, 6, 8, 10] payable

UnhandledPromiseRejectionWarning: Error: The contract code couldn't be stored, please check your gas limit

独自空忆成欢 提交于 2019-11-30 17:28:17
I am trying to deploy my simple solidity smart contract onto the Rinkeby Network but I keep getting the error: UnhandledPromiseRejectionWarning: Error: The contract code couldn't be stored, please check your gas limit. My solidity code is simple pragma solidity ^0.4.18; contract Greetings{ string public message; function Greetings(string initialMessage) public{ message = initialMessage; } function setMessage(string newMessage) public { message = newMessage; } } and my deploy script is: const HDWalletProvider = require('truffle-hdwallet-provider'); const Web3 = require('web3'); const {

java程序员使用web3j进行以太坊开发详解

落爺英雄遲暮 提交于 2019-11-29 19:18:26
如何使用web3j为Java应用或Android App增加以太坊区块链支持,教程内容即涉及以太坊中的核心概念,例如账户管理包括账户的创建、钱包创建、交易转账,交易与状态、智能合约开发与交互、过滤器和事件等,同时也详细说明如何使用web3j提供的API开发接口与以太坊进行交互,是java工程师学习以太坊应用开发的不二选择。 以太坊概述 以太坊是备受关注的区块链,它基于密码学技术和P2P通信技术 构建了一个去中心化的平台,所有的交易同步保存在每个节点中, 通过将区块单向级联成链,以太坊有效的保证了交易的不可篡改: 智能合约平台 以太坊是第一个实现了虚拟机的区块链,因此为智能合约 - Smart Contract - 的运行提供了良好的支持环境。也正因为这个原因,以太坊被称为区块链 2.0,以区别于比特币代表的以数字加密货币为核心特征的区块链1.0。 可以将智能合约理解为机器之间的合同约定,在满足一定条件时自动 执行约定好的逻辑,例如在保险理赔流程中,如果理赔条件满足就自动 将赔偿金释放给出险人,这个流程就可以使用智能合约来实现。 有多种语言可以开发以太坊智能合约,但目前最常用的是类似于JavaScript的 Solidity语言。本课程中将采用Solidity讲解智能合约的开发。 课程地址: http://xc.hubwiz.com/course