Created
January 29, 2020 13:37
-
-
Save matejthetree/725e651e83b2de8d1d9a50d2e09fb32d to your computer and use it in GitHub Desktop.
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
payable contract MemeVote = | |
record meme = | |
{ creatorAddress : address, | |
url : string, | |
name : string, | |
voteCount : int | |
} | |
record state = | |
{ memes : list(meme), | |
votes : map(int, int) | |
} | |
entrypoint init() = | |
{ memes = [], votes = {} } | |
entrypoint getMeme(index : int) : meme => | |
state.memes[index] | |
entrypoint getVote(index : int) : int = | |
switch(Map.lookup(index, state.votes)) | |
None => abort("There are no votes for meme with this index.") | |
Some(x) => x | |
stateful entrypoint registerMeme(url' : string, name' : string) = | |
let meme = { creatorAddress = Call.caller, url = url', name = name', voteCount = 0} | |
let newMemes = state.memes ++ meme | |
put(state{ memes = newMemes}) | |
entrypoint getMemesLength() : int = | |
len(memes) | |
payable stateful entrypoint voteMeme(index : int) = | |
let meme = getMeme(index) | |
Chain.spend(meme.creatorAddress, Call.value) | |
let updatedVoteCount = meme.voteCount + Call.value | |
let updatedVotes = state.votes{ [index] = updatedVoteCount } | |
put(state{ votes = updatedVotes }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment