| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $POOL_BASE_AA = "5DNICJLNOBPANH536Y7YVBN5ZDBLQ4LE"; |
| 9 | $asset = trigger.output[[asset!="base"]].asset == "none" ? "base" : trigger.output[[asset!="base"]].asset; |
| 10 | |
| 11 | |
| 12 | if (NOT trigger.data["_fwto"]) |
| 13 | bounce("_fwto field is mandatory"); |
| 14 | if (NOT is_valid_address(trigger.data["_fwto"])) |
| 15 | bounce("_fwto field is not a valid address"); |
| 16 | if (trigger.output[[asset!="base"]].asset == "ambiguous") |
| 17 | bounce("AAs cannot handle payments with multiple assets"); |
| 18 | if (NOT trigger.data["_opr"]) |
| 19 | bounce("_opr field is mandatory"); |
| 20 | if (NOT is_valid_amount(trigger.data["_opr"])) |
| 21 | bounce("_opr must be a positive amount"); |
| 22 | if (NOT trigger.data["_pl"]) |
| 23 | bounce("_pl field is mandatory"); |
| 24 | if (NOT is_aa(trigger.data["_pl"])) |
| 25 | bounce("_pl is not a valid oswap pool"); |
| 26 | |
| 27 | |
| 28 | $definition = definition[trigger.data["_pl"]][1]; |
| 29 | if ($definition["base_aa"] != $POOL_BASE_AA) |
| 30 | bounce("That pool version is unsupported"); |
| 31 | |
| 32 | $getAmount = ($outputAmount, $outputReserve, $inputReserve, $fee)=>{ |
| 33 | $swapNoFee = 1e11 - $fee; |
| 34 | $numerator = $outputAmount * $inputReserve * 1e11; |
| 35 | $denominator = ($outputReserve - $outputAmount) * $swapNoFee; |
| 36 | return ceil($numerator / $denominator, 0); |
| 37 | }; |
| 38 | $calculateNeededAmount = ()=>{ |
| 39 | $asset0 = $definition["params"]["asset0"]; |
| 40 | $asset1 = $definition["params"]["asset1"]; |
| 41 | $fee = $definition["params"]["swap_fee"]; |
| 42 | $reserve0 = balance[trigger.data["_pl"]][$asset0]; |
| 43 | $reserve1 = balance[trigger.data["_pl"]][$asset1]; |
| 44 | if ($asset == $asset0) |
| 45 | return $getAmount(trigger.data["_opr"], $reserve1, $reserve0, $fee); |
| 46 | return $getAmount(trigger.data["_opr"], $reserve0, $reserve1, $fee); |
| 47 | }; |
| 48 | $price = $calculateNeededAmount(); |
| 49 | if ($price > ($asset=="base" ? trigger.output[[asset="base"]].amount - 5000 : trigger.output[[asset=$asset]].amount)) |
| 50 | bounce("Not enough amount"); |
| 51 | $remaining = $asset=="base" ? trigger.output[[asset="base"]].amount - $price - 5000 : trigger.output[[asset=$asset]].amount - $price; |
| 52 | }", |
| 53 | "messages": [ |
| 54 | { |
| 55 | "app": "payment", |
| 56 | "payload": { |
| 57 | "asset": "{$asset}", |
| 58 | "outputs": [ |
| 59 | { |
| 60 | "address": "{trigger.data["_loa"] OTHERWISE trigger.initial_address}", |
| 61 | "amount": "{$remaining}" |
| 62 | }, |
| 63 | { |
| 64 | "address": "{trigger.data["_pl"]}", |
| 65 | "amount": "{$price}" |
| 66 | } |
| 67 | ] |
| 68 | } |
| 69 | }, |
| 70 | { |
| 71 | "app": "data", |
| 72 | "payload": { |
| 73 | "to": "{trigger.data["_fwto"]}" |
| 74 | } |
| 75 | } |
| 76 | ] |
| 77 | } |
| 78 | ] |