Skip to content

Instantly share code, notes, and snippets.

@marceljay
Created November 12, 2020 20:44
Show Gist options
  • Save marceljay/97f24df9f48caeb420b71b862dfd2427 to your computer and use it in GitHub Desktop.
Save marceljay/97f24df9f48caeb420b71b862dfd2427 to your computer and use it in GitHub Desktop.
pragma solidity 0.5.17;
contract Counter {
uint256 private _count;
address private _owner;
address private _factory;
modifier onlyOwner(address caller) {
require(caller == _owner, "You're not the owner of the contract");
_;
}
modifier onlyFactory() {
require(msg.sender == _factory, "You need to use the factory");
_;
}
constructor(address owner) public {
_owner = owner;
_factory = msg.sender;
}
function getCount() public view returns (uint256) {
return _count;
}
function incrementor(address caller) public onlyFactory onlyOwner(caller) {
_count++;
}
}
contract CounterFactory {
// Explanation: address maps to contract address
mapping(address => Counter) _counters;
function createCounter() public {
require (_counters[msg.sender] == Counter(0));
_counters[msg.sender] = new Counter(msg.sender);
}
function increment() public {
require (_counters[msg.sender] != Counter(0));
Counter(_counters[msg.sender]).incrementor(msg.sender);
}
function getCounter() public view returns (Counter){
return _counters[msg.sender];
}
// Return the address of the contract
function getCount(address account) public view returns (uint256) {
require (_counters[account] != Counter(0));
return (_counters[account].getCount());
}
function getMyCount() public view returns (uint256) {
return (getCount(msg.sender));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment