mintTicketPayable
This function allows the minting of tickets for specified recipients, events, access levels, and ticket amounts while collecting payment in Ether. It takes four parameters:
_to
: An array containing the addresses of recipients for whom tickets are to be minted._eventIds
: An array containing the unique identifiers for the events for which tickets are to be minted._accessLevels
: An array containing the access levels associated with the tickets being minted._amounts
: An array containing the amounts of tickets to be minted for each recipient.
// Some code
```remix-solidity
/**
MintTicketPayable: Mint tickets for the specified recipients, events, access levels, and amounts, collecting payment in Ether.
@param _to: Array of recipient addresses for whom tickets are to be minted.
@param _eventIds: Array of event IDs for which tickets are to be minted.
@param _accessLevels: Array of access levels associated with the tickets being minted.
@param _amounts: Array of amounts of tickets to be minted for each recipient.
Format: to, eventIds, accessLevels, amounts
*/
function mintTicketPayable(
address[] calldata _to,
string[] calldata _eventIds,
string[] calldata _accessLevels,
uint256[] calldata _amounts
) public payable nonReentrant {
// Check if the lengths of input arrays match
if (!checkLengths(_to, _eventIds, _accessLevels, _amounts))
revert DecastGating__UnmatchedArrayLength();
// Initialize variable to store total collected amount
uint256 totalCollectedAmount = 0;
// Iterate through each recipient
for (uint256 i = 0; i < _to.length; ++i) {
// Get the token ID for the specified event and access level
uint256 _tokenId = getTokenIdOfAccessLevel(
_eventIds[i],
_accessLevels[i]
);
// Check if the ticket price is not 0 and the amount is valid
if (getTicketPriceFromTokenId(_tokenId) == 0)
revert DecastGating__UnMatchedInputAmount();
if (_amounts[i] <= 0) revert DecastGating__InvalidInputAmount();
// Check if the token ID exists for the specified event
if (!checkTokenIdExistsForEventId(_eventIds[i], _tokenId))
revert DecastGating__TokenIdDoesNotExists();
// Calculate and accumulate the total amount to be collected
totalCollectedAmount +=
getTicketPriceFromTokenId(_tokenId) *
_amounts[i];
// Update collected amount for the event ID
s_collectedAmountForEventId[_eventIds[i]] +=
_amounts[i] *
getTicketPriceFromTokenId(_tokenId);
// Mint the ticket
_mintTicket(_eventIds[i], _to[i], _tokenId, _amounts[i]);
}
// Check if the total collected amount matches the sent Ether value
if (totalCollectedAmount != msg.value)
revert DecastGating__UnMatchedAccumulatedAmount();
}
```
Explanation:
The function begins by verifying whether the lengths of the input arrays match using the checkLengths
function to ensure data consistency. It then initializes a variable to store the total collected amount of Ether. Next, the function iterates through each recipient in the _to
array. For each recipient, it retrieves the corresponding token ID for the specified event and access level using the getTokenIdOfAccessLevel
function. It checks whether the ticket price associated with the token ID is not zero and whether the requested ticket amount is valid. Additionally, it verifies whether the token ID exists for the specified event. If all validation checks pass, the function calculates and accumulates the total amount to be collected in Ether based on the ticket prices and amounts. It updates the collected amount for the respective event ID and then mints the tickets using the _mintTicket
internal function.
Finally, the function checks whether the total collected amount matches the sent Ether value. If the amounts do not match, indicating an inconsistency, the function reverts the transaction to maintain data integrity.
Last updated