| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $balance = var['balance'] otherwise 0; |
| 9 | $id = var['last_id'] otherwise 1; |
| 10 | }", |
| 11 | "messages": { |
| 12 | "cases": [ |
| 13 | { |
| 14 | "if": "{ |
| 15 | trigger.data.type == 'make_order_sell' |
| 16 | AND trigger.data.price_in_eth |
| 17 | }", |
| 18 | "init": "{ |
| 19 | if(trigger.output[[asset=base]].amount < 10000) |
| 20 | bounce('Not enough money'); |
| 21 | }", |
| 22 | "messages": [ |
| 23 | { |
| 24 | "app": "state", |
| 25 | "state": "{ |
| 26 | var[$id || '_owner'] = trigger.address; |
| 27 | var[$id || '_type'] = 'sell'; |
| 28 | var[$id || '_amount'] = trigger.output[[asset=base]].amount; |
| 29 | var[$id || '_price_in_eth'] = trigger.data.price_in_eth; |
| 30 | var[$id || '_status'] = 'open'; |
| 31 | var['last_id'] = $id + 1; |
| 32 | response['message'] = 'Oreder created'; |
| 33 | response['id'] = $id; |
| 34 | }" |
| 35 | } |
| 36 | ] |
| 37 | }, |
| 38 | { |
| 39 | "if": "{ |
| 40 | trigger.data.type == 'make_order_buy' |
| 41 | AND trigger.data.amount |
| 42 | AND trigger.data.price_in_gb |
| 43 | }", |
| 44 | "init": "{ |
| 45 | if(trigger.data.amount <= 0) |
| 46 | bounce('The amount must be greater than 0'); |
| 47 | }", |
| 48 | "messages": [ |
| 49 | { |
| 50 | "app": "state", |
| 51 | "state": "{ |
| 52 | var[$id || '_owner'] = trigger.address; |
| 53 | var[$id || '_type'] = 'buy'; |
| 54 | var[$id || '_amount'] = trigger.data.amount; |
| 55 | var[$id || '_price_in_gb'] = trigger.data.price_in_gb; |
| 56 | var[$id || '_status'] = 'open'; |
| 57 | var['last_id'] = $id + 1; |
| 58 | response['message'] = 'Oreder created'; |
| 59 | response['id'] = $id; |
| 60 | }" |
| 61 | } |
| 62 | ] |
| 63 | }, |
| 64 | { |
| 65 | "if": "{ |
| 66 | $thisId = trigger.data.id; |
| 67 | trigger.data.type == 'redeem' |
| 68 | AND trigger.data.id |
| 69 | AND trigger.data.hash |
| 70 | AND trigger.data.address |
| 71 | }", |
| 72 | "init": "{ |
| 73 | if(!var[$thisId || '_owner']) |
| 74 | bounce('Order not found'); |
| 75 | |
| 76 | if(var[$thisId || '_type'] != 'buy') |
| 77 | bounce('This order is not for purchase'); |
| 78 | |
| 79 | if(var[$thisId || '_status'] != 'open') |
| 80 | bounce('Order it not opened'); |
| 81 | |
| 82 | if(trigger.output[[asset=base]].amount != var[$thisId || '_price_in_gb']) |
| 83 | bounce('Amount is incorrect'); |
| 84 | }", |
| 85 | "messages": [ |
| 86 | { |
| 87 | "app": "state", |
| 88 | "state": "{ |
| 89 | var[$thisId || '_status'] = 'lock'; |
| 90 | var[$thisId || '_address'] = trigger.data.address; |
| 91 | var[$thisId || '_hash'] = trigger.data.hash; |
| 92 | var[$thisId || '_unlock_time'] = timestamp + 3600; |
| 93 | response['message'] = 'done'; |
| 94 | }" |
| 95 | } |
| 96 | ] |
| 97 | }, |
| 98 | { |
| 99 | "if": "{ |
| 100 | $thisId = trigger.data.id; |
| 101 | trigger.data.type == 'lock' |
| 102 | AND trigger.data.id |
| 103 | AND trigger.data.address |
| 104 | AND trigger.data.hash |
| 105 | }", |
| 106 | "init": "{ |
| 107 | if(trigger.address != var[$thisId || '_owner']) |
| 108 | bounce("You're not the owner"); |
| 109 | }", |
| 110 | "messages": [ |
| 111 | { |
| 112 | "app": "state", |
| 113 | "state": "{ |
| 114 | var[$thisId || '_status'] = 'lock'; |
| 115 | var[$thisId || '_address'] = trigger.data.address; |
| 116 | var[$thisId || '_hash'] = trigger.data.hash; |
| 117 | var[$thisId || '_unlock_time'] = timestamp + 3600; |
| 118 | response['message'] = 'done'; |
| 119 | }" |
| 120 | } |
| 121 | ] |
| 122 | }, |
| 123 | { |
| 124 | "if": "{ |
| 125 | $thisId = trigger.data.id; |
| 126 | trigger.data.type == 'unlock' |
| 127 | AND trigger.data.id |
| 128 | }", |
| 129 | "init": "{ |
| 130 | if(trigger.address != var[$thisId || '_owner']) |
| 131 | bounce("You're not the owner"); |
| 132 | |
| 133 | if(timestamp < var[$thisId || '_unlock_time']) |
| 134 | bounce('Lock time has not yet expired'); |
| 135 | }", |
| 136 | "messages": [ |
| 137 | { |
| 138 | "app": "state", |
| 139 | "state": "{ |
| 140 | var[$thisId || '_status'] = 'open'; |
| 141 | var[$thisId || '_address'] = false; |
| 142 | var[$thisId || '_hash'] = false; |
| 143 | var[$thisId || '_unlock_time'] = false; |
| 144 | response['message'] = 'done'; |
| 145 | }" |
| 146 | } |
| 147 | ] |
| 148 | }, |
| 149 | { |
| 150 | "if": "{ |
| 151 | $thisId = trigger.data.id; |
| 152 | trigger.data.type == 'withdraw' |
| 153 | AND trigger.data.id |
| 154 | AND trigger.data.key |
| 155 | }", |
| 156 | "init": "{ |
| 157 | if (trigger.address != var[$thisId || '_address']){ |
| 158 | bounce('Incorrect address'); |
| 159 | } |
| 160 | if (sha256(trigger.data.key) != var[$thisId || '_hash']){ |
| 161 | bounce('Incorrect key |' || sha256(trigger.data.key) || ' _ ' || var[$thisId || '_hash']); |
| 162 | } |
| 163 | $fees_aa = var[$thisId || '_amount'] * 0.25 / 100; |
| 164 | }", |
| 165 | "messages": [ |
| 166 | { |
| 167 | "app": "payment", |
| 168 | "payload": { |
| 169 | "outputs": [ |
| 170 | { |
| 171 | "address": "{trigger.address}", |
| 172 | "amount": "{ceil(var[$thisId || '_amount'] - $fees_aa)}" |
| 173 | } |
| 174 | ] |
| 175 | } |
| 176 | }, |
| 177 | { |
| 178 | "app": "data", |
| 179 | "payload": { |
| 180 | "id": "{$thisId}", |
| 181 | "timestamp": "{timestamp}", |
| 182 | "type": "{var[$thisId || '_type']}", |
| 183 | "amount": "{var[$thisId || '_amount']}", |
| 184 | "price": "{(var[$thisId || '_price_in_eth']) ? var[$thisId || '_price_in_eth'] : var[$thisId || 'price_in_gb']}" |
| 185 | } |
| 186 | }, |
| 187 | { |
| 188 | "app": "state", |
| 189 | "state": "{ |
| 190 | var['balance'] = $balance + $fees_aa; |
| 191 | var[$thisId || '_owner'] = false; |
| 192 | var[$thisId || '_type'] = false; |
| 193 | var[$thisId || '_amount'] = false; |
| 194 | var[$thisId || '_price_in_eth'] = false; |
| 195 | var[$thisId || '_price_in_gb'] = false; |
| 196 | var[$thisId || '_status'] = false; |
| 197 | var[$thisId || '_address'] = false; |
| 198 | var[$thisId || '_hash'] = false; |
| 199 | var[$thisId || '_unlock_time'] = false; |
| 200 | response['message'] = 'done'; |
| 201 | }" |
| 202 | } |
| 203 | ] |
| 204 | }, |
| 205 | { |
| 206 | "if": "{ |
| 207 | trigger.data.type == 'remove' |
| 208 | AND trigger.data.id |
| 209 | }", |
| 210 | "init": "{ |
| 211 | $orderId = trigger.data.id; |
| 212 | if(!var[$orderId || '_owner']) |
| 213 | bounce('Order not found'); |
| 214 | |
| 215 | if(var[$orderId || '_owner'] != trigger.address) |
| 216 | bounce('You not owner'); |
| 217 | |
| 218 | if(var[$orderId || '_status'] == 'lock') |
| 219 | bounce('Order locked'); |
| 220 | }", |
| 221 | "messages": [ |
| 222 | { |
| 223 | "app": "state", |
| 224 | "state": "{ |
| 225 | var[$thisId || '_owner'] = false; |
| 226 | var[$thisId || '_type'] = false; |
| 227 | var[$thisId || '_amount'] = false; |
| 228 | var[$thisId || '_price_in_eth'] = false; |
| 229 | var[$thisId || '_price_in_gb'] = false; |
| 230 | var[$thisId || '_status'] = false; |
| 231 | response['message'] = 'done'; |
| 232 | }" |
| 233 | } |
| 234 | ] |
| 235 | }, |
| 236 | { |
| 237 | "if": "{ |
| 238 | trigger.data.type == 'withdraw_balance' |
| 239 | }", |
| 240 | "init": "{ |
| 241 | if(trigger.address != '2UDX4Q6BPDHVIXIAWZA3RR5OQGKOS6SA') |
| 242 | bounce('You not ownder'); |
| 243 | }", |
| 244 | "messages": [ |
| 245 | { |
| 246 | "app": "payment", |
| 247 | "payload": { |
| 248 | "outputs": [ |
| 249 | { |
| 250 | "address": "{trigger.address}", |
| 251 | "amount": "{ceil(var['balance'])}" |
| 252 | } |
| 253 | ] |
| 254 | } |
| 255 | }, |
| 256 | { |
| 257 | "app": "state", |
| 258 | "state": "{ |
| 259 | var['balance'] = var['balance'] - ceil(var['balance']); |
| 260 | }" |
| 261 | } |
| 262 | ] |
| 263 | } |
| 264 | ] |
| 265 | } |
| 266 | } |
| 267 | ] |