<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      【工具使用】利用 Web3.js 在 ganache 上部署以及調(diào)用智能合約

      合約部署

      要部署的合約

      pragma solidity ^0.4.23;
      contract  test  {
          uint256 value;
          
          function setValue(uint256 _value) public{
               value = _value;
          }
          
          function getValue() public returns (uint256){
              return value;
          }
          
          function () public payable{
              
          }
      }
      

      獲取合約的ABI和bytecode

      合約ABI

      [
          {
            "payable": true,
            "stateMutability": "payable",
            "type": "fallback"
          },
          {
            "constant": false,
            "inputs": [
              {
                "name": "_value",
                "type": "uint256"
              }
            ],
            "name": "setValue",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
          },
          {
            "constant": false,
            "inputs": [],
            "name": "getValue",
            "outputs": [
              {
                "name": "",
                "type": "uint256"
              }
            ],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
          }
      ]
      

      合約bytecode

      // add '0x' in front of the bytecode
      0x608060405234801561001057600080fd5b5060dc8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632096525514604b57806355241077146073575b005b348015605657600080fd5b50605d609d565b6040518082815260200191505060405180910390f35b348015607e57600080fd5b50609b6004803603810190808035906020019092919050505060a6565b005b60008054905090565b80600081905550505600a165627a7a72305820437e5b6f23c9e202f4188fde72ebdd65de3a5c7fca347bea516a2f576748a9240029
      

      部署代碼

      const Web3 = require('web3')
      // RPC SERVER
      const web3 = new Web3('http://localhost:7545')
      var Tx     = require('ethereumjs-tx').Transaction
      
      // your account and private key
      const account = '0x1275270073b7CA41Cd1a62736795f80f7b52487c'
      const pk1 = '56a0717cbc04b0e47732d5be497ad57052594c03088b9ef889fefca107ffeecb'
      const private_key = Buffer.from(pk1, 'hex')
      
      // get the nonce of account
      web3.eth.getTransactionCount(account, (err, txCount) => {
      	
      	// the bytecode of contract
        const data = "0x608060405234801561001057600080fd5b5060dc8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632096525514604b57806355241077146073575b005b348015605657600080fd5b50605d609d565b6040518082815260200191505060405180910390f35b348015607e57600080fd5b50609b6004803603810190808035906020019092919050505060a6565b005b60008054905090565b80600081905550505600a165627a7a72305820437e5b6f23c9e202f4188fde72ebdd65de3a5c7fca347bea516a2f576748a9240029"
      
        // set transaction object
        const txObject = {
          nonce:    web3.utils.toHex(txCount),
          gasLimit: web3.utils.toHex(6721975),
          gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
          data: data
        }
      
        // sign transaction
        const tx = new Tx(txObject)
        tx.sign(private_key)
      
        const serializedTx = tx.serialize()
        const raw = '0x' + serializedTx.toString('hex')
      
        // send transaction
        web3.eth.sendSignedTransaction(raw, (err, txHash) => {
          console.log('txHash:', txHash)
        })
      })
      

      運(yùn)行結(jié)果

      image
      注意一個(gè)問(wèn)題,和利用 truffle 部署的智能合約不同,直接用 Web3.js 部署的合約是不會(huì)顯示在 ganache 的 Contract頁(yè)面里的,也就是說(shuō),它存在,只是沒(méi)顯示。

      合約調(diào)用

      調(diào)用代碼

      var Tx     = require('ethereumjs-tx').Transaction
      // search their github for the detail of usage of 'Common' class
      const Common = require('ethereumjs-common').default
      const Web3 = require('web3')
      const web3 = new Web3('http://localhost:7545')
      
      const account = '0x1275270073b7CA41Cd1a62736795f80f7b52487c'
      const pk1 = '56a0717cbc04b0e47732d5be497ad57052594c03088b9ef889fefca107ffeecb'
      const private_key = Buffer.from(pk1, 'hex')
      
      const contractAddress = '0x6F20B2F0149A680116411a01d5Bc6D4B09E869F4'
      const contractABI = [
          {
            "payable": true,
            "stateMutability": "payable",
            "type": "fallback"
          },
          {
            "constant": false,
            "inputs": [
              {
                "name": "_value",
                "type": "uint256"
              }
            ],
            "name": "setValue",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
          },
          {
            "constant": false,
            "inputs": [],
            "name": "getValue",
            "outputs": [
              {
                "name": "",
                "type": "uint256"
              }
            ],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
          }
        ]
      
      // initialize the contract object
      const contract = new web3.eth.Contract(contractABI, contractAddress)
      
      // call the setValue() function with parameter 123
      const calldateABI = contract.methods.setValue(123).encodeABI()
      
      // All of these network's params are the same than mainnets', except for name, chainId, and
      // networkId, so we use the Common.forCustomChain method.
      const customCommon = Common.forCustomChain(
        'mainnet',
        {
      		// set the parameter as your ganache
          name: 'my-network',
          networkId: 5777,
          chainId: 1337,
        },
        'petersburg',
      )
      
      web3.eth.getTransactionCount(account, (err, count) => {
      	const txObject = {
      	    nonce:    web3.utils.toHex(count),
      			// set the gas limit as same as of ganache's
      	    gasLimit: web3.utils.toHex(6721975),
      	    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
      	    to: contractAddress,
      		// contract.methods.<function name>(<parameter>).encodeABI()
      	    data: contract.methods.setValue(123).encodeABI()
      		}
      		
      	// We pass our custom Common object whenever we create a transaction
      	const transaction = new Tx(txObject, { common: customCommon },)
      	console.log('transaction:', transaction)
      	
      	// sign the transaction
      	transaction.sign(private_key)
      	console.log('transaction.sign:', transaction)
      	
      	// returns the rlp encoding of the transaction
      	const serializedTx = transaction.serialize()
      	const raw = '0x' + serializedTx.toString('hex')
      	
      	web3.eth.sendSignedTransaction(raw, (err, txHash) => {
          console.log('txHash:', txHash)
      	if(err){
      	    throw err;
      		}
      	})
      })
      

      運(yùn)行結(jié)果

      image

      參考文章

      1. 奇客谷:web3.js 教程
      2. 關(guān)于ethereumjs-tx在私鏈部署
      3. ethereumjs-common 調(diào)用方法
      posted @ 2021-06-16 17:23  ACai_sec  閱讀(834)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 国产成人AV一区二区三区在线| 乱人伦人妻精品一区二区| 国产免费性感美女被插视频| 亚洲国产午夜福利精品| 亚洲毛片多多影院| 最近中文字幕完整版2019| 日韩区中文字幕在线观看| 国产精品中文一区二区| 无码粉嫩虎白一线天在线观看| 国产精品无码av在线一区| 中文字幕人妻不卡精品| 国产乱码精品一区二区三区四川人 | 日本一本无道码日韩精品| 69精品丰满人妻无码视频a片| 亚洲欧美日韩综合一区在线 | 三人成全免费观看电视剧高清| 亚洲精品国产摄像头| 久久亚洲色WWW成人男男| 又黄又爽又无遮挡免费的网站| 国内在线视频一区二区三区| 一区二区三区四区五区黄色| 双腿张开被5个男人调教电影| 亚洲成人精品综合在线| 起碰免费公开97在线视频| 日韩精品一区二区三区影院| 综合偷自拍亚洲乱中文字幕| 久久久久亚洲精品无码系列| 美女无遮挡免费视频网站| 亚洲乱熟乱熟女一区二区| 日韩精品av一区二区三区| 精品国产不卡在线观看免费| 四虎永久播放地址免费| 久久国产乱子伦免费精品| 国产精品无码一区二区牛牛| 国产suv精品一区二区四| 97久久精品无码一区二区天美| 欧美三级欧美成人高清| 久久精品国产91精品亚洲| 亚洲欭美日韩颜射在线二| 亚洲男人的天堂网站| 激情啪啪啪一区二区三区|