Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# STDLIB_VERSION 3 #-}
- {-# CONTENT_TYPE DAPP #-}
- {-# SCRIPT_TYPE ACCOUNT #-}
- let ACTIVE = "active"
- let VOTING = "voting"
- let CLOSED = "closed"
- let DECLINED = "declined"
- let ACCEPTED = "accepted"
- let USED = "used"
- let NONE = "none"
- func getUserMilestoneKey(address: String) = {
- address + "_campaign-milestone-decision"
- }
- func getUserCampaignKey(address: String) = {
- address + "_campaign-decision"
- }
- func getMilestoneStatusKey() = {
- "campaign-milestone-status"
- }
- func getMilestoneDecisionKey(decision: String) = {
- "campaign-milestone-decision_" + decision
- }
- func getCampaignDecisionKey(decision: String) = {
- "campaign-decision_" + decision
- }
- func getCampaignMainAddressKey() = {
- "campaign-main-address"
- }
- func getCampaignMainBalanceKey() = {
- "campaign-main-balance"
- }
- func getCampaignCumBalanceKey() = {
- "campaign-cum-balance"
- }
- func getCampaignQuorumKey() = {
- "campaign-main-quorum"
- }
- func getCampaignDataKey() = {
- "campaign-main-data"
- }
- func getCampaignStatusKey() = {
- "campaign-main-status"
- }
- func getCampaignMilestoneAmountKey() = {
- "campaign-main-milestone-amount"
- }
- func getInvetsorKey(address: String) = {
- address + "_" + "ib"
- }
- func getInvetsorBalance(address: String) = {
- let num = match getInteger(this, getInvetsorKey(address)) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getCampaignBalance() = {
- let num = match getInteger(this, getCampaignMainBalanceKey()) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getCampaignCumBalance() = {
- let num = match getInteger(this, getCampaignCumBalanceKey()) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getCampaignStatus() = {
- let val = match getString(this, getCampaignStatusKey()) {
- case a:String => a
- case _ => NONE
- }
- val
- }
- func getCampaignQuorum() = {
- let num = match getInteger(this, getCampaignQuorumKey()) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getCampaignMainAddress() = {
- let val = match getString(this, getCampaignMainAddressKey()) {
- case a:String => a
- case _ => NONE
- }
- val
- }
- func getCampaignMainBalance() = {
- let num = match getInteger(this, getCampaignMainBalanceKey()) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getCampaignMilestoneAmount() = {
- let num = match getInteger(this, getCampaignMilestoneAmountKey()) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getMilestoneStatus() = {
- let val = match getString(this, getMilestoneStatusKey()) {
- case a:String => a
- case _ => NONE
- }
- val
- }
- func getMilestoneDecision(milestoneDecisionKey: String) = {
- let num = match getInteger(this, milestoneDecisionKey) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getCampaignDecision(cmpDecisionKey: String) = {
- let num = match getInteger(this, cmpDecisionKey) {
- case a:Int => a
- case _ => 0
- }
- num
- }
- func getUserMilestone(address: String) = {
- let val = match getString(this, getUserMilestoneKey(address)) {
- case a:String => a
- case _ => NONE
- }
- val
- }
- func getUserCampaign(address: String) = {
- let val = match getString(this, getUserCampaignKey(address)) {
- case a:String => a
- case _ => NONE
- }
- val
- }
- func getShareByAddress(address: String) = {
- let fund = getCampaignCumBalance()
- let safeFund = if(fund > 0) then fund else -1
- let balance = if(safeFund == -1) then 0 else getInvetsorBalance(address)
- (100*balance)/fund
- }
- @Callable(i)
- func deposit() = {
- let pmt = extract(i.payment)
- let currentKey = toBase58String(i.caller.bytes)
- if (isDefined(pmt.assetId)) then throw("can hodl waves only at the moment")
- else {
- WriteSet([
- DataEntry(getInvetsorKey(currentKey), getInvetsorBalance(currentKey) + pmt.amount),
- DataEntry(getCampaignCumBalanceKey(), getCampaignCumBalance() + pmt.amount)
- ])
- }
- }
- @Callable(i)
- func withdraw(address: String) = {
- let amount = getInvetsorBalance(address)
- let share = getShareByAddress(address)
- let remainAmount = (amount*share)/100
- if (amount == 0) then throw("Can't withdraw zero amount")
- else if (getCampaignStatusKey() != CLOSED) then throw("campaign is still active")
- else ScriptResult(
- WriteSet([DataEntry(getInvetsorKey(address), 0)]),
- TransferSet([ScriptTransfer(addressFromStringValue(address), remainAmount, unit)])
- )
- }
- @Callable(i)
- func registerCampaign(address: String, quorum: Int, data: String) = {
- let cStatus = getCampaignStatus()
- if (cStatus != NONE) then throw("this campaign already registered")
- else if (quorum > 100 || quorum < 1) then throw("quorum must be between 1 and 100")
- else {
- WriteSet([
- DataEntry(getCampaignStatusKey(), ACTIVE),
- DataEntry(getCampaignMainBalanceKey(), 0),
- DataEntry(getCampaignMainAddressKey(), address),
- DataEntry(getCampaignDataKey(), data),
- DataEntry(getCampaignQuorumKey(), quorum)
- ])
- }
- }
- @Callable(i)
- func addMilestone(amount: Int, data: String) = {
- let currStatus = getCampaignStatus()
- let currAmount = getCampaignBalance()
- if (currAmount < amount && currAmount > 0) then throw("you should collect funds before a crwodfunding")
- else if (currStatus == VOTING) then throw("it's voting now")
- else if (currStatus == CLOSED) then throw("the campaign is closed now")
- else {
- WriteSet([
- DataEntry(getMilestoneStatusKey(), VOTING),
- DataEntry(getCampaignMilestoneAmountKey(), amount),
- DataEntry(getCampaignStatusKey(), VOTING)
- ])
- }
- }
- @Callable(i)
- func vote(decision: Int) = {
- let currentKey = toBase58String(i.caller.bytes)
- let share = getShareByAddress(currentKey)
- let flag = getUserMilestone(currentKey)
- let quorum = getCampaignQuorum()
- let positiveCnt = getMilestoneDecision(getMilestoneDecisionKey(toString(1))) + (if(decision == 1) then 1 else 0)*share
- let negativeCnt = getMilestoneDecision(getMilestoneDecisionKey(toString(0))) + (if(decision == 0) then 1 else 0)*share
- let newMLStatus = if(positiveCnt >= quorum) then ACCEPTED else (if (negativeCnt >= quorum) then DECLINED else VOTING)
- let newCMPStatus = if(newMLStatus == DECLINED || newMLStatus == ACCEPTED) then ACTIVE else VOTING
- if (getCampaignStatus() != VOTING) then throw("can't vote")
- else if (share == 0) then throw("can't vote with zero share")
- else {
- WriteSet([
- DataEntry(getMilestoneDecisionKey(toString(1)), positiveCnt),
- DataEntry(getMilestoneDecisionKey(toString(0)), negativeCnt),
- DataEntry(getMilestoneStatusKey(), newMLStatus),
- DataEntry(getCampaignStatusKey(), newCMPStatus),
- DataEntry(getUserMilestoneKey(currentKey), toString(decision))
- ])
- }
- }
- @Callable(i)
- func voteForClose(decision: Int) = {
- let currentKey = toBase58String(i.caller.bytes)
- let currStatus = getCampaignStatus()
- let share = getShareByAddress(currentKey)
- let quorum = getCampaignQuorum()
- let flag = getUserCampaign(currentKey)
- let positiveCnt = getCampaignDecision(getCampaignDecisionKey(toString(1))) + (if(decision == 1) then 1 else 0)*share
- let negativeCnt = getCampaignDecision(getCampaignDecisionKey(toString(0))) + (if(decision == 0) then 1 else 0)*share
- let newCMPStatus = if(positiveCnt >= quorum) then ACTIVE else (if (negativeCnt >= quorum) then CLOSED else VOTING)
- let votingFinishedMultp = if ((newCMPStatus == CLOSED || newCMPStatus == ACTIVE) && currStatus == VOTING) then 0 else 1
- if (getCampaignStatus() != VOTING) then throw("can't vote")
- else if (share == 0) then throw("can't vote with zero share")
- else if (flag != NONE) then throw("can't vote twice")
- else {
- WriteSet([
- DataEntry(getCampaignDecisionKey(toString(1)), positiveCnt*votingFinishedMultp),
- DataEntry(getCampaignDecisionKey(toString(0)), negativeCnt*votingFinishedMultp),
- DataEntry(getUserCampaignKey(currentKey), if (votingFinishedMultp == 0) then NONE else toString(decision)),
- DataEntry(getCampaignStatusKey(), newCMPStatus)
- ])
- }
- }
- @Callable(i)
- func getFunds() = {
- let lastMilestoneStatus = getMilestoneStatus()
- let currStatus = getCampaignStatus()
- let amount = getCampaignMilestoneAmount()
- let cmpBalance = getCampaignMainBalance()
- if (lastMilestoneStatus != ACCEPTED) then throw("wrong milestone status")
- else if (currStatus != ACTIVE) then throw("wrong campaign status")
- else ScriptResult(
- WriteSet([
- DataEntry(getMilestoneStatusKey(), USED),
- DataEntry(getCampaignMainBalanceKey(), cmpBalance - amount)
- ]),
- TransferSet([ScriptTransfer(addressFromStringValue(getCampaignMainAddress()), amount, unit)])
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement