Skip to content

Instantly share code, notes, and snippets.

@KardanovIR
Created June 9, 2020 13:57
Show Gist options
  • Save KardanovIR/267c732ed9d19c25f76d1eb5a1c4ccf0 to your computer and use it in GitHub Desktop.
Save KardanovIR/267c732ed9d19c25f76d1eb5a1c4ccf0 to your computer and use it in GitHub Desktop.
@Callable(i)
func fund(id: Int) = {
# receiving an attached payment; if there is no payment, an exception will be created
let pmt = i.payments[0].extract()
# receiving campaign parameters
let targetSum = getIntegerValue(this, keyTargetSum(id))
let endTimestamp = getIntegerValue(this, keyEndTimestamp(id))
let owner = getStringValue(this, keyOwner(id))
# receiving the number of collected tokens at this point
let raised = getIntegerValue(this, keyRaised(id))
# receiving the number of tokens the current user has already sent to the same campaign
# put 0 if none
let alreadyFundedByUser = match getInteger(this, keyUserFunded(id, i.callerPublicKey.toBase58String())){
case v: Int => v
case _ => 0
}
# making sure that all attached tokens are this team’s tokens
if (pmt.assetId != thanksTokenId) then throw("You have to attach proper tokens with id: " + thanksTokenId.toBase58String()) else
# making sure the campaign initiator is not trying to provide tokens
if (owner == i.callerPublicKey.toBase58String()) then throw("You cannot fund your own target") else
# making sure the campaign end time has not yet arrived
if (endTimestamp > lastBlock.timestamp) then throw("This target is finished already") else
# making sure a user’s contribution after this transaction is below 33%
if (((alreadyFundedByUser + pmt.amount) > targetSum / 3 + 1)) then throw("You cannot fund more than 33% of the whole sum which is " + ((targetSum / 3 + 1) - alreadyFundedByUser).toString())
else {
[
# saving the new number of tokens received from the current calling account for this campaign
IntegerEntry(keyUserFunded(id, i.callerPublicKey.toBase58String()), alreadyFundedByUser + pmt.amount),
# updating the number of tokens collected so far
IntegerEntry(keyRaised(id), raised + pmt.amount)
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment