This experiment is a private chain of Ubuntu 14.04, truffle 3.2.4, node 6.9.1, NPM 3.10.8 and Geth 1.4.5. Truffle official Documentation: Http://truffleframework.com/docs/used several truffle commands: Truffle init Create project truffle compile compile truffle migrate deploy
First, in order to have a node environment, and then use NPM Global installation truffle, with NPM install-f truffle can be.
Second, create the project with truffle to create a project is convenient, directly with truffle init can. Create a directory in any location, such as Truffletest, go to directory, open a terminal here (Terminal 1), execute truffle init, and there will be contracts, migrations and tests after execution. Three directories and truffle.js documentation, which is a configuration file. Where the contracts is a contract, truffle compile will look for contract documents when compiling it. The migrations directory contains JavaScript files that help deploy contracts to the Ethernet network, which represent the steps to deploy tasks that are named if your deployment needs to be constantly changing. The file names in it begin with a number, and suffixes often indicate the role of the file.
Third, the preparation of contract contracts are stored in the contracts directory, the implementation of truffle compile will automatically go to contracts directory search. Create a file named Mycontract.sol, code as follows: pragma solidity ^0.4.0; Contract mycontract{function multiply (uint a) returns (UINT D) {return a * 7;} The contract is the example in the ether square, the solidity, very simple. Four, modify the configuration file do not need to do more, just the network_id field of "X" to a number, such as 1123. V. Add a migration file such as 3_example_migartion.js var mycontract = Artifacts.require ("./mycontract.sol"); Module.exports = function (deployer) {Deployer.deploy (mycontract);}; Six, start a previously built private chain node to build a method, I have written on the blog. Start another terminal (Terminal 2), enter Geth--identity "ethtest"--rpc--rpccorsdomain "*"--datadir ~/ether/node1--port 30303--rpcapi "Db,eth" , net,web3 "--networkid 1123 console,--datadir for your node path,--networkid 1123 to be consistent with the configuration file Truffle.js, so that the console is turned on.
1, in Terminal 2, unlock your account, deploy the time to use, so the account must be available: Personal.unlockaccount (Eth.accounts[0]). 2, enter Terminal 1, compile contract truffle compile, compile the result is as follows: Then deploy: Truffle migrate--network development. Development for you to configure the file Truffle.js networks inside the key. At this point, Terminal 2 will be shown as follows: Terminal 1 results for this is not a successful deployment, because only the transaction sent to the network, not be included in the block, in the Terminal 2 can use Txpool.status to view an outstanding transaction. Start ore digging: Miner.start (), the successful deployment will show the results as follows: Terminal 2 end of mining, Miner.stop (). Seven, after the successful interaction with the contract, the Build/contracts directory is a JSON file. It is the metadata of the contract, and we have to interact with the contract to use the ABI and address. First, use the online compression escape tool (http://www.bejson.com/zhuanyi/) to convert the ABI JSON format to a string and assign it to the variable ABI in Terminal 2. Addresses are assigned to the variable address. You can then create objects, call the contract, the result is as follows: OK, so we have to deploy the contract to their own set up the private chain, rather than the official website of the TESTRPC, the main point is in the configuration file to write the network ID for you to start the private chain with the ID. The contract is then invoked with the ABI of the corresponding contract and address on the block chain.
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.