-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ride
188 lines (134 loc) · 7.01 KB
/
config.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
{-# STDLIB_VERSION 6 #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# CONTENT_TYPE DAPP #-}
let WavesId = "WAVES"
let Arr10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
let AssetsQuantityKey = "assetsQuantity"
let AssetOperationsQuantityKey = "assetOperationsQuantity"
let InputsQuantityKey = "inputsQuantity"
let AssetsQuantity = this.getInteger(AssetsQuantityKey).valueOrElse(0)
let AssetOperationsQuantity = this.getInteger(AssetOperationsQuantityKey).valueOrElse(0)
let InputsQuantity = this.getInteger(InputsQuantityKey).valueOrElse(0)
func makeAssetIndexKey(asset: String) = "assetIndex_" + asset
func makeAssetIdKey(assetIndex: Int) = "assetId_" + assetIndex.toString()
func makeAssetOperationKey(chain: String, asset: String, operation: String) = makeString(["assetOperation", chain, asset, operation], "_")
func makeChainIdKey(chain: String) = "chainId_" + chain
func makeBlacklistedDelegateKey(delegate: String) = "blacklistedDelegate_" + delegate
func makeInputAssetIndexKey(chain: String, asset: String) = makeString([ "inputAssetIndex", chain, asset], "_")
func makeUnitsChainContractKey(name: String) = "unitsChainContract_" + name
func makeUnitsChainNameKey(address: String) = "unitsChainName_" + address
func getChainId(chain: String) = this.getInteger(makeChainIdKey(chain))
@Callable(i)
func setAddress(key: String, address: String) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
if (addressFromString(address) == unit) then throw("Invalid address: " + address) else
[StringEntry(key, address)]
}
@Callable(i)
func setAssetId(key: String, assetId: String) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
let id = fromBase58String(assetId)
if (assetInfo(id) == unit) then throw("Unknown assetId: " + assetId) else
[BinaryEntry(key, id)]
}
@Callable(i)
func setAssetIndexes(assets: List[String]) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
let assetsSize = assets.size()
func assetsFold(accum: List[IntegerEntry|BinaryEntry], index: Int) = {
if (index >= assetsSize) then accum else
let asset = assets[index]
let assetIndexKey = makeAssetIndexKey(asset)
if (this.getInteger(assetIndexKey) != unit) then throw("Already exists: " + asset) else
let assetId = if (asset == WavesId) then base58'' else assetInfo(fromBase58String(asset)).value().id
let assetIndex = AssetsQuantity + index
accum :+ BinaryEntry(makeAssetIdKey(assetIndex), assetId) :+ IntegerEntry(assetIndexKey, assetIndex)
}
FOLD<10>(Arr10, [], assetsFold) :+ IntegerEntry(AssetsQuantityKey, AssetsQuantity + assetsSize)
}
@Callable(i)
func setAssetOperations(chains: List[String], assets: List[String], operations: List[String]) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
let chainsSize = chains.size()
let assetsSize = assets.size()
let operationsSize = operations.size()
if (chainsSize != assetsSize || assetsSize != operationsSize) then throw("Mismatch argument sizes") else
func operationsFold(accum: List[IntegerEntry], index: Int) = {
if (index >= operationsSize) then accum else
let chain = chains[index]
if (getChainId(chain) == unit) then throw("Invalid chain: " + chain) else
let asset = assets[index]
let operation = operations[index]
let assetOperationKey = makeAssetOperationKey(chain, asset, operation)
if (this.getInteger(assetOperationKey) != unit) then throw("Already exists: " + chain + "," + asset + "," + operation) else
accum :+ IntegerEntry(assetOperationKey, AssetOperationsQuantity + index)
}
FOLD<10>(Arr10, [], operationsFold) :+ IntegerEntry(AssetOperationsQuantityKey, AssetOperationsQuantity + operationsSize)
}
@Callable(i)
func setBlacklistedDelegates(delegates: List[String]) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
let delegatesSize = delegates.size()
func delegatesFold(accum: List[BooleanEntry], index: Int) = {
if (index >= delegatesSize) then accum else
let delegate = delegates[index]
let isValidWavesAddress = addressFromString(delegate) != unit
let isValidEthAddress = delegate.take(2) == "0x" && fromBase16String(delegate.drop(2)).size() == 20
if (!(isValidWavesAddress || isValidEthAddress)) then throw("Invalid delegate: " + delegate) else
let blacklistedDelegateKey = makeBlacklistedDelegateKey(delegate)
if (this.getBoolean(blacklistedDelegateKey) != unit) then throw("Already exists: " + delegate) else
accum :+ BooleanEntry(blacklistedDelegateKey, true)
}
FOLD<10>(Arr10, [], delegatesFold)
}
@Callable(i)
func setChainId(chainName: String, chainId: Int) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
if (chainId <= 0) then throw("chainId should be positive: " + chainId.toString()) else
[IntegerEntry(makeChainIdKey(chainName), chainId)]
}
@Callable(i)
func setInputAssets(chains: List[String], assets: List[String]) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
let chainsSize = chains.size()
let assetsSize = assets.size()
if (chainsSize != assetsSize) then throw("Mismatch argument sizes") else
func inputsFold(accum: List[IntegerEntry], index: Int) = {
if (index >= assetsSize) then accum else
let chain = chains[index]
if (getChainId(chain) == unit) then throw("Invalid chain: " + chain) else
let asset = assets[index]
let inputAssetKey = makeInputAssetIndexKey(chain, asset)
if (this.getInteger(inputAssetKey) != unit) then throw("Already exists: " + asset) else
accum :+ IntegerEntry(inputAssetKey, InputsQuantity + index)
}
FOLD<10>(Arr10, [], inputsFold) :+ IntegerEntry(InputsQuantityKey, InputsQuantity + assetsSize)
}
@Callable(i)
func setPublicKey(key: String, publicKey: String) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
let pubKey = fromBase58String(publicKey)
strict validation = addressFromPublicKey(pubKey)
[BinaryEntry(key, pubKey)]
}
@Callable(i)
func setUnitsContract(address: String, name: String) = {
if (i.caller != this) then throw("Only self call is allowed") else
if (i.payments.size() > 0) then throw("Payments are prohibited") else
if (addressFromString(address) == unit) then throw("Invalid address: " + address) else
if (getChainId(name) == unit) then throw("Invalid chain: " + name) else
[
StringEntry(makeUnitsChainContractKey(name), address),
StringEntry(makeUnitsChainNameKey(address), name)
]
}
@Verifier(tx)
func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)