0

i am able to send eth (with a payable function ) to the my contract but what i really need is that when the user interact with the contract it takes a specific amount on the msg sender and add it to its balance.

dev
  • 15
  • 3
  • A contract cannot withdraw ether from other addresses. It can only withdraw from its own balance. You can achieve something similar using ERC20 function transferFrom. See this answer https://ethereum.stackexchange.com/a/46458/ to learn how to use it. – Ismael May 17 '22 at 06:58

1 Answers1

1

The sender must attach the required amount of ETH to the transaction.

mywallet.sendTransaction({to: contractAddress, value: "1000000000"})

Contract will then be receiving this amount as part of the transaction.

Kof
  • 2,954
  • 6
  • 23
  • so if i understand i do msg.sender.sendTransaction({to: contractAddress, value: "1000000000"}) do i need to import any library for this function sendTransaction ? – dev May 16 '22 at 07:21
  • No, that's just an example of how to send value to a contract. – Kof May 16 '22 at 07:22
  • but how should i define that sendTransaction function ? – dev May 16 '22 at 07:26