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()? } }

user938363
  • 669
  • 1
  • 6
  • 23
  • 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. – goodvibration Dec 27 '20 at 09: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 Dec 27 '20 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 Dec 27 '20 at 16:57
  • One more time - msg.value has nothing to do with ERC20 transfer. – goodvibration Dec 27 '20 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 Dec 27 '20 at 17:04
  • You seem to be missing some fundamental concepts (theoretical and practical), namely the difference between Ether and Token. – goodvibration Dec 27 '20 at 17:09
  • I forgot to mention that all above happens on a private network instead of on Ethereum network. – user938363 Dec 27 '20 at 19:32

1 Answers1

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.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
  • 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 Dec 27 '20 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. – Lauri Peltonen Dec 28 '20 at 11:26