-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ride
303 lines (241 loc) · 13.8 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{-# STDLIB_VERSION 6 #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# CONTENT_TYPE DAPP #-}
let WavesId = "WAVES"
let MinerFee = 5
let PayoutEmptyStatus = 0
let PayoutInitiatedStatus = 1
let PayoutCompletedStatus = 2
let BlocksBatchMaxSize = 10
let RecipientsBatchMaxSize = 100
let Height = height
let Arr10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let ConfigAddressKey = "configAddress"
let LastPayoutKey = "lastPayout"
let MassTransferFeesKey = "massTransferFees"
let WavesChainIdKey = "chainId_" + WavesId
let DaemonAddressKey = "daemonAddress"
let DaemonPublicKeyKey = "daemonPublicKey"
let MaintainerAddressKey = "maintainerAddress"
let PuzzleAggregatorAddressKey = "puzzleAggregatorAddress"
let PuzzleBuybackAddressKey = "puzzleBuybackAddress"
let SettingsAddressKey = "settingsAddress"
let SignerPublicKeyKey = "signerPublicKey"
let WavesDaoFactoryAddressKey = "wavesDaoFactoryAddress"
let ConfigAddress = addressFromStringValue(this.getStringValue(ConfigAddressKey))
let WavesChainId = ConfigAddress.getIntegerValue(WavesChainIdKey)
let DaemonAddress = addressFromStringValue(ConfigAddress.getStringValue(DaemonAddressKey))
let DaemonPublicKey = ConfigAddress.getBinaryValue(DaemonPublicKeyKey)
let MaintainerAddress = addressFromStringValue(ConfigAddress.getStringValue(MaintainerAddressKey))
let PuzzleAggregatorAddress = addressFromStringValue(ConfigAddress.getStringValue(PuzzleAggregatorAddressKey))
let PuzzleBuybackAddress = addressFromStringValue(ConfigAddress.getStringValue(PuzzleBuybackAddressKey))
let SignerPublicKey = ConfigAddress.getBinary(SignerPublicKeyKey)
let WavesDaoFactoryAddress = addressFromStringValue(ConfigAddress.getStringValue(WavesDaoFactoryAddressKey))
func makePayoutDataKey(payoutId: Int) = "payoutData_" + payoutId.toString()
func makePayoutStatusKey(payoutId: Int) = "payoutStatus_" + payoutId.toString()
func makePayoutAdditionalRewardsKey(payoutId: Int) = "payoutAdditionalRewards_" + payoutId.toString()
func getLastPayout() = this.getIntegerValue(LastPayoutKey)
func getPayoutData(payoutId: Int) = {
let payoutString = getStringValue(makePayoutDataKey(payoutId))
let payoutDataList = payoutString.split("_")
let startHeight = payoutDataList[0].parseIntValue()
let endHeight = payoutDataList[1].parseIntValue()
(startHeight, endHeight)
}
func getPayoutStatus(payoutId: Int) = getInteger(makePayoutStatusKey(payoutId)).valueOrElse(PayoutEmptyStatus)
func getPayoutAdditionalRewards(payoutId: Int) = getInteger(makePayoutAdditionalRewardsKey(payoutId)).valueOrElse(0)
@Callable(i)
func setup(configAddress: String) = {
if (i.caller != this) then throw("Access denied") else
if (i.payments.size() != 0) then throw("Payments are prohibited") else
if (addressFromString(configAddress) == unit) then throw("Invalid configAddress: " + configAddress) else
[StringEntry(ConfigAddressKey, configAddress)]
}
@Callable(i)
func initiatePayout(payoutId: Int, startHeight: Int, endHeight: Int, amounts: List[Int], assets: List[String]) = {
if (i.caller != DaemonAddress) then throw("Access denied") else
if (i.payments.size() != 0) then throw("Payments are prohibited") else
let lastPayout = getLastPayout()
if (payoutId != lastPayout + 1) then throw("Wrong payoutId: " + payoutId.toString() + ", should be: " + (lastPayout + 1).toString()) else
let lastPayoutStatus = getPayoutStatus(lastPayout)
if (lastPayoutStatus != PayoutCompletedStatus) then throw("Last payout: " + lastPayout.toString() + " was not completed") else
let (lastStartHeight, lastEndHeight) = getPayoutData(lastPayout)
if (startHeight != lastEndHeight + 1) then throw("Wrong startHeight: " + startHeight.toString() + ", should be: " + (lastEndHeight + 1).toString()) else
if (endHeight < startHeight) then throw("endHeight: " + endHeight.toString() + " less than startHeight: " + startHeight.toString()) else
let amountsSize = amounts.size()
let assetsSize = assets.size()
if (amountsSize != assetsSize) then throw("Wrong size of amounts or assets") else
let availableWavesDaoLpToClaim = WavesDaoFactoryAddress.getInteger("%s%s__available__" + this.toString()).valueOrElse(0)
let wavesDaoNextBlockToProcess = WavesDaoFactoryAddress.getIntegerValue("%s__nextBlockToProcess")
strict wavesDlpClaim = if (availableWavesDaoLpToClaim > 0 && wavesDaoNextBlockToProcess + BlocksBatchMaxSize >= Height)
then invoke(WavesDaoFactoryAddress, "claimLP", nil, nil)
else unit
let currentAdditionalRewards = getPayoutAdditionalRewards(payoutId)
func assetsFold(accum: (List[ScriptTransfer], List[String]), index: Int) = {
if (index >= assetsSize) then accum else
let assetStr = assets[index]
if (ConfigAddress.getInteger("assetIndex_" + assetStr) == unit) then throw("Unknown asset: " + assetStr) else
let (asset, amount) = if (assetStr == WavesId)
then (unit, amounts[index] + currentAdditionalRewards)
else (fromBase58String(assetStr), amounts[index])
if (amount < 0) then throw("Wrong asset amount: " + amount.toString() + ", should be positive or zero") else
let minerFee = fraction(amount, MinerFee, 100)
let maintainerFee = minerFee / 2
let puzzleBuybackFee = minerFee - maintainerFee
let lessorsAmount = amount - maintainerFee - puzzleBuybackFee
(
accum._1 :+ ScriptTransfer(MaintainerAddress, maintainerFee, asset) :+ ScriptTransfer(PuzzleBuybackAddress, puzzleBuybackFee, asset),
accum._2 :+ lessorsAmount.toString()
)
}
let (transfers, amountKeys) = FOLD<10>(Arr10, ([], []), assetsFold)
let newPayoutData = makeString([startHeight.toString(), endHeight.toString()] ++ amountKeys, "_")
transfers ++ [
IntegerEntry(LastPayoutKey, payoutId),
StringEntry(makePayoutDataKey(payoutId), newPayoutData),
IntegerEntry(makePayoutStatusKey(payoutId), PayoutInitiatedStatus)
]
}
@Callable(i)
func swap(payoutId: Int, swapFrom: String, amountIn: Int, route: String, minToReceive: Int) = {
if (i.caller != DaemonAddress) then throw("Access denied") else
if (i.payments.size() != 0) then throw("Payments are prohibited") else
let lastPayout = getLastPayout()
if (lastPayout != payoutId) then throw("Wrong payoutId: " + payoutId.toString() + ", should be: " + lastPayout.toString()) else
let payoutStatus = getPayoutStatus(payoutId)
if (payoutStatus != PayoutInitiatedStatus) then throw("Wrong payout status: " + payoutStatus.toString() + ", should be 1") else
let (balanceInBeforeSwap, swapFromId) = if (swapFrom == WavesId)
then (wavesBalance(this).available, unit)
else {
let assetId = fromBase58String(swapFrom)
(assetBalance(this, assetId), assetId)
}
strict swapInvoke = invoke(PuzzleAggregatorAddress, "swapWithReferral", [route, minToReceive, "puzzlenode"], [AttachedPayment(swapFromId, amountIn)])
let balanceInAfterSwap = if (swapFrom == WavesId)
then wavesBalance(this).available
else assetBalance(this, swapFromId.value())
let refund = balanceInAfterSwap - (balanceInBeforeSwap - amountIn)
if (refund > 10) then throw("Rollback aggregator refund: " + refund.toString()) else
nil
}
@Callable(i)
func distributeTokens(payoutId: Int, recipients: List[ByteVector], amounts: List[Int], assets: List[Int], attachment: String) = {
if (i.caller != DaemonAddress) then throw("Access denied") else
if (i.payments.size() != 0) then throw("Payments are prohibited") else
let lastPayout = getLastPayout()
if (lastPayout != payoutId) then throw("Wrong payoutId: " + payoutId.toString() + ", should be: " + lastPayout.toString()) else
let payoutStatus = getPayoutStatus(payoutId)
if (payoutStatus != PayoutInitiatedStatus) then throw("Wrong payout status: " + payoutStatus.toString() + ", should be 1") else
let recipientsSize = recipients.size()
let amountsSize = amounts.size()
let assetsSize = assets.size()
if (recipientsSize != amountsSize || recipientsSize != assetsSize) then throw("Wrong recipients size: " + recipientsSize.toString() + ", amounts size: " + amountsSize.toString() + ", assets size: " + assetsSize.toString()) else
if (recipientsSize == 0) then throw("Recipients list should not be empty") else
if (recipientsSize > RecipientsBatchMaxSize) then throw("Should be not greater than " + RecipientsBatchMaxSize.toString() + " recipients, current: " + recipientsSize.toString()) else
let magicByte = 1.toBytes().drop(7)
let chainIdByte = WavesChainId.toBytes().drop(7)
let addressPrefix = magicByte + chainIdByte
func scriptTransfersFold(accum: List[ScriptTransfer], index: Int) = {
if (index >= recipientsSize) then accum else
let publicKeyHash = recipients[index]
let addressData = addressPrefix + publicKeyHash
let addressChecksum = keccak256(blake2b256(addressData)).take(4)
let recipient = Address(addressData + addressChecksum)
let amount = amounts[index]
let assetIndex = assets[index]
let assetId = ConfigAddress.getBinaryValue("assetId_" + assetIndex.toString())
let asset = if (assetId == base58'') then unit else assetId
accum :+ ScriptTransfer(recipient, amount, asset)
}
let indexes = [
0, 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
]
FOLD<100>(indexes, [], scriptTransfersFold)
}
@Callable(i)
func finalizePayout(payoutId: Int) = {
if (i.caller != DaemonAddress) then throw("Access denied") else
let lastPayout = getLastPayout()
if (lastPayout != payoutId) then throw("Wrong payoutId: " + payoutId.toString() + ", should be: " + lastPayout.toString()) else
let payoutStatus = getPayoutStatus(lastPayout)
if (payoutStatus != PayoutInitiatedStatus) then throw("Wrong payout status: " + payoutStatus.toString() + ", should be: 1") else
[
IntegerEntry(makePayoutStatusKey(lastPayout), PayoutCompletedStatus)
]
}
@Callable(i)
func addAdditionalRewards() = {
if (i.payments.size() != 1) then throw("At least one payment should be attached") else
if (i.payments[0].assetId != unit) then throw("Only waves can be attached") else
let lastPayout = getLastPayout()
let lastPayoutStatus = getPayoutStatus(lastPayout)
if (lastPayoutStatus != PayoutCompletedStatus) then throw("Last payout: " + lastPayout.toString() + " was not completed") else
let nextPayoutId = lastPayout + 1
let currentAdditionalRewards = getPayoutAdditionalRewards(nextPayoutId)
let newAdditionalRewards = currentAdditionalRewards + i.payments[0].amount
[
IntegerEntry(makePayoutAdditionalRewardsKey(nextPayoutId), newAdditionalRewards)
]
}
@Callable(i)
func addMassTransferFees() = {
if (i.payments.size() != 1) then throw("At least one payment should be attached") else
if (i.payments[0].assetId != unit) then throw("Only waves can be attached") else
let currentMassTransferFees = getInteger(MassTransferFeesKey).valueOrElse(0)
let newMassTransferFees = currentMassTransferFees + i.payments[0].amount
[
IntegerEntry(MassTransferFeesKey, newMassTransferFees)
]
}
@Callable(i)
func withdrawMassTransferFees() = {
if (i.caller != MaintainerAddress) then throw("Access denied") else
if (i.payments.size() != 0) then throw("Payments are prohibited") else
let currentMassTransferFees = getInteger(MassTransferFeesKey).valueOrElse(0)
if (currentMassTransferFees == 0) then throw("Nothing to withdraw") else
[
IntegerEntry(MassTransferFeesKey, 0),
ScriptTransfer(MaintainerAddress, currentMassTransferFees, unit)
]
}
@Verifier(tx)
func verify () = {
match tx {
case massTx: MassTransferTransaction => {
let configAddressStr = this.getString(ConfigAddressKey)
let configAddress = addressFromStringValue(configAddressStr.value())
let publicKey = if (configAddressStr.isDefined())
then configAddress.getBinary(DaemonPublicKeyKey).valueOrElse(tx.senderPublicKey)
else tx.senderPublicKey
sigVerify_8Kb(massTx.bodyBytes, massTx.proofs[0], publicKey)
}
case invokeTx: InvokeScriptTransaction => {
let validFunctionName = invokeTx.function == "extendMainChain" || invokeTx.function == "appendBlock" || invokeTx.function == "startAltChain" || invokeTx.function == "extendAltChain"
let configAddressStr = this.getString(ConfigAddressKey)
let configAddress = addressFromStringValue(configAddressStr.value())
let unitsChain = configAddress.getString("unitsChainName_" + invokeTx.dApp.exactAs[Address].toString())
let signerPublicKey = configAddress.getBinary(SignerPublicKeyKey).valueOrElse(tx.senderPublicKey)
let publicKey = if (configAddressStr.isDefined())
then if (unitsChain.isDefined() && validFunctionName) then tx.senderPublicKey else signerPublicKey
else tx.senderPublicKey
sigVerify_8Kb(invokeTx.bodyBytes, invokeTx.proofs[0], publicKey)
}
case _ => {
let configAddressStr = this.getString(ConfigAddressKey)
let configAddress = addressFromStringValue(configAddressStr.value())
let publicKey = if (configAddressStr.isDefined())
then configAddress.getBinary(SignerPublicKeyKey).valueOrElse(tx.senderPublicKey)
else tx.senderPublicKey
sigVerify_128Kb(tx.bodyBytes, tx.proofs[0], publicKey)
}
}
}