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