Advertisement
Guest User

Untitled

a guest
May 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 9.15 KB | None | 0 0
  1. {-# STDLIB_VERSION 3 #-}
  2. {-# CONTENT_TYPE DAPP #-}
  3. {-# SCRIPT_TYPE ACCOUNT #-}
  4. let ACTIVE = "active"
  5. let VOTING = "voting"
  6. let CLOSED = "closed"
  7.  
  8. let DECLINED = "declined"
  9. let ACCEPTED = "accepted"
  10. let USED = "used"
  11.  
  12. let NONE = "none"
  13.  
  14. func getUserMilestoneKey(address: String) = {
  15.     address + "_campaign-milestone-decision"
  16. }
  17. func getUserCampaignKey(address: String) = {
  18.     address + "_campaign-decision"
  19. }
  20.  
  21. func getMilestoneStatusKey() = {
  22.     "campaign-milestone-status"
  23. }
  24.  
  25. func getMilestoneDecisionKey(decision: String) = {
  26.     "campaign-milestone-decision_" + decision
  27. }
  28. func getCampaignDecisionKey(decision: String) = {
  29.     "campaign-decision_" + decision
  30. }
  31.  
  32. func getCampaignMainAddressKey() = {
  33.     "campaign-main-address"
  34. }
  35. func getCampaignMainBalanceKey() = {
  36.     "campaign-main-balance"
  37. }
  38.  
  39. func getCampaignCumBalanceKey() = {
  40.     "campaign-cum-balance"
  41. }
  42.  
  43. func getCampaignQuorumKey() = {
  44.     "campaign-main-quorum"
  45. }
  46.  
  47. func getCampaignDataKey() = {
  48.     "campaign-main-data"
  49. }
  50.  
  51. func getCampaignStatusKey() = {
  52.     "campaign-main-status"
  53. }
  54.  
  55. func getCampaignMilestoneAmountKey() = {
  56.     "campaign-main-milestone-amount"
  57. }
  58.  
  59. func getInvetsorKey(address: String) = {
  60.     address + "_" + "ib"
  61. }
  62. func getInvetsorBalance(address: String) = {
  63.     let num = match getInteger(this, getInvetsorKey(address)) {
  64.             case a:Int => a
  65.             case _ => 0
  66.     }
  67.     num
  68. }
  69. func getCampaignBalance() = {
  70.     let num = match getInteger(this, getCampaignMainBalanceKey()) {
  71.             case a:Int => a
  72.             case _ => 0
  73.     }
  74.     num
  75. }
  76. func getCampaignCumBalance() = {
  77.     let num = match getInteger(this, getCampaignCumBalanceKey()) {
  78.             case a:Int => a
  79.             case _ => 0
  80.     }
  81.     num
  82. }
  83. func getCampaignStatus() = {
  84.     let val = match getString(this, getCampaignStatusKey()) {
  85.             case a:String => a
  86.             case _ => NONE
  87.     }
  88.     val
  89. }
  90. func getCampaignQuorum() = {
  91.     let num = match getInteger(this, getCampaignQuorumKey()) {
  92.             case a:Int => a
  93.             case _ => 0
  94.     }
  95.     num
  96. }
  97. func getCampaignMainAddress() = {
  98.     let val = match getString(this, getCampaignMainAddressKey()) {
  99.             case a:String => a
  100.             case _ => NONE
  101.     }
  102.     val
  103. }
  104. func getCampaignMainBalance() = {
  105.     let num = match getInteger(this, getCampaignMainBalanceKey()) {
  106.             case a:Int => a
  107.             case _ => 0
  108.     }
  109.     num
  110. }
  111.  
  112. func getCampaignMilestoneAmount() = {
  113.     let num = match getInteger(this, getCampaignMilestoneAmountKey()) {
  114.             case a:Int => a
  115.             case _ => 0
  116.     }
  117.     num
  118. }
  119.  
  120. func getMilestoneStatus() = {
  121.     let val = match getString(this, getMilestoneStatusKey()) {
  122.             case a:String => a
  123.             case _ => NONE
  124.     }
  125.     val
  126. }
  127.  
  128. func getMilestoneDecision(milestoneDecisionKey: String) = {
  129.     let num = match getInteger(this, milestoneDecisionKey) {
  130.             case a:Int => a
  131.             case _ => 0
  132.     }
  133.     num
  134. }
  135. func getCampaignDecision(cmpDecisionKey: String) = {
  136.     let num = match getInteger(this, cmpDecisionKey) {
  137.             case a:Int => a
  138.             case _ => 0
  139.     }
  140.     num
  141. }
  142.  
  143. func getUserMilestone(address: String) = {
  144.     let val = match getString(this, getUserMilestoneKey(address)) {
  145.             case a:String => a
  146.             case _ => NONE
  147.     }
  148.     val
  149. }
  150. func getUserCampaign(address: String) = {
  151.     let val = match getString(this, getUserCampaignKey(address)) {
  152.             case a:String => a
  153.             case _ => NONE
  154.     }
  155.     val
  156. }
  157. func getShareByAddress(address: String) = {
  158.     let fund = getCampaignCumBalance()
  159.     let safeFund = if(fund > 0) then fund else -1
  160.     let balance = if(safeFund == -1) then 0 else getInvetsorBalance(address)
  161.     (100*balance)/fund
  162. }
  163.  
  164. @Callable(i)
  165. func deposit() = {
  166.    let pmt = extract(i.payment)
  167.    let currentKey = toBase58String(i.caller.bytes)
  168.    if (isDefined(pmt.assetId)) then throw("can hodl waves only at the moment")
  169.    else {
  170.         WriteSet([
  171.             DataEntry(getInvetsorKey(currentKey), getInvetsorBalance(currentKey) + pmt.amount),
  172.             DataEntry(getCampaignCumBalanceKey(), getCampaignCumBalance() + pmt.amount)
  173.         ])
  174.    }
  175. }
  176. @Callable(i)
  177. func withdraw(address: String) = {
  178.     let amount = getInvetsorBalance(address)
  179.     let share = getShareByAddress(address)
  180.     let remainAmount = (amount*share)/100
  181.     if (amount == 0) then throw("Can't withdraw zero amount")
  182.     else if (getCampaignStatusKey() != CLOSED) then throw("campaign is still active")
  183.     else ScriptResult(
  184.         WriteSet([DataEntry(getInvetsorKey(address), 0)]),
  185.         TransferSet([ScriptTransfer(addressFromStringValue(address), remainAmount, unit)])
  186.     )
  187. }
  188. @Callable(i)
  189. func registerCampaign(address: String, quorum: Int, data: String) = {
  190.     let cStatus = getCampaignStatus()
  191.     if (cStatus != NONE) then throw("this campaign already registered")
  192.     else if (quorum > 100 || quorum < 1) then throw("quorum must be between 1 and 100")
  193.     else {
  194.         WriteSet([
  195.             DataEntry(getCampaignStatusKey(), ACTIVE),
  196.             DataEntry(getCampaignMainBalanceKey(), 0),
  197.             DataEntry(getCampaignMainAddressKey(), address),
  198.             DataEntry(getCampaignDataKey(), data),
  199.             DataEntry(getCampaignQuorumKey(), quorum)
  200.         ])
  201.    }
  202. }
  203. @Callable(i)
  204. func addMilestone(amount: Int, data: String) = {
  205.     let currStatus = getCampaignStatus()
  206.     let currAmount = getCampaignBalance()
  207.     if (currAmount < amount && currAmount > 0) then throw("you should collect funds before a crwodfunding")
  208.     else if (currStatus == VOTING) then throw("it's voting now")
  209.     else if (currStatus == CLOSED) then throw("the campaign is closed now")
  210.     else {
  211.         WriteSet([
  212.             DataEntry(getMilestoneStatusKey(), VOTING),
  213.             DataEntry(getCampaignMilestoneAmountKey(), amount),
  214.             DataEntry(getCampaignStatusKey(), VOTING)
  215.         ])
  216.     }
  217. }
  218. @Callable(i)
  219. func vote(decision: Int) = {
  220.     let currentKey = toBase58String(i.caller.bytes)
  221.     let share = getShareByAddress(currentKey)
  222.  
  223.     let flag = getUserMilestone(currentKey)
  224.     let quorum = getCampaignQuorum()
  225.  
  226.     let positiveCnt = getMilestoneDecision(getMilestoneDecisionKey(toString(1))) + (if(decision == 1) then 1 else 0)*share
  227.     let negativeCnt = getMilestoneDecision(getMilestoneDecisionKey(toString(0))) + (if(decision == 0) then 1 else 0)*share
  228.  
  229.     let newMLStatus = if(positiveCnt >= quorum) then ACCEPTED else (if (negativeCnt >= quorum) then DECLINED else VOTING)
  230.     let newCMPStatus = if(newMLStatus == DECLINED || newMLStatus == ACCEPTED) then ACTIVE else VOTING
  231.  
  232.     if (getCampaignStatus() != VOTING) then throw("can't vote")
  233.     else if (share == 0) then throw("can't vote with zero share")
  234.     else {
  235.         WriteSet([
  236.             DataEntry(getMilestoneDecisionKey(toString(1)), positiveCnt),
  237.             DataEntry(getMilestoneDecisionKey(toString(0)), negativeCnt),
  238.             DataEntry(getMilestoneStatusKey(), newMLStatus),
  239.             DataEntry(getCampaignStatusKey(), newCMPStatus),
  240.             DataEntry(getUserMilestoneKey(currentKey), toString(decision))
  241.         ])
  242.     }
  243. }
  244. @Callable(i)
  245. func voteForClose(decision: Int) = {
  246.     let currentKey = toBase58String(i.caller.bytes)
  247.     let currStatus = getCampaignStatus()
  248.     let share = getShareByAddress(currentKey)
  249.     let quorum = getCampaignQuorum()
  250.     let flag = getUserCampaign(currentKey)
  251.  
  252.     let positiveCnt = getCampaignDecision(getCampaignDecisionKey(toString(1))) + (if(decision == 1) then 1 else 0)*share
  253.     let negativeCnt = getCampaignDecision(getCampaignDecisionKey(toString(0))) + (if(decision == 0) then 1 else 0)*share
  254.     let newCMPStatus = if(positiveCnt >= quorum) then ACTIVE else (if (negativeCnt >= quorum) then CLOSED else VOTING)
  255.     let votingFinishedMultp = if ((newCMPStatus == CLOSED || newCMPStatus == ACTIVE) && currStatus == VOTING) then 0 else 1
  256.  
  257.     if (getCampaignStatus() != VOTING) then throw("can't vote")
  258.     else if (share == 0) then throw("can't vote with zero share")
  259.     else if (flag != NONE) then throw("can't vote twice")
  260.     else {
  261.         WriteSet([
  262.             DataEntry(getCampaignDecisionKey(toString(1)), positiveCnt*votingFinishedMultp),
  263.             DataEntry(getCampaignDecisionKey(toString(0)), negativeCnt*votingFinishedMultp),
  264.             DataEntry(getUserCampaignKey(currentKey), if (votingFinishedMultp == 0) then NONE else toString(decision)),
  265.             DataEntry(getCampaignStatusKey(), newCMPStatus)
  266.         ])
  267.     }
  268.  
  269. }
  270. @Callable(i)
  271. func getFunds() = {
  272.     let lastMilestoneStatus = getMilestoneStatus()
  273.     let currStatus = getCampaignStatus()
  274.     let amount = getCampaignMilestoneAmount()
  275.     let cmpBalance = getCampaignMainBalance()
  276.     if (lastMilestoneStatus != ACCEPTED) then throw("wrong milestone status")
  277.     else if (currStatus != ACTIVE) then throw("wrong campaign status")
  278.     else ScriptResult(
  279.         WriteSet([
  280.             DataEntry(getMilestoneStatusKey(), USED),
  281.             DataEntry(getCampaignMainBalanceKey(), cmpBalance - amount)
  282.         ]),
  283.         TransferSet([ScriptTransfer(addressFromStringValue(getCampaignMainAddress()), amount, unit)])
  284.     )
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement