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.

1
  • 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 ethereum.stackexchange.com/a/46458 to learn how to use it.
    – Ismael
    Commented May 17, 2022 at 6:58

1 Answer 1

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.

3
  • 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
    Commented May 16, 2022 at 7:21
  • No, that's just an example of how to send value to a contract.
    – Kof
    Commented May 16, 2022 at 7:22
  • but how should i define that sendTransaction function ?
    – dev
    Commented May 16, 2022 at 7: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.