70 | | - | func isAsset (assetToTest) = match assetToTest { |
---|
71 | | - | case isString: String => |
---|
72 | | - | isDefined(assetInfo(fromBase58String(isString))) |
---|
73 | | - | case isByteVector: ByteVector => |
---|
74 | | - | isDefined(assetInfo(isByteVector)) |
---|
75 | | - | case _ => |
---|
76 | | - | false |
---|
77 | | - | } |
---|
78 | | - | |
---|
79 | | - | |
---|
80 | | - | func getForgeAssetState (assetToCheck) = match getBoolean(this, (keyForgeSettingsEnabled + assetToCheck)) { |
---|
81 | | - | case isBoolean: Boolean => |
---|
82 | | - | if ((isBoolean == true)) |
---|
83 | | - | then "enabled" |
---|
84 | | - | else "disabled" |
---|
85 | | - | case _ => |
---|
86 | | - | "notWhitelisted" |
---|
87 | | - | } |
---|
88 | | - | |
---|
89 | | - | |
---|
90 | | - | func hasPrivilege (callerAddress,privilegeName) = if ((addressFromStringValue(getStringValue(addrPrivileges, sharedKeyMainOperator)) == callerAddress)) |
---|
91 | | - | then true |
---|
92 | | - | else match getInteger(addrPrivileges, (((((sharedKeyPrivilege + separator) + toBase58String(callerAddress.bytes)) + separator) + toBase58String(this.bytes)) + privilegeName)) { |
---|
93 | | - | case isInteger: Int => |
---|
94 | | - | true |
---|
95 | | - | case _ => |
---|
96 | | - | false |
---|
97 | | - | } |
---|
98 | | - | |
---|
99 | | - | |
---|
100 | | - | func calcForgeWeight (forgeSupplies) = (maxInt / forgeSupplies) |
---|
101 | | - | |
---|
102 | | - | |
---|
103 | | - | @Callable(i) |
---|
104 | | - | func addAssetToWhitelist (assetToAdd,assetTotalSupplies) = if ((hasPrivilege(i.caller, "addAssetToWhitelist") != true)) |
---|
105 | | - | then throw("Access denied") |
---|
106 | | - | else if (if ((isAsset(assetToAdd) != true)) |
---|
107 | | - | then true |
---|
108 | | - | else (isSafeInteger(assetTotalSupplies) != true)) |
---|
109 | | - | then throw("Data validation") |
---|
110 | | - | else if ((getForgeAssetState(assetToAdd) != "notWhitelisted")) |
---|
111 | | - | then throw("Asset already whitelisted") |
---|
112 | | - | else { |
---|
113 | | - | let forgeWeight = calcForgeWeight(assetTotalSupplies) |
---|
114 | | - | let newForgesTotalWeight = (getIntegerValue(this, keyForgesTotalWeight) + forgeWeight) |
---|
115 | | - | [BooleanEntry((keyForgeSettingsEnabled + assetToAdd), true), IntegerEntry((keyForgeSettingsSupplies + assetToAdd), assetTotalSupplies), IntegerEntry((keyForgeSettingsWeight + assetToAdd), forgeWeight), IntegerEntry(keyForgesTotalWeight, newForgesTotalWeight)] |
---|
116 | | - | } |
---|
117 | | - | |
---|
118 | | - | |
---|
119 | | - | |
---|
120 | | - | @Callable(i) |
---|
121 | | - | func removeAssetFromWhitelist (assetToRemove) = if ((hasPrivilege(i.caller, "removeAssetFromWhitelist") != true)) |
---|
122 | | - | then throw("Access denied") |
---|
123 | | - | else if ((isAsset(assetToRemove) != true)) |
---|
124 | | - | then throw("Data validation") |
---|
125 | | - | else { |
---|
126 | | - | let forgeAssetState = getForgeAssetState(assetToRemove) |
---|
127 | | - | if ((forgeAssetState == "notWhitelisted")) |
---|
128 | | - | then throw("Asset not whitelisted") |
---|
129 | | - | else { |
---|
130 | | - | let actions = [DeleteEntry((keyForgeSettingsEnabled + assetToRemove)), DeleteEntry((keyForgeSettingsSupplies + assetToRemove)), DeleteEntry((keyForgeSettingsWeight + assetToRemove))] |
---|
131 | | - | if ((forgeAssetState == "disabled")) |
---|
132 | | - | then actions |
---|
133 | | - | else IntegerEntry(keyForgesTotalWeight, (getIntegerValue(this, keyForgesTotalWeight) - getIntegerValue(this, (keyForgeSettingsWeight + assetToRemove)))) :: actions |
---|
134 | | - | } |
---|
135 | | - | } |
---|
136 | | - | |
---|
137 | | - | |
---|
138 | | - | |
---|
139 | | - | @Callable(i) |
---|
140 | | - | func enableDisableForge (assetToToggle) = if ((hasPrivilege(i.caller, "enableDisableForge") != true)) |
---|
141 | | - | then throw("Access denied") |
---|
142 | | - | else if ((isAsset(assetToToggle) != true)) |
---|
143 | | - | then throw("Data validation") |
---|
144 | | - | else { |
---|
145 | | - | let forgeAssetState = getForgeAssetState(assetToToggle) |
---|
146 | | - | if ((forgeAssetState == "notWhitelisted")) |
---|
147 | | - | then throw("Asset not whitelisted") |
---|
148 | | - | else { |
---|
149 | | - | let forgeWeight = getIntegerValue(this, keyForgeSettingsWeight) |
---|
150 | | - | let forgesTotalWeight = getIntegerValue(this, keyForgesTotalWeight) |
---|
151 | | - | if ((forgeAssetState == "disabled")) |
---|
152 | | - | then [IntegerEntry(keyForgesTotalWeight, (forgesTotalWeight + forgeWeight)), BooleanEntry((keyForgeSettingsEnabled + assetToToggle), true)] |
---|
153 | | - | else [IntegerEntry(keyForgesTotalWeight, (forgesTotalWeight - forgeWeight)), BooleanEntry((keyForgeSettingsEnabled + assetToToggle), false)] |
---|
154 | | - | } |
---|
155 | | - | } |
---|
156 | | - | |
---|
157 | | - | |
---|
158 | | - | |
---|
159 | | - | @Callable(i) |
---|
160 | | - | func updateForgeAssetSupplies (assetToUpdate,newSuppliesValue) = if ((hasPrivilege(i.caller, "updateForgeAssetSupplies") != true)) |
---|
161 | | - | then throw("Access denied") |
---|
162 | | - | else if (if ((isAsset(assetToUpdate) != true)) |
---|
163 | | - | then true |
---|
164 | | - | else (isSafeInteger(newSuppliesValue) != true)) |
---|
165 | | - | then throw("Data validation") |
---|
166 | | - | else { |
---|
167 | | - | let forgeAssetState = getForgeAssetState(assetToUpdate) |
---|
168 | | - | if ((forgeAssetState == "notWhitelisted")) |
---|
169 | | - | then throw("Asset not whitelisted") |
---|
170 | | - | else { |
---|
171 | | - | let currentForgeWeight = getIntegerValue(this, (keyForgeSettingsWeight + assetToUpdate)) |
---|
172 | | - | let newForgeWeight = calcForgeWeight(newSuppliesValue) |
---|
173 | | - | let actions = [IntegerEntry((keyForgeSettingsWeight + assetToUpdate), newForgeWeight), IntegerEntry((keyForgeSettingsSupplies + assetToUpdate), newSuppliesValue)] |
---|
174 | | - | if ((forgeAssetState == "disabled")) |
---|
175 | | - | then actions |
---|
176 | | - | else IntegerEntry(keyForgesTotalWeight, ((getIntegerValue(this, keyForgesTotalWeight) + newForgeWeight) - currentForgeWeight)) :: actions |
---|
177 | | - | } |
---|
178 | | - | } |
---|
179 | | - | |
---|
180 | | - | |
---|
181 | | - | |
---|
182 | | - | @Callable(i) |
---|
183 | | - | func claim () = if ((this == i.caller)) |
---|
184 | | - | then throw("Self-invoke forbidden") |
---|
185 | | - | else if (if ((size(i.payments) != 1)) |
---|
186 | | - | then true |
---|
187 | | - | else (i.payments[0].amount != 1)) |
---|
188 | | - | then throw("Payment issue") |
---|
189 | | - | else { |
---|
190 | | - | let receiptInfo = valueOrErrorMessage(assetInfo(valueOrErrorMessage(i.payments[0].assetId, "Wrong asset type")), "Unknown asset") |
---|
191 | | - | if ((receiptInfo.issuer != addrVault)) |
---|
192 | | - | then throw("Unknown issuer") |
---|
193 | | - | else { |
---|
194 | | - | let receiptFields = split(receiptInfo.description, separator) |
---|
195 | | - | if (if ((size(receiptFields) != 8)) |
---|
196 | | - | then true |
---|
197 | | - | else (receiptFields[0] != sharedReceiptTag)) |
---|
198 | | - | then throw("Not a receipt") |
---|
199 | | - | else { |
---|
200 | | - | let forgeAssetState = getForgeAssetState(receiptFields[3]) |
---|
201 | | - | if ((forgeAssetState != "enabled")) |
---|
202 | | - | then throw("Not whitelisted/enabled") |
---|
203 | | - | else { |
---|
204 | | - | let assetQuantity = toSafeInteger(receiptFields[4]) |
---|
205 | | - | let eligibleBlocks = (toSafeInteger(receiptFields[5]) - toSafeInteger(receiptFields[1])) |
---|
206 | | - | if ((1 > eligibleBlocks)) |
---|
207 | | - | then throw("No eligible blocks") |
---|
208 | | - | else { |
---|
209 | | - | let forgeWeight = getIntegerValue(this, (keyForgeSettingsWeight + receiptFields[3])) |
---|
210 | | - | let forgeSupplies = getIntegerValue(this, (keyForgeSettingsSupplies + receiptFields[3])) |
---|
211 | | - | let forgesTotalWeight = getIntegerValue(this, keyForgesTotalWeight) |
---|
212 | | - | let amountToDeliverPerBlock = fraction(fraction(nibelAmountPerBlocks, forgeWeight, forgesTotalWeight), assetQuantity, forgeSupplies) |
---|
213 | | - | if ((1 > amountToDeliverPerBlock)) |
---|
214 | | - | then throw("Below minimum required") |
---|
215 | | - | else { |
---|
216 | | - | let amountToDeliverTotal = (amountToDeliverPerBlock * eligibleBlocks) |
---|
217 | | - | if ((isSafeInteger(amountToDeliverTotal) != true)) |
---|
218 | | - | then throw("System error") |
---|
219 | | - | else [Reissue(nibelAsset, amountToDeliverTotal, true), ScriptTransfer(i.caller, amountToDeliverTotal, nibelAsset), IntegerEntry("assetQuantity", assetQuantity), IntegerEntry("eligibleBlocks", eligibleBlocks), IntegerEntry("forgeWeight", forgeWeight), IntegerEntry("forgeSupplies", forgeSupplies), IntegerEntry("forgesTotalWeight", forgesTotalWeight), IntegerEntry("amountToDeliverPerBlock", amountToDeliverPerBlock), IntegerEntry("amountToDeliverTotal", amountToDeliverTotal)] |
---|
220 | | - | } |
---|
221 | | - | } |
---|
222 | | - | } |
---|
223 | | - | } |
---|
224 | | - | } |
---|
225 | | - | } |
---|
226 | | - | |
---|
| 14 | + | @Verifier(tx) |
---|
| 15 | + | func verify () = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|