1

why I always have undefined from

contract(_contract).setProvider(this.web3Provided);

Component:

import React, {Component, PropTypes} from 'react';
import { default as Web3 } from 'web3';
import { default as contract } from 'truffle-contract';
import Products from '../../build/contracts/Products.json';
import SimpleStorage from '../../build/contracts/SimpleStorage.json';
import TruffleConfig from '../../truffle-config';

class TruffleContractWrapper extends Component {

  constructor(props) {
    super(props);
    this.web3Provided;
    this.web3Inizialize = this.web3Inizialize.bind(this);
    this.contracts = [
      Products, 
      SimpleStorage
    ];
  }

  componentWillMount() {
    this.web3Inizialize();
  }

  web3Inizialize() {
    let { NODE_ENV = 'development' } = process.env;
    let {
      host = 'localhost', 
      port = 8545 } = TruffleConfig.networks[NODE_ENV];
    let web3Location = `http://${host}:${port}`;

    if (typeof web3 !== 'undefined') {                            
      // Use the Mist/wallet provider.     
      // DEVELOPER NOTE: removing the next commented line will break the app
      // eslint-disable-next-line
      this.web3Provided = new Web3(web3.currentProvider);
    } else {                                                      
      // DEVELOPER NOTE: What happens in the wild if the 
      // user does not have a browser based wallet? What happens
      // if the Web3 object cannot be initialized with the httpProvider
      // given from the loction in the truffle-config file?
      this.web3Provided = new Web3(new Web3.providers.HttpProvider(web3Location));
    }
  }

  buildTruffleContracts() {
    let contracts = {};

    this.contracts.forEach( (_contract) => {
      let {contract_name = ''} = _contract;
      contracts[contract_name] = contract(_contract).setProvider(
        this.web3Provided
        );
    });
    return contracts;
  }

  getChildContext() {
    let contracts = this.buildTruffleContracts();
    return { contracts, web3: this.web3Provided };
  }

  render() {
    return <div>{this.props.children}</div>;
  }
}

TruffleContractWrapper.childContextTypes = {
  contracts: React.PropTypes.object,
  web3: React.PropTypes.object
};

export default TruffleContractWrapper;

Is like a loop problem, this block of code works:

componentWillMount() {
    this.web3Inizialize();
    let provider = new Web3.providers.HttpProvider('http://localhost:8545');
    var meta = contract(Products);
    meta.setProvider(provider);
    console.log(meta.deployed().then((poppo) => {
      console.log(poppo);
    }));
  }
2
  • Find the problem, this is the right implementation: buildTruffleContracts() { let contracts = {}; let meta; this.contracts.forEach( (_contract) => { let {contract_name = ''} = _contract; meta = contract(_contract); meta.setProvider(this.web3Provided.currentProvider); contracts[contract_name] = meta; }); return contracts; }
    – underdog
    Mar 6, 2017 at 21:49
  • Please consider posting an answer for yourself and accept it as correct answer.
    – niksmac
    Mar 7, 2017 at 1:46

1 Answer 1

1

Right implementation of buildTruffleContract function

buildTruffleContracts() {
    let contracts = {};
    let meta;

    this.contracts.forEach( (_contract) => {
      let {contract_name = ''} = _contract;
      meta = contract(_contract);
      meta.setProvider(this.web3Provided.currentProvider);
      contracts[contract_name] = meta;
    });
    return contracts;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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