1

I'm trying to use javascript to access the token variables and functions provided in the MintToken.sol file in openzeppelin.

I used "truffle init webpack" to create the metacoin example provided by truffle. I use "truffle compile", "truffle migrate" and "npm run dev" to have an application running in javascript that, using web3, let me interact with the smart contract.

I would like to do the same with zeppelin-solidity. So I created my crowdsale smart contract :

pragma solidity ^0.4.11;

import '../node_modules/zeppelin-solidity/contracts/crowdsale/Crowdsale.sol';

contract MyCrowdsale is Crowdsale {
    function MyCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet)
        Crowdsale(_startTime, _endTime, _rate, _wallet)
    {
    }  
}

Crowdsale.sol has some fields and a mintable token:

contract Crowdsale {
  using SafeMath for uint256;

   MintableToken public token;

  // start and end timestamps where investments are allowed (both inclusive)
  uint256 public startTime;
  uint256 public endTime;
....

MintableToken.sol inherits from StandardToken (that has balanceOf method) and has some variables:

contract MintableToken is StandardToken, Ownable {
  event Mint(address indexed to, uint256 amount);
  event MintFinished();

  bool public mintingFinished = false;
....

The problem I have is that I'm able to access to the crowdsale variables like startTime and endTime, but I'm not able to access to token variables/methods like balanceOf, mintingFinished, etc.

In my app.js I tried to copy the code used to access the metacoin contract so I'm able to get the startDate, for example:

MyCrowdsale.deployed().then(function(instance) {
    return instance.startTime.call();
  }).then(function(value) {
    var startDate = value.valueOf();
    console.log("The startDate is " + startDate);
  }).catch(function(e) {
    console.log(e);
    self.setStatus("Error getting startDate; see log.");
  });

I can get the token address using this call:

MyCrowdsale.deployed().then(function(instance) {
    return instance.token.call();
  }).then(function(mintedToken) {
    console.log("The mintedToken is " + mintedToken.valueOf());
  }).catch(function(e) {
    console.log(e);
  });

But I'm stuck here. I would like to access to token.balanceOf() or token.mintingFinished but I don't know how to do it.

I tried (after getting the token address like I wrote before):

console.log("mintingFinished = " + mintedToken.mintingFinished);

and also:

mintedToken.mintingFinished.call().then(function(x) {
      console.log("mintingFinished = " + x);
    })

But none of these approaches worked.

Any help would be appreciated.

1
  • If you have questions on using OpenZeppelin you can ask in the Community Forum: forum.openzeppelin.com Disclosure: I am the Community Manager at OpenZeppelin
    – abcoathup
    Sep 3, 2019 at 21:14

2 Answers 2

0

I hope I got your question right. The Syntax for public vars and mappings is:

YourContract.deployed().then(function(instance) {
    return instance.publicVar.call();
  }).then(function(result) {
    console.log(result.toString());
  }).catch(function(e) {
    console.log(e);
  });

and mappings:

YourContract.deployed().then(function(instance) {
    return instance.publicMapping.call(mappingKey);
  }).then(function(result) {
    console.log(result.toString());
  }).catch(function(e) {
    console.log(e);
  });

where mappingKey stands for the solidity equivalent of

mapping (datatype1 => datatype2) public publicMapping;

publicMapping[mappingKey];

of course don't forget to make sure mappingKey is equivalent to the datatype1 .

0

Try with this code:

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 user's balance.
          console.log(this.account);
          this.coinInstance = instance2;
          //NOW you got a hold of the token instance, call any function you want with it.
          this.balanceOf(this.account);
        })
      })
    })
    .catch(e => {
      console.log("ERR",e);
    });
  }

balanceOf(address:string){
    this.coinInstance.balanceOf(address,{
      from: this.account
    })
    .then(value =>{
      console.log("balance",this.web3.fromWei(value, "ether").toString(10));
    })
    .catch(e => {
      console.log(e);
    });
  }

Your Answer

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

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