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

How to develop token contract with solidity

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

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to develop token contracts with solidity". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The application of Ethernet Square is called decentralized Application (DApp). The development of DApp mainly includes two parts:

Development of Intelligent contract

Development of user interface

In this article, we will introduce solidity, the development language of intelligent contracts.

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.

This is the end of the content of "how to develop token contracts with solidity". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report