| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $pool_aa = params.pool_aa; |
| 6 | $fee = 2000; |
| 7 | $pool_params = definition[$pool_aa][1].params; |
| 8 | $asset0 = $pool_params.asset0; |
| 9 | $asset1 = $pool_params.asset1; |
| 10 | $amount0 = trigger.output[[asset=$asset0]] - ($asset0 == 'base' ? $fee : 0); |
| 11 | $amount1 = trigger.output[[asset=$asset1]] - ($asset1 == 'base' ? $fee : 0); |
| 12 | if ($amount0 <= 0) |
| 13 | bounce("amount0 = " || $amount0); |
| 14 | if ($amount1 <= 0) |
| 15 | bounce("amount1 = " || $amount1); |
| 16 | $reserve0 = balance[$pool_aa][$asset0]; |
| 17 | $reserve1 = balance[$pool_aa][$asset1]; |
| 18 | $ratio = $reserve1 / $reserve0; |
| 19 | $expectedAmount1 = round($ratio * $amount0); |
| 20 | if ($amount1 > $expectedAmount1){ |
| 21 | $change0 = 0; |
| 22 | $change1 = $amount1 - $expectedAmount1; |
| 23 | } |
| 24 | else if ($amount1 < $expectedAmount1){ |
| 25 | $used_amount0 = floor($amount1 / $ratio); |
| 26 | if ($used_amount0 > $amount0) |
| 27 | bounce("BUG: " || $used_amount0 || " > " || $amount0); |
| 28 | $newExpectedAmount1 = round($ratio * $used_amount0); |
| 29 | if ($amount1 < $newExpectedAmount1) |
| 30 | bounce("rounding issue: adjusted amount0 from " || $amount0 || " to " || $used_amount0 || " but amount1 " || $amount1 || " is still less than the new expected amount1 " || $newExpectedAmount1); |
| 31 | $change0 = $amount0 - $used_amount0; |
| 32 | $change1 = $amount1 - $newExpectedAmount1; |
| 33 | } |
| 34 | else{ |
| 35 | $change0 = 0; |
| 36 | $change1 = 0; |
| 37 | } |
| 38 | }", |
| 39 | "messages": [ |
| 40 | { |
| 41 | "app": "payment", |
| 42 | "payload": { |
| 43 | "asset": "{$asset0}", |
| 44 | "outputs": [ |
| 45 | { |
| 46 | "address": "{$pool_aa}", |
| 47 | "amount": "{$amount0 - $change0}" |
| 48 | }, |
| 49 | { |
| 50 | "address": "{trigger.address}", |
| 51 | "amount": "{$change0}" |
| 52 | } |
| 53 | ] |
| 54 | } |
| 55 | }, |
| 56 | { |
| 57 | "app": "payment", |
| 58 | "payload": { |
| 59 | "asset": "{$asset1}", |
| 60 | "outputs": [ |
| 61 | { |
| 62 | "address": "{$pool_aa}", |
| 63 | "amount": "{$amount1 - $change1}" |
| 64 | }, |
| 65 | { |
| 66 | "address": "{trigger.address}", |
| 67 | "amount": "{$change1}" |
| 68 | } |
| 69 | ] |
| 70 | } |
| 71 | }, |
| 72 | { |
| 73 | "if": "{$asset0 != 'base' AND $asset1 != 'base'}", |
| 74 | "app": "payment", |
| 75 | "payload": { |
| 76 | "asset": "base", |
| 77 | "outputs": [ |
| 78 | { |
| 79 | "address": "{$pool_aa}" |
| 80 | } |
| 81 | ] |
| 82 | } |
| 83 | } |
| 84 | ] |
| 85 | } |
| 86 | ] |