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