Last active
July 2, 2018 05:27
-
-
Save saharshagrawal/ba72fb55a8117e035fb3553234634958 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
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
pragma solidity ^0.4.13; | |
import "./Redemption.sol"; | |
import "./Rewards.sol"; | |
import "https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; | |
contract Factory { | |
mapping (uint => address) private contracts; | |
address private owner; | |
uint private numberOfContracts; | |
address private redemptionAddress; | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
function Factory(address _redemptionAddress) public{ | |
owner = msg.sender; | |
numberOfContracts = 0; | |
redemptionAddress = _redemptionAddress; | |
} | |
function createContract (string name, bytes merkleRoot, uint contractId, address distributorAddress) | |
onlyOwner() | |
public | |
{ | |
address newContract = new Rewards(name, merkleRoot,redemptionAddress,distributorAddress); | |
contracts[contractId] = newContract; | |
numberOfContracts +=1; | |
Redemption RedContract = Redemption(redemptionAddress); | |
RedContract.addRewardContract(newContract); | |
} | |
function getContract(uint contractId) public constant returns (address) { | |
return contracts[contractId]; | |
} | |
function getNumberOfContracts() public constant returns (uint){ | |
return numberOfContracts; | |
} | |
function getOwner() public constant returns (address) { | |
return owner; | |
} | |
} |
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
pragma solidity ^0.4.13; | |
import "https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; | |
contract Redemption { | |
using SafeMath for uint; | |
struct Request { | |
uint orderNumber; | |
address retailerAddress; | |
requestState state; | |
} | |
struct Retailer { | |
uint points; | |
uint ID; | |
} | |
mapping (address => Retailer) private retailerPoints; | |
mapping (address => bool) private retailerExist; | |
mapping (address => mapping (uint => Request)) private retailerRequests; | |
mapping (uint => bool) private requestExist; | |
mapping(address => bool) private rewardsAccess; | |
enum requestState {initialized, inProgress, flagged, closed} | |
address private owner; | |
address private factoryAddress; | |
bool private factoryAdded; | |
modifier enoughPoints (address retailerID, uint amount) { | |
require (retailerPoints[retailerID].points >= amount); | |
_; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
modifier retailerExists(address id) { | |
require(retailerExist[id] == true); | |
_; | |
} | |
modifier retailerNotExist(address id) { | |
require(retailerExist[id] == false); | |
_; | |
} | |
modifier requestExists(uint orderNum) { | |
require (requestExist[orderNum] == true); | |
_; | |
} | |
modifier isFactoryNotAdded() { | |
require(factoryAdded == false); | |
_; | |
} | |
modifier isFactory() { | |
require(msg.sender == factoryAddress); | |
_; | |
} | |
modifier isReward() { | |
require(rewardsAccess[msg.sender] == true); | |
_; | |
} | |
function Redemption() public{ | |
owner = msg.sender; | |
factoryAdded = false; | |
} | |
function addFactoryContract(address factory_address) | |
onlyOwner() | |
isFactoryNotAdded() | |
public | |
{ | |
factoryAddress = factory_address; | |
} | |
function addRewardContract(address reward_address) | |
isFactory() | |
public | |
{ | |
rewardsAccess[reward_address] = true; | |
} | |
function addPoints (uint points, address retailerId) | |
onlyOwner() | |
public | |
{ | |
if (retailerExist[retailerId] == false) { | |
if (points > 0) { | |
retailerPoints[retailerId] = Retailer(0, 1); | |
retailerExist[retailerId] = true; | |
retailerPoints[retailerId].points = points; | |
} | |
} | |
if (points > 0) { | |
retailerPoints[retailerId].points = (retailerPoints[retailerId].points).add(points); | |
} | |
} | |
function subtractPoints(uint amount, address retailerId) | |
enoughPoints(retailerId,amount) | |
onlyOwner() | |
retailerExists(retailerId) | |
public | |
{ | |
retailerPoints[retailerId].points = (retailerPoints[retailerId].points).sub(amount); | |
} | |
function getPoints(address retailerId) | |
retailerExists(retailerId) | |
public | |
constant | |
returns (uint){ | |
return retailerPoints[retailerId].points; | |
} | |
function addRetailers(address retailerId, uint ID) | |
onlyOwner() | |
retailerNotExist(retailerId) | |
public | |
{ | |
retailerPoints[retailerId] = Retailer(0, ID); | |
retailerExist[retailerId] = true; | |
} | |
function removeRetailers(address retailerId, uint ID) | |
onlyOwner() | |
retailerExists(retailerId) | |
public | |
{ | |
retailerPoints[retailerId] = Retailer(0, ID); | |
retailerExist[retailerId] = false; | |
} | |
function outside_addPoints (uint points, address retailerId) | |
retailerExists(retailerId) | |
public | |
{ | |
if (points > 0) { | |
retailerPoints[retailerId].points = (retailerPoints[retailerId].points).add(points); | |
} | |
} | |
function outside_subtractPoints(uint amount, address retailerId) | |
enoughPoints(retailerId,amount) | |
retailerExists(retailerId) | |
public | |
{ | |
retailerPoints[retailerId].points = (retailerPoints[retailerId].points).sub(amount); | |
} | |
function outside_addRetailers(address retailerId, uint ID, uint balance) | |
retailerNotExist(retailerId) | |
public | |
{ | |
retailerPoints[retailerId] = Retailer(balance, ID); | |
retailerExist[retailerId] = true; | |
} | |
function outside_removeRetailers(address retailerId, uint ID) | |
retailerExists(retailerId) | |
public | |
{ | |
retailerPoints[retailerId] = Retailer(0, ID); | |
retailerExist[retailerId] = false; | |
} | |
function makeRequest (uint orderNum) | |
retailerExists(msg.sender) | |
public | |
{ | |
address retailer = msg.sender; | |
Request memory R = Request(orderNum, retailer, requestState.initialized); | |
retailerRequests[msg.sender][orderNum] = R; | |
retailerExist[msg.sender] = true; | |
} | |
function removeRequest(uint orderNum) | |
retailerExists(msg.sender) | |
requestExists(orderNum) | |
public | |
{ | |
retailerExist[msg.sender] = false; | |
} | |
function updateRequestInProgess(uint orderNum) | |
retailerExists(msg.sender) | |
requestExists(orderNum) | |
public | |
{ | |
retailerRequests[msg.sender][orderNum].state = requestState.inProgress; | |
} | |
function updateRequestFlagged(uint orderNum) | |
retailerExists(msg.sender) | |
requestExists(orderNum) | |
public | |
{ | |
retailerRequests[msg.sender][orderNum].state = requestState.flagged; | |
} | |
function updateRequestClosed(uint orderNum) | |
retailerExists(msg.sender) | |
requestExists(orderNum) | |
public | |
{ | |
retailerRequests[msg.sender][orderNum].state = requestState.closed; | |
} | |
function getRequestState(address _retailerAddress, uint orderNum) | |
constant | |
requestExists(orderNum) | |
public | |
returns (string) | |
{ | |
if (retailerRequests[_retailerAddress][orderNum].state == requestState.initialized) { | |
return "Initialized"; | |
} | |
else if (retailerRequests[_retailerAddress][orderNum].state == requestState.inProgress) { | |
return "inProgress"; | |
} | |
else if (retailerRequests[_retailerAddress][orderNum].state == requestState.flagged) { | |
return "flagged"; | |
} | |
else if (retailerRequests[_retailerAddress][orderNum].state == requestState.closed) { | |
return "closed"; | |
} | |
} | |
} |
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
pragma solidity ^0.4.13; | |
import "./Redemption.sol"; | |
import "https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; | |
contract Rewards{ | |
using SafeMath for uint; | |
struct Retailer { | |
uint points; | |
bool isExist; | |
address distId; | |
} | |
enum contractState {open, inReview, closed} | |
address private redemptionAddress; | |
mapping (address => Retailer) private retailerPoints; | |
mapping (address => bool) private distAddresses; | |
mapping(uint => address) private addressCounter; | |
string private name; | |
uint private numberOfRetailers; | |
address private owner; | |
bytes private merkleRoot; | |
contractState private state; | |
modifier contractOpen () { | |
require (state == contractState.open); | |
_; | |
} | |
modifier validMerkle (bytes mRoot) { | |
require (keccak256(mRoot) == keccak256(merkleRoot)); | |
_; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner || distAddresses[msg.sender] == true); | |
_; | |
} | |
modifier enoughPoints (address retailerID, uint amount) { | |
require (retailerPoints[retailerID].points >= amount); | |
_; | |
} | |
modifier retailerExist(address id) { | |
require(retailerPoints[id].isExist == true); | |
_; | |
} | |
modifier retailerNotExist(address id) { | |
require(retailerPoints[id].isExist == false); | |
_; | |
} | |
modifier distNotExist(address id) { | |
require(distAddresses[id] == false); | |
_; | |
} | |
function Rewards(string _name, bytes _merkleRoot, address _redemptionAddress, address _distributorAddress) public{ | |
state = contractState.open; | |
merkleRoot = _merkleRoot; | |
name = _name; | |
owner = _distributorAddress; | |
redemptionAddress = _redemptionAddress; | |
numberOfRetailers = 0; | |
} | |
function addBalance(uint amount, address retailerId) | |
contractOpen() | |
retailerExist(retailerId) | |
public | |
{ | |
retailerPoints[retailerId].points = (retailerPoints[retailerId].points).add(amount); | |
} | |
function addDistributor(address distId) | |
onlyOwner() | |
contractOpen() | |
distNotExist(distId) | |
public | |
{ | |
distAddresses[distId] = true; | |
} | |
function addRetailers(address retailerId, address distId) | |
onlyOwner() | |
contractOpen() | |
retailerNotExist(retailerId) | |
public | |
{ | |
retailerPoints[retailerId] = Retailer(0,true, distId); | |
addressCounter[numberOfRetailers] = retailerId; | |
numberOfRetailers = numberOfRetailers + 1; | |
} | |
function inReviewContract() onlyOwner() public{ | |
state = contractState.inReview; | |
} | |
function onlyCloseContract(bytes _merkleRoot) public { | |
state = contractState.closed; | |
merkleRoot = _merkleRoot; | |
} | |
function closeContract(bytes _merkleRoot) onlyOwner() public{ | |
state = contractState.closed; | |
merkleRoot = _merkleRoot; | |
Redemption RedContract = Redemption(redemptionAddress); | |
for (uint i = 0; i<=numberOfRetailers; i+=1) { | |
address retailer_address = addressCounter[i]; | |
uint retailer_points = retailerPoints[retailer_address].points; | |
RedContract.outside_addRetailers(retailer_address,i,retailer_points); | |
} | |
} | |
function getPoints(address retailerId) | |
retailerExist(retailerId) | |
public | |
constant | |
returns (uint) { | |
return retailerPoints[retailerId].points; | |
} | |
function getOwner() public constant returns (address) { | |
return owner; | |
} | |
function getRoot() public constant returns (bytes) { | |
return merkleRoot; | |
} | |
function getName() public constant returns (string) { | |
return name; | |
} | |
function getState() public constant returns (string) { | |
if (state == contractState.open) { | |
return "open"; | |
} | |
if (state == contractState.closed) { | |
return "closed"; | |
} | |
if (state == contractState.inReview) { | |
return "inReview"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment