Solidity

在 Web3 中執行方法時“this._eth.sendTransaction 不是函式”

  • May 28, 2018

我正在使用 react-web3 (它基本上給了我一個 web3 對象),我正在努力讓我的契約工作。這是我正在使用的程式碼:

const web3 = window.web3;
   const contract = new web3.eth.contract(ABI.abi).at("0xdbb05ed3b5acb77cbd2366d037fe051703958dac");
   contract.buyTicket.sendTransaction(3000, {
       from: '0x5c1a92217e456a7eB4a051B567FC751A534991a3',
       value: web3.toWei(1, 'ether')
   });

發件人是我在 MetaMask 中的預設地址。

我的契約:

"abi": [
   {
     "constant": false,
     "inputs": [],
     "name": "restartLottery",
     "outputs": [],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [
       {
         "name": "myid",
         "type": "bytes32"
       },
       {
         "name": "result",
         "type": "string"
       }
     ],
     "name": "__callback",
     "outputs": [],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [],
     "name": "toggleActive",
     "outputs": [],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [
       {
         "name": "myid",
         "type": "bytes32"
       },
       {
         "name": "result",
         "type": "string"
       },
       {
         "name": "proof",
         "type": "bytes"
       }
     ],
     "name": "__callback",
     "outputs": [],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [],
     "name": "getTotalPotSize",
     "outputs": [
       {
         "name": "",
         "type": "uint256"
       }
     ],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [
       {
         "name": "_estimate",
         "type": "uint256"
       }
     ],
     "name": "buyTicket",
     "outputs": [
       {
         "name": "",
         "type": "uint256"
       }
     ],
     "payable": true,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [],
     "name": "getActiveState",
     "outputs": [
       {
         "name": "",
         "type": "bool"
       }
     ],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [],
     "name": "getKrakenPrice",
     "outputs": [],
     "payable": true,
     "type": "function"
   },
   {
     "constant": true,
     "inputs": [],
     "name": "owner",
     "outputs": [
       {
         "name": "",
         "type": "address"
       }
     ],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [
       {
         "name": "_gameId",
         "type": "uint256"
       }
     ],
     "name": "getEstimationPerGame",
     "outputs": [
       {
         "name": "",
         "type": "uint256"
       }
     ],
     "payable": false,
     "type": "function"
   },
   {
     "constant": false,
     "inputs": [
       {
         "name": "_gameId",
         "type": "uint256"
       }
     ],
     "name": "getBuyersPerGame",
     "outputs": [
       {
         "name": "",
         "type": "uint256"
       }
     ],
     "payable": false,
     "type": "function"
   },
   {
     "inputs": [],
     "payable": false,
     "type": "constructor"
   },
   {
     "anonymous": false,
     "inputs": [
       {
         "indexed": false,
         "name": "price",
         "type": "uint256"
       }
     ],
     "name": "estimateAndPayout",
     "type": "event"
   },
   {
     "anonymous": false,
     "inputs": [
       {
         "indexed": false,
         "name": "price",
         "type": "uint256"
       }
     ],
     "name": "priceRetrieved",
     "type": "event"
   }
 ],

我不斷收到此錯誤:

Uncaught TypeError: this._eth.sendTransaction is not a function
   at c.sendTransaction (inpage.js:8253)
   at test (Landingpage.jsx:22)
   at Landingpage.componentDidMount (Landingpage.jsx:46)
   at ReactCompositeComponent.js:264
   at measureLifeCyclePerf (ReactCompositeComponent.js:75)
   at ReactCompositeComponent.js:263
   at CallbackQueue.notifyAll (CallbackQueue.js:76)
   at ReactReconcileTransaction.close (ReactReconcileTransaction.js:80)
   at ReactReconcileTransaction.closeAll (Transaction.js:209)
   at ReactReconcileTransaction.perform (Transaction.js:156)
   at batchedMountComponentIntoNode (ReactMount.js:126)
   at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
   at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
   at Object.batchedUpdates (ReactUpdates.js:97)
   at Object._renderNewRootComponent (ReactMount.js:319)
   at Object._renderSubtreeIntoContainer (ReactMount.js:401)
   at Object.render (ReactMount.js:422)
   at Object../src/index.js (index.js:34)
   at __webpack_require__ (bootstrap 2d91de1…:659)
   at fn (bootstrap 2d91de1…:85)
   at Object.0 (web3Reducer.js:21)
   at __webpack_require__ (bootstrap 2d91de1…:659)
   at ./node_modules/ansi-regex/index.js.module.exports (bootstrap 2d91de1…:708)
   at bundle.js:712

任何的想法?

資訊 我正在使用與 testrpc 的契約並用松露將其部署在那裡

您不需要sendTransaction顯式呼叫該函式,只需像這樣呼叫您的智能合約的函式:

contract.buyTicket(3000, {
       from: '0x5c1a92217e456a7eB4a051B567FC751A534991a3',
       gas: 1000000,
       value: web3.toWei(1, 'ether')
   });

也不要忘記定義氣體限制

編輯:您不需要用 實例化新對象new,web3 應該為您提供智能合約的實例:

const contract = web3.eth.contract(ABI.abi).at("0xdbb05ed3b5acb77cbd2366d037fe051703958dac");

引用自:https://ethereum.stackexchange.com/questions/23777