Dapp Development (i)

Source: Internet
Author: User

Position
Your configuration file is truffle.js. is located under the root directory of the project. This file is a JavaScript file that supports the execution of code to create a configuration. It must export an object to represent the project configuration, as in the following example.

namespace conflicts under Windows
When you use the command line for Windows, the default profile name conflicts with truffle. In this case, we recommend using Windows Power Shell or git BASH. You can also rename the configuration file to Truffle-config.js to avoid conflicts.

Example
Module.exports = {
Build: {
"Index.html": "Index.html",
"App.js": [
"Javascripts/app.js"
],
"App.css": [
"Stylesheets/app.css"
],
"images/": "Images/"
},
RPC: {
Host: "LocalHost",
port:8545
}
};
The default profile is already configured, build, and RPC. These default and Non-default options are described later.

Options
Build
This is the build configuration of the front-end. By default, the default builder is invoked, as described in the previous build section. But you can also customize the build process to view the advanced build process chapters to learn more.

Example:

Build: {
"Index.html": "Index.html",
"App.js": [
"Javascripts/app.js"
],
"App.css": [
"Stylesheets/app.css"
],
"images/": "Images/"
}
NETWORKS
Specifies which network to use when porting (migration). When compiling or running a transplant on a particular network, the contract is cached for subsequent use. When your contract abstraction checks that you are connected to a network, it uses the original cache contract on the network to simplify the deployment process. The network is identified by the net_version in the RPC call to the Ethernet square.

The following networks objects, which are configured by a network name, define their network parameters by value correspondence. Networks's corresponding options are not required, but once specified, each network must define a corresponding network_id. If you want to specify a default network, you can do so by marking the value of the netword_id as default and using the default network when no other network is matched. It should be noted that the entire configuration should have a network with only one default. In general, the default network is mainly used for development, configuration, contracts and other data is not long-term preservation needs, network ID will also be due to testrpc restart and frequent changes.

The network name is used for user interface invocation and is used in porting as follows:

$ Truffle Migrate–network Live
You can also optionally specify RPC configuration information. Here is an example:

Networks: {
"Live": {
Network_id:1,//Ethereum public network
Optional config values
Host-defaults to "localhost"
Port-defaults to 8545
Gas
Gasprice
From-default address to the any transaction truffle makes during migrations
},
"Morden": {
Network_id:2,//official Ethereum test Network
Host: "178.25.19.88",//Random IP For example purposes (don't use)
Port:80
},
"Staging": {
network_id:1337//Custom private network
Use default RPC settings
},
"Development": {
NETWORK_ID: "Default"
}
}
Rpc
Some details on how to connect to the etheric square client. Host and Port are required, and others are required.

Host: Point to the Ethernet square client's address. When this machine is developed, it is generally localhost
PORT: Ethernet Square The client receives the requested ports, the default is 8545
Gas: The gas limit at deployment time, default is 4712388
Gasprice: Gas price at deployment. Default is 100000000000 (Shannon)
From: The source address to use when porting. If not specified, the default is the first available account for your Ethernet client.
Example:

RPC: {
Host: "LocalHost",
port:8545
}
MOCHA
MOCHAJS test Framework configuration options, detailed reference documentation.

Example:

Mocha: {
Usecolors:true
}

Network and App Deployment
Background
Even the smallest project will work with more than two block chains, one is the test chain on the development machine, such as Ethereumjs Testrpc. The other is the network you will eventually deploy, such as the Ethernet network, the private chain within your own company, and so on. Truffle provides a system to manage build and deploy resources under different networks to simplify the final deployment process.
Configuration
See 1 for details. Profile section for more information.

Specify a network
Most truffle provide commands that behave differently depending on the specified network, using contracts and configuration information under the corresponding network. You can control the parameters by using the –network option.

$ Truffle Migrate–network Live
Networks: {
Development: {
Host: "LocalHost",
port:8545,
NETWORK_ID: "*"//Match any network
},
Live: {
Host: "178.25.19.88",//Random IP For example purposes (don't use)
PORT:80,
Network_id:1,//Ethereum public network
Optional config values
Gas
Gasprice
From-default address to the any transaction truffle makes during migrations
}
}
In the example above, truffle will be ported in the live network. If you configure what is specified in the Example section of the above configuration example, you will eventually deploy on the etheric square network.

Building Resources
As 4. As mentioned in the compilation contract section, the constructed resource is stored in the./build/contracts directory, and a file such as. Sol.js exists. When you compile a contract file, or run a transplant on a network, truffle will update these. sol.js files that contain information about the network. When these resources are used in the future, such as in the front-end application. It automatically detects the currently used network and automatically uses the corresponding contract resources based on the network.

Apply Deployment
Because the network is automatically detected at run time, this means you only need to deploy your application or front-end once. When your program is running, it dynamically detects the currently used network to invoke the right resources, which makes your program very flexible.

For example, if you post a program to http://mydapp.io/, your program will run well when you use a wallet browser. If your wallet browser is running on a formal network, your dapp will use a contract that is deployed on the official network and, if it is on a test network, use the resources of the test network.
3. Construction process
Custom Build Process
Through the history of truffle, the default builder is not for everyone. It has some obvious drawbacks and is less mature than other build systems. As a result, truffle offers three ways to expand the default build system, but allows you to experience most of the truffle features.

