You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Solidity smart contract example fundMe application
fundMe1 example
1- Users can contribute funds by calling the contribute function and sending Ether with the transaction.
2- The contributed amount is stored in the contributions mapping, which maps addresses to their contributed amounts.
3- The addresses of contributors are stored in the contributors array.
4- The owner of the contract (deployer) can withdraw the funds by calling the withdraw function, which transfers the contract's balance to the owner's address.
5- This is a basic example. Depending on your requirements, you might want to add more features such as withdrawal limits, fundraising goals, or refund functionality.
// SPDX-License-Identifier: MITpragma solidity^0.8.23;
contractfundMe1 {
// mapping store the amount of contributedmapping (address=>uint256) public contributions;
// array store address of contributorsaddress [] public contributors;
addresspublic owner;
constructor() {
owner =msg.sender;
}
// the main two function in this contract function contribute() publicpayable{
//contributions must be greater than 0require(msg.value>0,"contributions must be greater than 0");
// add address of contributions to mappingif (contributions[msg.sender]==0){
contributors.push(msg.sender);
}
}
function withdraw() public{
require(msg.sender== owner,"owner can only withdraw this fund");
// withdraw all balance payable(msg.sender).transfer(address(this).balance);
}
}
fundMe2 example
Another basic example fundMe smart contract that allow user to contribute funds
// SPDX-License-Identifier: MITpragma solidity0.8.23;
contractfundMe2{
// address of the owner of the contractaddresspublic owner;
// track total amount of fund raiseduint256public totalFund;
// mapping to store contributions amount of addressmapping(address=>uint256) public contributions;
// constructor to set ownerconstructor() {
owner=msg.sender;
}
// function to allow user to contributefunction contribute() publicpayable{
require(msg.value>0,"contributions must be greater than 0");
contributions[msg.sender]+=msg.value;
totalFund+=msg.value;
}
//function to withdraw fund by the ownerfunction withdraw() publicpayable onlyOwner{
payable(owner).transfer(address(this).balance);
}
// Modifier to restrict functions to the ownermodifier onlyOwner() {
require(msg.sender== owner, "Only owner can withdraw funds");
_;// This line allows the function code to run after the modifier check.
}
}
fundMe3 Advanced
I take this contract example from patric collins course
First we start with two main function
// SPDX-License-Identifier: MITpragma solidity0.8.23;
contractfundMe3{
/* 1- Function allow user to send money */function fund() public {
}
function withdraw() public{
}
}
1- We have to make fund function to recieve and sent ethereum to do that we use payable keyword
2- user should be send 1 ether at least we use require to do that
3- the require statement has the power to control the behavior of the transaction
4- soldity have global variable like (msg.value) number of wei send with transaction
// SPDX-License-Identifier: MITpragma solidity0.8.23;
contractfundMe3{
function fund() publicpayable {
require (msg.value>=1e18,"you have to send at least 1 eth");
}
function withdraw() public{
}
}
1- replace minimum fund from 1 eth to 5 dollar
// SPDX-License-Identifier: MITpragma solidity0.8.23;
contractfundMe3{
uint256 minimumUsd=5;
function fund() publicpayable {
require (msg.value>=minimumUsd,"you have to send at least 1 eth");
}
function withdraw() public{
}
}
1- how to convert ethereum to dollar using chainlink data feeds
2- This conversion requires us to identify the price of Ethereum
// SPDX-License-Identifier: MITpragma solidity0.8.23;
contractfundMe3{
uint256 minimumUsd=5;
function fund() publicpayable {
require (msg.value>=minimumUsd,"you have to send at least 1 eth");
}
function withdraw() public{
}
function getPrice() public{
}
function getConversionRate() public{
}
}