0

I have an ERC20 contract which is deployed from another contract as following:

function createDao(string _tokenName) public payable returns (address tokenAddress) {
    tokenNumberIndex = safeAdd(tokenNumberIndex, 1);
    string memory _tokenSymbol = bytes32ToString(bytes32(tokenNumberIndex));
    address _tokenAddress = new REP(_tokenSymbol, _tokenName);
    erc20SymbolAddresses[_tokenSymbol] = _tokenAddress;
    return _tokenAddress;
}

The ERC20 contract is as following:

contract REP is ERC20Interface, Owned, SafeMath {
string public symbol;
string public  name;
uint8 public decimals;
uint public _totalSupply;

mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;

constructor(string _tokenSymbol, string _name) public payable{
    symbol = _tokenSymbol;
    name = _name;
    decimals = 18;
    _totalSupply = msg.value;
    balances[msg.sender] = _totalSupply;
    emit Transfer(address(0), msg.sender, _totalSupply);
}

}

My assumption is that since createDao function is payable, whatever is sent in createDao will be passed on to the ERC20 contract as msg.value. Though both the functions are payable, how do I pass on the value while initializing ERC20 token?

2

By default, when you call new REP(), msg.value is not passed through, since the msg changes to the contract deployment.

You need to pass value explicitly, as described in this section of the documentation.

Try:

address _tokenAddress = (new REP).value(msg.value)("param1", "param2");
  • I am unable to change the value while confirming the transaction on calling createDao function in the other contract in Metamask. Can you help me on how I can pass a non-zero value ? – dhiraka Oct 12 '18 at 11:37
  • I'm not super familiar with metamask, sorry. I imagine you can set a value via web3 when building the tx for metamask. – Raghav Sood Oct 12 '18 at 12:14

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.