-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrome-keeper.ride
164 lines (125 loc) · 4.96 KB
/
rome-keeper.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
{-# STDLIB_VERSION 6 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
let VERSION = "RK-1.0.4"
let Scale8 = 100000000
let Scale6 = 1000000
let aggregatorAddress = getString("setup_aggregator").valueOrElse("").addressFromString().valueOrElse(Address(base58'3PGFHzVGT4NTigwCKP1NcwoXkodVZwvBuuU'))
let maxSwapAmount = getInteger("setup_maxSwapAmount").valueOrElse(10000000)
let maxLPCap = getInteger("setup_maxLPCap").valueOrElse(20000 * Scale6)
func getAssetString(assetId: ByteVector|Unit) = {
match assetId {
case b:ByteVector => b.toBase58String()
case _ => "WAVES"
}
}
func getAssetBytes(assetIdStr: String) = {
if (assetIdStr == "WAVES") then {unit} else {assetIdStr.fromBase58String()}
}
func getBalance(assetIdStr: String) = {
if (assetIdStr == "WAVES") then {wavesBalance(this).available} else {assetBalance(this, assetIdStr.fromBase58String())}
}
func getTargetReturn(assetFrom: String, assetsRatio: Int) = {
let ratios = getStringValue("setup_targets_"+assetFrom).split(";") # "80000000,103000000;50000000,105000000;30000000,110000000;10000000,120000000;0,200000000"
func f(accum: Int, next: String) = {
let li = next.split(",")
if (assetsRatio < li[0].parseIntValue()) then {li[1].parseIntValue()} else {accum}
}
FOLD<10>(ratios, 100000000, f)
}
func getLPQuantity() = {
assetInfo(getStringValue("setup_lpId").fromBase58String()).value().quantity
}
func getLPContractPrice() = {
let assets = getStringValue("setup_assets").split(",")
let treasury = getBalance(assets[0].value()) + getBalance(assets[1].value())
let quant = getLPQuantity()
if (quant == 0) then Scale6 else fraction(treasury, Scale6, quant)
}
@Callable(i)
func init() = {
if (i.caller != this) then {throw("available for self-invoke only")}
else {
let lp = Issue("Rome Keeper", "Rome Keeper - derivative to sustain ROME stablecoin peg. https://rome.puzzleswap.org.", 0, 6, true)
let lpId = lp.calculateAssetId()
[
StringEntry("setup_lpId", lpId.toBase58String()),
StringEntry("setup_assets", "9wc3LXNA4TEBsXyKtoLE9mrbDD7WMHXvXrCjZvabLAsi,AP4Cb5xLYGH6ZigHreCZHoXpQTWDkPsG2BHqfDUx6taJ"),
StringEntry("setup_targets_9wc3LXNA4TEBsXyKtoLE9mrbDD7WMHXvXrCjZvabLAsi", "80000000,103000000;50000000,105000000;30000000,110000000;10000000,120000000;0,200000000"),
StringEntry("setup_targets_AP4Cb5xLYGH6ZigHreCZHoXpQTWDkPsG2BHqfDUx6taJ", "80000000,100000000;50000000,100000000;30000000,101000000;10000000,110000000;0,150000000")
]
}
}
@Callable(i)
func doSwap(asset0Str: String, asset1Str: String, amount: Int, route: String) = {
let asset0Bal = getBalance(asset0Str)
let asset1Bal = getBalance(asset1Str)
let currentRatio = fraction(asset0Bal, Scale8, asset1Bal+asset0Bal)
let targetReturn = getTargetReturn(asset0Str, currentRatio)
let asset0 = getAssetBytes(asset0Str)
let asset1 = getAssetBytes(asset1Str)
strict asset1BalBefore = getBalance(asset1Str)
strict swapTx = invoke(aggregatorAddress, "swap", [route, amount], [AttachedPayment(asset0, amount)])
strict asset1BalChange = getBalance(asset1Str) - asset1BalBefore
if (maxSwapAmount < amount) then {throw("amount to swap exceeds a limit")}
else if (fraction(asset1BalChange, Scale8, amount) < targetReturn) then { throw("swap premium is not enough to perform arbitrage") }
else {
[]
}
}
@Callable(i)
func mintLP() = {
let assetId = i.payments[0].assetId
let amount = i.payments[0].amount
let quantity = getLPQuantity()
let toMint = fraction(amount, Scale6, getLPContractPrice())
if (getStringValue("setup_assets").indexOf(assetId.getAssetString()) == unit) then {throw("wrong asset attached")}
else if ((quantity + toMint) > maxLPCap) then {throw("LP cap reached")}
else {
let lpId = getStringValue("setup_lpId").fromBase58String()
let reissue = Reissue(lpId, toMint, true)
[
reissue,
ScriptTransfer(i.caller, toMint, lpId)
]
}
}
@Callable(i)
func redeemLP() = {
[
]
}
@Callable(i)
func updateString(key: String, val: String) = {
if (this != i.caller) then {throw("admin only")}
else {
[
StringEntry(key, val)
]
}
}
@Callable(i)
func updateInt(key: String, val: Int) = {
if (this != i.caller) then {throw("admin only")}
else {
[
IntegerEntry(key, val)
]
}
}
@Callable(i)
func getRKPrice(debug: Boolean) = {
let res = getLPContractPrice()
if (debug) then { throw(res.toString()) } else { ([], res)}
}
@Callable(i)
func getTargets(debug: Boolean) = {
let assets = getStringValue("setup_assets").split(",")
let asset0Bal = getBalance(assets[0])
let asset1Bal = getBalance(assets[1])
let currentRatio = fraction(asset0Bal, Scale8, asset1Bal+asset0Bal)
let targetReturn = getTargetReturn(assets[0], currentRatio)
let currentRatioRev = fraction(asset1Bal, Scale8, asset1Bal+asset0Bal)
let targetReturnRev = getTargetReturn(assets[1], currentRatio)
if (debug) then { throw(targetReturn.toString() + "," + targetReturnRev.toString()) } else { ([], (targetReturn, targetReturnRev))}
}