Created
May 13, 2018 01:42
-
-
Save SeptiyanAndika/448b05e6e5f0effd7aff56dd7ce49509 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.23+commit.124ca40d.js&optimize=false&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.0; | |
| contract Campaign { | |
| uint256 public maxContributor; | |
| uint256 public minContributor; | |
| address public owner; | |
| bool public campaignEnd; | |
| bool public campaignSuccess; | |
| address[] private contributorIndex; | |
| address[] private publisherIndex; | |
| address tokenAddress; | |
| struct Contributor { | |
| string ipfsHash; | |
| string policy_id; | |
| string capsule; | |
| address contributorAddress; | |
| address publisherAddress; | |
| uint index; | |
| } | |
| struct PublisherData { | |
| uint256 contributionCount; | |
| uint256 index; | |
| } | |
| mapping(address => Contributor) private contributors; | |
| mapping(address => PublisherData) private publishers; | |
| event LogNewContributor (address indexed contributorAddress, address publisherAddress, string ipfsHash); | |
| event LogCampaignResult (address publisherAddress, uint256 contributionCount); | |
| constructor(uint256 _minContributor,uint256 _maxContributor) public { | |
| owner = msg.sender; | |
| minContributor = _minContributor; | |
| maxContributor = _maxContributor; | |
| campaignEnd = false; | |
| campaignSuccess = false; | |
| } | |
| function isContributor(address userAddress) public constant returns(bool isIndeed) { | |
| if(contributorIndex.length == 0) return false; | |
| return (contributorIndex[contributors[userAddress].index] == userAddress); | |
| } | |
| function isPublisher(address publisherAddress) public constant returns(bool isIndeed) { | |
| if(publisherIndex.length == 0) return false; | |
| return (publisherIndex[publishers[publisherAddress].index] == publisherAddress); | |
| } | |
| function getTotalContributor() public constant returns(uint count){ | |
| return contributorIndex.length; | |
| } | |
| function setTokenAddress(address _tokenAddress) public{ | |
| require(msg.sender == owner); | |
| require(!campaignEnd); | |
| tokenAddress = _tokenAddress; | |
| } | |
| function contribute(address _contributorAddress, address _publisherAddress, string _policy_id,string _ipfsHash, string _capsule) public returns(uint index){ | |
| require(!campaignEnd); | |
| require(!isContributor(_contributorAddress)); | |
| require(getTotalContributor()<maxContributor); | |
| contributors[_contributorAddress].policy_id = _policy_id; | |
| contributors[_contributorAddress].ipfsHash = _ipfsHash; | |
| contributors[_contributorAddress].capsule = _capsule; | |
| contributors[_contributorAddress].publisherAddress = _publisherAddress; | |
| contributors[_contributorAddress].index = contributorIndex.push(_contributorAddress)-1; | |
| emit LogNewContributor(_contributorAddress,_publisherAddress,_ipfsHash); | |
| publishers[_publisherAddress].contributionCount += 1; | |
| if(!isPublisher(_publisherAddress)){ | |
| publishers[_publisherAddress].index = publisherIndex.push(_publisherAddress)-1; | |
| } | |
| return contributorIndex.length-1; | |
| } | |
| function getTotalContributorPublisher (address _publisherAddress) public view returns(uint index){ | |
| return publishers[_publisherAddress].contributionCount; | |
| } | |
| function endCampaign() public{ | |
| require(msg.sender == owner); | |
| require(!campaignEnd); | |
| campaignEnd = true; | |
| // check getTotalContributor | |
| if(getTotalContributor()>=minContributor){ | |
| campaignSuccess = true; | |
| for (uint i = 0; i < publisherIndex.length; i++) { | |
| emit LogCampaignResult(publisherIndex[i], publishers[publisherIndex[i]].contributionCount); | |
| } | |
| } | |
| } | |
| function disburseReward() public view{ | |
| require(msg.sender == owner); | |
| require(campaignSuccess); | |
| for (uint i = 0; i < publisherIndex.length; i++) { | |
| // uint256 _tokensToDistributePub = 0.5* (10**18); | |
| // trasnfer token to publisher | |
| } | |
| for (uint j = 0; j < contributorIndex.length; j++) { | |
| //uint256 _tokensToDistributeCont = 1* (10**18); | |
| // trasnfer token to contributor | |
| } | |
| } | |
| function getDataContributor(address _contributorAddress) public view returns (string,string,string){ | |
| require(msg.sender == owner); | |
| require(campaignSuccess); | |
| return (contributors[_contributorAddress].policy_id,contributors[_contributorAddress].ipfsHash,contributors[_contributorAddress].capsule); | |
| } | |
| function getAllContributors() public view returns (address[]){ | |
| require(msg.sender == owner); | |
| require(campaignSuccess); | |
| return contributorIndex; | |
| } | |
| } |
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.0; | |
| import "./stringUtils.sol"; | |
| contract MultiCampaign { | |
| struct Campaign { | |
| uint256 maxContributor; | |
| uint256 minContributor; | |
| bool campaignEnd; | |
| bool campaignSuccess; | |
| address[] contributorIndex; | |
| address[] publisherIndex; | |
| mapping(address => Contributor) contributors; | |
| mapping(address => PublisherData) publishers; | |
| uint index; | |
| uint256 reward; | |
| } | |
| struct Contributor { | |
| string ipfsHash; | |
| string policy_id; | |
| string capsule; | |
| address contributorAddress; | |
| address publisherAddress; | |
| uint index; | |
| } | |
| struct PublisherData { | |
| uint256 contributionCount; | |
| uint256 index; | |
| } | |
| address public owner; | |
| address public tokenAddress; | |
| mapping(string => Campaign) private campaigns; | |
| string[] private campaignIndex; | |
| event LogNewCampaign (string campaignId, uint256 index); | |
| event LogNewContributor (address indexed contributorAddress, address publisherAddress, string ipfsHash); | |
| event LogCampaignResult (address publisherAddress, uint256 contributionCount); | |
| constructor() public { | |
| owner = msg.sender; | |
| } | |
| function newCampaign(string campaignId,uint256 _minContributor,uint256 _maxContributor,uint256 _reward) public { | |
| require(msg.sender == owner); | |
| campaigns[campaignId].minContributor = _minContributor; | |
| campaigns[campaignId].maxContributor = _maxContributor; | |
| campaigns[campaignId].reward = _reward; | |
| campaigns[campaignId].index = campaignIndex.push(campaignId)-1; | |
| emit LogNewCampaign(campaignId, campaigns[campaignId].index); | |
| } | |
| function getTotalCampaign() public view returns(uint256) { | |
| return campaignIndex.length; | |
| } | |
| function isCampaign(string campaignId) public view returns(bool) { | |
| if(campaignIndex.length == 0) return false; | |
| return StringUtils.equal(campaignIndex[campaigns[campaignId].index],campaignId); | |
| } | |
| function isContributor(string campaignId,address userAddress) public constant returns(bool isIndeed) { | |
| if(campaigns[campaignId].contributorIndex.length == 0) return false; | |
| return (campaigns[campaignId].contributorIndex[campaigns[campaignId].contributors[userAddress].index] == userAddress); | |
| } | |
| function isPublisher(string campaignId,address publisherAddress) public constant returns(bool isIndeed) { | |
| if(campaigns[campaignId].publisherIndex.length == 0) return false; | |
| return (campaigns[campaignId].publisherIndex[campaigns[campaignId].publishers[publisherAddress].index] == publisherAddress); | |
| } | |
| function getTotalContributor(string campaignId) public constant returns(uint count){ | |
| return campaigns[campaignId].contributorIndex.length; | |
| } | |
| function setTokenAddress(address _tokenAddress) public{ | |
| require(msg.sender == owner); | |
| tokenAddress = _tokenAddress; | |
| } | |
| function contribute(string campaignId,address _contributorAddress, address _publisherAddress, string _policy_id,string _ipfsHash, string _capsule) public returns(uint index){ | |
| require(!campaigns[campaignId].campaignEnd); | |
| require(!isContributor(campaignId,_contributorAddress)); | |
| require(getTotalContributor(campaignId)<campaigns[campaignId].maxContributor); | |
| campaigns[campaignId].contributors[_contributorAddress].policy_id = _policy_id; | |
| campaigns[campaignId].contributors[_contributorAddress].ipfsHash = _ipfsHash; | |
| campaigns[campaignId].contributors[_contributorAddress].capsule = _capsule; | |
| campaigns[campaignId].contributors[_contributorAddress].publisherAddress = _publisherAddress; | |
| campaigns[campaignId].contributors[_contributorAddress].index = campaigns[campaignId].contributorIndex.push(_contributorAddress)-1; | |
| emit LogNewContributor(_contributorAddress,_publisherAddress,_ipfsHash); | |
| campaigns[campaignId].publishers[_publisherAddress].contributionCount += 1; | |
| if(!isPublisher(campaignId,_publisherAddress)){ | |
| campaigns[campaignId].publishers[_publisherAddress].index = campaigns[campaignId].publisherIndex.push(_publisherAddress)-1; | |
| } | |
| return campaigns[campaignId].contributorIndex.length-1; | |
| } | |
| function getTotalContributorPublisher (string campaignId,address _publisherAddress) public view returns(uint index){ | |
| return campaigns[campaignId].publishers[_publisherAddress].contributionCount; | |
| } | |
| function endCampaign(string campaignId) public{ | |
| require(msg.sender == owner); | |
| require(!campaigns[campaignId].campaignEnd); | |
| campaigns[campaignId].campaignEnd = true; | |
| // check getTotalContributor | |
| if(getTotalContributor(campaignId)>=campaigns[campaignId].minContributor){ | |
| campaigns[campaignId].campaignSuccess = true; | |
| for (uint i = 0; i < campaigns[campaignId].publisherIndex.length; i++) { | |
| emit LogCampaignResult(campaigns[campaignId].publisherIndex[i], campaigns[campaignId].publishers[campaigns[campaignId].publisherIndex[i]].contributionCount); | |
| } | |
| } | |
| } | |
| function disburseReward(string campaignId) public view{ | |
| require(msg.sender == owner); | |
| require(campaigns[campaignId].campaignSuccess); | |
| for (uint i = 0; i < campaigns[campaignId].publisherIndex.length; i++) { | |
| // uint256 _tokensToDistributePub = 0.5* (10**18); | |
| // trasnfer token to publisher | |
| } | |
| for (uint j = 0; j < campaigns[campaignId].contributorIndex.length; j++) { | |
| //uint256 _tokensToDistributeCont = 1* (10**18); | |
| // trasnfer token to contributor | |
| } | |
| } | |
| function getAllContributors(string campaignId) public view returns (address[]){ | |
| require(msg.sender == owner); | |
| require(campaigns[campaignId].campaignSuccess); | |
| return campaigns[campaignId].contributorIndex; | |
| } | |
| function getDataContributor(string campaignId,address _contributorAddress) public view returns (string,string,string){ | |
| require(msg.sender == owner); | |
| require(campaigns[campaignId].campaignSuccess); | |
| return (campaigns[campaignId].contributors[_contributorAddress].policy_id,campaigns[campaignId].contributors[_contributorAddress].ipfsHash,campaigns[campaignId].contributors[_contributorAddress].capsule); | |
| } | |
| } |
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
| // from https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol | |
| library StringUtils { | |
| /// @dev Does a byte-by-byte lexicographical comparison of two strings. | |
| /// @return a negative number if `_a` is smaller, zero if they are equal | |
| /// and a positive numbe if `_b` is smaller. | |
| function compare(string _a, string _b) returns (int) { | |
| bytes memory a = bytes(_a); | |
| bytes memory b = bytes(_b); | |
| uint minLength = a.length; | |
| if (b.length < minLength) minLength = b.length; | |
| //@todo unroll the loop into increments of 32 and do full 32 byte comparisons | |
| for (uint i = 0; i < minLength; i ++) | |
| if (a[i] < b[i]) | |
| return -1; | |
| else if (a[i] > b[i]) | |
| return 1; | |
| if (a.length < b.length) | |
| return -1; | |
| else if (a.length > b.length) | |
| return 1; | |
| else | |
| return 0; | |
| } | |
| /// @dev Compares two strings and returns true iff they are equal. | |
| function equal(string _a, string _b) returns (bool) { | |
| return compare(_a, _b) == 0; | |
| } | |
| /// @dev Finds the index of the first occurrence of _needle in _haystack | |
| function indexOf(string _haystack, string _needle) returns (int) | |
| { | |
| bytes memory h = bytes(_haystack); | |
| bytes memory n = bytes(_needle); | |
| if(h.length < 1 || n.length < 1 || (n.length > h.length)) | |
| return -1; | |
| else if(h.length > (2**128 -1)) // since we have to be able to return -1 (if the char isn't found or input error), this function must return an "int" type with a max length of (2^128 - 1) | |
| return -1; | |
| else | |
| { | |
| uint subindex = 0; | |
| for (uint i = 0; i < h.length; i ++) | |
| { | |
| if (h[i] == n[0]) // found the first char of b | |
| { | |
| subindex = 1; | |
| while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) // search until the chars don't match or until we reach the end of a or b | |
| { | |
| subindex++; | |
| } | |
| if(subindex == n.length) | |
| return int(i); | |
| } | |
| } | |
| return -1; | |
| } | |
| } | |
| } |
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.0; | |
| contract Test{ | |
| event callMeMaybeEvent(address _from); | |
| function testFunction() public { | |
| emit callMeMaybeEvent(msg.sender); | |
| } | |
| } | |
| contract Test2{ | |
| function testFunction1() public constant returns (address){ | |
| return this; | |
| } | |
| function testFunction2() public constant returns (address){ | |
| return msg.sender; | |
| } | |
| function testFunction3(address _contractAddress) public{ | |
| Test t = Test(_contractAddress); | |
| t.testFunction(); | |
| } | |
| function testFunction4(address _contractAddress) public { | |
| _contractAddress.call(bytes4(keccak256("testFunction()"))); | |
| } | |
| function testFunction5(address _contractAddress) public { | |
| _contractAddress.delegatecall(bytes4(keccak256("testFunction()"))); | |
| } | |
| } |
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
| contract TestBaru { | |
| event callMeMaybeEvent(uint256 index); | |
| string[] public campaignIndex; | |
| function newData(string data){ | |
| emit callMeMaybeEvent(campaignIndex.push(data)); | |
| } | |
| function getTotal() public view returns (uint256){ | |
| return campaignIndex.length; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment