Created
May 14, 2018 19:56
-
-
Save GeekyShiva/fc91272269dda4120025cb2f70bc8df7 to your computer and use it in GitHub Desktop.
ad smart contract based on ethereum
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
// in src/ethereum/ZeroDollarHomePage.sol | |
contract ZeroDollarHomePage { | |
uint constant ShaLength = 40; | |
enum ResponseCodes { | |
Ok, | |
InvalidPullRequestId, | |
InvalidAuthorName, | |
InvalidImageUrl, | |
RequestNotFound, | |
EmptyQueue, | |
PullRequestAlreadyClaimed | |
} | |
struct Request { | |
uint id; | |
string authorName; | |
string imageUrl; | |
uint createdAt; | |
uint displayedAt; | |
} | |
// what the contract stores | |
mapping (uint => Request) _requests; // key is the pull request id | |
uint public numberOfRequests; | |
uint[] _queue; | |
uint public queueLength; | |
uint _current; | |
address owner; | |
// constructor | |
function ZeroDollarHomePage() { | |
owner = msg.sender; | |
numberOfRequests = 0; | |
queueLength = 0; | |
_current = 0; | |
} | |
// a contract must give a way to destroy itself once uploaded to the blockchain | |
function remove() { | |
if (msg.sender == owner){ | |
suicide(owner); | |
} | |
} | |
// the following three methods are public contracts entry points | |
function newRequest(uint pullRequestId, string authorName, string imageUrl) returns (uint8 code, uint displayDate) { | |
if (pullRequestId <= 0) { | |
// Solidity is a strong typed language. You get compilation errors when types mismatch | |
code = uint8(ResponseCodes.InvalidPullRequestId); | |
return; | |
} | |
if (_requests[pullRequestId].id == pullRequestId) { | |
code = uint8(ResponseCodes.PullRequestAlreadyClaimed); | |
return; | |
} | |
if (bytes(authorName).length <= 0) { | |
code = uint8(ResponseCodes.InvalidAuthorName); | |
return; | |
} | |
if (bytes(imageUrl).length <= 0) { | |
code = uint8(ResponseCodes.InvalidImageUrl); | |
return; | |
} | |
// store new pull request details | |
numberOfRequests += 1; | |
_requests[pullRequestId].id = pullRequestId; | |
_requests[pullRequestId].authorName = authorName; | |
_requests[pullRequestId].imageUrl = imageUrl; | |
_requests[pullRequestId].createdAt = now; | |
_queue.push(pullRequestId); | |
queueLength += 1; | |
code = uint8(ResponseCodes.Ok); | |
displayDate = now + (queueLength * 1 days); | |
// no need to explicitly return code and displayDate as they are in the method signature | |
} | |
function closeRequest() returns (uint8) { | |
if (queueLength == 0) { | |
return uint8(ResponseCodes.EmptyQueue); | |
} | |
_requests[_queue[_current]].displayedAt = now; | |
delete _queue[0]; | |
queueLength -= 1; | |
_current = _current + 1; | |
return uint8(ResponseCodes.Ok); | |
} | |
function getLastNonPublished() returns (uint8 code, uint id, string authorName, string imageUrl, uint createdAt) { | |
if (queueLength == 0) { | |
code = uint8(ResponseCodes.EmptyQueue); | |
return; | |
} | |
var request = _requests[_queue[_current]]; | |
id = request.id; | |
authorName = request.authorName; | |
imageUrl = request.imageUrl; | |
createdAt = request.createdAt; | |
code = uint8(ResponseCodes.Ok); | |
} | |
} | |
https://marmelab.com/blog/2016/05/20/blockchain-for-web-developers-in-practice.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment