Step-by-step learning Blockchain (5) Smart contracts __ Blockchain

Source: Internet
Author: User
Tags constant
Ethereum Blockchain Technology version 2.0 the main feature of the development of industry applications is the realization of smart contracts, which are essentially event-driven,
A program that has status that runs on a copy, shared ledger, and that is capable of keeping assets on the ledger.
It is an operation that can be trusted and always executed in accordance with the prior rules. But at the same time, the inability to modify a smart contract after it has been deployed can lead to other problems, another issue. The dry goods are directly below.
This article is based on the go client through the command line to complete the writing, publishing, invocation of the smart contract.
Reference Project Address: http://www.ethdocs.org/en/latest/contracts-and-transactions/contracts.html
1, install solidity Intelligent Contract development language
Brew tap Ethereum/ethereum
Brew Install solidity
which SOLC
2. Open the command line, enter the private chain created before, and set the log output file
Jwter-wifi:csdnblog jwter$ geth--datadir "privatechain" console 2>> log_output Welcome to the
geth JavaScript C onsole!

instance:geth/v1.4.18-stable-c72f5459/darwin/go1.7.3
coinbase:0x56a957bbde2b2dcb6321c9aceb11c21d6dfe68f9
at block:164 (Sat, 16:29:24 CST)
 datadir:privatechain
 modules:admin:1.0 debug:1.0 eth:1.0 Mine r:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0


3. Setting up the compilation environment

> web3.eth.getCompilers ()
["solidity"]
> Admin.setsolc ("/usr/local/bin/solc")
"Solc, the Solidity compiler commandline Interface\nversion:0.4.2+commit.af6afb04.darwin.appleclang
\n\npath:/usr/local /bin/solc "
4, write the smart contract and compile
> contractsource = "contract Test {function multiply (uint a) returns (UINT D) {return a * 7;}}" "Contract Test {function multiply (uint a) returns (UINT D) {return a * 7;}}" > Contract = eth.compile.solidity (contr Actsource). Test {code: "
0x606060405260308060106000396000f3606060405260e060020a6000350463c6888fa18114601c575b6002565b3
        46002576007600435026060908152602090f3 ", Info: {abidefinition: [{constant:false, inputs: [...], Name: "Multiply", outputs: [...], payable:false, type: "function"}], Compileroptio NS: "--bin--abi--userdoc--devdoc--add-std--optimize-o/var/folders/c6/1vhz7hcd7w9g883rwrn4vzvr0000gn/t/ solc271136546 ", CompilerVersion:" 0.4.2 ", Developerdoc: {methods: {}}, Language:" Solidity ", l
    Anguageversion: "0.4.2", Source: "Contract test {function multiply (uint a) returns (UINT D) {return a * 7;}}", Userdoc: {methods: {}}}}
5. Define the smart contract ABI and compile
> Abi = [{constant:false, Inputs:{name: ' A ', type: ' uint256 '}}]
[{
    constant:false,
    inputs: {
      name: ' A ",
      type:" uint256 "
    }
}]
> Myabi = eth.contract (ABI)
6. Pre-deployment: Unlock accounts, monitor log files
> accountaddress = eth.accounts[0]
"0X56A957BBDE2B2DCB6321C9ACEB11C21D6DFE68F9"
> Personal.unlockaccount (accountaddress)
Unlock account 0x56a957bbde2b2dcb6321c9aceb11c21d6dfe68f9
Passphrase: 
true
>
The monitoring log file requires a new window to execute the following command. Used to see if the deployed smart contracts are synced to the blockchain network
Jwter-wifi:csdnblog jwter$ tail-f Log_output
7. Deploy Smart Contracts
> mycontract = myabi.new ({from:accountaddress,data:contract.code})
{
  abi: [{
      constant:false,
      Inputs: {
        name: "A",
        type: "uint256"
      }
  ],
  address:undefined,
  transactionhash: " 0xf330f4affd3989d72a979410e3a53f3a4d2d4d832faaa6c1bb32f72998acbd4b "
}
8. View the deployment status and synchronize to the blockchain the smart contract must start mining mode is synchronized to the blockchain
Depending on the Monitoring Log window, you can see whether the transaction is executing to determine when to execute the Miner.stop ()
> Txpool.status
{
  pending:1,
  queued:0
}
> Miner.start ()
true
> Miner.stop ()
true
> Txpool.status
{
  pending:0,
  queued:0
}
9. Call the Smart Contract
> Contractabi = eth.contract (contract.info.abiDefinition)
> 
> testcontract = contractabi.at ( mycontract.address)
{
  abi: [{
      constant:false,
      inputs: [{...}],
      name: "Multiply",
      outputs: [{...}],
      payable:false,
      type: "function"
  }],
  address: " 0xcb0895d4b3b35b2a45a31fab853614c14e7759a4 ",
  transactionhash:null,
  allevents:function (),
  Multiply:function ()
}
> TestContract.multiply.call (3)
21

Note: Remember to save the address of the smart contract deployment, as well as the ABI parameters, because after deployment can not be modified,
The forgotten address cannot be called again, and the deployment of the smart contract consumes gas, and when completed, the user's balance is observed to change.


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.