Acceder a las funciones de un contrato de terceros usando Truffle

Estoy tratando de crear un tablero básico que me permita rastrear el estado de cualquier ICO dado (suponiendo que el token sea ERC20).

En este momento, logré implementar un contrato Crowdsale + Coin simple basado en OpenZeppelin y Truffle y puedo acceder a las funciones y variables de estado en una interfaz de Angular 2 que construí.

Lo siguiente que hice, que también funciona bien, fue usar web3 (sin Truffle) para acceder a otro Token implementado, por ejemplo, BAT y leer sus variables de estado públicas. Esto se logró con este código:

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

Esto me permitió acceder efectivamente a totalSupply of BAT desde mi propia interfaz.

Ahora, estoy teniendo dificultades para descubrir cómo podría replicar este código, pero usando Truffle en lugar de llamadas web3.

Actualmente estoy accediendo a mis propios contratos implementados como este:

//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);
    });
  }

Respuestas (1)

Lo averigué. Aquí está el código:

      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);
        });
      })
Gracias, esto también funcionó para mí. Aquí también se proporciona una buena descripción de .deployed() vs.at(): npmjs.com/package/@truffle/contract