|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.24; |
| 3 | +import {console} from "forge-std/Test.sol"; |
| 4 | +import "@chainlink/contracts/src/v0.7/interfaces/AggregatorV3Interface.sol"; |
| 5 | +import "../../contracts/with-foundry/RewardToken.sol"; |
| 6 | +import "../../contracts/with-foundry/RewardNft.sol"; |
| 7 | + |
| 8 | +contract CrowdfundingV2 { |
| 9 | + address public Owner; |
| 10 | + uint public constant FUNDING_GOAL_IN_USD = 50000; |
| 11 | + uint public constant NFT_THRESHOLD = 1000; |
| 12 | + uint256 public totalFundsRaised; |
| 13 | + bool public isFundingComplete; |
| 14 | + |
| 15 | + RewardToken public rewardToken; |
| 16 | + RewardNft public rewardNFT; |
| 17 | + uint256 public tokenRewardRate; |
| 18 | + |
| 19 | + // Contribution tracking |
| 20 | + mapping(address => uint256) public contributions; |
| 21 | + mapping(address => bool) public hasReceivedNFT; |
| 22 | + |
| 23 | + // Chainlink PriceFeed |
| 24 | + AggregatorV3Interface priceFeed; |
| 25 | + address public constant ETH_USD_ADDR = 0x694AA1769357215DE4FAC081bf1f309aDC325306; |
| 26 | + // Events |
| 27 | + event ContributionReceived(address indexed contributor, uint256 amount); |
| 28 | + event TokenRewardSent(address indexed contributor, uint256 amount); |
| 29 | + event NFTRewardSent(address indexed contributor, uint256 tokenId); |
| 30 | + event FundsWithdrawn(address indexed projectOwner, uint256 amount); |
| 31 | + |
| 32 | + constructor(uint256 _tokenRewardRate, address _rewardToken, address _rewardNft) { |
| 33 | + /** |
| 34 | + * Network: Sepolia |
| 35 | + * Data Feed: ETH/USD |
| 36 | + * Address: 0x694AA1769357215DE4FAC081bf1f309aDC325306 |
| 37 | + */ |
| 38 | + priceFeed = AggregatorV3Interface(ETH_USD_ADDR); |
| 39 | + Owner = msg.sender; |
| 40 | + rewardToken = RewardToken(_rewardToken); |
| 41 | + rewardNFT = RewardNft(_rewardNft); |
| 42 | + tokenRewardRate = _tokenRewardRate; |
| 43 | + } |
| 44 | + |
| 45 | + // function to retrieve ETH price in USD with Chainlink priceFeed |
| 46 | + function getLatestPrice() public view returns (int256) { |
| 47 | + ( |
| 48 | + , |
| 49 | + // uint80 roundID |
| 50 | + int256 answer, // uint256 startedAt |
| 51 | + , |
| 52 | + , |
| 53 | + |
| 54 | + ) = // uint256 updatedAt |
| 55 | + priceFeed.latestRoundData(); |
| 56 | + |
| 57 | + return answer; // Price has 8 decimals, e.g., 3000.00000000 |
| 58 | + } |
| 59 | + |
| 60 | + function contribute() external payable returns (bool) { |
| 61 | + require(msg.value > 0, "Contribution must be greater than 0"); |
| 62 | + require(!isFundingComplete, "Funding goal already reached"); |
| 63 | + |
| 64 | + // Calculate contribution amount and process any refunds |
| 65 | + |
| 66 | + uint256 refundableAmount = _determineIfAmountIsRefundable(msg.value); |
| 67 | + uint256 actualContribution = msg.value - refundableAmount; |
| 68 | + |
| 69 | + // check if refundable amount is > 0 |
| 70 | + if (refundableAmount > 0) { |
| 71 | + transferRefundableAmount(refundableAmount, msg.sender); |
| 72 | + } |
| 73 | + |
| 74 | + // Update contribution record |
| 75 | + // uint256 contributionsValue = msg.value - refundableAmount; |
| 76 | + contributions[msg.sender] += actualContribution; |
| 77 | + totalFundsRaised += actualContribution; |
| 78 | + |
| 79 | + // Check if funding goal is reached |
| 80 | + if (totalFundsRaised >= FUNDING_GOAL_IN_USD) { |
| 81 | + isFundingComplete = true; |
| 82 | + } |
| 83 | + |
| 84 | + // Calculate token reward |
| 85 | + uint256 tokenReward = calculateReward(actualContribution); |
| 86 | + |
| 87 | + if (tokenReward > 0) { |
| 88 | + bool isTransfered = sendRewardToken(tokenReward, msg.sender); |
| 89 | + require(isTransfered, "Token transfer failed"); |
| 90 | + |
| 91 | + // Check for NFT eligibility |
| 92 | + if (checkNftEligibility(msg.sender)) { |
| 93 | + mintNft(msg.sender); |
| 94 | + } |
| 95 | + |
| 96 | + emit ContributionReceived(msg.sender, actualContribution); |
| 97 | + return true; |
| 98 | + } else { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + function checkNftEligibility(address _address) private view returns (bool) { |
| 104 | + return contributions[_address] >= NFT_THRESHOLD && !hasReceivedNFT[_address]; |
| 105 | + } |
| 106 | + |
| 107 | + function mintNft(address _contributor) private returns (bool) { |
| 108 | + // require(checkNftEligibilty(_contributor), "Not eligible for NFT reward"); |
| 109 | + uint256 tokenId = rewardNFT.mintNFT(_contributor); |
| 110 | + hasReceivedNFT[_contributor] = true; |
| 111 | + emit NFTRewardSent(_contributor, tokenId); |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + function calculateReward(uint256 _value) private view returns (uint256) { |
| 116 | + uint256 tokenReward = (_value * tokenRewardRate) / 1 ether; |
| 117 | + return tokenReward; |
| 118 | + } |
| 119 | + |
| 120 | + function sendRewardToken(uint256 _tokenReward, address _recipient) private returns (bool) { |
| 121 | + bool success = rewardToken.transfer(_recipient, _tokenReward); |
| 122 | + require(success, "Token transfer failed"); |
| 123 | + emit TokenRewardSent(msg.sender, _tokenReward); |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + function _determineIfAmountIsRefundable(uint256 _contributionAmount) private view returns (uint256) { |
| 129 | + // Calculate the remaining amount needed to complete the funding goal |
| 130 | + uint256 amountToReachThreshold = FUNDING_GOAL_IN_USD - totalFundsRaised; |
| 131 | + if (_contributionAmount >= amountToReachThreshold) { |
| 132 | + // return the excess amount |
| 133 | + uint256 refundAmount = _contributionAmount - amountToReachThreshold; |
| 134 | + return refundAmount; |
| 135 | + } |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + function transferRefundableAmount(uint256 _amount, address _contributor) private { |
| 140 | + // uint256 refundable = _determineIfAmountIsRefundable(_amount); |
| 141 | + uint256 refundable = _amount; |
| 142 | + if (refundable > 0) { |
| 143 | + (bool success, ) = _contributor.call{value: refundable}(""); |
| 144 | + require(success, "Transfer failed"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + function withdrawFunds() external { |
| 149 | + require(msg.sender == Owner, "Only project owner can withdraw"); |
| 150 | + require(isFundingComplete, "Funding goal not yet reached"); |
| 151 | + require(address(this).balance > 0, "No funds to withdraw"); |
| 152 | + |
| 153 | + uint256 amount = address(this).balance; |
| 154 | + payable(Owner).transfer(amount); |
| 155 | + |
| 156 | + emit FundsWithdrawn(Owner, amount); |
| 157 | + } |
| 158 | + |
| 159 | + function getContribution(address contributor) external view returns (uint256) { |
| 160 | + return contributions[contributor]; |
| 161 | + } |
| 162 | +} |
0 commit comments