-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.ride
87 lines (68 loc) · 3.22 KB
/
settings.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
{-# STDLIB_VERSION 6 #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# CONTENT_TYPE DAPP #-}
let WavesId = "WAVES"
let NodeAddressStr = "3PCrRrwHEjGXFjYtXDsNv78f3Ch3CH3p6V1"
let NextAssetIndexKey = "nextAssetIndex"
func makeAssetIndexKey(assetIdStr: String) = "assetIndex_" + assetIdStr
func makeAssetIdKey(assetIndex: Int) = "assetId_" + assetIndex.toString()
func getNextAssetIndex() = getInteger(NextAssetIndexKey).valueOrElse(0)
func getAssetIndex(assetIdStr: String) = getInteger(makeAssetIndexKey(assetIdStr))
@Callable(i)
func registerAsset(assetIdStr: 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 (getAssetIndex(assetIdStr) != unit) then throw("Already registered") else
let assetIndex = getNextAssetIndex()
let assetId = if (assetIdStr == WavesId) then base58'' else assetInfo(fromBase58String(assetIdStr)).value().id
[
IntegerEntry(makeAssetIndexKey(assetIdStr), assetIndex),
BinaryEntry(makeAssetIdKey(assetIndex), assetId),
IntegerEntry(NextAssetIndexKey, assetIndex + 1)
]
}
@Callable(i)
func applySettings(swapWavesTo: String, swapWavesDlpTo: String, delegateTo: String) = {
if (i.payments.size() > 0) then throw("Payments are prohibited") else
if (i.caller == this) then throw("Self call is prohibited") else
let callerAddressStr = i.caller.toString()
let swapWavesToBytes = fromBase58String(swapWavesTo)
let swapWavsDlpToBytes = fromBase58String(swapWavesDlpTo)
if (swapWavesTo != WavesId && assetInfo(swapWavesToBytes) == unit) then throw("Unknown asset ID: " + swapWavesTo) else
if (swapWavesDlpTo != WavesId && assetInfo(swapWavsDlpToBytes) == unit) then throw("Unknown asset ID: " + swapWavesDlpTo) else
if (addressFromString(delegateTo) == unit) then throw("Invalid address: " + delegateTo) else
if (delegateTo == NodeAddressStr || delegateTo == this.toString()) then throw("Prohibited address: " + delegateTo) else
let nextAssetIndex = getNextAssetIndex()
let (firstAssetToRegister, nextAssetIndex1) = if (getAssetIndex(swapWavesTo).isDefined())
then ([], nextAssetIndex)
else {
let assetIndex = nextAssetIndex
(
[
IntegerEntry(makeAssetIndexKey(swapWavesTo), nextAssetIndex),
BinaryEntry(makeAssetIdKey(assetIndex), swapWavesToBytes)
], nextAssetIndex + 1
)
}
let (secondAssetToRegister, nextAssetIndex2) = if (getAssetIndex(swapWavesDlpTo).isDefined())
then ([], nextAssetIndex1)
else {
let assetIndex = nextAssetIndex1
(
[
IntegerEntry(makeAssetIndexKey(swapWavesDlpTo), assetIndex),
BinaryEntry(makeAssetIdKey(assetIndex), swapWavsDlpToBytes)
], nextAssetIndex1 + 1
)
}
let nextAssetIndexToSave = if (nextAssetIndex2 == nextAssetIndex)
then []
else [IntegerEntry(NextAssetIndexKey, nextAssetIndex2)]
[
StringEntry(callerAddressStr + "_swapWavesTo", swapWavesTo),
StringEntry(callerAddressStr + "_swapWavesDlpTo", swapWavesDlpTo),
StringEntry(callerAddressStr + "_delegateTo", delegateTo)
] ++ firstAssetToRegister ++ secondAssetToRegister ++ nextAssetIndexToSave
}
@Verifier(tx)
func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)