Solidity

使用 truffle-contracts 部署時出錯:無法讀取未定義的屬性“應用”

  • June 14, 2018

我正在使用truffle-contract包來部署通過使用編譯的契約truffle compile

部署.js

const Web3 = require('web3');
var provider = new Web3.providers.HttpProvider("http://localhost:8545");

const contract = require('truffle-contract');
var FooArtifacts = require("./build/contracts/Foo.json");
var Foo = contract(FooArtifacts);
Foo.setProvider(provider);  

var myContractAddress = "0x6139e1b82ffbdbeafbb403a77a04e8374d2521b1";
Foo.new(myContractAddress);

執行node deploy.js給出錯誤

無法讀取未定義的屬性“應用”

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'apply' of undefined
at Provider.sendAsync (C:\Users\y\test\node_modules\truffle-contract\contract.js:24:36)
at RequestManager.sendAsync (C:\Users\y\test\node_modules\truffle-contract\node_modules\web3\lib\web3\requestmanager.js:80:19)
at Object.get [as getNetwork] (C:\Users\y\test\node_modules\truffle-contract\node_modules\web3\lib\web3\property.js:116:33)
at C:\Users\y\test\node_modules\truffle-contract\contract.js:512:27
at new Promise (<anonymous>)
at Function.detectNetwork (C:\Users\y\test\node_modules\truffle-contract\contract.js:503:14)
at Function.deployed (C:\Users\y\test\node_modules\truffle-contract\contract.js:451:19)
at Object.<anonymous> (C:\Users\y\test\deploy.js:10:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3

根據 packages.json使用truffle-contract3.0.5 。web3 1.0.0-beta.34

這裡出了什麼問題?

Foo.json 工件

https://gist.github.com/nyxynyx/0c48280e5e881b62409047eedc0d2919

富溶膠

pragma solidity ^0.4.18;

import './MyContract.sol';

contract Foo {
   uint256 public x;
   MyContract myContract;
   address myContractAddress;

   constructor(address _myContractAddress) public {
       myContractAddress = _myContractAddress;
   }

   function baz() public {
       myContract = MyContract(myContractAddress);
       x = myContract.baz();
   }
}

這是一個 web3 1.0 問題。

解決方法:

if (typeof contract.currentProvider.sendAsync !== "function") {
 contract.currentProvider.sendAsync = function() {
   return contract.currentProvider.send.apply(
     contract.currentProvider,
         arguments
   );
 };
}

學分1學分2

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