407 For Loop

laurahsu.roc
發佈於sol. Projects
閱讀時間約 9 分鐘

do something repeately

withdraw money

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {PriceConverter} from "./PriceConverter.sol";

contract FundMe {
using PriceConverter for uint256;

uint256 public minimumUsd = 5e18;

address[] public funders;
mapping(address funder => uint256 amountFunded) public addressToAmountFunded;

function fund() public payable {
require(msg.value.getConversionRate() >= minimumUsd, "didn't send enought ETH");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = addressToAmountFunded[msg.sender] + msg.value;
}

function withdraw() public {}
// for loop
// [1, 2, 3, 4] elements
// 0, 1, 2, 3 indexed
// for(/* starting index, ending index, step amount */)
for(uint256 funderIndex = 0; funderIndex < funders.length; funder Index++;){
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
}
}

resetting an array

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {PriceConverter} from "./PriceConverter.sol";

contract FundMe {
using PriceConverter for uint256;

uint256 public minimumUsd = 5e18;

address[] public funders;
mapping(address funder => uint256 amountFunded) public addressToAmountFunded;

function fund() public payable {
require(msg.value.getConversionRate() >= minimumUsd, "didn't send enought ETH");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = addressToAmountFunded[msg.sender] + msg.value;
}

function withdraw() public {}
// for loop
// [1, 2, 3, 4] elements
// 0, 1, 2, 3 indexed
// for(/* starting index, ending index, step amount */)
for(uint256 funderIndex = 0; funderIndex < funders.length; funder Index++;){
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
// reset the array
funders = new address[](0);
// withdaw the funds
}
}


sending ETH from a contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {PriceConverter} from "./PriceConverter.sol";

contract FundMe {
using PriceConverter for uint256;

uint256 public minimumUsd = 5e18;

address[] public funders;
mapping(address funder => uint256 amountFunded) public addressToAmountFunded;

function fund() public payable {
require(msg.value.getConversionRate() >= minimumUsd, "didn't send enought ETH");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = addressToAmountFunded[msg.sender] + msg.value;
}

function withdraw() public {}
// for loop
// [1, 2, 3, 4] elements
// 0, 1, 2, 3 indexed
// for(/* starting index, ending index, step amount */)
for(uint256 funderIndex = 0; funderIndex < funders.length; funder Index++;){
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
// reset the array
funders = new address[](0);
// actually withdaw the funds

// transfer the fund to whoever calling this withdraw function
// msg.sender = address
// payable(msg.sender) = payable address
payable msg.sender.transfer(address(this).balance);

// send
bool sendSuccress = payable(msg.sender).send(address(this).balance);
require(sendSuccress, "Send failed");
// call
(bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed");
}
}

[Reference]

Sending Ether (transfer, send, call)

2會員
173內容數
尋大神腳印, 亦步亦趨。
留言0
查看全部
發表第一個留言支持創作者!