0

When deploying a smart contract to a private network, how to send ERC20 token (on the same private network) in msg.value to the smart contract in constructor?

Here is the contract:

pragma ^0.7.0;

interface ERC20 {
  //standard ERC20 functions
}
contract MyContract () {
  address erc20Contract;
  uint256 value;

  constructor (address _erc20) {
    ERC20 token = ERC20(_erc20); //_erc20 is the deployed address of ERC20 token
    erc20Contract = _erc20;
    //need to transfer msg.value token to the contract
    value = msg.value; //<<==how to transfer msg.value to the contract with ERC20 function, such as token.transfer()?
  }
}
7
  • 2
    "How to send ERC20 token in msg.value?" - That's an oxymoron, since msg.value denotes the amount of ether included in the transaction. Commented Dec 27, 2020 at 9:27
  • With msg.value, does it mean that ERC20token.transfer() has already done before submitting transaction with msg.value? So if it is dapp submitting a transaction, then dapp needs to do ERC20token.transfer() before calling contract constructor with the msg.value. Isn't it?
    – user938363
    Commented Dec 27, 2020 at 16:53
  • The question may better be when submitting a transaction with msg.value, when the transfer of token in qty of msg.value needs to happen for the transaction?.
    – user938363
    Commented Dec 27, 2020 at 16:57
  • One more time - msg.value has nothing to do with ERC20 transfer. Commented Dec 27, 2020 at 16:57
  • hi goodvibration, the smart contract needs to hold token in value, the transfer of the token to the smart contract needs to happen. I thought calling constructor with msg.value and token transfer can happen at the same time in constructor. But it seems not correct.
    – user938363
    Commented Dec 27, 2020 at 17:04

1 Answer 1

1

As you were already told in comments, you can't send tokens the same way you send Ethers. msg.value always refers to Ether value and never to tokens.

Tokens are just smart contracts which have some standard functionality. To transfer tokens the easiest way is to call the token contract's transfer function (if it's an ERC20 token). Yes, you can even call that in a constructor explicitly.

2
  • Hi Lauri Peltonen, you may help me with my understanding of msg.value. msg.value is a member of message object and assign a value to msg.value in transaction does not mean automatic transfer ether/token between the address of transaction sender and the address of the contract. The sender needs to explicitly transfer ether/token to the contract address if he/she really mean to send value to the contract. Is the above correct? Many thanks.
    – user938363
    Commented Dec 27, 2020 at 22:42
  • No that's not correct. The value of msg.value is assigned automatically by the EVM and it always means the amount of Ether sent with the transaction. And it is never related to token transfers. Commented Dec 28, 2020 at 11:26

Your Answer

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

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