|
| 1 | +# Minimum swap fees |
| 2 | + |
| 3 | +This tutorial demonstrates the behaviour of the min swap fee functionality |
| 4 | + |
| 5 | +1. Start and run the chain: |
| 6 | + |
| 7 | +```bash |
| 8 | +make init |
| 9 | +make run |
| 10 | +``` |
| 11 | + |
| 12 | +2. Create a pool: |
| 13 | + |
| 14 | +```bash |
| 15 | +sifnoded tx clp create-pool \ |
| 16 | + --from sif \ |
| 17 | + --keyring-backend test \ |
| 18 | + --symbol ceth \ |
| 19 | + --nativeAmount 2000000000000000000 \ |
| 20 | + --externalAmount 2000000000000000000 \ |
| 21 | + --fees 100000000000000000rowan \ |
| 22 | + --broadcast-mode block \ |
| 23 | + --chain-id localnet \ |
| 24 | + -y |
| 25 | +``` |
| 26 | + |
| 27 | +3. Confirm pool has been created: |
| 28 | + |
| 29 | +```bash |
| 30 | +sifnoded q clp pools --output json | jq |
| 31 | +``` |
| 32 | + |
| 33 | +returns: |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "pools": [ |
| 38 | + { |
| 39 | + "external_asset": { |
| 40 | + "symbol": "ceth" |
| 41 | + }, |
| 42 | + "native_asset_balance": "2000000000000000000", |
| 43 | + "external_asset_balance": "2000000000000000000", |
| 44 | + "pool_units": "2000000000000000000", |
| 45 | + "swap_price_native": "1.000000000000000000", |
| 46 | + "swap_price_external": "1.000000000000000000", |
| 47 | + "reward_period_native_distributed": "0", |
| 48 | + "external_liabilities": "0", |
| 49 | + "external_custody": "0", |
| 50 | + "native_liabilities": "0", |
| 51 | + "native_custody": "0", |
| 52 | + "health": "0.000000000000000000", |
| 53 | + "interest_rate": "0.000000000000000000", |
| 54 | + "last_height_interest_rate_computed": "0", |
| 55 | + "unsettled_external_liabilities": "0", |
| 56 | + "unsettled_native_liabilities": "0", |
| 57 | + "block_interest_native": "0", |
| 58 | + "block_interest_external": "0" |
| 59 | + } |
| 60 | + ], |
| 61 | + "clp_module_address": "sif1pjm228rsgwqf23arkx7lm9ypkyma7mzr3y2n85", |
| 62 | + "height": "5", |
| 63 | + "pagination": { |
| 64 | + "next_key": null, |
| 65 | + "total": "0" |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +4. Query the current swap fee params: |
| 71 | + |
| 72 | +```bash |
| 73 | +sifnoded q clp swap-fee-params --output json | jq |
| 74 | +``` |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "swap_fee_rate": "0.003000000000000000", |
| 79 | + "token_params": [] |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +5. Set new swap fee params |
| 84 | + |
| 85 | +```bash |
| 86 | +sifnoded tx clp set-swap-fee-params \ |
| 87 | + --from sif \ |
| 88 | + --keyring-backend test \ |
| 89 | + --chain-id localnet \ |
| 90 | + --broadcast-mode block \ |
| 91 | + --fees 100000000000000000rowan \ |
| 92 | + -y \ |
| 93 | + --path <( echo '{ |
| 94 | + "swap_fee_rate": "0.003", |
| 95 | + "token_params": [{ |
| 96 | + "asset": "ceth", |
| 97 | + "min_swap_fee": "0" |
| 98 | + }, |
| 99 | + { |
| 100 | + "asset": "rowan", |
| 101 | + "min_swap_fee": "600000000000" |
| 102 | + } |
| 103 | + ] |
| 104 | + }' ) |
| 105 | +``` |
| 106 | + |
| 107 | + |
| 108 | +4. Check swap fee params have been updated: |
| 109 | + |
| 110 | +```bash |
| 111 | +sifnoded q clp swap-fee-params --output json | jq |
| 112 | +``` |
| 113 | + |
| 114 | +```json |
| 115 | +{ |
| 116 | + "swap_fee_rate": "0.003000000000000000", |
| 117 | + "token_params": [ |
| 118 | + { |
| 119 | + "asset": "ceth", |
| 120 | + "min_swap_fee": "0" |
| 121 | + }, |
| 122 | + { |
| 123 | + "asset": "rowan", |
| 124 | + "min_swap_fee": "600000000000" |
| 125 | + } |
| 126 | + ] |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +6. Do a swap: |
| 131 | + |
| 132 | +```bash |
| 133 | +sifnoded tx clp swap \ |
| 134 | + --from sif \ |
| 135 | + --keyring-backend test \ |
| 136 | + --sentSymbol ceth \ |
| 137 | + --receivedSymbol rowan \ |
| 138 | + --sentAmount 200000000000000 \ |
| 139 | + --minReceivingAmount 0 \ |
| 140 | + --fees 100000000000000000rowan \ |
| 141 | + --chain-id localnet \ |
| 142 | + --broadcast-mode block \ |
| 143 | + --output json \ |
| 144 | + -y | jq '.logs[0].events[] | select(.type=="swap_successful").attributes[] | select(.key=="swap_amount" or .key=="liquidity_fee")' |
| 145 | +``` |
| 146 | + |
| 147 | +Returns: |
| 148 | + |
| 149 | +```json |
| 150 | +{ |
| 151 | + "key": "swap_amount", |
| 152 | + "value": "199380001999800" |
| 153 | +} |
| 154 | +{ |
| 155 | + "key": "liquidity_fee", |
| 156 | + "value": "600000000000" |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +The fee and swap amount are as expected: |
| 161 | +``` |
| 162 | +adjusted_output = x * Y / ((x + X)(1 + r)) |
| 163 | + = 200000000000000 * 2000000000000000000 / ((200000000000000 + 2000000000000000000) * (1 + 0)) |
| 164 | + = 199980001999800 |
| 165 | +
|
| 166 | +fee = min(max(f * adjusted_output, min_swap_fee), adjusted_output) |
| 167 | + = min(max(0.003 * 199980001999800, 600000000000), adjusted_output) |
| 168 | + = min(max(599940005999, 600000000000), 199980001999800) |
| 169 | + = 600000000000 |
| 170 | +
|
| 171 | +y = adjusted_amount - fee |
| 172 | + = 199980001999800 - 600000000000 |
| 173 | + = 199380001999800 |
| 174 | +``` |
0 commit comments