Hello World - A Belief in Smart Contracts

foreword

One of the most important rituals for any programmer to learn a new programming language is to write a Hello World!program that can output. When done, this opens the door to a new world. This article mainly introduces how to use a smart contract to output one Hello World!. The step instructions in this article are very simple (but the steps are complete). If you want to see the detailed step instructions, please refer to this article on smart contract writing examples , the example descriptions are more detailed.

Open the console of the test network

Enter the following command:

geth --datadir "~/ethdev" --dev console 2>> geth.log

In this way, we open the console of the test network, and the subsequent steps will be completed in the console unless otherwise specified.

Write smart contracts

> source = "contract test { function hello() returns(string s){return 'hello world!';}}"
"contract test { function hello() returns(string s){return 'hello world!';}}"

Compile the smart contract

> contract = eth.compile.solidity(source).test

Specify the external account to create the contract

We first need to select an external account from the current account as an external account for creating smart contracts:

> address = eth.accounts[0]
"0x62b1746767522b36f6421e630fa0198151d72964"

Then activate the account:

> personal.unlockAccount(address,'123456',10000)
true

Finally, start mining and prepare for processing transactions:

> miner.start()
true

deploy contract

Deploying a contract is to deploy the compiled contract bytecode to the Ethereum blockchain in the form of sending transactions through an external account. Enter the following command:

> abi = [{constant:false,inputs:null}]
[{
    constant: false,
    inputs: null
}]
> MyContract = eth.contract(abi)
{
  abi: [{
      constant: false,
      inputs: null
  }],
  eth: {
    accounts: ["0x62b1746767522b36f6421e630fa0198151d72964", "0xaa79b5468a4f4cf35a32cc976f689a5db5873f0c"],
    blockNumber: 600,
    coinbase: "0x62b1746767522b36f6421e630fa0198151d72964",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()
    },
    defaultAccount: "0x62b1746767522b36f6421e630fa0198151d72964",
    defaultBlock: "latest",
    gasPrice: 20000000000,
    hashrate: 3302,
    mining: true,
    pendingTransactions: [],
    syncing: false,
    call: function(),
    contract: function(abi),
    estimateGas: function(),
    filter: function(fil, callback),
    getAccounts: function(callback),
    getBalance: function(),
    getBlock: function(),
    getBlockNumber: function(callback),
    getBlockTransactionCount: function(),
    getBlockUncleCount: function(),
    getCode: function(),
    getCoinbase: function(callback),
    getCompilers: function(),
    getGasPrice: function(callback),
    getHashrate: function(callback),
    getMining: function(callback),
    getNatSpec: function(),
    getPendingTransactions: function(callback),
    getStorageAt: function(),
    getSyncing: function(callback),
    getTransaction: function(),
    getTransactionCount: function(),
    getTransactionFromBlock: function(),
    getTransactionReceipt: function(),
    getUncle: function(),
    getWork: function(),
    iban: function(iban),
    icapNamereg: function(),
    isSyncing: function(callback),
    namereg: function(),
    resend: function(),
    sendIBANTransaction: function(),
    sendRawTransaction: function(),
    sendTransaction: function(),
    sign: function(),
    signTransaction: function(),
    submitTransaction: function(),
    submitWork: function()
  },
  at: function(address, callback),
  getData: function(),
  new: function()
}
> myContract = MyContract.new({from:address,data:contract.code})
{
  abi: [{
      constant: false,
      inputs: null
  }],
  address: undefined,
  transactionHash: "0x9f0f739a5827796fc1466a09e7771eef0978262a007f8c75c789cdcf1d9c3d4b"
}

At this point, we can check the transaction pool to see the pending status of the current transaction:

> txpool.status
{
  pending: 1,
  queued: 0
}

Wait patiently for a while, wait for the miner to confirm the completion, we use the txpool.statuscommand again to check the status of the transaction pool:

> txpool.status
{
  pending: 0,
  queued: 0
}

This means that the transaction has been successfully processed.

Interact with contracts

First we need to use eth.contractto define a contract class, the defined contract class conforms to ABI definition 1 :

> Hello = eth.contract(contract.info.abiDefinition)
{
  abi: [{
      constant: false,
      inputs: [],
      name: "hello",
      outputs: [{...}],
      payable: false,
      type: "function"
  }],
  eth: {
    accounts: ["0x62b1746767522b36f6421e630fa0198151d72964", "0xaa79b5468a4f4cf35a32cc976f689a5db5873f0c"],
    blockNumber: 623,
    coinbase: "0x62b1746767522b36f6421e630fa0198151d72964",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()
    },
    defaultAccount: "0x62b1746767522b36f6421e630fa0198151d72964",
    defaultBlock: "latest",
    gasPrice: 20000000000,
    hashrate: 3158,
    mining: true,
    pendingTransactions: [],
    syncing: false,
    call: function(),
    contract: function(abi),
    estimateGas: function(),
    filter: function(fil, callback),
    getAccounts: function(callback),
    getBalance: function(),
    getBlock: function(),
    getBlockNumber: function(callback),
    getBlockTransactionCount: function(),
    getBlockUncleCount: function(),
    getCode: function(),
    getCoinbase: function(callback),
    getCompilers: function(),
    getGasPrice: function(callback),
    getHashrate: function(callback),
    getMining: function(callback),
    getNatSpec: function(),
    getPendingTransactions: function(callback),
    getStorageAt: function(),
    getSyncing: function(callback),
    getTransaction: function(),
    getTransactionCount: function(),
    getTransactionFromBlock: function(),
    getTransactionReceipt: function(),
    getUncle: function(),
    getWork: function(),
    iban: function(iban),
    icapNamereg: function(),
    isSyncing: function(callback),
    namereg: function(),
    resend: function(),
    sendIBANTransaction: function(),
    sendRawTransaction: function(),
    sendTransaction: function(),
    sign: function(),
    signTransaction: function(),
    submitTransaction: function(),
    submitWork: function()
  },
  at: function(address, callback),
  getData: function(),
  new: function()
}

Then get the contract instance:

> hello = Hello.at(myContract.address)
{
  abi: [{
      constant: false,
      inputs: [],
      name: "hello",
      outputs: [{...}],
      payable: false,
      type: "function"
  }],
  address: "0x67392c276e261d916243b7ba200d2c005d35ae57",
  transactionHash: null,
  allEvents: function(),
  hello: function()
}

Use call()call:

> hello.hello.call()
"hello world!"

The ceremony is complete!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325375459&siteId=291194637