| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $asset = 'n9y3VomFeWFeZZ2PcSEcmyBb/bI7kzZduBJigNetnkY='; |
| 6 | $mm_asset = var['mm_asset']; |
| 7 | }", |
| 8 | "messages": { |
| 9 | "cases": [ |
| 10 | { |
| 11 | "if": "{ trigger.data.define AND !$mm_asset }", |
| 12 | "messages": [ |
| 13 | { |
| 14 | "app": "asset", |
| 15 | "payload": { |
| 16 | "is_private": false, |
| 17 | "is_transferrable": true, |
| 18 | "auto_destroy": false, |
| 19 | "fixed_denominations": false, |
| 20 | "issued_by_definer_only": true, |
| 21 | "cosigned_by_definer": false, |
| 22 | "spender_attested": false |
| 23 | } |
| 24 | }, |
| 25 | { |
| 26 | "app": "state", |
| 27 | "state": "{ |
| 28 | var['mm_asset'] = response_unit; |
| 29 | response['mm_asset'] = response_unit; |
| 30 | }" |
| 31 | } |
| 32 | ] |
| 33 | }, |
| 34 | { |
| 35 | "if": "{$mm_asset AND trigger.output[[asset=base]] > 1e5 AND trigger.output[[asset=$asset]] > 0}", |
| 36 | "init": "{ |
| 37 | $asset_balance = balance[$asset] - trigger.output[[asset=$asset]]; |
| 38 | $bytes_balance = balance[base] - trigger.output[[asset=base]]; |
| 39 | if ($asset_balance == 0 OR $bytes_balance == 0){ |
| 40 | $issue_amount = balance[base]; |
| 41 | return; |
| 42 | } |
| 43 | $current_ratio = $asset_balance / $bytes_balance; |
| 44 | $expected_asset_amount = round($current_ratio * trigger.output[[asset=base]]); |
| 45 | if ($expected_asset_amount != trigger.output[[asset=$asset]]) |
| 46 | bounce('wrong ratio of amounts, expected ' || $expected_asset_amount || ' of asset'); |
| 47 | $investor_share_of_prev_balance = trigger.output[[asset=base]] / $bytes_balance; |
| 48 | $issue_amount = round($investor_share_of_prev_balance * var['mm_asset_outstanding']); |
| 49 | }", |
| 50 | "messages": [ |
| 51 | { |
| 52 | "app": "payment", |
| 53 | "payload": { |
| 54 | "asset": "{$mm_asset}", |
| 55 | "outputs": [ |
| 56 | { |
| 57 | "address": "{trigger.address}", |
| 58 | "amount": "{ $issue_amount }" |
| 59 | } |
| 60 | ] |
| 61 | } |
| 62 | }, |
| 63 | { |
| 64 | "app": "state", |
| 65 | "state": "{ |
| 66 | var['mm_asset_outstanding'] += $issue_amount; |
| 67 | }" |
| 68 | } |
| 69 | ] |
| 70 | }, |
| 71 | { |
| 72 | "if": "{$mm_asset AND trigger.output[[asset=$mm_asset]]}", |
| 73 | "init": "{ |
| 74 | $mm_asset_amount = trigger.output[[asset=$mm_asset]]; |
| 75 | $investor_share = $mm_asset_amount / var['mm_asset_outstanding']; |
| 76 | }", |
| 77 | "messages": [ |
| 78 | { |
| 79 | "app": "payment", |
| 80 | "payload": { |
| 81 | "asset": "{$asset}", |
| 82 | "outputs": [ |
| 83 | { |
| 84 | "address": "{trigger.address}", |
| 85 | "amount": "{ round($investor_share * balance[$asset]) }" |
| 86 | } |
| 87 | ] |
| 88 | } |
| 89 | }, |
| 90 | { |
| 91 | "app": "payment", |
| 92 | "payload": { |
| 93 | "asset": "base", |
| 94 | "outputs": [ |
| 95 | { |
| 96 | "address": "{trigger.address}", |
| 97 | "amount": "{ round($investor_share * balance[base]) }" |
| 98 | } |
| 99 | ] |
| 100 | } |
| 101 | }, |
| 102 | { |
| 103 | "app": "state", |
| 104 | "state": "{ |
| 105 | var['mm_asset_outstanding'] -= trigger.output[[asset=$mm_asset]]; |
| 106 | }" |
| 107 | } |
| 108 | ] |
| 109 | }, |
| 110 | { |
| 111 | "if": "{trigger.output[[asset=base]] > 1e5 AND trigger.output[[asset=$asset]] == 0 AND var['mm_asset_outstanding']}", |
| 112 | "init": "{ |
| 113 | $asset_balance = balance[$asset] - trigger.output[[asset=$asset]]; |
| 114 | $bytes_balance = balance[base] - trigger.output[[asset=base]]; |
| 115 | |
| 116 | $p = $asset_balance * $bytes_balance; |
| 117 | $new_asset_balance = round($p / balance[base]); |
| 118 | $amount = $asset_balance - $new_asset_balance; |
| 119 | }", |
| 120 | "messages": [ |
| 121 | { |
| 122 | "app": "payment", |
| 123 | "payload": { |
| 124 | "asset": "{$asset}", |
| 125 | "outputs": [ |
| 126 | { |
| 127 | "address": "{trigger.address}", |
| 128 | "amount": "{ $amount }" |
| 129 | } |
| 130 | ] |
| 131 | } |
| 132 | } |
| 133 | ] |
| 134 | }, |
| 135 | { |
| 136 | "if": "{trigger.output[[asset=$asset]] > 0 AND var['mm_asset_outstanding']}", |
| 137 | "init": "{ |
| 138 | $asset_balance = balance[$asset] - trigger.output[[asset=$asset]]; |
| 139 | $bytes_balance = balance[base] - trigger.output[[asset=base]]; |
| 140 | |
| 141 | $p = $asset_balance * $bytes_balance; |
| 142 | $new_bytes_balance = round($p / balance[$asset]); |
| 143 | $amount = $bytes_balance - $new_bytes_balance; |
| 144 | }", |
| 145 | "messages": [ |
| 146 | { |
| 147 | "app": "payment", |
| 148 | "payload": { |
| 149 | "asset": "base", |
| 150 | "outputs": [ |
| 151 | { |
| 152 | "address": "{trigger.address}", |
| 153 | "amount": "{ $amount }" |
| 154 | } |
| 155 | ] |
| 156 | } |
| 157 | } |
| 158 | ] |
| 159 | } |
| 160 | ] |
| 161 | } |
| 162 | } |
| 163 | ] |