| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $return_fee = 1000; |
| 6 | $balance_key = 'balance_'||trigger.address; |
| 7 | $timestamp_key = 'timestamp_'||trigger.address; |
| 8 | }", |
| 9 | "messages": { |
| 10 | "cases": [ |
| 11 | { |
| 12 | "if": "{!trigger.data.operation}", |
| 13 | "messages": [ |
| 14 | { |
| 15 | "app": "state", |
| 16 | "state": "{ |
| 17 | bounce("Welcome user! Please add 'operation' data field with 'keep' or 'return' values. Otherwise the transaction will be bounced."); |
| 18 | }" |
| 19 | } |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "if": "{trigger.data.operation == 'keep'}", |
| 24 | "init": "{ |
| 25 | if (!var[$balance_key] AND !trigger.data.unlock_date) bounce("Please add 'unlock_date' data field with date format: YYYY-MM-DD."); |
| 26 | if (!var[$balance_key] AND trigger.data.unlock_date AND typeof(parse_date(trigger.data.unlock_date)) != "number") bounce("The format of the 'unlock_date' field is YYYY-MM-DD."); |
| 27 | if (var[$balance_key] AND trigger.data.unlock_date) |
| 28 | { |
| 29 | if (parse_date(trigger.data.unlock_date) != var[$timestamp_key]) bounce("This address have been already deposited before with 'unlock_date' set to " || timestamp_to_string(var[$timestamp_key], 'date') || ". You can't change the date. To increase the balance of this address either remove 'unlock_date' data field or make it the same as before."); |
| 30 | } |
| 31 | }", |
| 32 | "messages": [ |
| 33 | { |
| 34 | "app": "state", |
| 35 | "state": "{ |
| 36 | if (!var[$balance_key]) |
| 37 | var[$timestamp_key] = parse_date(trigger.data.unlock_date); |
| 38 | var[$balance_key] = var[$balance_key] + trigger.output[[asset=base]]; |
| 39 | response['message'] = "Total balance will be "|| var[$balance_key] || " bytes. The bytes will be locked till "|| trigger.data.unlock_date || " 0:00 UTC. You can withdraw your bytes only from the address you made this deposit: " || trigger.address || ". Please make a wallet backup and store it in a safe place."; |
| 40 | }" |
| 41 | } |
| 42 | ] |
| 43 | }, |
| 44 | { |
| 45 | "if": "{trigger.data.operation == 'return'}", |
| 46 | "init": "{ |
| 47 | if (!var[$balance_key]) bounce('No bytes to return for this address.'); |
| 48 | if (var[$timestamp_key] > timestamp) bounce('The unlock date has not come yet. Bytes are locked till '|| timestamp_to_string(var[$timestamp_key], 'date') || ' 0:00 UTC.'); |
| 49 | |
| 50 | }", |
| 51 | "messages": [ |
| 52 | { |
| 53 | "app": "payment", |
| 54 | "payload": { |
| 55 | "asset": "base", |
| 56 | "outputs": [ |
| 57 | { |
| 58 | "address": "{trigger.address}", |
| 59 | "amount": "{ var[$balance_key] - $return_fee}" |
| 60 | } |
| 61 | ] |
| 62 | } |
| 63 | }, |
| 64 | { |
| 65 | "app": "state", |
| 66 | "state": "{ |
| 67 | response['message'] = var[$balance_key] || ' bytes will be returned. Transaction fee is ' || $return_fee || ' bytes. You will receive ' || (var[$balance_key] - $return_fee) || ' bytes to your account.'; |
| 68 | var[$balance_key] = false; |
| 69 | var[$timestamp_key] = false; |
| 70 | }" |
| 71 | } |
| 72 | ] |
| 73 | }, |
| 74 | { |
| 75 | "messages": [ |
| 76 | { |
| 77 | "app": "state", |
| 78 | "state": "{ |
| 79 | bounce("'operation' data field can have only 'keep' or 'return' values."); |
| 80 | }" |
| 81 | } |
| 82 | ] |
| 83 | } |
| 84 | ] |
| 85 | } |
| 86 | } |
| 87 | ] |