-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautomate.ride
113 lines (97 loc) · 5.39 KB
/
automate.ride
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{-# STDLIB_VERSION 5 #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# CONTENT_TYPE DAPP #-}
let oracle = Address(base58'') # Need to filled
let balance = oracle.getString("balance_address").valueOrErrorMessage("Empty key balance_address").addressFromStringValue()
let SWOPgovernanceAdr = oracle.getString("swop_governance").valueOrErrorMessage("Empty key swop_governance").addressFromStringValue()
let SWOP = oracle.getString("SWOP").valueOrErrorMessage("Empty key SWOP").fromBase58String()
let consumers = oracle.getString("consumers").valueOrElse("Empty key consumer").split(",")
let admin1 = oracle.getString("admin_1").valueOrElse("Empty key admin_1").addressFromStringValue()
let admin2 = oracle.getString("admin_2").valueOrElse("Empty key admin_2").addressFromStringValue()
let admin3 = oracle.getString("admin_3").valueOrElse("Empty key admin_3").addressFromStringValue()
let admin4 = oracle.getString("admin_4").valueOrElse("Empty key admin_4").addressFromStringValue()
let admin5 = oracle.getString("admin_5").valueOrElse("Empty key admin_5").addressFromStringValue()
let isProtocolActive = oracle.getBoolean("is_protocol_active").valueOrErrorMessage("Empty key is_protocol_active")
let keyIsDappActive = "is_dapp_active"
let keyAutomateOwner = "owner"
let isDappActive = this.getBoolean(keyIsDappActive).valueOrErrorMessage("Empty key is_dapp_active")
let ownerStr = this.getString(keyAutomateOwner).valueOrErrorMessage("Empty keyAutomateOwner")
let owner = ownerStr.addressFromString()
# START Part from 'https://github.com/swopfi/swopfi-smart-contracts/blob/master/dApps/SWOP/governance.ride'
let keyLastInterest = "last_interest"
let keyUserSWOPLocked = "_SWOP_amount"
let keyUserLastInterest = "_last_interest"
let scaleValue = 100000000 # 10^8
func getLastInterestInfo () = {
let lastInterest = valueOrElse(getInteger(SWOPgovernanceAdr, keyLastInterest), 0)
lastInterest
}
func getUserSWOPLocked (user:Address) = valueOrElse(getInteger(SWOPgovernanceAdr, (toString(user) + keyUserSWOPLocked)), 0)
func getUserInterestInfo (user:Address,lastInterest:Int) = {
let userSWOPAmount = getUserSWOPLocked(user)
let userLastInterest = getInteger(SWOPgovernanceAdr, (toString(user) + keyUserLastInterest))
let userLastInterestValue = match userLastInterest {
case userLastInterest: Int =>
userLastInterest
case _ =>
lastInterest
}
(userLastInterestValue, userSWOPAmount)
}
func claimCalc(caller:Address) = {
let lastInterest = getLastInterestInfo()
let uifo = getUserInterestInfo(caller, lastInterest)
let userLastInterest = uifo._1
let userSWOPLocked = uifo._2
let claimAmount = fraction(userSWOPLocked, (lastInterest - userLastInterest), scaleValue)
let userNewInterest = lastInterest
(userNewInterest, claimAmount)
}
# END Part from 'https://github.com/swopfi/swopfi-smart-contracts/blob/master/dApps/SWOP/governance.ride'
@Callable(i)
func governanceLockSWOP() = if !(isProtocolActive && isDappActive) then throw("Protol stopped by admin") else {
let (pmtAmount, pmtAssetId) = (i.payments[0].amount, i.payments[0].assetId)
if pmtAssetId != SWOP then
throw("Only SWOP payment available in governanceLockSWOP()")
else
strict z = invoke(SWOPgovernanceAdr, "lockSWOP", [], [AttachedPayment(SWOP, pmtAmount)])
[]
}
@Callable(i)
func governanceClaimAndStake(minSWOPreward: Int) = if !(isProtocolActive && isDappActive) then throw("Protol stopped by admin") else {
if (i.caller != owner) && (!consumers.containsElement(i.caller.bytes.toBase58String())) then throw("Only owner or admin can call this function") else
let claimAmount = claimCalc(this)._2
if (claimAmount < minSWOPreward) then throw("Amount to claim is less than minSWOPreward") else
let isFeeExtract = if i.caller == owner then false else true
if isFeeExtract then
strict z = invoke(SWOPgovernanceAdr, "claimAndStakeSWOP", [], [])
strict y = invoke(balance, "extractFee", [], [])
[]
else
strict z = invoke(SWOPgovernanceAdr, "claimAndStakeSWOP", [], [])
[]
}
@Callable(i)
func governanceWithdraw(amtToWithdraw:Int) = if !(isProtocolActive && isDappActive) then throw("Protol stopped by admin") else {
if i.caller != owner then throw("Only owner can call this function") else
strict z = invoke(SWOPgovernanceAdr, "withdrawSWOP", [amtToWithdraw], [])
[
ScriptTransfer(i.caller, amtToWithdraw, SWOP)
]
}
@Callable(i)
func shutdownDapp() = if !(isProtocolActive && isDappActive) then throw("Protol stopped by admin") else {
if ![admin1, admin2, admin3, admin4, admin5, owner].containsElement(i.caller) && !consumers.containsElement(i.caller.bytes.toBase58String()) then
throw("Only admin can call this function")
else
[BooleanEntry("is_dapp_active", false)]
}
@Verifier(tx)
func verify() = {
let admin1Signed = if admin1.getBoolean(tx.id.toBase58String()).valueOrElse(false) then 1 else 0
let admin2Signed = if admin2.getBoolean(tx.id.toBase58String()).valueOrElse(false) then 1 else 0
let admin3Signed = if admin3.getBoolean(tx.id.toBase58String()).valueOrElse(false) then 1 else 0
let admin4Signed = if admin4.getBoolean(tx.id.toBase58String()).valueOrElse(false) then 1 else 0
let admin5Signed = if admin5.getBoolean(tx.id.toBase58String()).valueOrElse(false) then 1 else 0
admin1Signed + admin2Signed + admin3Signed + admin4Signed + admin5Signed >= 3
}