Deploy Ethereum Smart contracts to blockchain using truffle

Source: Internet
Author: User
Truffle is the most popular framework for the Ethereum development of Smart Contract, a tutorial to install a basic truffle project and deploy a smart contract to the blockchain. Before starting this article, I hope you understand the basic concepts of blockchain, ethereum, and smart contracts. # Installing truffle Installing the truffle framework is very simple and requires only one line of command: "NPM install-g Truffle" If you have installed Nodejs and the version is above 5.0. Tunffle also requires a running Ethereum client to support the standard JSON RPC API, with many options such as Ganache, Geth. # Build a truffle project to use a lot of TUNFFLE commands, we learn by using a ready-made Tunffle project. The first step is to create a truffle project. We can create an empty project template, but just start building the project, we can use [Tunffle boxs] (http://truffleframework.com/boxes), which has a lot of sample applications and project templates. This article uses [metacoin box] (Http://truffleframework.com/boxes/metacoin), 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 ' 2. Download metacoin box and use ' truffle unbox <box-name> ' to download a variety of examples, If you are building an empty project that does not include a smart contract, you can use ' truffle init '. "' Truffle unbox metacoin ' When the above command is complete, we get a project with the following directory structure: * contracts/: The smart contract directory written by solidity. * migrations/: Script deployment directory. * test/: Test Catalog for testing applications and smart contracts. * Truffle.js/:truffle configuration file. # Take a look at this item 1. Open ' Contracts/metacoin.sol ', this is a smart contract file written in solidity, this smart contract built Metacoin tokens, we can notice that it refers to the same directory of another solidity writing file ' Contracts/convertlib.sol '. 2. Open ' conTracts/migrations.sol ', which is a separate solidity file that is used to manage and update the status of a deployed smart contract. This file is available in every Tunffle project, usually without a tube. 3. Open ' migrations/1_initial_deployment.js ' file, this script is to deploy the ' migrations ' contract in ' Migrations.sol ' file. 4. Open the ' migrations/2_deploy_contracts.js ' file, this script is to deploy the ' metacoin ' contract, executed sequentially after the previous step of the script. 5. Open the ' Test/testmetacoin.sol ' file, which is a test file written by solidity to ensure your contract is working properly. 6. Open the ' test/metacoin.js ' file, which is similar to the test file above. 7. Open the ' truffle.js ' file for setting up network information and other project-related content. The file is blank, okay, because we will use a built-in truffle command with default values. # test Project 1. Open the terminal and execute the command: ' truffle test./test/testmetacoin.sol ' ' Output is this: ' ' testmetacoin√ Testinitialbalanceusingdeployedcontract (71ms) √testinitialbalancewithnewmetacoin (59ms) 2 passing (794MS) "2. Run the JavaScript test: "' truffle test./test/metacoin.js ' output is this: ' ' contract:metacoin√should put 10000 metacoin in the FIR St Account√should call a function this depends on a linked library (40ms) √should send coin correctly (129MS) 3 passing (255ms) ' # Compile Smart contract ' ' truffle compile ' ' output is this: ' ' compiling. \contracts\convertlib.sol ... Compiling. \contracts\metacoin.sol... compiling. \contracts\migrations.sol ... Writing artifacts to. \build\contracts "# Use the Ganache deployment project when you use Tuffle to develop a multifunctional private chain and console, you can also use ganache, which starts the private chain as a desktop application. For Ethereum and Blockchain novices, ganache is a much easier tool to understand because it will show more information to us. In addition to running ganache, you need to edit the Tunffle configuration file to point to the ganache instance. 1. Download and install [ganache] (Http://truffleframework.com/ganache) 2. 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 the file. 4. Start ganache! [Truffle-quickstart] (http://blog.hubwiz.com/2018/06/15/truffle-quickstart/accounts.png) 5. Open the terminal and use ganache to deploy the smart contract to the blockchain "truffle migrate" output is 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 ... ' content displays the transaction ID and the address of the smart contract you are deploying. 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, the only difference is that it connects to an existing blockchain (in this case, generated by ganache) ' Truffle console ' and you will see the following prompt: ' Truffle (development) > "# Interact with the smart contract using the console in the following way: * View account balance ' metacoin.deployed (). Then (function (instance) {return Instance.getbalance ( Web3.eth.accounts[0]). Then (function (value) {return Value.tonumber ()}); "* See how much ether is appropriate (and notice that the contract defines 1 metacoin value of 2 ether) ' metacoin.deployed (). Then (function (instance) {return Instance.getbalanceineth (Web3.eth.accounts[0]);}). Then (function (value) {REturn value.tonumber ()}); ' * Transfer from one account to another account ' metacoin.deployed (). Then (function (instance) {return Instance.sendcoin (Web3.eth.accounts[1], 500);}); "* Check if you receive Metacoin ' Metacoin.deployed (). Then (function (instance) {return Instance.getbalance (web3.eth.accounts[1 ]);}). Then (function (value) {return Value.tonumber ()}); ' * Check the account for transfer to others ' metacoin.deployed (). Then (function (instance) { Return Instance.getbalance (Web3.eth.accounts[0]). Then (function (value) {return Value.tonumber ()}); The Ok,truffle QuickStart tutorial is here, and if you have questions you can visit our online interactive course:>-[Ethereum tutorial] (http:/ /xc.hubwiz.com/course/5a952991adb3847553d205d1?affid=studygolang615), mainly introduces intelligent contract and DAPP application development, suitable for getting started. >-[Ethereum Development] (http://xc.hubwiz.com/course/5abbb7acc02e6b6a59171dd6?affid=studygolang615), mainly introducing the use of node. js, MongoDB , blockchain, IPFs to achieve a central e-commerce Dapp actual combat, suitable for advanced. Original reprint: "Using [truffle] (http://blog.hubwiz.com/2018/06/15/truffle-quickstart/) to deploy Ethereum smart contracts to blockchain" 162 reads  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.