Created
June 2, 2018 11:04
-
-
Save dima91/c06d1619a80798f09bb752bb0dd74b21 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=true&gist=
This file contains hidden or 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.24; | |
import "./sharedTypes.sol"; | |
contract BaseContentManagementContract { | |
address private authorAddress; // Address of contract's author | |
address private catalogAddress; // Address of catalog to check if determined functions are called only by catalog | |
SharedTypes.contentType private typeOfContent; // Tpe of content which this contract contains | |
string private contentTitle; // Title which identifies this contract in catalog | |
string private content; // Content of resource | |
uint private numberOfViews; // Number of viwes about content of this contract | |
mapping (address => bool) allowedUsers; // Map of users that are allowed to access this content | |
constructor (SharedTypes.contentType _conType, string _conTitle, string _cnt, address _catAddr) public { | |
typeOfContent = _conType; | |
authorAddress= msg.sender; | |
numberOfViews= 0; | |
contentTitle= _conTitle; | |
content= _cnt; | |
catalogAddress= _catAddr; | |
} | |
// ***** ***** // | |
// ***** Modifiers ***** // | |
modifier onlyCatalog () { | |
require (msg.sender == catalogAddress); | |
_; | |
} | |
modifier onlyAllowedUsers (address _userAddr) { | |
require (allowedUsers[_userAddr] == true); | |
_; | |
} | |
// ***** ***** // | |
// ***** Helper private/internal functions ***** // | |
// ***** ***** // | |
// ***** Public functions ***** // | |
// Function used by catalog to check that has the same address of catalogAddress local variable | |
function getCatalogAddress () public view returns (address) { | |
return catalogAddress; | |
} | |
function getViewsCount () public view returns (uint) { | |
return numberOfViews; | |
} | |
function getTitle () public view returns (string) { | |
return contentTitle; | |
} | |
// This function allows (only to catalog) to grant access to a user with address _userAddr | |
function grantAccessToUser (address _userAddr) public onlyCatalog () { | |
allowedUsers[_userAddr]= true; | |
} | |
/* This function allows subContracts of this contract to retreive content if and only if user | |
* with addrs _userAddr is into "allowedUsers" map | |
*/ | |
function retreiveContent (address _userAddr) internal onlyAllowedUsers(_userAddr) returns (string) { | |
allowedUsers[_userAddr]= false; | |
numberOfViews++; | |
return content; | |
} | |
// ***** ***** // | |
// ***** Abastract functions ***** // | |
function consumeContent (address _userAddr) public returns (string); | |
} |
This file contains hidden or 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.24; | |
pragma experimental ABIEncoderV2; | |
import "./sharedTypes.sol"; | |
import "./ownable.sol"; | |
import "./contentManagementContracts.sol"; | |
contract CatalogSmartContract is Ownable { | |
// True if 'selfdestruct' function wasn't called --> is this necessary? | |
bool active= false; | |
// Mapping which contains a User struct for each user that hav published or have requeested a content | |
mapping (address => SharedTypes.User) usersMapping; | |
// Array containing addresses of users registered to system (for loop functions) | |
address [] usersArray; | |
// Number of user (also index of next user in the array) | |
uint usersCount; | |
// Mapping which contains an ExtendedContent struct for each content published on the platform (identified by a string) | |
mapping (string => SharedTypes.ExtendedContent) contentsMapping; | |
// Array containing addresses of contents published on the system (for loop functions) | |
string [] contentsArray; | |
// Number of contents (also index of next content in the array) | |
uint contentsCount; | |
constructor () public { | |
active= true; | |
usersCount=0; | |
contentsCount= 0; | |
} | |
// ***** ***** // | |
// ***** Modifiers ***** // | |
modifier isActive () { | |
require (active == true); | |
_; | |
} | |
modifier notPublished (string _contentTitle) { | |
require (alreadyPublished(_contentTitle) == false); | |
_; | |
} | |
// ***** ***** // | |
// ***** Helper private functions ***** // | |
// Function to check if a user exists or not in usersMapping | |
function userExists (address _addr) private view returns (bool){ | |
return (usersMapping[_addr].exists == true); | |
} | |
// Function which returns wether a content is already published on the platform | |
function alreadyPublished (string conTitle) private view returns (bool) { | |
return (contentsMapping[conTitle].exists == true); | |
} | |
// Function to register an user to system | |
function addUser (address _addr) private { | |
usersCount++; | |
usersArray.push (_addr); | |
usersMapping[_addr].accType= SharedTypes.accountType.standard; | |
usersMapping[_addr].exists= true; | |
usersMapping[_addr].expirationTime= 0; | |
usersMapping[_addr].latestContent = ""; | |
} | |
// Function to add new content with address 'contAddr' registered by user with address 'userAddr' | |
function addContent (string _conTitle, address _contAddr, address _userAddr, SharedTypes.contentType _ct) private { | |
contentsCount++; | |
contentsArray.push (_conTitle); | |
contentsMapping[_conTitle].exists= true; | |
contentsMapping[_conTitle].owner= _userAddr; | |
contentsMapping[_conTitle].contractAddress= _contAddr; | |
contentsMapping[_conTitle].cType= _ct; | |
} | |
// ***** ***** // | |
// ***** Public functions ***** // | |
function killMe () public onlyOwner() { | |
active= false; | |
selfdestruct (owner); | |
} | |
// Function to link a content with the system | |
function publishContent (string _contentTitle, address _contentAddr, SharedTypes.contentType _ct) public { | |
if (!userExists(msg.sender)) { | |
// Registering new user | |
addUser (msg.sender); | |
} | |
addContent (_contentTitle, _contentAddr, msg.sender, _ct); | |
} | |
// Returns the number of views for each content | |
function getStatistics () public returns (uint [], string []) { | |
uint [] storage stats= new uint[] (contentsCount); | |
stats.push (10); | |
return (stats, contentsArray); | |
} | |
// Returns the list of contents without the number of views | |
function getContentList () public view returns (string []) { | |
string [] storage conList; | |
// TODO | |
return conList; | |
} | |
/* // Returns the list of x newest contents | |
function getNewContentList (uint n) public view returns (...) { | |
} | |
// Returns the most recent content with genre x | |
function getLatestByGenre (...) public view returns (...) { | |
} | |
// Returns the content with genre x, which has received the maximum number of views | |
function getMostPopularByGenre (...) public view returns (...) { | |
} | |
// Returns the most recent content of the author x | |
function getLatestByAuthor (...) public view returns (...) { | |
} | |
// Returns the content with most views of the author x | |
function getMostPopularByAuthor (...) public view returns (...) { | |
} | |
// Returns true if x holds a still valid premium account, false otherwise | |
function isPremium (...) public view returns (...) { | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
// Pays for access to content x | |
/*function getContent (...) public { | |
} | |
// Requests access to content x without paying, premium accounts only | |
function getContentPremium (...) public { | |
} | |
// Pays for granting access to content x to the user u | |
function giftContent (..., ...) public { | |
} | |
// Pays for granting a Premium Account to the user u | |
function giftPremium (...) public { | |
} | |
// Starts a new premium subscription | |
function buyPremium () public { | |
} | |
*/ | |
} |
This file contains hidden or 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.24; | |
import "./baseContentManagementContract.sol"; | |
contract SongManagementContract is BaseContentManagementContract { | |
constructor (string _cnt, string _title, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.song, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (address _userAddr) public returns (string) { | |
return retreiveContent (_userAddr); | |
} | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
contract VideoManagementContract is BaseContentManagementContract { | |
constructor (string _cnt, string _title, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.video, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (address _userAddr) public returns (string) { | |
return retreiveContent (_userAddr); | |
} | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
contract PhotoManagementContract is BaseContentManagementContract { | |
constructor (string _cnt, string _title, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.photo, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (address _userAddr) public returns (string) { | |
return retreiveContent (_userAddr); | |
} | |
} | |
/* ********************************************* */ | |
/* ********************************************* */ | |
/* ********************************************* */ | |
contract DocumentManagementContract is BaseContentManagementContract { | |
constructor (string _cnt, string _title, address _catAddr) | |
BaseContentManagementContract(SharedTypes.contentType.document, _title, _cnt, _catAddr) public { | |
} | |
function consumeContent (address _userAddr) public returns (string) { | |
return retreiveContent (_userAddr); | |
} | |
} |
This file contains hidden or 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.24; | |
import "./mortal.sol"; | |
contract Crowdfund { | |
struct Funder { | |
address addr; | |
uint amount; | |
} | |
struct Project { | |
bool created; | |
bool opened; | |
string name; | |
address owner; | |
mapping (uint => Funder) funders; | |
uint fundersSize; | |
uint amount; | |
uint fundingGoal; | |
} | |
event ProjectCreated (string _name, uint _fundingGoal); | |
event FundTransferred (address _backer, string _project, uint _amount, uint _remainingAmount); | |
event fundingGoalReached (string project); | |
address private owner; | |
mapping (string => Project) private projects; | |
modifier ProjectNotExists (string _name) { | |
require (projects[_name].created == false); | |
_; | |
} | |
modifier ProjectExists (string _name) { | |
require (projects[_name].created == true); | |
_; | |
} | |
constructor () public { | |
owner= msg.sender; | |
} | |
function createProject (string _projectName, uint _amount) public ProjectNotExists (_projectName) { | |
projects[_projectName]= Project (true, true, _projectName, msg.sender, 0, 0, _amount); | |
emit ProjectCreated (_projectName, _amount); | |
} | |
function sendMoney (string _projectName, uint _sum) public ProjectExists (_projectName) { | |
Project storage p= projects[_projectName]; | |
p.funders[p.fundersSize]= Funder (msg.sender, _sum); | |
p.fundersSize++; | |
p.amount += _sum; | |
uint remaining= p.fundingGoal - _sum; | |
emit FundTransferred (msg.sender, _projectName, _sum, remaining); | |
checkGoalReached (_projectName); | |
} | |
function checkGoalReached (string _projectName) private returns (bool) { | |
Project storage p= projects[_projectName]; | |
if (p.fundingGoal <= p.amount) { | |
emit fundingGoalReached (_projectName); | |
p.opened= false; | |
return true; | |
} | |
return false; | |
} | |
function getProject (string _projectName) public view returns (bool, address, uint, uint, uint, bool) { | |
Project storage p= projects[_projectName]; | |
return (p.opened, p.owner, p.fundersSize, p.amount, p.fundingGoal, p.opened); | |
} | |
} |
This file contains hidden or 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.24; | |
contract Mortal { | |
address owner; | |
constructor () public { | |
owner= msg.sender; | |
} | |
function kill () public { | |
if (msg.sender == owner) | |
selfdestruct (owner); | |
} | |
} |
This file contains hidden or 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.24; | |
contract Ownable { | |
address owner; | |
constructor () public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner () { | |
require (msg.sender == owner); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment