-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fund Sent into Contract
68 lines (54 loc) · 2.43 KB
/
Fund Sent into Contract
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import"./PriceConvertLib.sol";
contract Fundme{
using PriceConverter for uint256;
uint256 public minimumUsd = 50 * 1e18; // 1 * 10 ** 18
address[] public funders; // This array store all sender address who send the ETH
mapping(address => uint256) public addressToAmountGet; // This mapping function map the all address to amount get by users
/*
Want to be able to send a minimum usd fund in this contract
How to do we send ETH on this Contract
1e18 == 1 * 10 ** 18 = 1000000000000000000
1 ETH = 1e18
1 ETH = Gwei 1000000000
1 ETH = Wei 1000000000000000000
*/
function Fund() public payable {
//msg.value.getConverationRate();
require(msg.value.getConverationRate() >= minimumUsd, "Didn't send enough!");
funders.push(msg.sender);
addressToAmountGet[msg.sender] = msg.value;
}
/*
1.If we want to access any data from a list or array ,
mapping we used loop to retrieve the value one by one
2. funderIndex = funderIndex + 1 or funderIndex++ for increasing value */
/*We define condition as like *starting index, ending index, stop amount* */
function withdraw () public {
for(uint256 funderIndex =0; funderIndex < funders.length; funderIndex++){
address funder = funders[funderIndex];
addressToAmountGet[funder] = 0;
}
//Reset Array
funders = new address[](0);
// How actually withdraw the funds
/*
1. we three way to send ETH on contract
2. First Transfer
3. Second Send
4. Forth Call
*/
//msg.sender = address
// when we want to withdraw main chain coin we call payable function
// payable(msg.sender) = payable address
// 1 way to send ETH or Token this method automatically revert and consume more gas
payable(msg.sender).transfer(address(this).balance);
// 2 way way to send ETH or Token this method only revert when we add require add
bool sendSuccess = payable(msg.sender).send(address(this).balance);
require (sendSuccess, "Send failed");
// 3 way way to send ETH or Token this method only revert when we add require add
(bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call Failed");
}
}