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