In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to write a contract in solid language". The content in the article is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "how to write a contract in solidity language".
Solidity is a high-level language whose syntax is similar to JavaScript. It is designed to generate Ethernet Square virtual machine code in a compiled way. In the following content, you will find that it is easy to use it to create contracts for voting, crowdfunding, closed auctions, multi-signature wallets, etc.
Write the first contract
Let's start with a very basic example. Don't worry that you don't know anything yet. We'll learn more details step by step.
Contract SimpleStorage {uint storedData; function set (uint x) {storedData = x;} function get () constant returns (uint retVal) {return storedData;}}
In Solidity, a contract consists of a set of code (functions of the contract) and data (the status of the contract). The contract is located at a special address on the ethernet block chain.
The uint storedData; line declares a state variable named storedData with a type of uint (256bits unsigned integer). You can think of it as a storage unit in a database, just like managing a database, which can be queried and modified by calling functions. In Ethernet Square, usually only the owner of the contract can do this. In this example, the functions set and get are used to modify and query the value of the variable, respectively.
Like many other languages, when accessing state variables, you do not need to add this. Such a prefix.
The contract does not do much (limited to the infrastructure of Ethernet Fong), just allowing anyone to store a number. And anyone in the world can access this number, without a (reliable) way to protect your published numbers. Anyone can call the set method to set a different number to overwrite the number you publish. But your numbers will remain in the history of the blockchain. Later we will learn how to add an access restriction so that only you can modify this number.
Write a token contract
The next contract will implement a cryptocurrency in the simplest form. Anyone can send money to someone else without registering a user name and password, as long as there is a pair of public and private keys of Tai Fong.
The contract Coin {/ / keyword "public" enables variables to be accessed from outside the contract. The address public minter; mapping (address = > uint) public balances;// event allows light clients to respond to changes efficiently. Event Sent (address from, address to, uint amount); / / the code for this constructor is only run when the contract is created. 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;` 这行代码声明了一个可公开访问的状态变量,类型为address。address类型的值大小为160 bits,不支持任何算术操作。适用于存储合约的地址或其他人的公私钥。public关键字会自动为其修饰的状态变量生成访问函数。没有public关键字的变量将无法被其他合约访问。另外只有本合约内的代码才能写入。自动生成的函数如下: function minter() returns (address) { return minter; } 当然我们自己增加一个这样的访问函数是行不通的。编译器会报错,指出这个函数与一个状态变量重名。 下一行代码创建了一个public的状态变量,但是其类型更加复杂: mapping (address =>Uint) public balances
This type maps some address to unsigned integers. Mapping can be thought of as a hash table, and the value corresponding to each possible key is virtually initialized to all 0. 0. This analogy is not very rigorous, and for a mapping, it is impossible to get a linked list containing all its key or value. So we have to remember for ourselves what we have added to mapping. A better way is to maintain such a linked list, or to use other more advanced data types. Or use mapping only in scenarios that are not affected by this defect, as in this example. In this example, the access function generated by the public keyword will be more complex, and the code is roughly as follows:
Function balances (address _ account) returns (uint balance) {return balances [_ account];}
We can easily query the balance of a particular account through this function.
Event Sent (address from, address to, uint value)
This line declares an "event". Triggered by the last line of code of the send function. The client (server-side applications also apply) can listen to these events triggered by the blockchain with very low overhead. When the event is triggered, the listener will receive the from,to,value parameter values at the same time, which can be easily used to track the transaction. To listen for this event, you can use the following code:
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));}}
Notice how the automatically generated balances function is called on the client side.
Here is a special function, Coin. It is a constructor that runs when the contract is created and cannot be called afterwards. It permanently stores the address of the contract creator. Msg (as well as tx and block) is a magical global variable that contains attributes that belong to the blockchain that can be accessed by contract code. Msg.sender always holds the address of the external caller of the current function.
Finally, the functions that are really called by the user or other contract to complete the function of this contract are mint and send. If someone other than the contract creator calls mint, nothing happens. Send can be called by anyone (with a certain number of tokens) to send some coins to others. Note that when you send some tokens to an address through the contract, you won't see anything if you query that address in the blockchain browser. The balance change caused by sending the token is only stored in the data store of the token contract. Through the event, we can easily create a "blockchain browser" that can track your new currency transactions and balances.
Thank you for your reading. the above is the content of "how to write a contract in solidit language". After the study of this article, I believe you have a deeper understanding of the problem of how to write a contract in solidity. the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.