1

I am using truffle-contract package to deploy a contract that was compiled by using truffle compile

deploy.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);

Running node deploy.js gives the error

Cannot read property 'apply' of undefined

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

Using truffle-contract 3.0.5, web3 1.0.0-beta.34 according to packages.json.

What went wrong here?

Foo.json artifact

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

Foo.sol

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();
    }
}
1

it's a web3 1.0 issue.

Workaround:

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

See credit 1, credit2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.