| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | $FEE = 1000; |
| 11 | $COLAT = 4; |
| 12 | $STABLE_FEE = 0.01; |
| 13 | $addr = trigger.address; |
| 14 | $base_inp = trigger.output[[asset=base]]; |
| 15 | $balance = balance[base]-$base_inp; |
| 16 | if(trigger.output[[asset!=base]].asset != 'none') |
| 17 | $asset = trigger.output[[asset!=base]].asset; |
| 18 | if($asset){ |
| 19 | if($asset == 'ambiguous'){ |
| 20 | bounce("only 1 asset per tx"); |
| 21 | } |
| 22 | if(trigger.output[[asset!=base]] ==0) |
| 23 | bounce("zero asset tx"); |
| 24 | $asset_inp = trigger.output[[asset!=base]]; |
| 25 | } |
| 26 | if(var['stable']){ |
| 27 | if($asset and var['stable'] != $asset) |
| 28 | bounce("not a allowed asset"); |
| 29 | } |
| 30 | $gb_usd = data_feed[[oracles='F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N', feed_name='GBYTE_USD']]; |
| 31 | $ratio = round(10000000/$gb_usd); |
| 32 | $total_val = $balance*$gb_usd/10000000; |
| 33 | $stable_val = var['stable_issued']/100; |
| 34 | $day = substring(timestamp_to_string(timestamp), 8, 2); |
| 35 | $allowed_fee = $day == '01'; |
| 36 | $allowed_deposit = $day == '02'; |
| 37 | |
| 38 | }", |
| 39 | "messages": { |
| 40 | "cases": [ |
| 41 | { |
| 42 | "if": "{!var['stable']}", |
| 43 | "messages": [ |
| 44 | { |
| 45 | "app": "asset", |
| 46 | "payload": { |
| 47 | "is_private": false, |
| 48 | "is_transferrable": true, |
| 49 | "auto_destroy": true, |
| 50 | "fixed_denominations": false, |
| 51 | "issued_by_definer_only": false, |
| 52 | "cosigned_by_definer": false, |
| 53 | "spender_attested": false |
| 54 | } |
| 55 | }, |
| 56 | { |
| 57 | "app": "state", |
| 58 | "state": "{ |
| 59 | var['stable'] = response_unit; |
| 60 | var['stable_issued'] = 0; |
| 61 | var['stable_max'] = 0; |
| 62 | var['colat_issued'] = 0; |
| 63 | }" |
| 64 | } |
| 65 | ] |
| 66 | }, |
| 67 | { |
| 68 | "if": "{ |
| 69 | var['colat_'||$addr] and var['colat_'||$addr] > 0 and trigger.data.take_fees |
| 70 | }", |
| 71 | "init": "{ |
| 72 | if(!$allowed_fee) bounce('Fee collection only allowed first day of month'); |
| 73 | $tot_fees = var['total_fees_lm'] otherwise var['total_fees']; |
| 74 | $FEEs_for_me = round(var['colat'||$addr]/var['colat_issued']*$tot_fees); |
| 75 | $last_pay = attestation[[attestors=this_address, address=$addr]].last_pay; |
| 76 | if($last_pay and timestamp_to_string(timestamp,'date') == $last_pay){ |
| 77 | bounce('Fees for you already taken'); |
| 78 | } |
| 79 | $byte_amount = $base_inp - $FEE + $FEEs_for_me-$alr_wd; |
| 80 | }", |
| 81 | "messages": [ |
| 82 | { |
| 83 | "app": "attestation", |
| 84 | "payload": { |
| 85 | "address": "{$addr}", |
| 86 | "profile": { |
| 87 | "last_pay": "{timestamp_to_string(timestamp,'date')}" |
| 88 | } |
| 89 | } |
| 90 | }, |
| 91 | { |
| 92 | "app": "payment", |
| 93 | "payload": { |
| 94 | "asset": "base", |
| 95 | "outputs": [ |
| 96 | { |
| 97 | "address": "{trigger.address}", |
| 98 | "amount": "{$byte_amount}" |
| 99 | } |
| 100 | ] |
| 101 | } |
| 102 | }, |
| 103 | { |
| 104 | "app": "state", |
| 105 | "state": "{ |
| 106 | if(!var['total_fees_lm'] and var['total_fees'] > 0){ |
| 107 | var['total_fees_lm'] = var['total_fees']; |
| 108 | var['total_fees'] = 0; |
| 109 | } |
| 110 | |
| 111 | }" |
| 112 | } |
| 113 | ] |
| 114 | }, |
| 115 | { |
| 116 | "if": "{ |
| 117 | trigger.data.colat == 'withdraw' |
| 118 | }", |
| 119 | "init": "{ |
| 120 | if(var['stable_issued'] >= var['stable_max']) |
| 121 | bounce('Cant retire colat, please retire centUSD from circulation first'); |
| 122 | $max_to_retire = round((var['stable_max'] - var['stable_issued'])/$gb_usd*10000000/$COLAT); |
| 123 | $state = $total_val - (var['stable_issued']/100*($COLAT+1)); |
| 124 | if($state <= 0) |
| 125 | bounce('Cant retire colat, not enough edge for amount issued'); |
| 126 | $byte_amount = (var['colat_'||$addr]*4); |
| 127 | if($byte_amount > $max_to_retire) |
| 128 | bounce('what to do on this case'); |
| 129 | }", |
| 130 | "messages": [ |
| 131 | { |
| 132 | "app": "payment", |
| 133 | "payload": { |
| 134 | "asset": "base", |
| 135 | "outputs": [ |
| 136 | { |
| 137 | "address": "{trigger.address}", |
| 138 | "amount": "{$base_inp+$byte_amount-$FEE}" |
| 139 | } |
| 140 | ] |
| 141 | } |
| 142 | }, |
| 143 | { |
| 144 | "app": "state", |
| 145 | "state": "{ |
| 146 | var['colat_issued'] -= var['colat_'||$addr]; |
| 147 | var['colat_'||$addr] = false; |
| 148 | var['stable_max'] -= $byte_amount*$gb_usd/10000000; |
| 149 | response['message'] = 'redeemed '||$byte_amount||' colat'; |
| 150 | |
| 151 | |
| 152 | }" |
| 153 | } |
| 154 | ] |
| 155 | }, |
| 156 | { |
| 157 | "if": "{ |
| 158 | !$asset AND trigger.data.colat == 'deposit' |
| 159 | }", |
| 160 | "init": "{ |
| 161 | if(!$allowed_deposit) bounce('Deposit only allowed second day of month'); |
| 162 | $issue_amount = round($base_inp/$COLAT); |
| 163 | }", |
| 164 | "messages": [ |
| 165 | { |
| 166 | "app": "state", |
| 167 | "state": "{ |
| 168 | if(var['total_fees_lm']) var['total_fees_lm'] = false; |
| 169 | var['colat_issued'] += $issue_amount; |
| 170 | var['colat_'||$addr] += $issue_amount; |
| 171 | var['stable_max'] += $issue_amount*$gb_usd; |
| 172 | response['message'] = 'issued '||$issue_amount||' colat'; |
| 173 | }" |
| 174 | } |
| 175 | ] |
| 176 | }, |
| 177 | { |
| 178 | "if": "{ |
| 179 | $asset |
| 180 | }", |
| 181 | "messages": [ |
| 182 | { |
| 183 | "init": "{ |
| 184 | if($day == '01') bounce('Service unavaliable first day of month'); |
| 185 | $byte_amount = round($asset_inp*((1-$STABLE_FEE)*$ratio)); |
| 186 | if($byte_amount < $balance) |
| 187 | $am = $base_inp-$FEE+$byte_amount; |
| 188 | else |
| 189 | $am = $balance-$FEE; |
| 190 | }", |
| 191 | "app": "payment", |
| 192 | "payload": { |
| 193 | "asset": "base", |
| 194 | "outputs": [ |
| 195 | { |
| 196 | "address": "{trigger.address}", |
| 197 | "amount": "{$am}" |
| 198 | } |
| 199 | ] |
| 200 | } |
| 201 | }, |
| 202 | { |
| 203 | "app": "state", |
| 204 | "state": "{ |
| 205 | if(var['total_fees_lm']) var['total_fees_lm'] = false; |
| 206 | var['stable_issued'] -= $asset_inp; |
| 207 | var['total_fees'] += round($asset_inp*($STABLE_FEE*$ratio)); |
| 208 | response['message'] = 'redeemed '||$asset_inp||' centUSD'; |
| 209 | |
| 210 | }" |
| 211 | } |
| 212 | ] |
| 213 | }, |
| 214 | { |
| 215 | "if": "{ |
| 216 | !$asset |
| 217 | }", |
| 218 | "init": "{ |
| 219 | if($day == '01') bounce('Service unavaliable first day of month'); |
| 220 | if(var['stable_issued'] >= var['stable_max']) |
| 221 | bounce('Not enough liquidity, please add collateral'); |
| 222 | $issue_amount = round((($base_inp-$FEE)/$ratio)*(1-$STABLE_FEE)); |
| 223 | if((var['stable_issued']+$issue_amount) >= var['stable_max']) |
| 224 | bounce('Not enough liquidity, max amount available, send less bytes'); |
| 225 | |
| 226 | }", |
| 227 | "messages": [ |
| 228 | { |
| 229 | "app": "payment", |
| 230 | "payload": { |
| 231 | "asset": "{var['stable']}", |
| 232 | "outputs": [ |
| 233 | { |
| 234 | "address": "{$addr}", |
| 235 | "amount": "{$issue_amount}" |
| 236 | } |
| 237 | ] |
| 238 | } |
| 239 | }, |
| 240 | { |
| 241 | "app": "state", |
| 242 | "state": "{ |
| 243 | var['stable_issued'] += $issue_amount; |
| 244 | if(var['total_fees_lm']) var['total_fees_lm'] = false; |
| 245 | var['total_fees'] += round(($base_inp-$FEE)/$ratio*$STABLE_FEE); |
| 246 | response['message'] = 'issued '||$issue_amount||' centUSD'; |
| 247 | }" |
| 248 | } |
| 249 | ] |
| 250 | } |
| 251 | ] |
| 252 | } |
| 253 | } |
| 254 | ] |