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