In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 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 realize the EOS intelligent contract". 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!
With all the excitement about EOS, the area that remains challenging for most developers who want to participate is starting to use smart contracts. New developers often need to overcome two hurdles: getting tools and settings, and understanding how to write smart contracts themselves.
The EOS Smart contract is written in C++ and compiled into Web Assembly. Dan Larimer chose C++ to take advantage of its type and templating system, which made the contracts safer, adding that because smart contracts took so short running time, most memory problems would disappear.
Configuration
Part of the challenge with EOS is to set up local block chains. Fortunately, EOS provides some foundation for setting up a local EOS environment. For this guide, we will use EOSIO Dawn 3.0.
The summary of this guide can be condensed into several key commands:
Git clone https://github.com/EOSIO/eos-- recursive$ cd eos$. / eosio_build.sh$ cd build & & make install$ cd programs/nodeos$. / nodeos-e-p eosio-- plugin eosio::wallet_api_plugin-- plugin eosio::chain_api_plugin-- plugin eosio::account_history_api_plugin-- access-control-allow-origin=*
Installation takes some time, but it's simple. Once the local EOS blockchain is up and running locally, it's time to get started. Throughout the guide, we will refer to utilities such as cleos and eosiocpp. You can find these in the eos/programs folder.
Establish ping Smart contract
In this tutorial, we will create and deploy a "Hello World" of a distributed system: ping/pong. For inexperienced people, we will send a "ping" command to the server, which will respond with "pong". The smart contract consists of the following parts: C++ code, ABI (application binary interface) and WAST (Web assembly text file) based on C++ code. This looks like:
Implement ping
After we have finished setting up the tool environment, let's enter the contract! To write an ping smart contract, all we need is a contract that implements one operation: ping. All these methods need to do is print "Pong" in response.
Create a folder called ping in contract and a file in the ping/ping.cpp file:
# include # include class ping_contract: public eosio::contract {public: using eosio::contract::contract; void ping (account_name receiver) {eosio::print ("Pong");}; EOSIO_ABI (ping_contract, (ping))
Here is a simple little example that you can test to be more familiar with. Let's break down what's going on here:
We include some definitions to write our contract.
We created a new contract class that inherits from eos::contract.
We created a method to print "Pong".
The last line is the recently added macro, which saves us the effort of maintaining our own handwritten ABI by generating one based on the method we passed to the second parameter.
Build a contract
EOS block generators do not run C++ code when executing smart contracts; they expect web-assembly. EOS provides a tool called eosiocpp that converts C++ code into Web Assembly Text. Let's do this now eosiocpp-o ping.wast ping.cpp. This step will produce some warnings, but we can ignore them now.
Next, we need the application binary interface. Basically, the ABI of your smart contract will describe the method and its corresponding signature. Since we added the EOSIO_ ABI macro to the end of the file instead of writing it manually, we can generate it using the following command: eosiocpp-g ping.abi ping.cpp.
At this point, your folder should look like this:
├── ping.abi ├── ping.cpp └── ping.wast is deployed to the local network
Now that we have all the resources needed for a smart contract, let's deploy it. Make sure you have a wallet created cleos wallet create and make sure it is unlocked by running cleos wallet unlock and typing your wallet password when prompted. We will deploy our smart contract under another account.
To do this, we need to create a new key pair, which we can do by running: cleos create key. This will generate random public and private keys for you. For the rest of this tutorial, be sure to replace [public_key] / [private_key] with the value you just received.
Import the private key into the currently unlocked account wallet: cleos wallet import [private_key], and set up the contract account with the public key: cleos create account eosio ping.ctr [owner_key:public_key] [active_key:public_key].
Link the contract to the newly created account, ping.ctr.. / ping-p ping.ctr.
Interact with ping
Once the new contract is deployed, you can interact with it! Let's create a tester account with the same key to run the transaction: cleos create account eosio tester [public_key] [public_key]
Now we can test it on the command line:
$cleos push action ping.ctr ping'["tester"]'- p testerexecuted transaction: e89ebeaad8f3623e42d966f62d4d0adbf6a7412b6bb4d7be61f04a22d3cd485e 232 bytes 102400 cycles# ping.ctr > Received ping
That should do it!
This is exciting for our programmers, but most users don't set up their command line to interact with your smart contract. So let's bring this interaction to their more familiar interface: their browsers.
Interact with each other through the browser
To interact with EOS from the front end, we will use EOS.js. Since we are using dawn3 on EOS support, we need to make sure that we use the dawn3 branch: npm install eosjs @ dawn3 at installation time.
Let's start with the configuration:
Eos = require ('eosjs') eos = EOS.Localnet ({keyProvider: [' {replace_with_your_private_key}'], httpEndpoint: 'http://127.0.0.1:8888'})
After the configuration is complete, we must specify some details:
Eos.contract ('ping.ctr') .then ((contract) = > {contract.ping ("tester", {authorization: [' tester']}) .then ((res) = > {console.log (res)}))
Notice ping.ctr, which matches the name of the contract we previously deployed. Once we get the contract interface (or ABI), we can interact with it as if it were a native Javascript. The contract returns a promise containing the transaction details contained in the resolve function. This is the core idea of interacting with our ping smart contract from the front end.
This is the end of the content of "how to achieve EOS Smart contract". Thank you for your 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.
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.