Truffle: `TypeError: Cannot read property 'apply' of undefined` while getting deployed contract

Created on 20 Jun 2018  路  9Comments  路  Source: trufflesuite/truffle

Upon setting my contract provider I'm getting the following error (with Web3 version 0.20.6):

TypeError: Cannot read property 'apply' of undefined

I've seen the old issues relating to this problem but those just pose workarounds for using Web3 1.0 but it's still happening on an older version. Here's my code:

const contract = require('truffle-contract')
const Proxy = contract(require('./build/contracts/Proxy.json'))
Proxy.setProvider(web3.currentProvider)
const deployed = await Proxy.deployed()

[email protected]
truffle-contract@^3.0.5
[email protected]

Most helpful comment

@cckelly I was able to get this to work for [email protected] by doing the following (borrowing from @timqian's reproduction case):

  • Launch ganache-cli in a separate terminal tab (default port is 8545)
  • Run truffle-migrate after adding a network config following the pattern in the docs here
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const MetaCoinArtifact = require('./build/contracts/MetaCoin.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:8545");

(async () => {
    const MetaCoin = TruffleContract(MetaCoinArtifact);
    MetaCoin.setProvider(web3Provider);
    const instance = await MetaCoin.deployed();
    console.log('instance.address --> ' + instance.address)
})();

Output

Users-MacBook-Air:metacoin cgewecke$ node script.js
instance.address --> 0xe529baf3f7205cf00b5dd14e9ccaf12339707d41

@timqian In your case I think the issue might be a web3 mismatch. I will look into publishing the new work we've done on truffle-contract upgrading to web3 1.0 separately this morning. It should already by available in truffle exec if you install truffle@next.

Thanks to both of you for reporting.

All 9 comments

@cckelly What's your execution context?

You should be able to write the above as follows:

const Proxy = artifacts.require("Proxy");
const deployed = await Proxy.deployed();

@cgewecke Top level requires, unfortunately the above gives me an issue about not being able to find artifacts.

@cgewecke Met the same issue,

  • Execution context: node v10.0.0 (not truffle exec)
  • Dependencies:
    "truffle-contract": "^3.0.6",
    "web3": "^1.0.0-beta.34"
  • code
// frontend.js
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const AdoptionArtifact = require('./build/contracts/Adoption.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");

(async () => {
    const Adoption = TruffleContract(AdoptionArtifact);
    Adoption.setProvider(web3Provider);
    const instance = await Adoption.deployed();
})();
  • error info:
$ node frontend.js
TypeError: Cannot read property 'apply' of undefined
   at *****/node_modules/truffle-contract/contract.js:24:36

@cckelly I was able to get this to work for [email protected] by doing the following (borrowing from @timqian's reproduction case):

  • Launch ganache-cli in a separate terminal tab (default port is 8545)
  • Run truffle-migrate after adding a network config following the pattern in the docs here
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const MetaCoinArtifact = require('./build/contracts/MetaCoin.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:8545");

(async () => {
    const MetaCoin = TruffleContract(MetaCoinArtifact);
    MetaCoin.setProvider(web3Provider);
    const instance = await MetaCoin.deployed();
    console.log('instance.address --> ' + instance.address)
})();

Output

Users-MacBook-Air:metacoin cgewecke$ node script.js
instance.address --> 0xe529baf3f7205cf00b5dd14e9ccaf12339707d41

@timqian In your case I think the issue might be a web3 mismatch. I will look into publishing the new work we've done on truffle-contract upgrading to web3 1.0 separately this morning. It should already by available in truffle exec if you install truffle@next.

Thanks to both of you for reporting.

@cgewecke Awesome thanks for your help, I'll give this a shot and report back

@cgewecke I believe this is old and can be closed.

Closing this issue as it seems to have been resolved. Feel free to re-open if I'm mistaken.

// frontend.js
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const AdoptionArtifact = require('./build/contracts/Adoption.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");

(async () => {
    const Adoption = TruffleContract(AdoptionArtifact);
    Adoption.setProvider(web3Provider);
    const instance = await Adoption.deployed();
})();

I am trying the code .
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const AdoptionArtifact = require('./build/contracts/BaseApp.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");

(async () => {
const Adoption = TruffleContract(AdoptionArtifact);
Adoption.setProvider(web3Provider);
const instance = await Adoption.deployed();
const cst = "bce"
const content = "gfdgdggdfgdg"
const content2 = "dvcvcvxcvcx"
console.log(cst,content,content2)
await instance.createEntry(cst,content,content2)

})();
giving me an error
(node:15292) UnhandledPromiseRejectionWarning: Error: invalid address

@cckelly I was able to get this to work for [email protected] by doing the following (borrowing from @timqian's reproduction case):

  • Launch ganache-cli in a separate terminal tab (default port is 8545)
  • Run truffle-migrate after adding a network config following the pattern in the docs here
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const MetaCoinArtifact = require('./build/contracts/MetaCoin.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:8545");

(async () => {
    const MetaCoin = TruffleContract(MetaCoinArtifact);
    MetaCoin.setProvider(web3Provider);
    const instance = await MetaCoin.deployed();
    console.log('instance.address --> ' + instance.address)
})();

Output

Users-MacBook-Air:metacoin cgewecke$ node script.js
instance.address --> 0xe529baf3f7205cf00b5dd14e9ccaf12339707d41

@timqian In your case I think the issue might be a web3 mismatch. I will look into publishing the new work we've done on truffle-contract upgrading to web3 1.0 separately this morning. It should already by available in truffle exec if you install truffle@next.

Thanks to both of you for reporting.

`const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const BaseApp = require('./build/contracts/BaseApp.json');
const web3Provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");

(async () => {

const web3 = new Web3(web3Provider);
account = web3.eth.accounts[0]
console.log(web3.eth.accounts[0])
const MetaCoin = TruffleContract(BaseApp);
MetaCoin.setProvider(web3Provider);
const instance = await MetaCoin.deployed();
console.log('instance.address --> ' + instance.address)
    const cst = "bce"
    const content = "gfdgdggdfgdg"
    const content2 = "8.8.8.8"
    console.log(cst,content,content2)
    await instance.createEntry(cst,content,content2)

})();`
output:

// I am unable to understand this two two address issue. please help.

PS D:project\tryal> node try.js
0x907c2bb97af366a5ac5cca69decb62574707a01b
instance.address --> 0xa4E98bE9022c009d3d77f853fA402cBa9Af515D7
bce gfdgdggdfgdg 8.8.8.8
(node:34404) UnhandledPromiseRejectionWarning: Error: invalid address

Was this page helpful?
0 / 5 - 0 ratings