0

I want to know, can we add conditional parameters in constructor. Just for example, in constructor if we have UINT for

numberOfTimesAllowed = 1;

EDIT

I am trying to get balance of user msg.sender from another contract and on the basis of the number of tokens i want to allow users coupons using. WHich i have added in contructor of my contract like this. numberOfTimesAllowed = 1;

Now how I can write a constructor which can do this? I am using this to get token balance of user from other contract

 function getERC20Balance(address _address) view public returns (uint) {
    return ERC20Interface(tokenAddress).balanceOf(_address);
}

But how to achieve it on contract load so constructor value can be set accordingly!

  • Your constructor can do just about anything you want it to, as long as the transaction gas cost fits into the block ( 8m gas at the moment ). Regarding the numberOfTimesAllowed variable, to me it seems you want to use that variable based on the user's token balance from another contract? just make it a method that interrogates the balance and returns the number you want based on the balance. You can call this method in the constructor and set it up, or in any other method. Better yet try to update the question with a full example of what you're trying to achieve. – Micky Socaci Jan 30 '19 at 16:12
  • @micky-socaci yes I am trying to get balance form another contract and on the basis of the balance need to set the numberOfTimesAllowed in the constructor. can you plz guide me an example where we can get balance of user from other contract on contract load without any functions calling and then set constructor on the basis of that number – Ophelia Jan 30 '19 at 16:39
0

You could do something like this.

Instantiate the token contract interface and use it's balanceOf() function. This is the most common method to call a function in another contract.

Get the interface over here: https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC20/IERC20.sol

import "./IERC20.sol";

contract MyContract {

  constructor(address tokenContractAddress) public {
    IERC20 token = IERC20(tokenContractAddress); // token is cast as type IERC20, so it's a contract
    uint deployerBalance = token.balanceOf(msg.sender);
    if (deployerBalance > ...
  }
}

I'm not sure the constructor is the right place to do this given that the only obvious user to work with is the user who is deploying the contract. The same basic idea can be adapted to work in a regular function if that's a better fit.

Hope it helps.

  • if this is not a proper approach to use this in constructor then should I use mappings? NO.1 mapping for balanceOf and then in constructor use that mapping to define value of numberOfTimesAllowed on the basis of the balance of user. – Ophelia Jan 30 '19 at 17:51
  • We need a much clearer description of what you need to accomplish from a user perspective. – Rob Hitchens - B9lab Jan 30 '19 at 17:58
  • Please vote up answers that helped address the question as it was asked. Based on the comment above, you need a fundamentally different approach. It's a very advanced pattern due both to the interval and the need to efficiently handle state. You need to avoid updating user states in an intuitive way because it will cost too much. With some careful analysis, it should be possible to compute entitlements on-the-fly using data only from token contracts and spends in your contract. Sorry if that sounds cryptic. There is too much to encapsulate in short comment. – Rob Hitchens - B9lab Jan 30 '19 at 18:27

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.