| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $lock_time = 7 * 24 * 3600; |
| 6 | $registry_aa = "O6H6ZIFI57X3PLTYHOCVYPP5A553CYFQ"; |
| 7 | $address = trigger.address; |
| 8 | if (is_aa($address)) |
| 9 | bounce("Asset must be sent from a plain wallet, not an AA"); |
| 10 | }", |
| 11 | "messages": { |
| 12 | "cases": [ |
| 13 | { |
| 14 | "if": "{trigger.output[[asset!=base]].amount > 0}", |
| 15 | "init": "{ |
| 16 | $received_asset = trigger.output[[asset!=base]].asset; |
| 17 | if ($received_asset == "ambiguous") |
| 18 | bounce("Different assets cannot be handled at same time"); |
| 19 | }", |
| 20 | "messages": [ |
| 21 | { |
| 22 | "app": "state", |
| 23 | "state": "{ |
| 24 | var["amount_" || $received_asset || "_" || $address] += trigger.output[[asset!=base]].amount; |
| 25 | var["ts_" || $received_asset || "_" || $address] = timestamp; |
| 26 | response['info'] = "This asset will be locked until " || timestamp_to_string(timestamp + $lock_time); |
| 27 | }" |
| 28 | } |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "if": "{trigger.data.withdraw}", |
| 33 | "init": "{ |
| 34 | if (asset[trigger.data.withdraw].exists) |
| 35 | $asset = trigger.data.withdraw; |
| 36 | else { |
| 37 | $asset = var[$registry_aa]["s2a_" || trigger.data.withdraw]; |
| 38 | if (!$asset) |
| 39 | bounce("unknown symbol"); |
| 40 | } |
| 41 | $amount_available = var["amount_" || $asset || "_" || $address]; |
| 42 | if (!$amount_available) |
| 43 | bounce("You don't have deposit for this asset"); |
| 44 | $lock_timestamp = var["ts_" || $asset || "_" || $address]; |
| 45 | if ($lock_timestamp AND $lock_timestamp > timestamp - $lock_time) |
| 46 | bounce("This asset is still locked, retry in " || $lock_time / 3600 || ' hours'); |
| 47 | if (!$amount_available) |
| 48 | bounce("Nothing available for withdraw"); |
| 49 | if (trigger.data.amount){ |
| 50 | if (!is_valid_amount(trigger.data.amount)) |
| 51 | bounce("Invalid amount"); |
| 52 | if(trigger.data.amount < $amount_available) |
| 53 | bounce("Only " || $amount_available || " available for withdraw"); |
| 54 | $amount = trigger.data.amount; |
| 55 | } else { |
| 56 | $amount = $amount_available; |
| 57 | } |
| 58 | }", |
| 59 | "messages": [ |
| 60 | { |
| 61 | "app": "payment", |
| 62 | "payload": { |
| 63 | "asset": "{$asset}", |
| 64 | "outputs": [ |
| 65 | { |
| 66 | "address": "{$address}", |
| 67 | "amount": "{ $amount }" |
| 68 | } |
| 69 | ] |
| 70 | } |
| 71 | }, |
| 72 | { |
| 73 | "app": "state", |
| 74 | "state": "{ |
| 75 | var["amount_" || $asset || "_" || $address] -= $amount; |
| 76 | var["ts_" || $asset || "_" || $address] = false; |
| 77 | }" |
| 78 | } |
| 79 | ] |
| 80 | } |
| 81 | ] |
| 82 | } |
| 83 | } |
| 84 | ] |