| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://oswap-frontend.herokuapp.com/pair.json", |
| 5 | "init": "{ |
| 6 | $bounceFees = 1e4; |
| 7 | $minLiquidity = 1e3; |
| 8 | $factory = params.factory; |
| 9 | $asset0 = params.asset0; |
| 10 | $asset1 = params.asset1; |
| 11 | $asset = var[$factory]['assets.' || this_address]; |
| 12 | $supply = var['supply']; |
| 13 | $amount0 = trigger.output[[asset=$asset0]]; |
| 14 | $amount1 = trigger.output[[asset=$asset1]]; |
| 15 | $amount0WithoutFees = ($asset0 == 'base') ? $amount0 : $amount0 - $bounceFees; |
| 16 | $amount1WithoutFees = ($asset1 == 'base') ? $amount1 : $amount1 - $bounceFees; |
| 17 | $reserve0 = balance[$asset0] - $amount0; |
| 18 | $reserve1 = balance[$asset1] - $amount1; |
| 19 | if (trigger.data.to AND !is_valid_address(trigger.data.to)) bounce('invalid recipient address'); |
| 20 | $to = trigger.data.to OTHERWISE trigger.address; |
| 21 | $toAsset = trigger.data.toAsset; |
| 22 | $deadline = trigger.data.deadline; |
| 23 | $amountOutMin = trigger.data.amountOutMin; |
| 24 | }", |
| 25 | "messages": { |
| 26 | "cases": [ |
| 27 | { |
| 28 | "if": "{!$asset AND trigger.data.initialize}", |
| 29 | "messages": [ |
| 30 | { |
| 31 | "app": "asset", |
| 32 | "payload": { |
| 33 | "is_private": false, |
| 34 | "is_transferrable": true, |
| 35 | "auto_destroy": false, |
| 36 | "fixed_denominations": false, |
| 37 | "issued_by_definer_only": true, |
| 38 | "cosigned_by_definer": false, |
| 39 | "spender_attested": false |
| 40 | } |
| 41 | }, |
| 42 | { |
| 43 | "app": "payment", |
| 44 | "payload": { |
| 45 | "asset": "base", |
| 46 | "outputs": [ |
| 47 | { |
| 48 | "address": "{$factory}" |
| 49 | } |
| 50 | ] |
| 51 | } |
| 52 | }, |
| 53 | { |
| 54 | "app": "data", |
| 55 | "payload": { |
| 56 | "finalizePair": "1" |
| 57 | } |
| 58 | }, |
| 59 | { |
| 60 | "app": "state", |
| 61 | "state": "{response['type'] = 'initialize';}" |
| 62 | } |
| 63 | ] |
| 64 | }, |
| 65 | { |
| 66 | "if": "{ |
| 67 | $asset |
| 68 | AND $amount0WithoutFees > 0 |
| 69 | AND $amount1WithoutFees > 0 |
| 70 | AND ($amount0WithoutFees * $amount1WithoutFees) >= $minLiquidity |
| 71 | }", |
| 72 | "init": "{ |
| 73 | if ($reserve1 == 0 OR $reserve0 == 0) { |
| 74 | $minted = balance[$asset0]; |
| 75 | return; |
| 76 | } |
| 77 | $ratio = $reserve1 / $reserve0; |
| 78 | $expectedAmount1 = round($ratio * $amount0); |
| 79 | if ($expectedAmount1 != $amount1) |
| 80 | bounce('wrong ratio of amounts, expected ' || $expectedAmount1 || ' of ' || $asset1); |
| 81 | $investorShare = $amount0 / $reserve0; |
| 82 | $minted = round($investorShare * $supply); |
| 83 | }", |
| 84 | "messages": [ |
| 85 | { |
| 86 | "app": "payment", |
| 87 | "payload": { |
| 88 | "asset": "{$asset}", |
| 89 | "outputs": [ |
| 90 | { |
| 91 | "address": "{$to}", |
| 92 | "amount": "{$minted}" |
| 93 | } |
| 94 | ] |
| 95 | } |
| 96 | }, |
| 97 | { |
| 98 | "app": "state", |
| 99 | "state": "{ |
| 100 | var['supply'] += $minted; |
| 101 | response['type'] = 'mint'; |
| 102 | }" |
| 103 | } |
| 104 | ] |
| 105 | }, |
| 106 | { |
| 107 | "if": "{$asset AND trigger.output[[asset=$asset]]}", |
| 108 | "init": "{ |
| 109 | $burned = trigger.output[[asset=$asset]]; |
| 110 | $investorShare = $burned / $supply; |
| 111 | $minted0 = round($investorShare * balance[$asset0]); |
| 112 | $minted1 = round($investorShare * balance[$asset1]); |
| 113 | }", |
| 114 | "messages": [ |
| 115 | { |
| 116 | "app": "payment", |
| 117 | "payload": { |
| 118 | "asset": "{$asset0}", |
| 119 | "outputs": [ |
| 120 | { |
| 121 | "address": "{$to}", |
| 122 | "amount": "{$minted0}" |
| 123 | } |
| 124 | ] |
| 125 | } |
| 126 | }, |
| 127 | { |
| 128 | "app": "payment", |
| 129 | "payload": { |
| 130 | "asset": "{$asset1}", |
| 131 | "outputs": [ |
| 132 | { |
| 133 | "address": "{$to}", |
| 134 | "amount": "{$minted1}" |
| 135 | } |
| 136 | ] |
| 137 | } |
| 138 | }, |
| 139 | { |
| 140 | "app": "state", |
| 141 | "state": "{ |
| 142 | var['supply'] = ($supply - $burned == 0) ? false : $supply - $burned; |
| 143 | response['type'] = 'burn'; |
| 144 | }" |
| 145 | } |
| 146 | ] |
| 147 | }, |
| 148 | { |
| 149 | "if": "{ |
| 150 | $asset0ToAsset1 = ($amount0WithoutFees > 0 AND $amount1WithoutFees <= 0); |
| 151 | $asset1ToAsset0 = ($amount1WithoutFees > 0 AND $amount0WithoutFees <= 0); |
| 152 | $asset AND $supply AND ($asset0ToAsset1 || $asset1ToAsset0) |
| 153 | }", |
| 154 | "init": "{ |
| 155 | if ($deadline AND $deadline < timestamp) bounce('expired'); |
| 156 | $in = $asset0ToAsset1 ? '0' : '1'; |
| 157 | $out = $asset0ToAsset1 ? '1' : '0'; |
| 158 | $numerator = ${'amount' || $in} * ${'reserve' || $out} * 997; |
| 159 | $denominator = ${'reserve' || $in} * 1000 + ${'amount' || $in} * 997; |
| 160 | $amount = round($numerator / $denominator); |
| 161 | if ($amountOutMin AND $amount < $amountOutMin) bounce('insufficient output amount'); |
| 162 | $pair = var[$factory]['all_pairs.' || ${'asset' || $out} || '_' || $toAsset]; |
| 163 | if ($toAsset AND !$pair) bounce('invalid asset'); |
| 164 | $address = $toAsset ? $pair : $to; |
| 165 | }", |
| 166 | "messages": [ |
| 167 | { |
| 168 | "app": "payment", |
| 169 | "payload": { |
| 170 | "asset": "{${'asset' || $out}}", |
| 171 | "outputs": [ |
| 172 | { |
| 173 | "address": "{$address}", |
| 174 | "amount": "{$amount}" |
| 175 | } |
| 176 | ] |
| 177 | } |
| 178 | }, |
| 179 | { |
| 180 | "if": "{$toAsset}", |
| 181 | "app": "data", |
| 182 | "payload": { |
| 183 | "to": "{$to}" |
| 184 | } |
| 185 | }, |
| 186 | { |
| 187 | "app": "state", |
| 188 | "state": "{response['type'] = 'swap';}" |
| 189 | } |
| 190 | ] |
| 191 | } |
| 192 | ] |
| 193 | } |
| 194 | } |
| 195 | ] |