I have a struct mapped with an address as the key. I want to iterate through all values of that mapping with another key value, in this case a nonce that I generate in the contract. Is this possible? What is the correct terminology for this process if it is?
I have been pouring over all of Rob Hitchens' storage posts (Are there well-solved and simple storage patterns for Solidity? among others), but I'm stuck conceptually. I know I can just create an array, but since I want to be able to delete records by address, a mapping makes much more sense. I also know I can create a mapping and a second array to track all of the addresses, but that is much more wasteful than having a single mapping.
Here is current code and context:
struct Subscription {
address subscriberAddress; //Address of the subscriber.
uint256 timeOfLastCharge; //Time in Unix seconds.
uint256 nonce;
}
mapping(address => Subscriptions) public subscriptionRegistry; // List of all subscribers. Lookup via subscriber address.
//mapping(address => uint)
function initiateSubscription (address _subscriberAddress) external {
require(msg.sender == contractManager, "You are not authorized to call this function.");
require(subscriptionRegistry[_subscriberAddress].subscriberAddress == address(0), "A subscription already exists for this address.");
Subscription storage newSubscription = Subscriptions[_subscriberAddress];
newSubscription.subscriberAddress = _subscriberAddress;
newSubscription.timeOfLastCharge = now;
newSubscription.nonce = currentNonce;
tokenContract.transferFrom(_subscriberAddress, merchant, subscriptionPrice);
emit subscriptionEvent(_subscriberAddress, subscriptionPrice, billingInterval);
}