1

I'm trying to build a basic dashboard that will allow me to track the status of any given ICO (assuming the token is ERC20).

Right now, I managed to deploy a simple Crowdsale + Coin contract based on OpenZeppelin and Truffle and I am able to access the state variables and functions on an Angular 2 frontend I built.

The next thing I did, which is also working fine, is using web3 (without Truffle) to access another deployed Token, for example BAT and read its public state variables. This was achieved with this code:

  var abi = [...] ;
  // Copied ABI from Etherscan https://etherscan.io/address/0x0d8775f648430679a709e98d2b0cb6250d2887ef#code 

  var MyContract = this.web3.eth.contract(abi);

  // initiate contract for an address
  var myContractInstance = MyContract.at('0x0D8775F648430679A709E98d2b0Cb6250d2887EF'); //Address to which BAT token is deployed.

  // call constant function (synchronous way)
  //var owner = myContractInstance .owner.call();

  console.log(myContractInstance);

  myContractInstance.totalSupply.call({from: this.account},
    function(error, result){
      console.log(result.toString(10))
  });

This effectively allowed me to access totalSupply of BAT from my own frontend.

Now, I'm having a hard time figuring out how I could replicate this code, but using Truffle instead of web3 calls.

I'm currently accessing my own deployed contracts like this:

//Contracts have been built with Truffle here:
const crowdsaleArtifacts = require('../../build/contracts/PabloCoinCrowdsale.json');
const coinArtifacts = require('../../build/contracts/PabloCoin.json');

Crowdsale = contract(crowdsaleArtifacts);
Coin = contract(coinArtifacts);

//Bootstrap abstraction for use.
this.Crowdsale.setProvider(this.web3.currentProvider);
this.Coin.setProvider(this.web3.currentProvider);

//Once everything is loaded, for example, get totalSupply of Coin
this.getCoinInstance();

getCoinInstance(){
    this.Crowdsale
    .deployed()
    .then(instance =>{
      //Set the ref for the contract and look up it's associated token
      this.crowdsaleInstance = instance;
      this.crowdsaleInstance.token()
      .then(addr => {
        this.Coin.at(addr)
        .then(instance2 =>{
          // set the ref for the token and get totalSupply.
          this.coinInstance = instance2;
          this.totalSupply();
        })
      })
    })
    .catch(e => {
      console.log("ERR",e);
    });
  }

totalSupply(){
    this.coinInstance.totalSupply({
      from: this.account
    })
    .then(value =>{
      console.log("Total Supply:",this.web3.fromWei(value, "ether").toString(10));
    })
    .catch(e => {
      console.log(e);
    });
  }

1 Answer 1

1

Figured it out. Here's the code:

      var MyTruffleContract = contract({
        abi: abi //ABI obtained from Etherscan.
      })

      MyTruffleContract.setProvider(this.web3.currentProvider);
      console.log(MyTruffleContract);

      MyTruffleContract
      .at("0x0D8775F648430679A709E98d2b0Cb6250d2887EF") //Address of the contract, obtained from Etherscan
      .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);
        });
      })
1

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.