| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $type = trigger.data.type; |
| 9 | if(!exists($type)){ |
| 10 | bounce("'type' required"); |
| 11 | } |
| 12 | $owner = params.owner; |
| 13 | $asset = params.asset; |
| 14 | $goal = params.goal; |
| 15 | $days = params.days; |
| 16 | $unit = params.unit; |
| 17 | $start_timestamp = params.timestamp; |
| 18 | $balance = var['balance'] OTHERWISE 0; |
| 19 | }", |
| 20 | "messages": { |
| 21 | "cases": [ |
| 22 | { |
| 23 | "if": "{$type == 'addReward'}", |
| 24 | "init": "{ |
| 25 | if($owner != trigger.address){ |
| 26 | bounce("you are not admin"); |
| 27 | } |
| 28 | |
| 29 | $unit_data = unit[$unit].messages[[.app='data']].payload; |
| 30 | $amount = trigger.data.amount; |
| 31 | |
| 32 | if(exists($unit_data['reward_' || $amount]) OR exists(var['reward_' || $amount])){ |
| 33 | bounce('reward already exists'); |
| 34 | } |
| 35 | }", |
| 36 | "messages": [ |
| 37 | { |
| 38 | "app": "state", |
| 39 | "state": "{ |
| 40 | var['reward_' || $amount] = 1; |
| 41 | response['result'] = 'new_reward:' || $amount; |
| 42 | }" |
| 43 | } |
| 44 | ] |
| 45 | }, |
| 46 | { |
| 47 | "if": "{$type == 'buy'}", |
| 48 | "init": "{ |
| 49 | $amount = trigger.output[[asset=$asset]]; |
| 50 | if(!exists($amount)){ |
| 51 | if(exists(var['O6H6ZIFI57X3PLTYHOCVYPP5A553CYFQ']['a2s_' || $asset])) { |
| 52 | $name = var['O6H6ZIFI57X3PLTYHOCVYPP5A553CYFQ']['a2s_' || $asset]; |
| 53 | } else { |
| 54 | $name = $asset; |
| 55 | } |
| 56 | bounce("Donations can only occur in " || $name); |
| 57 | } |
| 58 | $unit_data = unit[$unit].messages[[.app='data']].payload; |
| 59 | $unit_reward = $unit_data['reward_' || $amount]; |
| 60 | $local_reward = var['reward_' || $amount]; |
| 61 | $finish_timestamp = (86400 * $days) + $start_timestamp; |
| 62 | |
| 63 | if(timestamp > $finish_timestamp){ |
| 64 | bounce("project finished"); |
| 65 | } |
| 66 | }", |
| 67 | "messages": [ |
| 68 | { |
| 69 | "app": "state", |
| 70 | "state": "{ |
| 71 | if(!exists($unit_reward) AND !exists($local_reward)){ |
| 72 | response['result'] = 'gratuitous donation'; |
| 73 | } else { |
| 74 | response['result'] = 'reward_amount'; |
| 75 | } |
| 76 | var['balance'] += $amount; |
| 77 | var['amount_' || trigger.address] += $amount; |
| 78 | response['amount'] = $amount; |
| 79 | }" |
| 80 | } |
| 81 | ] |
| 82 | }, |
| 83 | { |
| 84 | "if": "{$type == 'withdraw'}", |
| 85 | "init": "{ |
| 86 | if($owner != trigger.address){ |
| 87 | bounce("you are not admin"); |
| 88 | } |
| 89 | |
| 90 | if($balance < $goal){ |
| 91 | bounce("goal not reached"); |
| 92 | } |
| 93 | |
| 94 | $withdrawn = var['withdrawn'] OTHERWISE 0; |
| 95 | |
| 96 | if($balance - $withdrawn <= 0){ |
| 97 | bounce('not enough money'); |
| 98 | } |
| 99 | }", |
| 100 | "messages": [ |
| 101 | { |
| 102 | "app": "payment", |
| 103 | "payload": { |
| 104 | "asset": "{$asset}", |
| 105 | "outputs": [ |
| 106 | { |
| 107 | "address": "{trigger.address}", |
| 108 | "amount": "{$balance - $withdrawn}" |
| 109 | } |
| 110 | ] |
| 111 | } |
| 112 | }, |
| 113 | { |
| 114 | "app": "state", |
| 115 | "state": "{ |
| 116 | var['withdrawn'] += $balance - $withdrawn; |
| 117 | }" |
| 118 | } |
| 119 | ] |
| 120 | }, |
| 121 | { |
| 122 | "if": "{$type == 'refund'}", |
| 123 | "init": "{ |
| 124 | $finish_timestamp = (86400 * $days) + $start_timestamp; |
| 125 | |
| 126 | if(timestamp < $finish_timestamp){ |
| 127 | bounce("project didn't finish"); |
| 128 | } |
| 129 | |
| 130 | if($balance >= $goal){ |
| 131 | bounce("goal achieved"); |
| 132 | } |
| 133 | |
| 134 | $peer_amount = var['amount_' || trigger.address] OTHERWISE 0; |
| 135 | |
| 136 | if($peer_amount <= 0){ |
| 137 | bounce("I do not have your money"); |
| 138 | } |
| 139 | }", |
| 140 | "messages": [ |
| 141 | { |
| 142 | "app": "payment", |
| 143 | "payload": { |
| 144 | "asset": "{$asset}", |
| 145 | "outputs": [ |
| 146 | { |
| 147 | "address": "{trigger.address}", |
| 148 | "amount": "{$peer_amount}" |
| 149 | } |
| 150 | ] |
| 151 | } |
| 152 | }, |
| 153 | { |
| 154 | "app": "state", |
| 155 | "state": "{ |
| 156 | var['amount_' || trigger.address] = false; |
| 157 | response['result'] = 'done'; |
| 158 | }" |
| 159 | } |
| 160 | ] |
| 161 | } |
| 162 | ] |
| 163 | } |
| 164 | } |
| 165 | ] |