In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to write Ethereum smart contracts". In daily operation, I believe many people have doubts about how to write Ethereum smart contracts. The editor has consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to write Ethereum smart contracts"! Next, please follow the small series to learn together!
Development Language and Development Environment Selection
The most popular programming language for smart contracts is Solidity, but Solidity isn't the only one. As beginners, writing Solidity code, we can use Remix, which is a browser-based Solidity IDE. Remix supports writing, testing and deploying smart contracts.
write code
The first program to learn a language is undoubtedly HelloWorld, so let's write a smart contract for HelloWorld. The contract code is as follows.
pragma solidity ^0.4.21;contract HelloWorld { string hello = "Hello World!!! "; event say(string _value); function sayHello() public { emit say(hello); } }
The first line of the contract code specifies that the Solidity version used by the contract is 0.4.21 and does not support Solidity features higher than 0.4.21.
In Solidity, the code segment contained in the contract keyword represents a smart contract, which has some member variables that represent the data of the contract, such as hello in our HelloWorld, and we can modify these members. It also has functions that can be called by others.
Event is an event and subscription mechanism provided by Solidity. Smart contracts can issue events, and contract callers can listen to these events and react accordingly.
This contract doesn't do much, it just emits a say event when someone calls its sayHello method. Let's deploy and execute it.
Deploy and run contracts
First we need to compile this code, there is a Compile tab on the right side of Remix, click Start to compile, compile successfully, if there is an error prompt, correct it.
Then we need to deploy it to the blockchain, switching tab to Run.
Remix supports three environments to run contracts. If it is JavaScript VM, the contract will be executed in the browser JavaScript sandbox blockchain, which can be understood as Remix virtualizing a blockchain virtual machine using the browser JS environment. If Injected Provider, Remix will link Matamask or Mist blockchain wallets to indirectly deploy and invoke contracts. Finally, for Web3 Provider, Remix will link to remote payment connection nodes such as Geth to deploy and invoke contracts.
For simplicity, we'll use JavaScript VM, which will initialize five blockchain accounts for us, using the default account. Set Environment to JavaScript VM.
In addition to Environment and Account, we will see Gas Limit, which is the maximum cost we can accept to execute a Transaction. Value indicates how much ether we will transfer to the contract account on the next call.
HelloWorld next represents the contract we will create. Click Create to create the contract.
We can click sayHello to invoke it. You can see that the call succeeded, printing out the event we sent in logs.
At this point, the study of "how to write Ethereum smart contract" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone 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.
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.