Zugriff auf die Funktionen eines Drittanbietervertrags mit Truffle

Ich versuche, ein einfaches Dashboard zu erstellen, mit dem ich den Status eines bestimmten ICO verfolgen kann (vorausgesetzt, der Token ist ERC20).

Im Moment ist es mir gelungen, einen einfachen Crowdsale + Coin-Vertrag basierend auf OpenZeppelin und Truffle bereitzustellen, und ich kann auf die Zustandsvariablen und Funktionen auf einem von mir erstellten Angular 2-Frontend zugreifen.

Das nächste, was ich getan habe, was auch gut funktioniert, ist die Verwendung von web3 (ohne Truffle), um auf ein anderes bereitgestelltes Token zuzugreifen, zum Beispiel BAT, und seine öffentlichen Zustandsvariablen zu lesen. Dies wurde mit diesem Code erreicht:

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

Dies ermöglichte mir effektiv den Zugriff auf totalSupply of BAT von meinem eigenen Frontend aus.

Jetzt fällt es mir schwer herauszufinden, wie ich diesen Code replizieren könnte, aber Truffle anstelle von Web3-Aufrufen verwenden.

Ich greife derzeit wie folgt auf meine eigenen bereitgestellten Verträge zu:

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

Antworten (1)

Herausgefunden. Hier ist der 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);
        });
      })
Danke, das hat bei mir auch funktioniert. Eine gute Beschreibung von .deployed() vs.at() finden Sie auch hier: npmjs.com/package/@truffle/contract