0

I have a very simple contract already deployed to the kovan network

I want to create a very simple web3 interface, so I'm following the truffle webpack metacoin example with the following app.js

import "../stylesheets/app.css";
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'

var abi=[{"constant":false,"inputs":
[{"name":"_value","type":"uint256"}],"name":"setvalue","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]
var contractAddress = "0x8b5fe00cf3cb34caa9c3e968d5b4d96f927d3495"; //on Kovan
var simple = web3.eth.contract(contractAddress, abi);

window.App = { 
  start: function() {
    var self = this;
    simple.setProvider(web3.currentProvider);
    self.refreshBalance();
  },

  refreshBalance: function() {
    var self = this;
    var meta;
    simple.deployed().then(function(instance) {
      meta = instance;
      return meta.value.call();
    }).then(function(value) {
      var balance_element = document.getElementById("balance");
      balance_element.innerHTML = value.valueOf();
    }).catch(function(e) {
      console.log(e);
    });
  },

  updateValue: function() {
    var self = this;
    var amount = parseInt(document.getElementById("value").value);
    this.setStatus("Initiating transaction... (please wait)");
    var meta;
    simple.deployed().then(function(instance) {
      meta = instance;
      return meta.setvalue(amount);
    }).then(function() {
      self.refreshBalance();
    }).catch(function(e) {
      console.log(e);
    });
  }
};

window.addEventListener('load', function() {
  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  }
  App.start();
});

But I'm always getting the message

simple.setProvider is not a function

and

simple.deployed is not a function

Any help greatly appreciated. I'm using truffle-init-webpack@0.0.2

1

Try instantiating the contract like this:

var abi = [...]; // a valid abi
var contractAddress = 0x.......... // the address where the matching contract is deployed

      var MyTruffleContract = contract({
        abi: abi
      })
      MyTruffleContract.setProvider(this.web3.currentProvider);
      console.log(MyTruffleContract);

      MyTruffleContract
      .at(contractAddress)
      .then(instance =>{
        instance.totalSupply({
          from: this.account
        })
        .then(value =>{
          console.log("Total Supply:",this.web3.fromWei(value, "ether").toString(10));
        })
        .catch(e => {
          console.log(e);
        });
      })
  • Thanks! Can you explain why deployed() doesn't work but .at(contractAddress) does? – user1938620 Nov 13 '17 at 14:17

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.