以太坊智能合約部署與交互的方法是什么

這篇文章主要介紹“以太坊智能合約部署與交互的方法是什么”,在日常操作中,相信很多人在以太坊智能合約部署與交互的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”以太坊智能合約部署與交互的方法是什么”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)專注于麒麟企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站開發(fā)。麒麟網(wǎng)站建設(shè)公司,為麒麟等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

啟動(dòng)容器來執(zhí)行g(shù)eth命令

root@ubu-blockchain2:~# docker run -i blockchain101/ethereum-geth:1.6.5 geth attach http://45.32.252.88:8201Welcome to the Geth JavaScript console!instance: Geth/01/v1.6.5-stable/linux-amd64/go1.8
coinbase: 0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a
at block: 6064 (Wed, 02 Aug 2017 01:13:50 UTC)
 datadir: /ethcluster/779977/data/01
 modules: admin:1.0 eth:1.0 net:1.0 rpc:1.0 web3:1.0

查看我們的賬戶和余額

eth.getBalance(eth.accounts[0])11000000000000000000eth.getBalance(eth.accounts[1])0eth.accounts[0]"0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a"> eth.accounts[1]"0xe82e2f0a5abd8774767b9751659976f9c4f59181"

發(fā)起一筆交易

eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(3,'ether')})

"0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec"

查看交易細(xì)節(jié)

>  eth.getTransaction("0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec")

