| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "getters": "{ |
| 5 | $getPrice = ()=>{ |
| 6 | $priceStr = data_feed[[oracles=params.issuer, feed_name=this_address, ifseveral="last", type="string"]]; |
| 7 | $chunks = split($priceStr, " "); |
| 8 | $price = $chunks[0]; |
| 9 | return $price; |
| 10 | }; |
| 11 | $getPriceAsset = ()=>{ |
| 12 | $priceStr = data_feed[[oracles=params.issuer, feed_name=this_address, ifseveral="last", type="string"]]; |
| 13 | $chunks = split($priceStr, " "); |
| 14 | $asset = $chunks[0]; |
| 15 | return $asset; |
| 16 | }; |
| 17 | $getSales = ()=>var["sales"]; |
| 18 | }", |
| 19 | "init": "{ |
| 20 | $CT_ADDRESS = "ZIT3YPDTLGAO7MVO2ONNDMSEYS2VELX6"; |
| 21 | $OWNER = params.issuer; |
| 22 | $LICENSE_ASSET = var["licenseAsset"]; |
| 23 | if ($LICENSE_ASSET){ |
| 24 | $priceStr = data_feed[[oracles=$OWNER, feed_name=this_address, ifseveral="last", type="string"]]; |
| 25 | $chunks = split($priceStr, " "); |
| 26 | require(length($chunks) == 2, "Malformed data_feed. It should have only 2 items"); |
| 27 | $PRICE = $chunks[0]; |
| 28 | require(is_integer($PRICE), "Price must be an integer"); |
| 29 | require($PRICE > 0, "Price must be positive"); |
| 30 | require(is_valid_amount($PRICE), "Price must be a valid amount"); |
| 31 | $PRICE_ASSET = $chunks[1]; |
| 32 | require(asset[$PRICE_ASSET].exists, "Price asset does not exist"); |
| 33 | } |
| 34 | $method = trigger.data["method"]; |
| 35 | response["method"] = $method; |
| 36 | response["who"] = trigger.data["to"] OTHERWISE trigger.address; |
| 37 | $CUT = 0.03; |
| 38 | if ($LICENSE_ASSET AND NOT $method) |
| 39 | bounce("method field is mandatory"); |
| 40 | }", |
| 41 | "messages": { |
| 42 | "cases": [ |
| 43 | { |
| 44 | "if": "{NOT $LICENSE_ASSET}", |
| 45 | "init": "{ |
| 46 | response["method"] = "initialize"; |
| 47 | }", |
| 48 | "messages": [ |
| 49 | { |
| 50 | "if": "{params.cap}", |
| 51 | "app": "asset", |
| 52 | "payload": { |
| 53 | "is_private": false, |
| 54 | "cap": "{params.cap OTHERWISE 0}", |
| 55 | "is_transferrable": "{NOT NOT params.tradeable}", |
| 56 | "auto_destroy": false, |
| 57 | "fixed_denominations": false, |
| 58 | "issued_by_definer_only": true, |
| 59 | "cosigned_by_definer": false, |
| 60 | "spender_attested": false |
| 61 | } |
| 62 | }, |
| 63 | { |
| 64 | "if": "{NOT params.cap}", |
| 65 | "app": "asset", |
| 66 | "payload": { |
| 67 | "is_private": false, |
| 68 | "is_transferrable": "{NOT NOT params.tradeable}", |
| 69 | "auto_destroy": false, |
| 70 | "fixed_denominations": false, |
| 71 | "issued_by_definer_only": true, |
| 72 | "cosigned_by_definer": false, |
| 73 | "spender_attested": false |
| 74 | } |
| 75 | }, |
| 76 | { |
| 77 | "app": "state", |
| 78 | "state": "{ |
| 79 | var["licenseAsset"] = response_unit; |
| 80 | }" |
| 81 | } |
| 82 | ] |
| 83 | }, |
| 84 | { |
| 85 | "if": "{trigger.address == $OWNER AND $method == "payout"}", |
| 86 | "messages": { |
| 87 | "cases": [ |
| 88 | { |
| 89 | "if": "{$method == "payout"}", |
| 90 | "messages": [ |
| 91 | { |
| 92 | "app": "payment", |
| 93 | "payload": { |
| 94 | "asset": "{trigger.data.asset OTHERWISE $PRICE_ASSET}", |
| 95 | "outputs": [ |
| 96 | { |
| 97 | "address": "{$OWNER}" |
| 98 | } |
| 99 | ] |
| 100 | } |
| 101 | } |
| 102 | ] |
| 103 | } |
| 104 | ] |
| 105 | } |
| 106 | }, |
| 107 | { |
| 108 | "if": "{$method == "buy"}", |
| 109 | "init": "{ |
| 110 | $copies = floor(trigger.output[[asset=$PRICE_ASSET]]/$PRICE, 0); |
| 111 | require($copies > 0, "You cannot buy less than 1 license"); |
| 112 | $hasReferral = (trigger.data["r"] AND is_valid_address(trigger.data["r"])); |
| 113 | $cut = $hasReferral ? $CUT/2 : $CUT; |
| 114 | $cutAmount = round(trigger.output[[asset=$PRICE_ASSET]] * $cut, 0); |
| 115 | response["amount"] = $copies; |
| 116 | }", |
| 117 | "messages": [ |
| 118 | { |
| 119 | "app": "payment", |
| 120 | "payload": { |
| 121 | "asset": "{$LICENSE_ASSET}", |
| 122 | "outputs": [ |
| 123 | { |
| 124 | "address": "{trigger.data["to"] OTHERWISE trigger.initial_address}", |
| 125 | "amount": "{$copies}" |
| 126 | } |
| 127 | ] |
| 128 | } |
| 129 | }, |
| 130 | { |
| 131 | "app": "payment", |
| 132 | "payload": { |
| 133 | "asset": "{$PRICE_ASSET}", |
| 134 | "outputs": [ |
| 135 | { |
| 136 | "address": "{$CT_ADDRESS}", |
| 137 | "amount": "{$cutAmount}" |
| 138 | }, |
| 139 | { |
| 140 | "if": "{$hasReferral}", |
| 141 | "address": "{trigger.data["r"]}", |
| 142 | "amount": "{$cutAmount}" |
| 143 | } |
| 144 | ] |
| 145 | } |
| 146 | }, |
| 147 | { |
| 148 | "app": "state", |
| 149 | "state": "{ |
| 150 | var["sales"]+=1; |
| 151 | }" |
| 152 | } |
| 153 | ] |
| 154 | } |
| 155 | ] |
| 156 | } |
| 157 | } |
| 158 | ] |