| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/bonded-stablecoin-deposits.json", |
| 5 | "init": "{ |
| 6 | |
| 7 | |
| 8 | $interest_asset = params.interest_asset; |
| 9 | $curve_aa = params.curve_aa; |
| 10 | |
| 11 | |
| 12 | $asset = var['asset']; |
| 13 | |
| 14 | $interest_rate = var[$curve_aa]['interest_rate']; |
| 15 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 16 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 17 | }", |
| 18 | "messages": { |
| 19 | "cases": [ |
| 20 | { |
| 21 | "if": "{ trigger.data.define AND !$asset }", |
| 22 | "messages": [ |
| 23 | { |
| 24 | "app": "asset", |
| 25 | "payload": { |
| 26 | "is_private": false, |
| 27 | "is_transferrable": true, |
| 28 | "auto_destroy": false, |
| 29 | "fixed_denominations": false, |
| 30 | "issued_by_definer_only": true, |
| 31 | "cosigned_by_definer": false, |
| 32 | "spender_attested": false |
| 33 | } |
| 34 | }, |
| 35 | { |
| 36 | "if": "{trigger.data.factory}", |
| 37 | "app": "data", |
| 38 | "payload": { |
| 39 | "write_stable_asset": 1, |
| 40 | "curve_aa": "{$curve_aa}" |
| 41 | } |
| 42 | }, |
| 43 | { |
| 44 | "if": "{trigger.data.factory}", |
| 45 | "app": "payment", |
| 46 | "payload": { |
| 47 | "asset": "base", |
| 48 | "outputs": [ |
| 49 | { |
| 50 | "address": "{trigger.data.factory}" |
| 51 | } |
| 52 | ] |
| 53 | } |
| 54 | }, |
| 55 | { |
| 56 | "app": "state", |
| 57 | "state": "{ |
| 58 | var['asset'] = response_unit; |
| 59 | response['asset'] = response_unit; |
| 60 | }" |
| 61 | } |
| 62 | ] |
| 63 | }, |
| 64 | { |
| 65 | "if": "{ $asset AND trigger.output[[asset=$interest_asset]] > 0 }", |
| 66 | "init": "{ |
| 67 | if (exists(trigger.data.interest_recipient) AND !is_valid_address(trigger.data.interest_recipient)) |
| 68 | bounce("bad address of interest recipient"); |
| 69 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 70 | bounce("bad to-address"); |
| 71 | $owner = trigger.data.to OTHERWISE trigger.address; |
| 72 | $deposit_amount = trigger.output[[asset=$interest_asset]]; |
| 73 | $stable_amount = floor($deposit_amount * $growth_factor); |
| 74 | $id = trigger.unit; |
| 75 | }", |
| 76 | "messages": [ |
| 77 | { |
| 78 | "app": "payment", |
| 79 | "payload": { |
| 80 | "asset": "{$asset}", |
| 81 | "outputs": [ |
| 82 | { |
| 83 | "address": "{$owner}", |
| 84 | "amount": "{ $stable_amount }" |
| 85 | } |
| 86 | ] |
| 87 | } |
| 88 | }, |
| 89 | { |
| 90 | "if": "{trigger.data.to AND is_aa(trigger.data.to)}", |
| 91 | "app": "data", |
| 92 | "payload": { |
| 93 | "to": "{trigger.address}" |
| 94 | } |
| 95 | }, |
| 96 | { |
| 97 | "app": "state", |
| 98 | "state": "{ |
| 99 | response['id'] = $id; |
| 100 | var['deposit_' || $id || '_amount'] = $deposit_amount; |
| 101 | var['deposit_' || $id || '_stable_amount'] = $stable_amount; |
| 102 | var['deposit_' || $id || '_owner'] = $owner; |
| 103 | if (trigger.data.interest_recipient) |
| 104 | var['deposit_' || $id || '_interest_recipient'] = trigger.data.interest_recipient; |
| 105 | }" |
| 106 | } |
| 107 | ] |
| 108 | }, |
| 109 | { |
| 110 | "if": "{ $asset AND trigger.output[[asset=$asset]] > 0 AND trigger.data.id }", |
| 111 | "init": "{ |
| 112 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 113 | bounce("bad to-address"); |
| 114 | $to = trigger.data.to OTHERWISE trigger.address; |
| 115 | $id = trigger.data.id; |
| 116 | if (var['deposit_' || $id || '_owner'] != trigger.address) |
| 117 | bounce("you are not the owner"); |
| 118 | $deposit_amount = var['deposit_' || $id || '_amount']; |
| 119 | $stable_amount = var['deposit_' || $id || '_stable_amount']; |
| 120 | $interest_recipient = var['deposit_' || $id || '_interest_recipient']; |
| 121 | if ($interest_recipient AND $interest_recipient != trigger.address){ |
| 122 | $new_stable_amount = floor($deposit_amount * $growth_factor); |
| 123 | $interest = $new_stable_amount - $stable_amount; |
| 124 | $expected_stable_amount = $new_stable_amount; |
| 125 | } |
| 126 | else |
| 127 | $expected_stable_amount = $stable_amount; |
| 128 | if ($expected_stable_amount != trigger.output[[asset=$asset]]) |
| 129 | bounce("expected " || $expected_stable_amount); |
| 130 | $to_aa = trigger.data.to AND is_aa(trigger.data.to); |
| 131 | }", |
| 132 | "messages": [ |
| 133 | { |
| 134 | "app": "payment", |
| 135 | "payload": { |
| 136 | "asset": "{$interest_asset}", |
| 137 | "outputs": [ |
| 138 | { |
| 139 | "address": "{$to}", |
| 140 | "amount": "{ $deposit_amount }" |
| 141 | } |
| 142 | ] |
| 143 | } |
| 144 | }, |
| 145 | { |
| 146 | "if": "{$to_aa}", |
| 147 | "app": "data", |
| 148 | "payload": { |
| 149 | "to": "{trigger.address}" |
| 150 | } |
| 151 | }, |
| 152 | { |
| 153 | "if": "{$to_aa}", |
| 154 | "app": "payment", |
| 155 | "payload": { |
| 156 | "asset": "base", |
| 157 | "outputs": [ |
| 158 | { |
| 159 | "address": "{$to}", |
| 160 | "amount": 2000 |
| 161 | } |
| 162 | ] |
| 163 | } |
| 164 | }, |
| 165 | { |
| 166 | "if": "{$interest}", |
| 167 | "app": "payment", |
| 168 | "payload": { |
| 169 | "asset": "{$asset}", |
| 170 | "outputs": [ |
| 171 | { |
| 172 | "address": "{$interest_recipient}", |
| 173 | "amount": "{ $interest }" |
| 174 | } |
| 175 | ] |
| 176 | } |
| 177 | }, |
| 178 | { |
| 179 | "app": "state", |
| 180 | "state": "{ |
| 181 | var['deposit_' || $id || '_amount'] = false; |
| 182 | var['deposit_' || $id || '_stable_amount'] = false; |
| 183 | var['deposit_' || $id || '_owner'] = false; |
| 184 | var['deposit_' || $id || '_interest_recipient'] = false; |
| 185 | }" |
| 186 | } |
| 187 | ] |
| 188 | }, |
| 189 | { |
| 190 | "if": "{ $asset AND trigger.data.id AND trigger.data.change_interest_recipient }", |
| 191 | "init": "{ |
| 192 | |
| 193 | if (exists(trigger.data.interest_recipient) AND !is_valid_address(trigger.data.interest_recipient)) |
| 194 | bounce("bad address of new interest recipient"); |
| 195 | $id = trigger.data.id; |
| 196 | $deposit_amount = var['deposit_' || $id || '_amount']; |
| 197 | if (!$deposit_amount) |
| 198 | bounce("deposit not found"); |
| 199 | if (var['deposit_' || $id || '_owner'] != trigger.address) |
| 200 | bounce("you are not the owner"); |
| 201 | $interest_recipient = var['deposit_' || $id || '_interest_recipient']; |
| 202 | $old_recipient = $interest_recipient OTHERWISE trigger.address; |
| 203 | $new_stable_amount = floor($deposit_amount * $growth_factor); |
| 204 | $interest = $new_stable_amount - var['deposit_' || $id || '_stable_amount']; |
| 205 | if ($interest < 0) |
| 206 | bounce("negative interest?"); |
| 207 | }", |
| 208 | "messages": [ |
| 209 | { |
| 210 | "if": "{$interest > 0}", |
| 211 | "app": "payment", |
| 212 | "payload": { |
| 213 | "asset": "{$asset}", |
| 214 | "outputs": [ |
| 215 | { |
| 216 | "address": "{$old_recipient}", |
| 217 | "amount": "{ $interest }" |
| 218 | } |
| 219 | ] |
| 220 | } |
| 221 | }, |
| 222 | { |
| 223 | "app": "state", |
| 224 | "state": "{ |
| 225 | var['deposit_' || $id || '_stable_amount'] = $new_stable_amount; |
| 226 | var['deposit_' || $id || '_interest_recipient'] = trigger.data.interest_recipient; |
| 227 | }" |
| 228 | } |
| 229 | ] |
| 230 | }, |
| 231 | { |
| 232 | "if": "{ $asset AND trigger.data.id }", |
| 233 | "init": "{ |
| 234 | $id = trigger.data.id; |
| 235 | $deposit_amount = var['deposit_' || $id || '_amount']; |
| 236 | if (!$deposit_amount) |
| 237 | bounce("deposit not found"); |
| 238 | $interest_recipient = var['deposit_' || $id || '_interest_recipient']; |
| 239 | $recipient = $interest_recipient OTHERWISE trigger.address; |
| 240 | |
| 241 | if (!$interest_recipient AND var['deposit_' || $id || '_owner'] != trigger.address) |
| 242 | bounce("you are not the owner"); |
| 243 | $new_stable_amount = floor($deposit_amount * $growth_factor); |
| 244 | $interest = $new_stable_amount - var['deposit_' || $id || '_stable_amount']; |
| 245 | if ($interest < 0) |
| 246 | bounce("negative interest?"); |
| 247 | if ($interest == 0) |
| 248 | bounce("0 interest accrued"); |
| 249 | }", |
| 250 | "messages": [ |
| 251 | { |
| 252 | "app": "payment", |
| 253 | "payload": { |
| 254 | "asset": "{$asset}", |
| 255 | "outputs": [ |
| 256 | { |
| 257 | "address": "{$recipient}", |
| 258 | "amount": "{ $interest }" |
| 259 | } |
| 260 | ] |
| 261 | } |
| 262 | }, |
| 263 | { |
| 264 | "app": "state", |
| 265 | "state": "{ |
| 266 | var['deposit_' || $id || '_stable_amount'] = $new_stable_amount; |
| 267 | }" |
| 268 | } |
| 269 | ] |
| 270 | } |
| 271 | ] |
| 272 | } |
| 273 | } |
| 274 | ] |