-
-
Save SyedAsadKazmi/8873ee7a85bd57078e802e4963491b20 to your computer and use it in GitHub Desktop.
Test for the mintNFT() function, utilising MATIC-USD price feed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.24; | |
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | |
contract TestNFT { | |
error NetworkNotSupported(); | |
error NotOwner(); | |
error InsufficientFunds(uint256 required); | |
error UserIsOverpaying(uint256 required); | |
error AnswerIsNotGreaterThanZero(); | |
error InconsistentRoundId(); | |
error CantFetchLatestRoundData(); | |
AggregatorV3Interface internal s_priceFeed; | |
address immutable i_owner; | |
uint256 constant NFT_PRICE_USD = 1e16; | |
event NFTMinted(address); | |
constructor() { | |
i_owner = msg.sender; | |
if (block.chainid == 80002) { | |
s_priceFeed = AggregatorV3Interface( | |
0x001382149eBa3441043c1c66972b4772963f5D43 | |
); | |
} else if (block.chainid == 137) { | |
s_priceFeed = AggregatorV3Interface( | |
0xAB594600376Ec9fD91F8e885dADF0CE036862dE0 | |
); | |
} else { | |
revert NetworkNotSupported(); | |
} | |
} | |
modifier onlyOwner() { | |
if (msg.sender != i_owner) { | |
revert NotOwner(); | |
} | |
_; | |
} | |
function mintTest() external payable onlyOwner { | |
uint256 nftPriceInMatic = getMaticValueFromUsd(NFT_PRICE_USD); | |
// CHECKS | |
unchecked { | |
if (msg.value < nftPriceInMatic) | |
revert InsufficientFunds({required: NFT_PRICE_USD}); | |
} | |
if (msg.value > nftPriceInMatic) { | |
// Transfer MATIC equivalent to (msg.value - nftPriceInMatic) back to the sender account | |
(bool callSuccess, ) = payable(msg.sender).call{ | |
value: msg.value - nftPriceInMatic | |
}(""); | |
require(callSuccess, "Call failed"); | |
} | |
// EFFECTS | |
emit NFTMinted(msg.sender); | |
} | |
/** | |
* @dev Get the latest price from a Chainlink Price Feed and convert it to a fixed-point integer. | |
* @return The latest price in fixed-point integer format (with 10 additional decimal places). | |
*/ | |
function getPrice() public view returns (uint256) { | |
unchecked { | |
try s_priceFeed.latestRoundData() returns ( | |
uint80 roundId, | |
int256 answer, | |
uint256, | |
uint256, | |
uint80 answeredInRound | |
) { | |
if (answer <= 0) revert AnswerIsNotGreaterThanZero(); | |
if (roundId != answeredInRound) revert InconsistentRoundId(); | |
return uint256(answer); | |
} catch { | |
revert CantFetchLatestRoundData(); | |
} | |
} | |
} | |
/** | |
* @dev Convert an amount in MATIC to its equivalent value in USD using a Chainlink Price Feed. | |
* @param _maticAmount The amount in MATIC to convert. | |
* @return The equivalent value in USD (with 10 additional decimal places). | |
*/ | |
function getConversionRate(uint256 _maticAmount) | |
external | |
view | |
returns (uint256) | |
{ | |
unchecked { | |
uint256 maticPrice = getPrice(); | |
uint256 decimals = s_priceFeed.decimals(); | |
uint256 maticAmountInUsd = (maticPrice * _maticAmount) / | |
10**decimals; | |
// The actual MATIC/USD conversion rate, after adjusting the extra 0s. | |
return maticAmountInUsd; | |
} | |
} | |
/** | |
* @dev Convert an amount in USD to its equivalent value in MATIC using a Chainlink Price Feed. | |
* @param _usdAmount The amount in USD to convert. | |
* @return The equivalent value in MATIC (with 10 additional decimal places). | |
*/ | |
function getMaticValueFromUsd(uint256 _usdAmount) | |
public | |
view | |
returns (uint256) | |
{ | |
unchecked { | |
uint256 maticPrice = getPrice(); | |
uint256 decimals = s_priceFeed.decimals(); | |
uint256 maticValue = (_usdAmount * 10**decimals) / maticPrice; | |
// The actual USD/MATIC conversion rate, after adjusting the extra 0s. | |
return maticValue; | |
} | |
} | |
function withdraw() external onlyOwner { | |
(bool callSuccess, ) = payable(msg.sender).call{ | |
value: address(this).balance | |
}(""); | |
require(callSuccess, "Call failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment