FundMe/Withdraw for each individual funder. #187
-
In lesson 4, they explain the fundme and withdraw function, but mainly in a way that's designed for only the contract owner to withdraw funds. I want to know how you can get each individual funder address to be able to fund something and then be withdrawn the same amount that they individually put in. I would assume it has to do with getting solidity to identify their funder index (starting at 0) and then simply doing the withdraw function for each funder based on what they put in. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Maybe you can define a function that does that: function funderWithdraw() public payable{
for(uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++){
if (funders[funderIndex] == msg.sender){
uint256 amount = addressToAmountFunded[msg.sender];
(bool callSucces,) = payable(msg.sender).call{value: amount}("");
require(callSucces, "Call Failed!");
}
}
} Without Loop: function funderWithdraw() public payable{
uint256 amount = addressToAmountFunded[msg.sender];
if(amount > 0){
(bool callSucces,) = payable(msg.sender).call{value: amount}("");
require(callSucces, "Call Failed!");
}
} I tried it and looks like is working but probably not the most elegant way of doing it... Still learning ;) |
Beta Was this translation helpful? Give feedback.
Maybe you can define a function that does that:
Without Loop: