truffle-Voting
development environment
Ubuntu 16.10
web3 0.2.x
ganache
metamask
truffle4.0.5
webstorm
write in front
Use the truffle framework to develop a voting system Native web3js development can first look at the previously written https://github.com/jsphLim/VotingSystem
Project structure
src/ - The default directory where your application files run. This includes the recommended javascript files and css style file directories.
contract/ - Truffle's default contract file storage address.
migrations/ - store the release script file
test/ - test file used to test applications and contracts
truffle.js - Truffle configuration file
Environment construction
The project environment construction can refer to the document: http://truffle.tryblockchain.org/
1. Create a project directory
mkdir truffle-Voting && cd truffle-Voting
2. Download truffle first
(The premise is to install nodejs and npm. If you don't know how to install, please see https://github.com/jsphLim/VotingSystem)
npm install -g truffle
3. Initialize truffle
truffle init
Configure truffle.js
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// for more about customizing your Truffle configuration!
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*" // Match any network id
}
}
};
4. Deploy the contract
Put your own contract in the contracts directory, remember not to delete the ./contract/Migrations.sol contract, which is used by Truffle to help with deployment.
Next, create a 2_deploy_contract.js in the migrations directory and add the following:
var Voting = artifacts.require("Voting");
module.exports = function(deployer) {
deployer.deploy(Voting) ; //Configure the release of the contract
};
After the configuration is complete, enter the following command in the console to compile the contract
truffle compile
After the contract is compiled successfully, a json file will be generated in build/contracts.
Then enter the following command to deploy the contract
truffle migrate
If the deployment is successful, you will see the address of the contract in the console. (Every time you modify the contract or restart, you need to recompile and deploy)
5. ethereum client installation, here we use the ganache installation address: http://truffleframework.com/ganache/
After the installation is complete, start the client and you will see the following interface:
6. Install metamask
metamask is a browser plug-in installation address: https://metamask.io/ and then click on the upper left corner, cunstom RPC Next, copy the privateKey of the first address from the ganache client back to the upper right corner of metamask and the Import Account is basically configured.
operation result
After the above configuration is completed, keep ganache and metamask in the startup state, and enter the following command in the console to start the project
npm run dev
In the console of the browser, you can observe some related output of the project for debugging, enter the voter's name on the webpage and submit it. If there is no accident, metamask will automatically pop up to let you confirm the transaction. Click submit to
see it in ganache The process of sending a transaction.
The next step is to write js, which can be taught by yourself through the following documents:
http://truffleframework.com/docs/
Demo地址 : https://github.com/jsphLim/truffle-Voting
欢迎star~