Skip to content

Instantly share code, notes, and snippets.

@matejthetree
Created January 29, 2020 12:09
Show Gist options
  • Save matejthetree/4c92c96599b1067d553de48c4c02e516 to your computer and use it in GitHub Desktop.
Save matejthetree/4c92c96599b1067d553de48c4c02e516 to your computer and use it in GitHub Desktop.
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[i]
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