| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | if (NOT exists(trigger.data["method"])) |
| 9 | bounce("method field is mandatory"); |
| 10 | |
| 11 | $method = trigger.data["method"]; |
| 12 | |
| 13 | $spendableFunds = balance["base"] - storage_size; |
| 14 | $owner = var["owner"]; |
| 15 | }", |
| 16 | "getters": "{ |
| 17 | $getCurrency = $ticker=>{ |
| 18 | return var["CURRENCY_" || $ticker]; |
| 19 | }; |
| 20 | $getExchangeRate = $ticker=>{ |
| 21 | $currency = $getCurrency($ticker); |
| 22 | if (NOT $currency) |
| 23 | bounce("That currency was not found"); |
| 24 | $multiplier = $currency.feed2 |
| 25 | ? 1 / data_feed[[oracles=$currency.oracle2, feed_name=$currency.feed2, ifseveral="last"]] * data_feed[[oracles=$currency.oracle1, feed_name=$currency.feed1, ifseveral="last"]] |
| 26 | : data_feed[[oracles=$currency.oracle1, feed_name=$currency.feed1, ifseveral="last"]]; |
| 27 | return $multiplier / 1000000000; |
| 28 | }; |
| 29 | $convert = ($amount, $sourceTicker, $destinationTicker)=>{ |
| 30 | if ($amount < 0) |
| 31 | bounce("You cannot convert a negative amount!"); |
| 32 | |
| 33 | if ($sourceTicker != "GBYTE") |
| 34 | if (NOT var["CURRENCY_" || $sourceTicker]) |
| 35 | bounce("Source currency is unsupported"); |
| 36 | |
| 37 | if ($destinationTicker != "GBYTE") |
| 38 | if (NOT var["CURRENCY_" || $destinationTicker]) |
| 39 | bounce("Destination currency is unsupported"); |
| 40 | |
| 41 | if ($sourceTicker == "GBYTE") |
| 42 | return $amount * (1 / $getExchangeRate($destinationTicker)); |
| 43 | if ($destinationTicker == "GBYTE") |
| 44 | return round($amount * $getExchangeRate($sourceTicker), 0); |
| 45 | else{ |
| 46 | $multiplier = $getExchangeRate($sourceTicker); |
| 47 | $bytes = $amount * $multiplier; |
| 48 | $multiplier2 = 1 / $getExchangeRate($destinationTicker); |
| 49 | return $bytes * $multiplier2; |
| 50 | } |
| 51 | }; |
| 52 | }", |
| 53 | "messages": { |
| 54 | "cases": [ |
| 55 | { |
| 56 | "if": "{ |
| 57 | NOT $owner |
| 58 | }", |
| 59 | "messages": [ |
| 60 | { |
| 61 | "app": "state", |
| 62 | "state": "{ |
| 63 | var["owner"] = "IUU43O7TS2TBYKAPGKUARDZHOTAE275A"; |
| 64 | var["CURRENCY_USD"] = { |
| 65 | oracle1: "F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N", |
| 66 | feed1: "GBYTE_USD", |
| 67 | oracle2: false, |
| 68 | feed2: false |
| 69 | }; |
| 70 | }" |
| 71 | } |
| 72 | ] |
| 73 | }, |
| 74 | { |
| 75 | "if": "{ |
| 76 | trigger.address == $owner |
| 77 | AND ($method == "payout" |
| 78 | OR $method == "transferOwnership" |
| 79 | OR $method == "addCurrency" |
| 80 | OR $method == "delCurrency") |
| 81 | }", |
| 82 | "messages": { |
| 83 | "cases": [ |
| 84 | { |
| 85 | "if": "{ |
| 86 | $method == "payout" |
| 87 | }", |
| 88 | "messages": [ |
| 89 | { |
| 90 | "app": "payment", |
| 91 | "payload": { |
| 92 | "asset": "base", |
| 93 | "outputs": [ |
| 94 | { |
| 95 | "address": "{trigger.address}", |
| 96 | "amount": "{$spendableFunds}" |
| 97 | } |
| 98 | ] |
| 99 | } |
| 100 | } |
| 101 | ] |
| 102 | }, |
| 103 | { |
| 104 | "if": "{ |
| 105 | $method == "transferOwnership" |
| 106 | }", |
| 107 | "init": "{ |
| 108 | if (NOT exists(trigger.data["newOwner"])) |
| 109 | bounce("newOwner field is mandatory"); |
| 110 | if (NOT is_valid_address(trigger.data["newOwner"])) |
| 111 | bounce("newOwner field is not a valid address"); |
| 112 | }", |
| 113 | "messages": [ |
| 114 | { |
| 115 | "app": "payment", |
| 116 | "payload": { |
| 117 | "asset": "base", |
| 118 | "outputs": [ |
| 119 | { |
| 120 | "address": "{trigger.address}", |
| 121 | "amount": "{$spendableFunds}" |
| 122 | } |
| 123 | ] |
| 124 | } |
| 125 | }, |
| 126 | { |
| 127 | "app": "state", |
| 128 | "state": "{ |
| 129 | var["owner"] = trigger.data["newOwner"]; |
| 130 | }" |
| 131 | } |
| 132 | ] |
| 133 | }, |
| 134 | { |
| 135 | "if": "{ |
| 136 | $method == "addCurrency" |
| 137 | }", |
| 138 | "init": "{ |
| 139 | if (NOT exists(trigger.data["ticker"])) |
| 140 | bounce("ticker field is mandatory"); |
| 141 | if (NOT exists(trigger.data["oracle1"])) |
| 142 | bounce("oracle1 field is mandatory"); |
| 143 | if (NOT is_valid_address(trigger.data["oracle1"])) |
| 144 | bounce("oracle1 address is invalid"); |
| 145 | if (NOT exists(trigger.data["feed1"])) |
| 146 | bounce("feed1 field is mandatory"); |
| 147 | if (exists(trigger.data["oracle2"])){ |
| 148 | if (NOT is_valid_address(trigger.data["oracle2"])) |
| 149 | bounce("oracle2 address is invalid"); |
| 150 | if (NOT exists(trigger.data["oracle1"])) |
| 151 | bounce("If you provided oracle2 you have to provide oracle1"); |
| 152 | if (NOT exists(trigger.data["feed2"])) |
| 153 | bounce("If you provided oracle2 you have to provide feed2"); |
| 154 | if (NOT data_feed[[oracles=trigger.data["oracle2"], feed_name=trigger.data["feed2"], ifseveral="last", ifnone=false]]) |
| 155 | bounce("oracle2 does not post that feed"); |
| 156 | } |
| 157 | if (NOT data_feed[[oracles=trigger.data["oracle1"], feed_name=trigger.data["feed1"], ifseveral="last", ifnone=false]]) |
| 158 | bounce("oracle1 does not post that feed"); |
| 159 | }", |
| 160 | "messages": [ |
| 161 | { |
| 162 | "app": "state", |
| 163 | "state": "{ |
| 164 | var["CURRENCY_" || trigger.data["ticker"]] = { |
| 165 | oracle1: trigger.data["oracle1"], |
| 166 | feed1: trigger.data["feed1"], |
| 167 | oracle2: trigger.data["oracle2"], |
| 168 | feed2: trigger.data["feed2"] |
| 169 | }; |
| 170 | }" |
| 171 | } |
| 172 | ] |
| 173 | }, |
| 174 | { |
| 175 | "if": "{ |
| 176 | $method == "delCurrency" |
| 177 | }", |
| 178 | "init": "{ |
| 179 | if (NOT exists(trigger.data["ticker"])) |
| 180 | bounce("ticker field is mandatory"); |
| 181 | if (NOT var[trigger.data["ticker"]]) |
| 182 | bounce("That currency is unknown for the AA"); |
| 183 | }", |
| 184 | "messages": [ |
| 185 | { |
| 186 | "app": "state", |
| 187 | "state": "{ |
| 188 | var["CURRENCY_" || trigger.data["ticker"]] = false; |
| 189 | }" |
| 190 | } |
| 191 | ] |
| 192 | } |
| 193 | ] |
| 194 | } |
| 195 | } |
| 196 | ] |
| 197 | } |
| 198 | } |
| 199 | ] |