Skip to content

Commit d4b801d

Browse files
committed
Merge branch 'master' into test/integration-test
2 parents 59cffca + 4fd2524 commit d4b801d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3043
-1782
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DOCKER ?= docker
1111
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf
1212

1313
GOFLAGS:=""
14-
GOTAGS:=
14+
GOTAGS:=ledger
1515

1616
ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=sifchain \
1717
-X github.com/cosmos/cosmos-sdk/version.ServerName=sifnoded \

app/setup_handlers.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ import (
77
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
88
)
99

10-
const releaseVersion = "1.0-beta.12"
10+
const releaseVersion = "1.0-beta.13"
1111

1212
func SetupHandlers(app *SifchainApp) {
1313
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, plan types.Plan, vm m.VersionMap) (m.VersionMap, error) {
1414
app.Logger().Info("Running upgrade handler for " + releaseVersion)
15-
// This is part of the scheduled process , directly doing state transitions here instead to migrating consensus version
16-
// The following functions fix the state of sifnode caused by unexpected swap behaviour triggered by margin logic.
17-
1815
return app.mm.RunMigrations(ctx, app.configurator, vm)
1916
})
2017

docs/proposals/min_swap_fees.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Minimum swap fees
2+
3+
This is a proposal to set a minimum swap fee.
4+
5+
## Current swap behaviour
6+
7+
Due to ratio shifting the current swap behaviour depends on whether the user is buying or selling Rowan.
8+
9+
In the following formulas:
10+
11+
```
12+
X - input depth (balance + liabilities)
13+
Y - output depth (balance + liabilities)
14+
x - input amount
15+
y - output amount
16+
r - current ratio shifting running rate
17+
f - swap fee rate, this must satisfy 0 =< f =< 1
18+
```
19+
20+
### Swapping to Rowan:
21+
22+
```
23+
y = (1 - f) * x * Y / ((x + X) * (1 + r))
24+
fee = f * x * Y / ((x + X) * (1 + r))
25+
```
26+
27+
Equivalently this can be written as:
28+
29+
```
30+
raw_XYK_output = x * Y / (x + X)
31+
adjusted_output = raw_XYK_output / (1 + r)
32+
33+
(1) fee = f * adjusted_output
34+
y = adjusted_output - fee
35+
```
36+
37+
### Swapping from Rowan:
38+
39+
```
40+
y = (1 - f) * (1 + r) * x * Y / (x + X)
41+
fee = f * (1 + r) * x * Y / (x + X)
42+
```
43+
44+
Similar to the case of swapping to rowan, this can be written as:
45+
46+
```
47+
raw_XYK_output = x * Y / (x + X)
48+
adjusted_output = raw_XYK_output * (1 + r)
49+
50+
(2) fee = f * adjusted_output
51+
y = adjusted_output - fee
52+
```
53+
54+
## Proposed Change
55+
56+
Apply a minimum fee when swapping.
57+
58+
The fee calculation in equation (1) and (2) becomes:
59+
60+
```
61+
fee = min(max(f * adjusted_output, min_fee), adjusted_output)
62+
```
63+
64+
Where `min_fee` is a minimum fee parameter for the token being bought, which is set via an admin key. See CLI
65+
section for more details.
66+
67+
The min function is required to ensure that the fee is not greater than the adjusted output.
68+
69+
If a `min-fee` has not been set for a token then it defaults to zero.
70+
71+
## Events
72+
73+
There are no new events or updates to existing events.
74+
75+
## CLI
76+
77+
The CLI option for querying the swap fee rate (`sifnoded q clp swap-fee-rate`) and setting the swap fee
78+
rate (`sifnoded tx clp set-swap-fee-rate`), must be renamed to `sifnoded q clp swap-fee-params`
79+
and `sifnoded tx clp set-swap-fee-params` and updated to include the min fee.
80+
81+
### Setting
82+
83+
The CLI should validate that the min fees are valid cosmos Uint256.
84+
85+
```bash
86+
sifnoded tx clp set-swap-fee-params \
87+
--from sif \
88+
--path ./swap-fee-params.json \
89+
--keyring-backend test \
90+
--fees 100000000000000000rowan \
91+
--chain-id localnet \
92+
-y
93+
```
94+
95+
```json
96+
{
97+
"swap_fee_rate": "0.003",
98+
"token_params": [{
99+
"asset": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
100+
"min_swap_fee": "12"
101+
},
102+
{
103+
"asset": "cusdc",
104+
"min_swap_fee": "800"
105+
},
106+
{
107+
"asset": "rowan",
108+
"min_swap_fee": "12"
109+
}
110+
]
111+
}
112+
```
113+
114+
### Querying
115+
116+
```bash
117+
sifnoded q clp swap-fee-params --output json
118+
```
119+
120+
```json
121+
{
122+
"swap_fee_rate": "0.003000000000000000",
123+
"token_params": [{
124+
"asset": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
125+
"min_swap_fee": "12"
126+
},
127+
{
128+
"asset": "cusdc",
129+
"min_swap_fee": "800"
130+
},
131+
{
132+
"asset": "rowan",
133+
"min_swap_fee": "12"
134+
}
135+
]
136+
}
137+
```

docs/tutorials/ledger.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Ledger Command Line Usage
2+
3+
1. Install the cosmos app onto ledger device.
4+
5+
2. Add a ledger key to the keyring
6+
```shell
7+
sifnoded keys add ledger --ledger
8+
```
9+
3. Send a ledger signed transaction
10+
```shell
11+
sifnoded tx bank send ledger toAddress 1000rowan \
12+
--from ledger \
13+
--sign-mode amino-json \
14+
--ledger \
15+
--fees 100000000000000000rowan
16+
```

docs/tutorials/min_swap_fees.md

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
```

proto/sifnode/clp/v1/params.proto

+10-1
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,18 @@ message ProviderDistributionParams {
9696
repeated ProviderDistributionPeriod distribution_periods = 1;
9797
}
9898

99-
message SwapFeeRate {
99+
message SwapFeeParams {
100100
string swap_fee_rate = 1 [
101101
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
102102
(gogoproto.nullable) = false
103103
];
104+
repeated SwapFeeTokenParams token_params = 2;
105+
}
106+
107+
message SwapFeeTokenParams {
108+
string asset = 1;
109+
string min_swap_fee = 2 [
110+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
111+
(gogoproto.nullable) = false
112+
];
104113
}

0 commit comments

Comments
 (0)