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 use truffle to deploy Ethernet Square smart contracts to block chains". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!
Install Truffle
Installing the Truffle framework is very simple and requires only one command:
Npm install-g truffle
Of course, the premise is that you have installed NodeJS and the version is above 5.0.
Tunffle also requires a running Ethernet Square client to support standard JSON RPC API, with many options such as Ganache and geth.
Build a Truffle project
To use a large number of Tunffle commands, we learn by using an off-the-shelf Tunffle project. The first step is to create a Truffle project.
We can create an empty project template, but just starting to build the project, we can use Tunffle Boxs, which has a lot of sample applications and project templates. This article uses MetaCoin box, which creates an example of an application that can transfer tokens between accounts.
1. Create a new directory for building the Truffle project:
Mkdir MetaCoincd MetaCoin
two。 Download MetaCoin box, use truffle unbox to download various examples, and use truffle init if you want to build an empty project that does not include a smart contract.
Truffle unbox metacoin
After the above command is completed, we get a project with the following directory structure:
Intelligent contract catalogue written by contracts/: Solidity.
Migrations/: script deployment directory.
The test directory used by test/: to test applications and smart contracts.
Truffle.js/:Truffle configuration file.
Take a look at this project
Open contracts/MetaCoin.sol, which is an intelligent contract file written in Solidity. This smart contract builds the Metacoin token, and we can notice that it refers to the file contracts/ConvertLib.sol written by another solidity in the same directory.
Open contracts/Migrations.sol, which is a separate Solidity file that manages and updates the status of the deployed smart contract. This file is available for every Tunffle project and is usually left unattended.
Open the migrations/1_initial_deployment.js file, which is a script to deploy the Migrations contract in the Migrations.sol file.
Open the migrations/2_deploy_contracts.js file, which is used to deploy the MetaCoin contract and will be executed after the previous step is executed sequentially.
Open the test/TestMetacoin.sol file, which is a test file written by Solidity to make sure your contract works properly.
Open the test/metacoin.js file, which is similar to the test file above.
Open the truffle.js file to set up network information and other project-related content. The file is blank, so it doesn't matter, because we will use a Truffle command with built-in default values.
Test project
1. Open the terminal and execute the command:
Truffle test. / test/TestMetacoin.sol
The output looks like this:
TestMetacoin √ testInitialBalanceUsingDeployedContract (71ms) √ testInitialBalanceWithNewMetaCoin (59ms) 2 passing (794ms)
two。 Run the JavaScript test:
Truffle test. / test/metacoin.js
The output looks like this:
Contract: MetaCoin √ should put 10000 MetaCoin in the first account √ should call a function that depends on a linked library (40ms) √ should send coin correctly (129ms) 3 passing (255ms) compile Intelligent contract truffle compile
The output looks like this:
Compiling.\ contracts\ ConvertLib.sol...Compiling.\ contracts\ MetaCoin.sol...Compiling.\ contracts\ Migrations.sol...Writing artifacts to.\ build\ contracts deploy the project using Ganache
Ganache can also be used when developing a multi-functional private chain and console using Tuffle, which launches the private chain as a desktop application. Ganache is an easier tool for beginners of ethernet and blockchain to understand because it will show us more information.
In addition to running Ganache, you need to edit the Tunffle configuration file to point to the ganache instance.
1. Download and install Ganache
two。 Open truffle.js and replace it with the following:
Module.exports = {networks: {development: {host: "127.0.0.1", port: 7545, network_id: "*"}
This allows you to connect using the default parameters of Ganache.
3. Save this file.
4. Start Ganache
5. Open the terminal and deploy the smart contract to the blockchain with Ganache
Truffle migrate
The output looks like this:
Using network 'development'.Running migration: 1_initial_migration.js Replacing Migrations... ... 0x63b393bd50251ec5aa3e159070609ee7c61da55531ff5dea5b869e762263cb90 Migrations: 0xd6d1ea53b3a7dae2424a0525d6b1754045a0df9fSaving successful migration to network... ... 0xe463b4cb6a3bbba06ab36ac4d7ce04e2a220abd186c8d2bde092c3d5b2217ed6Saving artifacts...Running migration: 2_deploy_contracts.js Replacing ConvertLib... ... 0xa59221bc26a24f1a2ee7838c36abdf3231a2954b96d28dd7def7b98bbb8a7f35 ConvertLib: 0x33b217190208f7b8d2b14d7a30ec3de7bd722ac6 Replacing MetaCoin... ... 0x5d51f5dc05e5d926323d580559354ad39035f16db268b91b6db5c7baddef5de5 MetaCoin: 0xcd2c65cc0b498cb7a3835cfb1e283ccd25862086Saving successful migration to network... ... 0xeca6515f3fb47a477df99c3389d3452a48dfe507980bfd29a3c57837d6ef55c5Saving artifacts...
The content shows the trading id and the smart contract address you deployed.
6. In Ganache, click the Transactions button to see the transaction being processed.
7. To interact with the contract, you can use the Truffle console. Similar to Truffle Develop, except that it is connected to an existing blockchain (in this case, generated by Ganache)
Truffle console
You will see the following prompt:
Truffle (development) > interacts with smart contracts
Use the console to interact in the following ways:
Check the account balance
MetaCoin.deployed () .then (function (instance) {return instance.getBalance (web3.eth.accounts [0]);}) .then (function (value) {return value.toNumber ()})
See how many etheries are appropriate (and note that the contract defines 1 metacoin value 2 etheric)
MetaCoin.deployed () .then (function (instance) {return instance.getBalanceInEth (web3.eth.accounts [0]);}) .then (function (value) {return value.toNumber ()})
Transfer money from one account to another
MetaCoin.deployed () .then (function (instance) {return instance.sendCoin (web3.eth.accounts [1], 500);})
Check to see if metacoin is received
MetaCoin.deployed () .then (function (instance) {return instance.getBalance (web3.eth.accounts [1]);}) .then (function (value) {return value.toNumber ()})
Check the account transferred to someone else.
MetaCoin.deployed (). Then (function (instance) {return instance.getBalance (web3.eth.accounts [0]);}) .then (function (value) {return value.toNumber ()}); "how to deploy Ethernet Square Smart contracts to Block chains using truffle" ends here. 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.
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.