1

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 Answers 2

3

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");
2
  • 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, 2018 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. Oct 12, 2018 at 12:14
0

if you need only the amount of msg.value, pass another argument for totalSupply when initialing:

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

then:

address _tokenAddress = new REP(_tokenSymbol, _tokenName, msg.value);

plz vote it up if it works properly :)

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.