| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://oswap.io/pool.json", |
| 5 | "init": "{ |
| 6 | $bounceFees = 1e4; |
| 7 | $minLiquidity = 1e3; |
| 8 | $factory = params.factory; |
| 9 | $asset0 = params.asset0; |
| 10 | $asset1 = params.asset1; |
| 11 | $swapFee = params.swap_fee; |
| 12 | $swapNoFee = 1e11 - $swapFee; |
| 13 | $asset = var[$factory]['pools.' || this_address || '.asset']; |
| 14 | $supply = var['supply']; |
| 15 | $amount0 = trigger.output[[asset=$asset0]]; |
| 16 | $amount1 = trigger.output[[asset=$asset1]]; |
| 17 | $amount0NoFees = ($asset0 == 'base') ? $amount0 - $bounceFees : $amount0; |
| 18 | $amount1NoFees = ($asset1 == 'base') ? $amount1 - $bounceFees : $amount1; |
| 19 | $reserve0 = balance[$asset0] - $amount0; |
| 20 | $reserve1 = balance[$asset1] - $amount1; |
| 21 | if (trigger.data.to AND !is_valid_address(trigger.data.to)) bounce('invalid recipient address'); |
| 22 | if (trigger.data.to_aa AND !is_aa(trigger.data.to_aa)) bounce('invalid aa address'); |
| 23 | $to = trigger.data.to OTHERWISE trigger.initial_address; |
| 24 | $toAA = trigger.data.to_aa; |
| 25 | $deadline = trigger.data.deadline; |
| 26 | if ($deadline AND $deadline < timestamp) bounce('expired'); |
| 27 | }", |
| 28 | "messages": { |
| 29 | "cases": [ |
| 30 | { |
| 31 | "if": "{trigger.data.info}", |
| 32 | "messages": [ |
| 33 | { |
| 34 | "app": "state", |
| 35 | "state": "{ |
| 36 | response['factory'] = $factory; |
| 37 | response['asset0'] = $asset0; |
| 38 | response['asset1'] = $asset1; |
| 39 | response['swap_fee'] = $swapFee; |
| 40 | response['reserve0'] = $reserve0; |
| 41 | response['reserve1'] = $reserve1; |
| 42 | response['asset'] = $asset; |
| 43 | response['supply'] = $supply; |
| 44 | }" |
| 45 | } |
| 46 | ] |
| 47 | }, |
| 48 | { |
| 49 | "if": "{!$asset AND trigger.data.initialize AND trigger.address == $factory}", |
| 50 | "messages": [ |
| 51 | { |
| 52 | "app": "asset", |
| 53 | "payload": { |
| 54 | "is_private": false, |
| 55 | "is_transferrable": true, |
| 56 | "auto_destroy": false, |
| 57 | "fixed_denominations": false, |
| 58 | "issued_by_definer_only": true, |
| 59 | "cosigned_by_definer": false, |
| 60 | "spender_attested": false |
| 61 | } |
| 62 | }, |
| 63 | { |
| 64 | "app": "payment", |
| 65 | "payload": { |
| 66 | "asset": "base", |
| 67 | "outputs": [ |
| 68 | { |
| 69 | "address": "{$factory}" |
| 70 | } |
| 71 | ] |
| 72 | } |
| 73 | }, |
| 74 | { |
| 75 | "app": "data", |
| 76 | "payload": { |
| 77 | "finalize": "1" |
| 78 | } |
| 79 | }, |
| 80 | { |
| 81 | "app": "state", |
| 82 | "state": "{response['type'] = 'initialize';}" |
| 83 | } |
| 84 | ] |
| 85 | }, |
| 86 | { |
| 87 | "if": "{ |
| 88 | $both = ($amount0NoFees > 0 AND $amount1NoFees > 0); |
| 89 | $asset0Only = ($amount0NoFees > 0 AND $amount1NoFees <= 0); |
| 90 | $asset1Only = ($amount1NoFees > 0 AND $amount0NoFees <= 0); |
| 91 | $asset AND ($both OR trigger.data.mint AND ($asset0Only OR $asset1Only)) |
| 92 | }", |
| 93 | "init": "{ |
| 94 | if ( |
| 95 | ($both AND ($amount0NoFees * $amount1NoFees) < $minLiquidity) |
| 96 | OR (($asset0Only OR $asset1Only) AND ($amount0NoFees + $amount1NoFees) < $minLiquidity) |
| 97 | ) |
| 98 | bounce('not enough liquidity, minimum is ' || $minLiquidity); |
| 99 | if ($reserve1 == 0 OR $reserve0 == 0) { |
| 100 | if (!$both) bounce('initial contribution must have both tokens'); |
| 101 | $minted = balance[$asset0]; |
| 102 | return; |
| 103 | } |
| 104 | if ($both) { |
| 105 | $ratio = $reserve1 / $reserve0; |
| 106 | $expectedAmount1 = round($ratio * $amount0); |
| 107 | if ($expectedAmount1 != $amount1 OR $expectedAmount1 == 0) |
| 108 | bounce('wrong ratio of amounts, expected ' || $expectedAmount1 || ' of ' || $asset1); |
| 109 | $share = $amount0 / $reserve0; |
| 110 | } else if ($asset0Only) { |
| 111 | $share = (sqrt(1 + $amount0 / $reserve0) - 1) / 1e11 * $swapNoFee; |
| 112 | } else if ($asset1Only) { |
| 113 | $share = (sqrt(1 + $amount1 / $reserve1) - 1) / 1e11 * $swapNoFee; |
| 114 | } |
| 115 | $minted = floor($share * $supply); |
| 116 | if ($minted == 0) bounce('insufficient minted amount'); |
| 117 | }", |
| 118 | "messages": [ |
| 119 | { |
| 120 | "app": "payment", |
| 121 | "payload": { |
| 122 | "asset": "{$asset}", |
| 123 | "outputs": [ |
| 124 | { |
| 125 | "address": "{$to}", |
| 126 | "amount": "{$minted}" |
| 127 | } |
| 128 | ] |
| 129 | } |
| 130 | }, |
| 131 | { |
| 132 | "app": "state", |
| 133 | "state": "{ |
| 134 | var['supply'] += $minted; |
| 135 | response['type'] = 'mint'; |
| 136 | response['asset_amount'] = $minted; |
| 137 | }" |
| 138 | } |
| 139 | ] |
| 140 | }, |
| 141 | { |
| 142 | "if": "{$asset AND trigger.output[[asset=$asset]]}", |
| 143 | "init": "{ |
| 144 | $burned = trigger.output[[asset=$asset]]; |
| 145 | $share = $burned / $supply; |
| 146 | $minted0 = floor($share * $reserve0); |
| 147 | $minted1 = floor($share * $reserve1); |
| 148 | if ($minted0 == 0 OR $minted1 == 0) bounce('insufficient output amount'); |
| 149 | }", |
| 150 | "messages": [ |
| 151 | { |
| 152 | "app": "payment", |
| 153 | "payload": { |
| 154 | "asset": "{$asset0}", |
| 155 | "outputs": [ |
| 156 | { |
| 157 | "address": "{$to}", |
| 158 | "amount": "{$minted0}" |
| 159 | } |
| 160 | ] |
| 161 | } |
| 162 | }, |
| 163 | { |
| 164 | "app": "payment", |
| 165 | "payload": { |
| 166 | "asset": "{$asset1}", |
| 167 | "outputs": [ |
| 168 | { |
| 169 | "address": "{$to}", |
| 170 | "amount": "{$minted1}" |
| 171 | } |
| 172 | ] |
| 173 | } |
| 174 | }, |
| 175 | { |
| 176 | "app": "state", |
| 177 | "state": "{ |
| 178 | var['supply'] -= $burned; |
| 179 | response['type'] = 'burn'; |
| 180 | response['asset0_amount'] = $minted0; |
| 181 | response['asset1_amount'] = $minted1; |
| 182 | }" |
| 183 | } |
| 184 | ] |
| 185 | }, |
| 186 | { |
| 187 | "if": "{ |
| 188 | $asset0ToAsset1 = ($amount0NoFees > 0 AND $amount1NoFees <= 0); |
| 189 | $asset1ToAsset0 = ($amount1NoFees > 0 AND $amount0NoFees <= 0); |
| 190 | $asset AND $supply AND ($asset0ToAsset1 OR $asset1ToAsset0) |
| 191 | }", |
| 192 | "init": "{ |
| 193 | $amountOutMin = trigger.data.amount_out_min; |
| 194 | $in = $asset0ToAsset1 ? '0' : '1'; |
| 195 | $out = $asset0ToAsset1 ? '1' : '0'; |
| 196 | $numerator = ${'amount' || $in} * ${'reserve' || $out} * $swapNoFee; |
| 197 | $denominator = ${'reserve' || $in} * 1e11 + ${'amount' || $in} * $swapNoFee; |
| 198 | $amount = floor($numerator / $denominator); |
| 199 | if ($amount == 0 OR ($amountOutMin AND $amount < $amountOutMin)) |
| 200 | bounce('insufficient output amount'); |
| 201 | $address = $toAA ? $toAA : $to; |
| 202 | }", |
| 203 | "messages": [ |
| 204 | { |
| 205 | "app": "payment", |
| 206 | "payload": { |
| 207 | "asset": "{${'asset' || $out}}", |
| 208 | "outputs": [ |
| 209 | { |
| 210 | "address": "{$address}", |
| 211 | "amount": "{$amount}" |
| 212 | } |
| 213 | ] |
| 214 | } |
| 215 | }, |
| 216 | { |
| 217 | "if": "{$toAA}", |
| 218 | "app": "data", |
| 219 | "payload": { |
| 220 | "to": "{$to}" |
| 221 | } |
| 222 | }, |
| 223 | { |
| 224 | "app": "state", |
| 225 | "state": "{ |
| 226 | response['type'] = 'swap'; |
| 227 | response['asset' || $out || '_amount'] = $amount; |
| 228 | }" |
| 229 | } |
| 230 | ] |
| 231 | } |
| 232 | ] |
| 233 | } |
| 234 | } |
| 235 | ] |