Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Solidity basic Code with examples

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要介绍"Solidity基本代码举例分析",在日常操作中,相信很多人在Solidity基本代码举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Solidity基本代码举例分析"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Solidity是实施智能合约的契约导向的高级语言。它受到C ++,Python和JavaScript的影响,旨在针对以太坊虚拟机(EVM)。

Solidity是静态类型的,支持继承,库和复杂的用户定义类型等功能。

1 Solidity智能合约例子

我们从一个基础的solidity例子开始。开始的时候,你可能看不懂每一行具体的意思,但是没关系,我们会在后续的讲解中介绍每一个细节。

1234567891011

pragma solidity ^0.4.0;contract SimpleStorage { uint storedData; function set(uint x) { storedData = x; } function get() constant returns (uint) { return storedData; }}

第一行告诉快3平台出租出售Q1619668668【www.wkyule.com】该合约用的是0.4.0版本的solidity编写,并且这些代码具有向上兼容性。保证不会在不同solidity编译版本下编译会出现不同的行为。

从Solidity角度来看,合约就是存在于以太坊区块链中的一个特定地址中的代码和数据集合。uint storedData 声明了一个类型为 uint(256位的无符号整型)的变量,变量名称为 storedData。你可以把它想象为数据库中的一个字段,该字段是可以被数据库中的方法进行查询,修改。在以太坊中,这个字段是属于一个合约字段。在这个例子中,该变量可以通过提供的get,set方法进行获取或是修改。在Solidity中,访问一个变量是不需要通过this来引用的。

这个合约很简单,只是允许以太坊上的任何人来存储一个数据到某个节点,同时把这个操作发布到以太坊中,当然,以太坊上的其他节点同样可以通过调用set方法来修改你已经存储好的值。虽然有被修改,但是对该值操作的任何历史记录都是保存在以太坊中的。不用担心你的存储记录或是修改记录会丢失。后面我们会将到如何对合约进行限制,只允许你一个人修改这个数据。

2 Solidity子货币例子12345678910111213141516171819202122

pragma solidity ^0.4.0;contract Coin { //public关键字可以让外部访问该变量 address public minter; mapping (address => uint) public balances; //事件可以让轻客户端快速的响应变化 event Sent(address from, address to, uint amount); // 构造方法 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); }} 下面的例子将实现一个简单的加密货币例子。无中生币不在是梦想,当然只有合约的创建人才有这个特权。此外,任何人只要有一个以太坊密钥对就可以进行货币交易,根本不需要注册用户名和密码。这个合约引入了一些新的概念,让我们一个个都过一遍。   address public minter;   声明了一个public,类型为address的状态变量。Address类型是一个160位的值,不允许任何的算术操作。它适合于存储合约地址或是其他人的密钥对。Public关键字会自动产生用于外部访问该变量值的方法。如果不声明public,其他的合约是无法访问该变量的。自动产生的方法类似于:   function minter() returns (address) { return minter; }   当然如果你增加了一个和上面完全一样的方法是没有任何作用的,我们需要变量和产生的方法名完全一致。这块其实编译器会帮助我们完成,不需要我们自己动手编写,我们只要知道这个概念就可以。   mapping (address =>

uint) public balances;

A public state variable is also created, which is a more complex data type than address, similar to Map in Java, which describes a map relationship between an address and a uinit data type. The relationship of Mappings can be viewed as a hash table, where all possible keys correspond to a value of 0. Of course, there is no key value or value value only in the map. So we need to remember what kind of map relation we added or how to use it as in this example. Because this is a public variable, the system automatically generates a get method for it, similar to:

function balances(address _account) returns (uint) {

return balances[_account];

}

With the above method we can easily check the balance of an account.

event Sent(address from, address to, uint amount);

This line creates an event called event. The event is triggered on the last line of the example. The user or server application can listen for event triggers at a low cost (we'll see what the cost is later). Once this event is triggered, the listener receives three parameters: from, to, amount. These three parameters make it easy to track specific transactions. To listen for this event, we need to use the following code:

12345678910

Coin.Sent().watch({}, '', function(error, result) { if (! error) { console.log("Coin transfer: " + result.args.amount + " coins were sent from " + result.args.from + " to " + result.args.to + ". "); console.log("Balances now:\n" + "Sender: " + Coin.balances.call(result.args.from) + "Receiver: " + Coin.balances.call(result.args.to)); }})

Note how the user invokes the system's auto-generated balances method.

Coin methods are constructor methods that are called by the system when the contract is generated and are not allowed to be called later. Msg (and tx and block) is a global variable that holds properties that can be accessed by the blockchain. It persists the address of the node that created the contract. Msg.sender is the address of the caller of the method.

Finally, what really completes the contract and is called by other users are the mint and send methods. If mint is invoked by an account other than the one that created the contract, it has no effect. However, send can be invoked by any account (which must have ether) and send ether to another account. Note that if you send ether to another account with a contract, you won't see any changes through the blockchain browser, because the process of sending ether and the change in the amount are stored in a special ether contract. Not on the account. By using events, it is easy to create a blockchain browser to view transactions and account balances.

At this point, the study of "Solidity Basic Code Example Analysis" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report