| 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 | $owner = var["2NEH2TEG725U3JGJOIE3FCAEGY5HGVFU"]["owner"]; |
| 12 | if (NOT $owner) |
| 13 | bounce("The smart contract has not been initialized yet"); |
| 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 | if ($currency.lastRate) |
| 25 | return $currency.lastRate; |
| 26 | $multiplier = $ticker!="USD" |
| 27 | ? (1 / data_feed[[oracles=$currency.oracle2, feed_name=$currency.feed2, ifseveral="last"]]) * data_feed[[oracles=$currency.oracle1, feed_name=$currency.feed1, ifseveral="last"]] |
| 28 | : data_feed[[oracles=$currency.oracle1, feed_name=$currency.feed1, ifseveral="last"]]; |
| 29 | |
| 30 | if ($ticker != "USD") |
| 31 | return $multiplier; |
| 32 | return (1/$multiplier); |
| 33 | }; |
| 34 | $convert = ($amount, $sourceTicker)=>{ |
| 35 | if ($amount < 0) |
| 36 | bounce("You cannot convert a negative amount"); |
| 37 | $multiplier = $getExchangeRate($sourceTicker); |
| 38 | $bytes = $amount * $multiplier * 1e9; |
| 39 | return round($bytes, 0); |
| 40 | }; |
| 41 | }", |
| 42 | "messages": { |
| 43 | "cases": [ |
| 44 | { |
| 45 | "if": "{NOT var["CURRENCY_USD"]}", |
| 46 | "messages": [ |
| 47 | { |
| 48 | "app": "state", |
| 49 | "state": "{ |
| 50 | var["CURRENCY_USD"] = { |
| 51 | oracle1: "JPQKPRI5FMTQRJF4ZZMYZYDQVRD55OTC", |
| 52 | feed1: "GBYTE_USD", |
| 53 | oracle2: false, |
| 54 | feed2: false |
| 55 | }; |
| 56 | }" |
| 57 | } |
| 58 | ] |
| 59 | }, |
| 60 | { |
| 61 | "if": "{ |
| 62 | trigger.address == $owner |
| 63 | AND ($method == "payout" |
| 64 | OR $method == "addCurrency" |
| 65 | OR $method == "delCurrency") |
| 66 | }", |
| 67 | "messages": { |
| 68 | "cases": [ |
| 69 | { |
| 70 | "if": "{$method == "payout"}", |
| 71 | "messages": [ |
| 72 | { |
| 73 | "app": "payment", |
| 74 | "payload": { |
| 75 | "asset": "base", |
| 76 | "outputs": [ |
| 77 | { |
| 78 | "address": "{trigger.address}" |
| 79 | } |
| 80 | ] |
| 81 | } |
| 82 | } |
| 83 | ] |
| 84 | }, |
| 85 | { |
| 86 | "if": "{$method == "addCurrency"}", |
| 87 | "init": "{ |
| 88 | if (NOT exists(trigger.data["ticker"])) |
| 89 | bounce("ticker field is mandatory"); |
| 90 | if (NOT exists(trigger.data["oracle1"])) |
| 91 | bounce("oracle1 field is mandatory"); |
| 92 | if (NOT is_valid_address(trigger.data["oracle1"])) |
| 93 | bounce("oracle1 address is invalid"); |
| 94 | if (NOT exists(trigger.data["feed1"])) |
| 95 | bounce("feed1 field is mandatory"); |
| 96 | if (exists(trigger.data["oracle2"])){ |
| 97 | if (NOT is_valid_address(trigger.data["oracle2"])) |
| 98 | bounce("oracle2 address is invalid"); |
| 99 | if (NOT exists(trigger.data["oracle1"])) |
| 100 | bounce("If you provided oracle2 you have to provide oracle1"); |
| 101 | if (NOT exists(trigger.data["feed2"])) |
| 102 | bounce("If you provided oracle2 you have to provide feed2"); |
| 103 | if (NOT data_feed[[oracles=trigger.data["oracle2"], feed_name=trigger.data["feed2"], ifseveral="last", ifnone=false]]) |
| 104 | bounce("oracle2 does not post that feed"); |
| 105 | } |
| 106 | if (NOT data_feed[[oracles=trigger.data["oracle1"], feed_name=trigger.data["feed1"], ifseveral="last", ifnone=false]]) |
| 107 | bounce("oracle1 does not post that feed"); |
| 108 | }", |
| 109 | "messages": [ |
| 110 | { |
| 111 | "app": "state", |
| 112 | "state": "{ |
| 113 | var["DEPRECATED_" || trigger.data["ticker"]] = false; |
| 114 | var["CURRENCY_" || trigger.data["ticker"]] = { |
| 115 | oracle1: trigger.data["oracle1"], |
| 116 | feed1: trigger.data["feed1"], |
| 117 | oracle2: trigger.data["oracle2"], |
| 118 | feed2: trigger.data["feed2"] |
| 119 | }; |
| 120 | }" |
| 121 | } |
| 122 | ] |
| 123 | }, |
| 124 | { |
| 125 | "if": "{$method == "delCurrency"}", |
| 126 | "init": "{ |
| 127 | if (NOT exists(trigger.data["ticker"])) |
| 128 | bounce("ticker field is mandatory"); |
| 129 | if (NOT var["CURRENCY_" || trigger.data["ticker"]]) |
| 130 | bounce("That currency is unknown for the AA"); |
| 131 | }", |
| 132 | "messages": [ |
| 133 | { |
| 134 | "app": "state", |
| 135 | "state": "{ |
| 136 | var["DEPRECATED_" || trigger.data["ticker"]] = var["CURRENCY_" || trigger.data["ticker"]] || {lastRate: $getExchangeRate(trigger.data["ticker"])}; |
| 137 | var["CURRENCY_" || trigger.data["ticker"]] = false; |
| 138 | }" |
| 139 | } |
| 140 | ] |
| 141 | } |
| 142 | ] |
| 143 | } |
| 144 | } |
| 145 | ] |
| 146 | } |
| 147 | } |
| 148 | ] |