Preface
In the last article, we introduced Geth installation, and today we describe how to deploy smart contracts.If you haven't read the last introductory article yet, I suggest you read the last one first Guidelines for Taifang Block Chain Development from Zero .Deploying smart contracts first requires a contract file written by solidity and a framework for developing Ethernet Truffle
Install Solc
solc is the compiler used to compile smart contract files.
npm install -g solc
Check if the installation is successful after the installation is complete.
$solcjs --version 0.4.19+commit.c4cbbb05.Emscripten.clang
The output version number indicates that the installation was successful.
Install Truffle
npm install -g truffle
After installing Truffle, we run the command.
truffle version
output
Truffle v4.0.3 (core: 4.0.3) Solidity v0.4.18 (solc-js)
Indicates that we have successfully installed Truffle.
Create solidity project
- We select a directory to create a solidity project that runs under
truffle init
This command automatically creates a project and associated files.And created a contract file for us, Migrations.sol
- Create vote.sol
Add the Voting.sol file to the contracts folder created by truffle init above. This is a voting application. As long as you have programming experience, I believe the solidity syntax is not difficult for you. copy in the solidity code below
pragma solidity ^0.4.2; contract Voting{ //voter struct struct Voter{ bytes32 name; bool voted;//is voted or not uint vote;//vote who uint givenRightTime;//aurth time uint votetime;//vote time } struct Proposal{ bytes32 name; uint voteCount; } address public chairperson;//vote initiator mapping(address=>Voter)public voters;//voters Proposal[] public proposals;//can vote to who //constructor function Voting(bytes32[] proposalNames) public{ chairperson = msg.sender; //init proposals for(uint i = 0;i<proposalNames.length;i++){ proposals.push(Proposal({name:proposalNames[i],voteCount:0})); } } function giveRightToVote(address voter,bytes32 voterName) public{ if(msg.sender != chairperson || voters[voter].voted){ revert(); } voters[voter].name = voterName; voters[voter].voted = false; voters[voter].votetime = 0 ; voters[voter].givenRightTime = now; } //vote function vote(uint proposalIndex)public{ Voter storage sender = voters[msg.sender]; //check is voted if(sender.voted){ revert(); } //modify sender status sender.voted = true ; sender.votetime = now; sender.vote = proposalIndex; proposals[proposalIndex].voteCount += 1; } //get winner function winningProposalIndex()public constant returns(uint winningProposalIndex){ uint winningVoteCount = 0; for(uint p = 0 ;p<proposals.length;p++){ if(proposals[p].voteCount > winningVoteCount){ winningVoteCount = proposals[p].voteCount; winningProposalIndex = p ; } } } function winnerName()public constant returns(bytes32 winnerName){ winnerName = proposals[winningProposalIndex()].name; } }
- Compile contracts using Truffle
Open truffle.js inside the project and copy in the following configuration.
module.exports = { // See <http://truffleframework.com/docs/advanced/configuration> // to customize your Truffle configuration! networks: { development: { host: "localhost", port: 8081, network_id: "*", // Match any network id gas: 1000000 } } };
host and port are the IP addresses and ports of one of your local Ethernet nodes, gas is the coin for deployment, and then open 1_initial_migration.js under the migrations folder of the project file to change the file you want to deploy to Voting.sol.
// var Migrations = artifacts.require("./Migrations.sol"); var Voting = artifacts.require("./Voting.sol"); module.exports = function(deployer) { deployer.deploy(Voting,['hexo','tfboys']); };
- truffle deployment contract
Function
truffle compile
Run truffle migrate
truffle migrate
The corresponding output can be seen on the corresponding Ethernet node
This proves that our contract has been deployed to our private chain.
Run Command
txpool.status
We run
miner.start()
Then the truffle appears
Apparently our contract deployment was successful.
Okay, we've successfully deployed a contract, and in the next chapter we'll show you how to invoke it.