| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://oswap.io/initial-sale-pool.json", |
| 5 | "getters": "{ |
| 6 | |
| 7 | $get_prices = () => { |
| 8 | $r = var['total']; |
| 9 | $s = params.token_aa#7.$get_tokens($r); |
| 10 | $final_price = params.token_aa#7.$get_price($s); |
| 11 | $avg_price = $r/$s; |
| 12 | {final_price: $final_price, avg_price: $avg_price} |
| 13 | }; |
| 14 | |
| 15 | }", |
| 16 | "init": "{ |
| 17 | |
| 18 | $reserve_asset = params.reserve_asset OTHERWISE 'base'; |
| 19 | $token_aa = params.token_aa; |
| 20 | |
| 21 | $buy_freeze_period = params.buy_freeze_period OTHERWISE 1; |
| 22 | $buy_freeze_period_in_seconds = $buy_freeze_period * 24 * 3600; |
| 23 | $launch_date = var['launch_date'] OTHERWISE params.launch_date; |
| 24 | $launch_ts = parse_date($launch_date); |
| 25 | |
| 26 | |
| 27 | |
| 28 | $min_contribution = ($reserve_asset == 'base') ? 99999 : 0; |
| 29 | $network_fee = ($reserve_asset == 'base') ? 1000 : 0; |
| 30 | |
| 31 | |
| 32 | if (trigger.data.to AND !is_valid_address(trigger.data.to)) |
| 33 | bounce("bad to address"); |
| 34 | $to = trigger.data.to OTHERWISE trigger.address; |
| 35 | |
| 36 | }", |
| 37 | "messages": { |
| 38 | "cases": [ |
| 39 | { |
| 40 | "if": "{ trigger.output[[asset=$reserve_asset]] > $min_contribution }", |
| 41 | "init": "{ |
| 42 | $ok = timestamp < $launch_ts - $buy_freeze_period_in_seconds; |
| 43 | if (!$ok){ |
| 44 | $error = "no more deposits accepted"; |
| 45 | if (!trigger.data.soft_bounce) |
| 46 | bounce($error); |
| 47 | } |
| 48 | }", |
| 49 | "messages": [ |
| 50 | { |
| 51 | "if": "{$error}", |
| 52 | "app": "payment", |
| 53 | "payload": { |
| 54 | "asset": "{$reserve_asset}", |
| 55 | "outputs": [ |
| 56 | { |
| 57 | "address": "{$to}", |
| 58 | "amount": "{trigger.output[[asset=$reserve_asset]] - 10000}" |
| 59 | } |
| 60 | ] |
| 61 | } |
| 62 | }, |
| 63 | { |
| 64 | "app": "state", |
| 65 | "state": "{ |
| 66 | if ($error) return; |
| 67 | $amount = trigger.output[[asset=$reserve_asset]] - $network_fee; |
| 68 | var['user_'||$to] += $amount; |
| 69 | var['total'] += $amount; |
| 70 | response['added'] = $amount; |
| 71 | }" |
| 72 | } |
| 73 | ] |
| 74 | }, |
| 75 | { |
| 76 | "if": "{ trigger.data.withdraw }", |
| 77 | "init": "{ |
| 78 | require(timestamp < $launch_ts, "withdrawals not allowed any more"); |
| 79 | $balance = var['user_'||trigger.address]; |
| 80 | require($balance, "you have no balance"); |
| 81 | $amount = trigger.data.amount OTHERWISE $balance; |
| 82 | require($amount <= $balance, "amount cannot exceed your balance "||$balance); |
| 83 | }", |
| 84 | "messages": [ |
| 85 | { |
| 86 | "app": "payment", |
| 87 | "payload": { |
| 88 | "asset": "{$reserve_asset}", |
| 89 | "outputs": [ |
| 90 | { |
| 91 | "address": "{trigger.address}", |
| 92 | "amount": "{$amount}" |
| 93 | } |
| 94 | ] |
| 95 | } |
| 96 | }, |
| 97 | { |
| 98 | "app": "state", |
| 99 | "state": "{ |
| 100 | var['user_'||trigger.address] -= $amount; |
| 101 | var['total'] -= $amount; |
| 102 | response['withdrawn'] = $amount; |
| 103 | }" |
| 104 | } |
| 105 | ] |
| 106 | }, |
| 107 | { |
| 108 | "if": "{ trigger.data.buy }", |
| 109 | "init": "{ |
| 110 | require(timestamp >= $launch_ts, "too early"); |
| 111 | require(!var['tokens'], "already bought"); |
| 112 | }", |
| 113 | "messages": [ |
| 114 | { |
| 115 | "app": "payment", |
| 116 | "payload": { |
| 117 | "asset": "{$reserve_asset}", |
| 118 | "outputs": [ |
| 119 | { |
| 120 | "address": "{$token_aa}", |
| 121 | "amount": "{var['total'] + $network_fee}" |
| 122 | } |
| 123 | ] |
| 124 | } |
| 125 | }, |
| 126 | { |
| 127 | "app": "state", |
| 128 | "state": "{ |
| 129 | response['message'] = 'bought'; |
| 130 | }" |
| 131 | } |
| 132 | ] |
| 133 | }, |
| 134 | { |
| 135 | "if": "{ trigger.address == $token_aa }", |
| 136 | "init": "{ |
| 137 | $constants = var[$token_aa]['constants']; |
| 138 | $tokens = trigger.output[[asset=$constants.asset]]; |
| 139 | require($tokens > 0, "no tokens received"); |
| 140 | require(!var['tokens'], "already bought"); |
| 141 | }", |
| 142 | "messages": [ |
| 143 | { |
| 144 | "app": "state", |
| 145 | "state": "{ |
| 146 | var['tokens'] = $tokens; |
| 147 | response['tokens'] = $tokens; |
| 148 | }" |
| 149 | } |
| 150 | ] |
| 151 | }, |
| 152 | { |
| 153 | "if": "{ trigger.data.stake AND trigger.data.group_key AND trigger.data.percentages }", |
| 154 | "init": "{ |
| 155 | require(var['tokens'], "not bought yet"); |
| 156 | $balance = var['user_'||trigger.address]; |
| 157 | require($balance, "you have no balance"); |
| 158 | $amount = floor($balance / var['total'] * var['tokens']); |
| 159 | $constants = var[$token_aa]['constants']; |
| 160 | }", |
| 161 | "messages": [ |
| 162 | { |
| 163 | "app": "payment", |
| 164 | "payload": { |
| 165 | "asset": "{$constants.asset}", |
| 166 | "outputs": [ |
| 167 | { |
| 168 | "address": "{$token_aa}", |
| 169 | "amount": "{$amount}" |
| 170 | } |
| 171 | ] |
| 172 | } |
| 173 | }, |
| 174 | { |
| 175 | "app": "data", |
| 176 | "payload": { |
| 177 | "stake": 1, |
| 178 | "term": "{4*360}", |
| 179 | "group_key": "{trigger.data.group_key}", |
| 180 | "percentages": "{trigger.data.percentages}", |
| 181 | "to": "{trigger.address}" |
| 182 | } |
| 183 | }, |
| 184 | { |
| 185 | "app": "state", |
| 186 | "state": "{ |
| 187 | var['user_'||trigger.address] = false; |
| 188 | response['sent'] = $amount; |
| 189 | }" |
| 190 | } |
| 191 | ] |
| 192 | }, |
| 193 | { |
| 194 | "if": "{ params.admin_address AND trigger.address == params.admin_address AND trigger.data.launch_date }", |
| 195 | "init": "{ |
| 196 | require(!$launch_ts OR timestamp < $launch_ts, "already launched"); |
| 197 | $new_launch_ts = parse_date(trigger.data.launch_date); |
| 198 | require($new_launch_ts > 0, "invalid date"); |
| 199 | require(timestamp < $new_launch_ts - $buy_freeze_period_in_seconds, "new launch date should be at least buy freeze period in the future"); |
| 200 | }", |
| 201 | "messages": [ |
| 202 | { |
| 203 | "app": "state", |
| 204 | "state": "{ |
| 205 | var['launch_date'] = trigger.data.launch_date; |
| 206 | response['new launch date'] = trigger.data.launch_date; |
| 207 | }" |
| 208 | } |
| 209 | ] |
| 210 | } |
| 211 | ] |
| 212 | } |
| 213 | } |
| 214 | ] |