Skip to content

Instantly share code, notes, and snippets.

@KardanovIR
Created June 9, 2020 14:00
Show Gist options
  • Save KardanovIR/d4e0379a47bdd411ce7c00a5d06baea8 to your computer and use it in GitHub Desktop.
Save KardanovIR/d4e0379a47bdd411ce7c00a5d06baea8 to your computer and use it in GitHub Desktop.
@Callable(i)
func voteForRelease(id: Int) = {
# receiving campaign parameters
let endTimestamp = getIntegerValue(this, keyEndTimestamp(id))
let implEndTimestamp = getIntegerValue(this, keyImplEndTimestamp(id))
let votePower = getIntegerValue(this, keyUserFunded(id, i.callerPublicKey.toBase58String()))
let targetSum = getIntegerValue(this, keyTargetSum(id))
let raised = getIntegerValue(this, keyRaised(id))
let released = getBooleanValue(this, keyReleasedTokens(id))
let owner = keyOwner(id)
# receiving the number of tokens sent by investors who already voted "in favor"
let votedPower = getIntegerValue(this, keyReleaseVotes(id))
# making sure that the campaign execution time has passed
if (implEndTimestamp > lastBlock.timestamp) then throw("This target is not finished yet") else
# making sure that the campaign was successful and the target sum has been collected
if (raised < targetSum) then throw("This target didn't reach target") else
# making sure that the current investor has not yet voted on the campaign
if (released) then throw("This target was released already") else
# проверяем, что текущий инвестор уже не голосовал за кампанию
if (isDefined(getBoolean(this, keyUserVoted(id, i.callerPublicKey.toBase58String())))) then throw("You already voted for the target") else
# if the sum of the number of those who have already voted "In favor" plus the current investor’s vote
# exceeds 50%, we transfer the tokens to the campaign initiator and consider the campaign complete
# otherwise we increase the number of tokens from investors who have already voted "In favor"
if ((votedPower + votePower) > (raised / 2)) then {
# the campaign initiator receives 80% of the collected sum
# and the dApp keeps the remaining 20% as a fee
let ownerPrize = raised - (raised / 1 / 5)
[
IntegerEntry(keyReleaseVotes(id), votedPower + votePower),
BooleanEntry(keyReleasedTokens(id), true),
ScriptTransfer(addressFromPublicKey(owner.toBytes()), ownerPrize, thanksTokenId)
]
}else {
[
IntegerEntry(keyReleaseVotes(id), votedPower + votePower)
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment