| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 11000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $user = trigger.address; |
| 9 | $is_aa = $user == this_address; |
| 10 | $received_asset = trigger.output[[asset!=base]].asset; |
| 11 | $received_amount = trigger.output[[asset!=base]].amount; |
| 12 | }", |
| 13 | "messages": { |
| 14 | "cases": [ |
| 15 | { |
| 16 | "if": "{!var["indice_hash"] AND !!trigger.data.define}", |
| 17 | "init": "{ |
| 18 | |
| 19 | |
| 20 | if (!!var["temp_creator"]) |
| 21 | { |
| 22 | if (var["temp_creator"] != $user) |
| 23 | bounce("Only the creator of the indice is allowed to define it!"); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | if (!!var["internal_link_1"]) |
| 28 | { |
| 29 | $first_chain_link = var["internal_link_1"]; |
| 30 | if (!var["internal_link_"||$first_chain_link]) |
| 31 | bounce("the indice requires at least 2 underlying assets, please send the second underlying asset in the desired amount!"); |
| 32 | } |
| 33 | else |
| 34 | bounce("To create a new indice, start sending the wanted amount of the first underlying asset!"); |
| 35 | |
| 36 | $asset_name = trigger.data.define; |
| 37 | }", |
| 38 | "messages": [ |
| 39 | { |
| 40 | "app": "asset", |
| 41 | "payload": { |
| 42 | "is_private": false, |
| 43 | "is_transferrable": true, |
| 44 | "auto_destroy": true, |
| 45 | "fixed_denominations": false, |
| 46 | "issued_by_definer_only": true, |
| 47 | "cosigned_by_definer": false, |
| 48 | "spender_attested": false |
| 49 | } |
| 50 | }, |
| 51 | { |
| 52 | "app": "state", |
| 53 | "state": "{ |
| 54 | response["defined"] = $asset_name ||" is defined as "||response_unit; |
| 55 | var["indice_hash"] = response_unit; |
| 56 | var["indice_name"] = $asset_name; |
| 57 | var["temp_previous_underlying_asset"] = false; |
| 58 | }" |
| 59 | } |
| 60 | ] |
| 61 | }, |
| 62 | { |
| 63 | "if": "{!var["indice_hash"] }", |
| 64 | "init": "{ |
| 65 | |
| 66 | |
| 67 | if (!!var["temp_creator"]) |
| 68 | if (var["temp_creator"] != $user) |
| 69 | bounce("The indice is being created, and you are not the creator, try again later!"); |
| 70 | |
| 71 | $previous_underlying_asset_hash = var["temp_previous_underlying_asset"] otherwise "1"; |
| 72 | $received_underlying_asset_hash = $received_asset ; |
| 73 | $received_underlying_asset_amount = $received_amount; |
| 74 | }", |
| 75 | "messages": [ |
| 76 | { |
| 77 | "app": "state", |
| 78 | "state": "{ |
| 79 | var["temp_creator"] = $user; |
| 80 | |
| 81 | |
| 82 | var["amount_of_"||$received_underlying_asset_hash] = $received_underlying_asset_amount; |
| 83 | |
| 84 | |
| 85 | var["internal_link_"||$previous_underlying_asset_hash] = $received_underlying_asset_hash; |
| 86 | var["temp_previous_underlying_asset"] = $received_underlying_asset_hash; |
| 87 | |
| 88 | response["defined"] = "underlying asset received, waiting for next one or data.define=<indice name>"; |
| 89 | }" |
| 90 | } |
| 91 | ] |
| 92 | }, |
| 93 | { |
| 94 | "if": "{!!var["temp_creator"] and var["temp_creator"] == $user}", |
| 95 | "messages": [ |
| 96 | { |
| 97 | "app": "payment", |
| 98 | "payload": { |
| 99 | "asset": "{var["indice_hash"]}", |
| 100 | "outputs": [ |
| 101 | { |
| 102 | "address": "{var["temp_creator"] }", |
| 103 | "amount": "{ 1 }" |
| 104 | } |
| 105 | ] |
| 106 | } |
| 107 | }, |
| 108 | { |
| 109 | "app": "state", |
| 110 | "state": "{ |
| 111 | response["defined"] = "Creator got the first token and do not have any role to play"; |
| 112 | var["temp_creator"] = false; |
| 113 | }" |
| 114 | } |
| 115 | ] |
| 116 | }, |
| 117 | { |
| 118 | "if": "{($received_asset == var["indice_hash"]) or $is_aa}", |
| 119 | "init": "{ |
| 120 | if (!$is_aa) |
| 121 | { |
| 122 | $requester = $user; |
| 123 | $underlying_asset_hash = var["internal_link_1"]; |
| 124 | $amount = $received_amount; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | $requester = trigger.data.aa_reserved_requester; |
| 129 | $underlying_asset_hash = trigger.data.aa_reserved_underlying_asset_hash; |
| 130 | $amount = json_parse(trigger.data.aa_reserved_asset_amount); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | $next_underlying_asset_hash = var["internal_link_"||$underlying_asset_hash]; |
| 135 | }", |
| 136 | "messages": [ |
| 137 | { |
| 138 | "app": "payment", |
| 139 | "payload": { |
| 140 | "asset": "{$underlying_asset_hash }", |
| 141 | "outputs": [ |
| 142 | { |
| 143 | "address": "{$requester}", |
| 144 | "amount": "{$amount * json_parse(var["amount_of_"||$underlying_asset_hash])}" |
| 145 | } |
| 146 | ] |
| 147 | } |
| 148 | }, |
| 149 | { |
| 150 | "if": "{!!$next_underlying_asset_hash}", |
| 151 | "app": "data", |
| 152 | "payload": { |
| 153 | "aa_reserved_requester": "{$requester}", |
| 154 | "aa_reserved_underlying_asset_hash": "{$next_underlying_asset_hash}", |
| 155 | "aa_reserved_asset_amount": "{$amount}" |
| 156 | } |
| 157 | }, |
| 158 | { |
| 159 | "if": "{!!$next_underlying_asset_hash}", |
| 160 | "app": "payment", |
| 161 | "payload": { |
| 162 | "asset": "base", |
| 163 | "outputs": [ |
| 164 | { |
| 165 | "address": "{this_address}", |
| 166 | "amount": "{ 1 }" |
| 167 | } |
| 168 | ] |
| 169 | } |
| 170 | }, |
| 171 | { |
| 172 | "app": "state", |
| 173 | "state": "{ |
| 174 | response["redemption"] = "indice token has been excahnge for the underlying assets."; |
| 175 | }" |
| 176 | } |
| 177 | ] |
| 178 | }, |
| 179 | { |
| 180 | "if": "{ |
| 181 | (!!var["temp_"||$user||"_underlying_asset"] and ($received_asset == var["temp_"||$user||"_underlying_asset"])) OR |
| 182 | ( !var["temp_"||$user||"_underlying_asset"] and ($received_asset == var["internal_link_1"])) |
| 183 | }", |
| 184 | "init": "{ |
| 185 | if (!!var["temp_"||$user||"_underlying_asset"]) |
| 186 | { |
| 187 | $underlying_asset_hash = var["temp_"||$user||"_underlying_asset"]; |
| 188 | $etf_amount = var["temp_"||$user||"_etf_amount"]; |
| 189 | $needed_mount = $etf_amount * var["amount_of_"||$underlying_asset_hash]; |
| 190 | |
| 191 | if ($received_amount != $needed_mount) |
| 192 | bounce ("you should send "||$needed_mount|| " of "||$underlying_asset_hash); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | $underlying_asset_hash = var["internal_link_1"]; |
| 197 | $needed_mount = var["amount_of_"||$underlying_asset_hash]; |
| 198 | |
| 199 | |
| 200 | if (($received_amount % $needed_mount) != 0) |
| 201 | bounce ("you should send a multiple of "||$needed_mount|| " of "||$underlying_asset_hash); |
| 202 | else |
| 203 | $etf_amount = $received_amount / $needed_mount; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | $next_underlying_asset_hash = var["internal_link_"||$underlying_asset_hash]; |
| 209 | $all_underlying_asset_received = !$next_underlying_asset_hash; |
| 210 | }", |
| 211 | "messages": [ |
| 212 | { |
| 213 | "if": "{$all_underlying_asset_received}", |
| 214 | "app": "payment", |
| 215 | "payload": { |
| 216 | "asset": "{var["indice_hash"] }", |
| 217 | "outputs": [ |
| 218 | { |
| 219 | "address": "{$user}", |
| 220 | "amount": "{$etf_amount}" |
| 221 | } |
| 222 | ] |
| 223 | } |
| 224 | }, |
| 225 | { |
| 226 | "app": "state", |
| 227 | "state": "{ |
| 228 | if ($all_underlying_asset_received) |
| 229 | { |
| 230 | var["temp_"||$user||"_etf_amount"] = false; |
| 231 | var["temp_"||$user||"_underlying_asset"] = false; |
| 232 | response["processed"] = var["indice_name"]||" sent"; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | var["temp_"||$user||"_etf_amount"] = $etf_amount; |
| 237 | var["temp_"||$user||"_underlying_asset"] = $next_underlying_asset_hash; |
| 238 | response["in progress"] = $underlying_asset_hash ||" received and waiting for "||$next_underlying_asset_hash; |
| 239 | } |
| 240 | }" |
| 241 | } |
| 242 | ] |
| 243 | }, |
| 244 | { |
| 245 | "messages": [ |
| 246 | { |
| 247 | "app": "state", |
| 248 | "state": "{ |
| 249 | |
| 250 | if (!!var["temp_"||$user||"_underlying_asset"]) |
| 251 | { |
| 252 | $underlying_asset_hash = var["temp_"||$user||"_underlying_asset"]; |
| 253 | $etf_amount = var["temp_"||$user||"_etf_amount"]; |
| 254 | $needed_mount = $etf_amount * var["amount_of_"||$underlying_asset_hash]; |
| 255 | |
| 256 | bounce ("you should send "||$needed_mount|| " of "||$underlying_asset_hash); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | $underlying_asset_hash = var["internal_link_1"]; |
| 261 | $needed_mount = var["amount_of_"||$underlying_asset_hash]; |
| 262 | |
| 263 | bounce ("you should send a multiple of "||$needed_mount|| " of "||$underlying_asset_hash); |
| 264 | } |
| 265 | }" |
| 266 | } |
| 267 | ] |
| 268 | } |
| 269 | ] |
| 270 | } |
| 271 | } |
| 272 | ] |