{
  blockHash: "0x3115703894dc6015c96ef4de3e5615f416498ca1f985902b38cd70e27dab8871", blockNumber: 1250, from: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a", gas: 90000, gasPrice: 18000000000, hash: "0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec", input: "0x", nonce: 0, r: "0x2aef2c1fa03a0fa4172d21e3383d8c0431d45ec855b9d16efdd5eb2de90c414c", s: "0xc4d530fb7902bf509fe56bfbea4861bf6cc16791afc9c9103c1a18f77407d1f", to: "0xe82e2f0a5abd8774767b9751659976f9c4f59181", transactionIndex: 0, v: "0x17cdb6", value: 3000000000000000000 }
> eth.getBalance(eth.accounts[1])

3000000000000000000

驗(yàn)證用戶0的余額

> eth.getBalance(eth.accounts[0])7999622000000000000

編寫一個(gè)簡(jiǎn)單的合約

contract Sample {uint public value;

          function Sample(uint v){value=v;
          }

          function set(uint v){value=v;
          }

          function get() constant returns (uint){return value;
          }
}

在remix網(wǎng)頁(yè)編譯得到ABI接口和合約的二進(jìn)制代碼、

 abi=[{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"v","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"v","type":"uint256"}],"payable":false,"type":"constructor"}]



[{
    constant: true,
    inputs: [],
    name: "value",
    outputs: [{
        name: "",
        type: "uint256"}],
    payable: false,
    type: "function"}, {
    constant: false,
    inputs: [{
        name: "v",
        type: "uint256"}],
    name: "set",
    outputs: [],
    payable: false,
    type: "function"}, {
    constant: true,
    inputs: [],
    name: "get",
    outputs: [{
        name: "",
        type: "uint256"}],
    payable: false,
    type: "function"}, {
    inputs: [{
        name: "v",
        type: "uint256"}],
    payable: false,
    type: "constructor"}]

需要使用eth.contract來定義一個(gè)合約類

> sample=eth.contract(abi)

{
  abi: [{
      constant: true,
      inputs: [],
      name: "value",
      outputs: [{...}],
      payable: false,      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "set",
      outputs: [],
      payable: false,      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,      type: "function"
  }, {
      inputs: [{...}],
      payable: false,      type: "constructor"
  }],
  eth: {
    accounts: ["0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a", "0xe82e2f0a5abd8774767b9751659976f9c4f59181"],
    blockNumber: 6225,
    coinbase: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()},
    defaultAccount: undefined,
    defaultBlock: "latest",
    gasPrice: 18000000000,
    hashrate: 0,
    mining: false,
    pendingTransactions: [],
    protocolVersion: "0x3f",
    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),
    getPendingTransactions: function(callback),
    getProtocolVersion: function(callback),
    getRawTransaction: function(),
    getRawTransactionFromBlock: function(),
    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()}

合約的二進(jìn)制代碼賦值給SampleHEX方便使用

SampleHEX="0x6060604052341561000c57fe5b60405160208061013a833981016040528080519060200190919050505b806000819055505b505b60f9806100416000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514604e57806360fe47b11460715780636d4ce63c14608e575bfe5b3415605557fe5b605b60b1565b6040518082815260200191505060405180910390f35b3415607857fe5b608c600480803590602001909190505060b7565b005b3415609557fe5b609b60c2565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b600060005490505b905600a165627a7a72305820208c8101070c8ba5a9b32db2bf4b8062a9ba50bc2869c39ac2297938756540e80029"

把合約代碼部署上鏈

> thesample=sample.new(1,{from:eth.accounts[0],data:SampleHEX,gas:3000000})

{
  abi: [{
      constant: true,
      inputs: [],
      name: "value",
      outputs: [{...}],
      payable: false,
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "set",
      outputs: [],
      payable: false,
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,
      type: "function"
  }, {
      inputs: [{...}],
      payable: false,
      type: "constructor"
  }],
  address: undefined,
  transactionHash: "0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1"}

查看交易細(xì)節(jié)

> samplerecpt=eth.getTransactionReceipt("0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1")

{
  blockHash: "0xddba16545af882835fb9a69a0e5f3b9287c61664837d5ea0068b38575cb665c5",
  blockNumber: 6246,
  contractAddress: "0x7504fa9d64ab290844b82660d43b310f8fba0276",
  cumulativeGasUsed: 141836,  from: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a",
  gasUsed: 141836,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0xd1093ecaca9cc0d10e82a533a15feccedf7ff5c79fb3ebd9366ec0b35dbef478",  to: null,
  transactionHash: "0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1",
  transactionIndex: 0}

合約命名

> samplecontract=sample.at("0x7504fa9d64ab290844b82660d43b310f8fba0276")

{
  abi: [{
      constant: true,
      inputs: [],
      name: "value",
      outputs: [{...}],
      payable: false,
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "set",
      outputs: [],
      payable: false,
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "get",
      outputs: [{...}],
      payable: false,
      type: "function"
  }, {
      inputs: [{...}],
      payable: false,
      type: "constructor"
  }],
  address: "0x7504fa9d64ab290844b82660d43b310f8fba0276",
  transactionHash: null,
  allEvents: function(),
  get: function(),
  set: function(),
  value: function()
}

合約查看功能函數(shù)get(),然后調(diào)用set()函數(shù),再get()查看時(shí)已經(jīng)改變了

samplecontract.get.call()1> samplecontract.set.sendTransaction(9, {from:eth.accounts[0], gas:3000000})"0x822ee6fb4caceb7e844c533f7f3bc57806f7cb3676fb3066eb848cca46b2f38a"> samplecontract.get.call()9

我們?cè)俅蜷_一個(gè)終端,打開cluster1的peer02的控制臺(tái),直接at到上一個(gè)終端部署的智能合約地址并進(jìn)行set操作

root@ubu-blockchain2:~/ethereum-docker/ethereum-docker/ethereum-testnet-docker/dockercomposefiles# docker run -i blockchain101/ethereum-geth:1.6.5 geth attach http://45.32.252.88:9201Welcome to the Geth JavaScript console!


>  abi=[{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"v","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"v","type":"uint256"}],"payable":false,"type":"constructor"}]


> sample=eth.contract(abi)SampleHEX="0x6060604052341561000c57fe5b60405160208061013a833981016040528080519060200190919050505b806000819055505b505b60f9806100416000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514604e57806360fe47b11460715780636d4ce63c14608e575bfe5b3415605557fe5b605b60b1565b6040518082815260200191505060405180910390f35b3415607857fe5b608c600480803590602001909190505060b7565b005b3415609557fe5b609b60c2565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b600060005490505b905600a165627a7a72305820208c8101070c8ba5a9b32db2bf4b8062a9ba50bc2869c39ac2297938756540e80029"

直接把合約地址賦值并進(jìn)行set操作

samplecontract=sample.at("0x7504fa9d64ab290844b82660d43b310f8fba0276")

> samplecontract.get.call()9

簡(jiǎn)單的方法來了。智能合約的部署需要編譯,這里用在線編譯: 
https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.14+commit.c2215d46.js 
修改編譯好的abi和對(duì)象名稱:

這里在網(wǎng)上找了個(gè)代幣的只能合約,可以進(jìn)行充值、轉(zhuǎn)賬和查詢,issue 函數(shù)可以向充值以太到合約賬戶,transfer 函數(shù)可以向其他賬號(hào)發(fā)送token,getBalance 函數(shù)可以獲取某個(gè)賬號(hào)的token余額,代碼如下:
pragma solidity ^0.4.2;
contract Token {
             address issuer;
             mapping (address => uint) balances;

             event Issue(address account, uint amount);
             event Transfer(address from, address to,uint amount);             function Token() {
                 issuer = msg.sender;
             }             function issue(address account, uintamount) {                 if (msg.sender != issuer) throw;
                 balances[account] += amount;
             }             function transfer(address to, uint amount){                 if (balances[msg.sender] < amount)throw;

                 balances[msg.sender] -= amount;
                 balances[to] += amount;

                 Transfer(msg.sender, to, amount);
             }             function getBalance(address account)constant returns (uint) {                 return balances[account];
             }
         }
修改編譯好的gas和對(duì)象名稱:
varbrowser_untitled_sol_tokenContract =web3.eth.contract([{"constant":false,"inputs":[{"name":"account","type":"address"},{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"account","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}]);var token =browser_untitled_sol_tokenContract.new(
   {
     from: web3.eth.accounts[0],
     data:'0x6060604052341561000f57600080fd5b5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b5b6103d2806100616000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b414610054578063a9059cbb14610096578063f8b2cb4f146100d8575b600080fd5b341561005f57600080fd5b610094600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610125565b005b34156100a157600080fd5b6100d6600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101d2565b005b34156100e357600080fd5b61010f600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061035c565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561018057600080fd5b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561021e57600080fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b5050565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b9190505600a165627a7a723058204afe007a03446d43d13ac892e6dba9d032f540a11ff427d26c22560727cbea2f0029',
     gas: '4300000'
   }, function (e, contract){
    console.log(e, contract);if (typeof contract.address !=='undefined') {
         console.log('Contract mined! address:' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

此時(shí)輸入合約部署的實(shí)例a_demotypes, 可以看到a_demotypes的詳情。

控制臺(tái)調(diào)用

> a_demotypes
{
  abi: [{
      constant: false,
      inputs: [{...}],
      name: "f",
      outputs: [{...}],
      payable: false,
      type: "function"
  }],
  address: "0x54ed7a5f5a63ddada3bfe83b3e632adabaa5fc2f",
  transactionHash: "0x69cde62bcd6458e14f40497f4840f422911d63f5dea2b3a9833e6810db64a1c9",
  allEvents: function(),
  f: function()
}

這里重點(diǎn)就是address表示的是合約的地址,你會(huì)發(fā)現(xiàn)這個(gè)和賬號(hào)的信息結(jié)果一樣,其實(shí)你也可以把這個(gè)合約地址看做是一個(gè)賬號(hào)地址,后面我們外部調(diào)用到的就是這個(gè)合約地址。

充值token.issue.sendTransaction(eth.accounts[0],100, {from: eth.accounts[0]});

發(fā)送 tokentoken.transfer(eth.accounts[1], 30, {from:eth.accounts[0]})

查看余額token.getBalance()
控制臺(tái)調(diào)用就不多說,和Java對(duì)象調(diào)用一樣,直接調(diào)用即可

外部接口與智能合約交互 
以太坊對(duì)外提供的有很多接口JSON RPC接口,web3接口,這里我們用JSON RPC接口。 

合約交互的原理 
合約的交互都是一次交易,而我們要做的就是把要調(diào)用的方法和參數(shù)按照api規(guī)定的以參數(shù)的形式向區(qū)塊請(qǐng)求一次交易,ethereum接收到我們的請(qǐng)求后通過解析傳遞的參數(shù)來執(zhí)行相關(guān)的合約代碼。 
RPC接口給我們提供了倆個(gè)方法:eth_sendTransaction和eth_call。

eth_sendTransaction
Createsnew message call transaction or a contract creation, if the data field containscode.
ParametersObject - The transaction objectfrom: DATA, 20 Bytes - The address the transaction is send from.to: DATA, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.
gas: QUANTITY - (optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.
gasPrice: QUANTITY - (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gas
value: QUANTITY - (optional) Integer of the value send with this transaction
data: DATA - The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI
nonce: QUANTITY - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.

可以看到,如果我們創(chuàng)建的為合約時(shí),我們只需要from,to(文檔上寫的是可選的,但是實(shí)際操作中沒有to為null的話合約不能正常執(zhí)行,建議還是加上,這個(gè)值就是前面我們部署合約后生成的合約address),data。Data的屬性的值具體可以看Contract ABI。這里大概說下: 
Data的值相對(duì)來說不是固定的,具體怎么生成與合約的參數(shù)類型,參數(shù)數(shù)量都有關(guān)聯(lián)。這里我們以部署的token合約的三個(gè)方法為例:

充值issue (address account, uint amount) 
這個(gè)方法有倆個(gè)參數(shù),address充值賬號(hào),uint充值數(shù)量。 
根據(jù)Contract ABI,data值應(yīng)該為方法名的sha3的前8個(gè)字節(jié)+參數(shù)的64字節(jié),不夠前面補(bǔ)充為0。 
這里方法名并不是issue (address account, uint amount)而是issue(address,uint256)的sha3值。

web3.sha3("issue(address,uint256)")"0x867904b44b606800f6f10498e11292d04ea19bfc7fe4bc0f1695aa516381f73d"

我們往第一個(gè)賬號(hào)充值10,這里的數(shù)據(jù)不是以太幣,而是我們自己創(chuàng)建的代幣。

 eth.accounts[0]"0x0cc9684af605bae10de801218321c1336bb62946"

將10轉(zhuǎn)換為16進(jìn)制為

000000000000000000000000000000000000000000000000000000000000000a

那么data的數(shù)據(jù)為:

0x867904b4000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb000000000000000000000000000000000000000000000000000000000000000a

那么最后我們調(diào)用eth_sendTransaction方法傳遞參數(shù)JSON對(duì)象為:

{
    from:0xfdf57e81872562a6112656f961944ce821fdf7eb, to:0x7fe133950fc010ce41322d88f64f1918b9abb3a3, data: 0x867904b4000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb000000000000000000000000000000000000000000000000000000000000000a }

返回:此交易的hash值,此時(shí)該交易還沒有執(zhí)行,只是創(chuàng)建,還需要礦工通過挖礦才能完成。 
調(diào)用接口方法:

JsonRpcHttpClient client = newJsonRpcHttpClient(new URL(“http://127.0.0.1:8545”));Object result = client.invoke(methodName,params, Object.class);

通過控制臺(tái)查看第一個(gè)賬號(hào)已有代幣:

token.getbalance(eth.accounts[0])

執(zhí)行調(diào)用接口代碼。返回交易hash值:

0x2013764d1c3fea680f9015353497aa5f9f8d61580a3bd0524b3613b34329c095

此時(shí)控制臺(tái)輸入: 
以太坊智能合約部署與交互的方法是什么

交易和充值一樣,需要注意的是代幣轉(zhuǎn)出賬號(hào)為from屬性的值,代幣轉(zhuǎn)入賬號(hào)為data屬性里的值,to對(duì)應(yīng)的是合約地址。 
eth_call

Executes anew message call immediately without creating a transaction on the block chain.
Parameters

Object - The transaction call objectfrom: DATA, 20 Bytes - (optional) The address the transaction is sent from.to: DATA, 20 Bytes - The address the transaction is directed to.
gas: QUANTITY - (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.
gasPrice: QUANTITY - (optional) Integer of the gasPrice used for each paid gas
value: QUANTITY - (optional) Integer of the value send with this transactiondata: DATA - (optional) Hash of the method signature and encoded parameters. For details seeEthereum Contract ABI
QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter

這個(gè)方法返回一條信息給我們,相當(dāng)于數(shù)據(jù)庫(kù)的查詢功能,參數(shù)也是三個(gè),from,to,data,數(shù)據(jù)格式都是一樣的。 
查詢getBalance(address account) 
查詢方法hash碼:

 web3.sha3("getBalance(address)")"0xf8b2cb4f3943230388faeee074f1503714bff212640051caba01411868d14ae3"

查詢我們上一步充值的賬號(hào),那么傳遞的參數(shù)data為:

0xf8b2cb4f000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb

eth_call方法最后參數(shù)為:

{
       from: 0xfdf57e81872562a6112656f961944ce821fdf7eb, to:0x7fe133950fc010ce41322d88f64f1918b9abb3a3, data:0xf8b2cb4f000000000000000000000000fdf57e81872562a6112656f961944ce821fdf7eb }

注意,這個(gè)方法需要倆參數(shù),處理一個(gè)JSONobject外,還有一個(gè)字符串參數(shù),這倆可以為“”或者”latest”, “earliest” or “pending” 
調(diào)用接口返回一個(gè)16進(jìn)制字符串: 
0x0000000000000000000000000000000000000000000000000000000000000071就是該賬號(hào)的代幣數(shù)量,轉(zhuǎn)換為十進(jìn)制為:113,與控制查詢一致。

這就是一個(gè)智能合約的交互過程。是不是很簡(jiǎn)單啊。 

到此,關(guān)于“以太坊智能合約部署與交互的方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

當(dāng)前名稱:以太坊智能合約部署與交互的方法是什么
本文路徑:http://muchs.cn/article28/gppecp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、外貿(mào)網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、App開發(fā)小程序開發(fā)、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)