Execute external command
If you want to execute an external command each time the build is triggered. You can include an option in the project's configuration.

Module.exports = {
This would run the Webpack command on each build.
//
The following environment variables is set when running the command:
Working_directory:root Location of the project
build_destination_directory:expected destination of built assets (important for truffle serve)
Build_contracts_directory:root location of your build contract files (. sol.js)
WEB3_PROVIDER_LOCATION:RPC configuration as a string, as a URL needed for web3 ' s HTTP PROVIDER.
//
Build: "Webpack"
}
Note that you need to provide the corresponding environment variables to integrate these external scripting commands into truffle, as detailed in the configuration notes.

To provide a custom function
You can provide a custom build function. The framework provides you with engineering-related parameters to facilitate deep integration with truffle.

Module.exports = {
Build:function (options, callback) {
Do something when a is required. Options contains these values:
//
Working_directory:root Location of the project
Contracts:metadata about your contract files, code, etc.
Contracts_directory:root directory of. Sol files
RPC:RPC configuration defined in the configuration
Destination_directory:directory where Truffle expects the built assets (important for truffle serve)
}
}
To create a custom module
You can also create an interface by creating a module or object (an object that contains a build function, as in the previous section). This applies to the need to integrate truffle, but has its own publishing process.

The following is an example of using a default build module.

var Defaultbuilder = require ("Truffle-default-builder");
Module.exports = {
Build:new Defaultbuilder (...)//Specify the default builder configuration here.
}
Initialize Front end
Because you use your own build process, truffle no longer knows how to initialize your front-end. Here's a list of things to do:

Introduction of WEB3 Library
Initializes an instance of the WEB3, setting a provider point to your etheric square client. It is important to check if the Web3 object already exists, because if someone passes through the Wallet browser, such as Metamask or mist, the object is likely to exist, you should use the object instead of initializing a new one. See examples to learn more.
Require or import compiled sol.js files from the./build/contracts directory. You need to call Mycontract.setprovider () for each file to set the provider. This needs to be consistent with the use of provider by the WEB3 instance. You can use Web3.currentprovider to get the current provider.
var mycontract = require ("./build/contracts/mycontract.sol.js");
Mycontract.setprovider (Web3.currentprovider);
Using Webpack
4. Truffle command Guide
How to use
truffle [command] [options]
Command
Build
Build a developer version of the app and create a.

Truffle build
Optional parameters

–dist: Create a release version of the app. Available only when using the default builder.
View 6. Build the application chapters to learn more.

Console
Run a console that contains the initialized and readily available contract objects.

Truffle console
Once the console is up, you can use the command line to use your contract, as in the code. In addition, the commands for all truffle columns are available in the console.

Optional Parameters:

–network Name: Specifies the network to use
–VERBOSE-RPC: Output truffle with RPC communication details.
The other 9. Console section to learn more.

Compile
Smart compilation of your contract will only compile contracts that have been modified since the last compilation, unless a forced flush is specified otherwise.

Truffle compile
Optional Parameters:

–compile-all: Forces all contracts to be compiled.
–network Name: Specifies the network to use to save the compiled results to the specified network.
Create:contract
The tool method uses scaffolding to create a new contract. Names need to match the hump name:

$ truffle Create:contract Mycontract
Create:test
Tool method, using scaffolding to create a new test method. The name needs to match the hump name.

$ truffle Create:test MyTest
Migrate
The transplant of a running project. See the relevant chapters of the transplant for details.

Truffle migrate
Optional Parameters:

–reset: Run all the transplants from scratch.
–network Name: Specifies the network to use and saves the compiled data to that network.
–to Number: Porting the version from the current version to the version specified by the ordinal.
–compile-all: Force all contracts to be compiled
–VERBOSE-RPC: Print verbose log of truffle interaction with RPC.
Exec
Executes a JavaScript file in a truffle environment. The environment contains, WEB3, the default provider based on the network settings, your contract object as a global object. This JavaScript file needs to export a function so that truffle can be executed. View 10. External feet would have known more.

$ truffle Exec/path/to/my/script.js
Optional Parameters:

–network Name: Specifies the network to use and saves the compiled data to that network.
Init
Initializes a new app, a new project, in the current directory. The default contract and front-end configuration are brought in.

$ truffle Init
List
List all available commands, equivalent to –help.

Truffle list
Serve
The http://localhost:8080 provides the corresponding services for the compiled app and automatically builds and deploys automatically when needed. Similar to truffle watch, the difference is that the Web server features are added here.

Truffle serve
Optional Parameters:

-P Port: Specifies the port of the HTTP service. The default is 8080.
–network Name: Specifies the network to use and saves the compiled data to that network.
Test
Run all test cases that are in the./test directory. or optionally run a single test file.

$ truffle test [/path/to/test/file]
Optional Parameters:

–network Name: Specifies the network to use and saves the compiled data to that network.
–compile-all: Force all contracts to be compiled
–VERBOSE-RPC: Print verbose log of truffle interaction with RPC.
Version
Output version number and exit.

Truffle version
Watch
Watch contracts, apps, and configuration file changes, automatically build apps when needed.

Truffle Watch

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.