| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $PORTFOLIO_SIZE = 2; |
| 9 | |
| 10 | $asset = var['asset']; |
| 11 | $total_shares = var['total_shares'] OTHERWISE 0; |
| 12 | }", |
| 13 | "messages": { |
| 14 | "cases": [ |
| 15 | { |
| 16 | "if": "{ !$asset }", |
| 17 | "messages": [ |
| 18 | { |
| 19 | "app": "asset", |
| 20 | "payload": { |
| 21 | "is_private": false, |
| 22 | "is_transferrable": true, |
| 23 | "auto_destroy": false, |
| 24 | "fixed_denominations": false, |
| 25 | "issued_by_definer_only": true, |
| 26 | "cosigned_by_definer": false, |
| 27 | "spender_attested": false |
| 28 | } |
| 29 | }, |
| 30 | { |
| 31 | "app": "state", |
| 32 | "state": "{ |
| 33 | var['asset'] = response_unit; |
| 34 | response['message'] = 'Initialized'; |
| 35 | }" |
| 36 | } |
| 37 | ] |
| 38 | }, |
| 39 | { |
| 40 | "if": "{ trigger.data.intent == 'issue' }", |
| 41 | "init": "{ |
| 42 | $total_received_amount = reduce(params.portfolio, $PORTFOLIO_SIZE, ($sum, $portfolio_spec) => { |
| 43 | $amount = trigger.outputs[$portfolio_spec.asset] OTHERWISE 0; |
| 44 | return $sum + $amount; |
| 45 | }, 0); |
| 46 | |
| 47 | $shares = $total_received_amount; |
| 48 | |
| 49 | require($shares > 0, "No portfolio assets received"); |
| 50 | require(is_valid_amount($total_shares + $shares), "Too many asset coins received"); |
| 51 | |
| 52 | foreach(params.portfolio, $PORTFOLIO_SIZE, ($portfolio_spec) => { |
| 53 | $received_amount = trigger.outputs[$portfolio_spec.asset] OTHERWISE 0; |
| 54 | $expected_amount = round($total_received_amount * $portfolio_spec.percentage); |
| 55 | |
| 56 | require($expected_amount > 0, "Not enough assets coins received"); |
| 57 | require($received_amount == $expected_amount, "Incorrect ratio of assets coins received"); |
| 58 | }); |
| 59 | }", |
| 60 | "messages": [ |
| 61 | { |
| 62 | "app": "payment", |
| 63 | "payload": { |
| 64 | "asset": "{$asset}", |
| 65 | "outputs": [ |
| 66 | { |
| 67 | "address": "{trigger.address}", |
| 68 | "amount": "{$shares}" |
| 69 | } |
| 70 | ] |
| 71 | } |
| 72 | }, |
| 73 | { |
| 74 | "app": "state", |
| 75 | "state": "{ |
| 76 | var['total_shares'] += $shares; |
| 77 | response['message'] = 'Shares Issued'; |
| 78 | response['shares'] = $shares; |
| 79 | }" |
| 80 | } |
| 81 | ] |
| 82 | }, |
| 83 | { |
| 84 | "if": "{ trigger.data.intent == 'redeem' }", |
| 85 | "init": "{ |
| 86 | $shares_returned = trigger.output[[asset=$asset]]; |
| 87 | require($shares_returned > 0, "No shares returned"); |
| 88 | |
| 89 | $share = $shares_returned / $total_shares; |
| 90 | $amounts = []; |
| 91 | foreach(params.portfolio, $PORTFOLIO_SIZE, ($portfolio_spec) => { |
| 92 | $amounts[$portfolio_spec.asset] = floor(balance[$portfolio_spec.asset] * $share); |
| 93 | }); |
| 94 | }", |
| 95 | "messages": [ |
| 96 | { |
| 97 | "app": "payment", |
| 98 | "payload": { |
| 99 | "asset": "{params.portfolio[0].asset}", |
| 100 | "outputs": [ |
| 101 | { |
| 102 | "address": "{trigger.address}", |
| 103 | "amount": "{$amounts[params.portfolio[0].asset]}" |
| 104 | } |
| 105 | ] |
| 106 | } |
| 107 | }, |
| 108 | { |
| 109 | "app": "payment", |
| 110 | "payload": { |
| 111 | "asset": "{params.portfolio[1].asset}", |
| 112 | "outputs": [ |
| 113 | { |
| 114 | "address": "{trigger.address}", |
| 115 | "amount": "{$amounts[params.portfolio[1].asset]}" |
| 116 | } |
| 117 | ] |
| 118 | } |
| 119 | }, |
| 120 | { |
| 121 | "app": "state", |
| 122 | "state": "{ |
| 123 | var['total_shares'] -= $shares_returned; |
| 124 | response['message'] = 'Assets Redeemed'; |
| 125 | response['shares'] = $shares_returned; |
| 126 | }" |
| 127 | } |
| 128 | ] |
| 129 | }, |
| 130 | { |
| 131 | "init": "{ bounce('Bad request'); }", |
| 132 | "messages": [ |
| 133 | { |
| 134 | "app": "state", |
| 135 | "state": "{}" |
| 136 | } |
| 137 | ] |
| 138 | } |
| 139 | ] |
| 140 | } |
| 141 | } |
| 142 | ] |