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