I am looking to add a Refund function in my smart contract. Like If User A deposited 1 ETH in the contract, and uses refund function, he gets 1 ETH - Gas Fee back.

I can do the transfer(address.balance-fee) however the issue is there can be multiple depositors. How can I make sure they only get the amount they deposited.

  • Please show your code, so that any answer here can refer to it. – goodvibration Nov 17 '20 at 6:33

You simply need to keep track of who deposited and how much. It can be as simple as mapping(address => uint) - remember also that probably a user can make multiple deposits from the same address so you need to increase that value and not overwrite.

You can modify the mapping entries simply with something like this (assuming mymap is the mapping: mymap[msg.sender] += msg.value.

Depending on how you are planning to pay the deposits back you maybe need another data structure to store the addresses (an array), if you want to push the deposits. But if users can pull the deposits (call something like giveMeMyDepositBack) then you don't need a separate data structure and you can simply send back the deposit minus fee.

  • Is there any possibility of you providing me this as a code? I am not sure how mapping works. – Avi Thour Nov 17 '20 at 6:40
  • Added a bit of code – Lauri Peltonen Nov 17 '20 at 6:45

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