Skip to content

Commit 53a24d9

Browse files
authored
Merge branch 'master' into test/integration-test
2 parents 719aadd + 291b89d commit 53a24d9

Some content is hidden

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

44 files changed

+3928
-864
lines changed

app/ante/ante.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package ante
22

33
import (
4-
clptypes "github.com/Sifchain/sifnode/x/clp/types"
54
"strings"
65

6+
adminkeeper "github.com/Sifchain/sifnode/x/admin/keeper"
7+
clptypes "github.com/Sifchain/sifnode/x/clp/types"
78
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
9+
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
810

911
disptypes "github.com/Sifchain/sifnode/x/dispensation/types"
1012
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -24,8 +26,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
2426
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
2527
}
2628
return sdk.ChainAnteDecorators(
27-
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
28-
NewAdjustGasPriceDecorator(), // Custom decorator to adjust gas price for specific msg types
29+
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
30+
NewAdjustGasPriceDecorator(options.AdminKeeper), // Custom decorator to adjust gas price for specific msg types
2931
ante.NewRejectExtensionOptionsDecorator(),
3032
ante.NewMempoolFeeDecorator(),
3133
ante.NewValidateBasicDecorator(),
@@ -45,15 +47,19 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
4547

4648
// AdjustGasPriceDecorator is a custom decorator to reduce fee prices .
4749
type AdjustGasPriceDecorator struct {
50+
adminKeeper adminkeeper.Keeper
4851
}
4952

5053
// NewAdjustGasPriceDecorator create a new instance of AdjustGasPriceDecorator
51-
func NewAdjustGasPriceDecorator() AdjustGasPriceDecorator {
52-
return AdjustGasPriceDecorator{}
54+
func NewAdjustGasPriceDecorator(adminKeeper adminkeeper.Keeper) AdjustGasPriceDecorator {
55+
return AdjustGasPriceDecorator{adminKeeper: adminKeeper}
5356
}
5457

5558
// AnteHandle adjusts the gas price based on the tx type.
5659
func (r AdjustGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
60+
adminParams := r.adminKeeper.GetParams(ctx)
61+
submitProposalFee := adminParams.SubmitProposalFee
62+
5763
msgs := tx.GetMsgs()
5864
if len(msgs) == 1 && (strings.Contains(strings.ToLower(sdk.MsgTypeURL(msgs[0])), strings.ToLower(disptypes.MsgTypeCreateDistribution)) ||
5965
strings.Contains(strings.ToLower(sdk.MsgTypeURL(msgs[0])), strings.ToLower(disptypes.MsgTypeRunDistribution))) {
@@ -80,6 +86,8 @@ func (r AdjustGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
8086
minFee = sdk.NewInt(100000000000000000) // 0.1
8187
} else if strings.Contains(msgTypeURLLower, "transfer") && minFee.LTE(sdk.NewInt(10000000000000000)) {
8288
minFee = sdk.NewInt(10000000000000000) // 0.01
89+
} else if strings.Contains(msgTypeURLLower, "submitproposal") || strings.Contains(msgTypeURLLower, govtypes.TypeMsgSubmitProposal) {
90+
minFee = sdk.NewIntFromBigInt(submitProposalFee.BigInt())
8391
}
8492
}
8593
if minFee.Equal(sdk.ZeroInt()) {

app/ante/ante_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestAdjustGasPriceDecorator_AnteHandle(t *testing.T) {
2222
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
2323
initTokens := sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)
2424
addrs := sifapp.AddTestAddrs(app, ctx, 6, initTokens)
25-
decorator := ante.AdjustGasPriceDecorator{}
25+
decorator := ante.NewAdjustGasPriceDecorator(app.AdminKeeper)
2626
highGasPrice := sdk.DecCoin{
2727
Denom: "rowan",
2828
Amount: sdk.MustNewDecFromStr("0.5"),
@@ -76,7 +76,7 @@ func TestAdjustGasPriceDecorator_AnteHandle_MinFee(t *testing.T) {
7676
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
7777
initTokens := sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)
7878
addrs := sifapp.AddTestAddrs(app, ctx, 6, initTokens)
79-
decorator := ante.AdjustGasPriceDecorator{}
79+
decorator := ante.NewAdjustGasPriceDecorator(app.AdminKeeper)
8080
highFee := sdk.NewCoins(sdk.NewCoin("rowan", sdk.NewInt(100000000000000000))) // 0.1
8181
lowFee := sdk.NewCoins(sdk.NewCoin("rowan", sdk.NewInt(10000000000000000))) // 0.01
8282

app/ante/handler_options.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ante
22

33
import (
4+
adminkeeper "github.com/Sifchain/sifnode/x/admin/keeper"
45
sdk "github.com/cosmos/cosmos-sdk/types"
56
"github.com/cosmos/cosmos-sdk/types/tx/signing"
67
"github.com/cosmos/cosmos-sdk/x/auth/ante"
@@ -13,6 +14,7 @@ import (
1314
// HandlerOptions defines the list of module keepers required to run the Sifnode
1415
// AnteHandler decorators.
1516
type HandlerOptions struct {
17+
AdminKeeper adminkeeper.Keeper
1618
AccountKeeper ante.AccountKeeper
1719
BankKeeper bankkeeper.Keeper
1820
FeegrantKeeper ante.FeegrantKeeper

app/app.go

+1
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ func NewSifAppWithBlacklist(
620620
app.MountMemoryStores(memKeys)
621621
anteHandler, err := sifchainAnte.NewAnteHandler(
622622
sifchainAnte.HandlerOptions{
623+
AdminKeeper: app.AdminKeeper,
623624
AccountKeeper: app.AccountKeeper,
624625
BankKeeper: app.BankKeeper,
625626
StakingKeeper: app.StakingKeeper,

app/setup_handlers.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package app
22

33
import (
4-
sifTypes "github.com/Sifchain/sifnode/x/clp/types"
4+
admintypes "github.com/Sifchain/sifnode/x/admin/types"
55
storetypes "github.com/cosmos/cosmos-sdk/store/types"
66
sdk "github.com/cosmos/cosmos-sdk/types"
77
m "github.com/cosmos/cosmos-sdk/types/module"
@@ -14,8 +14,9 @@ func SetupHandlers(app *SifchainApp) {
1414
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, plan types.Plan, vm m.VersionMap) (m.VersionMap, error) {
1515
app.Logger().Info("Running upgrade handler for " + releaseVersion)
1616

17-
swapFeeParams := sifTypes.SwapFeeParams{DefaultSwapFeeRate: sdk.NewDecWithPrec(5, 4)}
18-
app.ClpKeeper.SetSwapFeeParams(ctx, &swapFeeParams)
17+
app.AdminKeeper.SetParams(ctx, &admintypes.Params{
18+
SubmitProposalFee: sdk.NewUintFromString("5000000000000000000000"),
19+
})
1920

2021
return app.mm.RunMigrations(ctx, app.configurator, vm)
2122
})

docs/proposals/asymmetric-adds.md

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Asymmetric Liquidity Adds
2+
3+
Sifnoded does not currently support asymmetric liquidity adds. This document proposes a procedure
4+
which would allow asymmetric adds.
5+
6+
## Symmetric Adds
7+
8+
When adding symmetrically to a pool the fraction of total pool units owned by the Liquidity Provider (LP)
9+
after the add must equal the amount of native token added to the pool as a fraction of total native asset token in the
10+
pool (after the add):
11+
12+
```
13+
l / (P + l) = r / (r + R)
14+
```
15+
16+
Where:
17+
```
18+
l - LP units
19+
P - total pool units (before)
20+
r - amount of native token added
21+
R - native asset pool depth (before)
22+
```
23+
Rearranging gives:
24+
25+
```
26+
(1) l = r * P / R
27+
```
28+
29+
## Asymmetric adds
30+
31+
In the asymmetric case, by definition:
32+
33+
```
34+
R/A =/= r/a
35+
```
36+
37+
(this includes the case where the division is not defined i.e. when a=0 the division is not defined
38+
in which case the add is considered asymmetric)
39+
40+
Where:
41+
```
42+
R - native asset pool depth (before adding liquidity)
43+
A - external asset pool depth (before adding liquidity)
44+
r - amount of native token added
45+
a - amount of external token added
46+
```
47+
Currently sifnoded blocks asymmetric adds. The following procedure is proposed to enable
48+
asymmetric adds.
49+
50+
### Proposed method
51+
52+
In the following formulas:
53+
54+
```
55+
p - current ratio shifting running rate
56+
f - swap fee rate
57+
```
58+
59+
If the pool is not in the same ratio as the add then either:
60+
61+
1. Some r must be swapped for a, such that after the swap the add is symmetric
62+
2. Some a must be swapped for r, such that after the swap the add is symmetric
63+
64+
#### Swap native token for external token
65+
66+
Swap an amount, s, of native token such that:
67+
68+
```
69+
(R + s) / (A - g.s) = (R + r) / (A + a) = (r − s) / (a + g.s)
70+
```
71+
72+
where g is the swap formula:
73+
74+
```
75+
g.x = (1 - f) * (1 + r) * x * Y / (x + X)
76+
```
77+
78+
Solving for s (using mathematica!) gives:
79+
80+
```
81+
s = abs((sqrt(pow((-1*f*p*A*r-f*p*A*R-f*A*r-f*A*R+p*A*r+p*A*R+2*a*R+2*A*R), 2)-4*(a+A)*(a*R*R-A*r*R)) + f*p*A*r + f*p*A*R + f*A*r + f*A*R - p*A*r - p*A*R - 2*a*R - 2*A*R) / (2 * (a + A))).
82+
```
83+
84+
The number of pool units is then given by the symmetric formula (1):
85+
86+
```
87+
l = (r - s) * P / (R + s)
88+
```
89+
90+
#### Swap external token for native token
91+
92+
Swap an amount, s, of native token such that:
93+
94+
```
95+
(R - s) / (A + g'.s) = (R + r) / (A + a) = (r + g'.s) / (a - s)
96+
```
97+
98+
Where g' is the swap formula:
99+
100+
```
101+
g' = (1 - f) * x * Y / ((x + X) * (1 + r))
102+
```
103+
104+
Solving for s (using mathematica!) gives:
105+
106+
```
107+
s = abs((sqrt(R*(-1*(a+A))*(-1*f*f*a*R-f*f*A*R-2*f*p*a*R+4*f*p*A*r+2*f*p*A*R+4*f*A*r+4*f*A*R-p*p*a*R-p*p*A*R-4*p*A*r-4*p*A*R-4*A*r-4*A*R)) + f*a*R + f*A*R + p*a*R - 2*p*A*r - p*A*R - 2*A*r - 2*A*R) / (2 * (p + 1) * (r + R)))
108+
```
109+
110+
The number of pool units is then given by the symmetric formula (1):
111+
112+
```
113+
l = (a - s) * P / (A + s)
114+
```
115+
116+
### Equivalence with swapping
117+
118+
Any procedure which assigns LP units should guarantee that if an LP adds (x,y) then removes all their
119+
liquidity from the pool, receiving (x',y') then it is never the case that x' > x and y' > y (all else being equal i.e.
120+
no LPD, rewards etc.). Furthermore
121+
assuming (without loss of generality) that x' =< x, if instead of adding to the pool then removing all liquidity
122+
the LP had swapped (x - x'), giving them a total of y'' (i.e. y'' = y + g.(x - x')), then y'' should equal y'. (Certainly y' cannot be greater than y'' otherwise
123+
the LP has achieved a cheap swap.)
124+
125+
In the case of the proposed add liquidity procedure the amount the LP would receive by adding then removing would equal the amounts
126+
of each token after the internal swap (at this stage the add is symmetric and with symmetric adds x' = x and y' = y), that is:
127+
```
128+
(2) x' = x - s
129+
(3) y' = y + g.s
130+
```
131+
Plugging these into the equation for y'', y'' = y + g.(x - x'):
132+
```
133+
y'' = y + g.(x - x')
134+
= y + g.s by rearranging (2) and substituting
135+
= y' by substituting (3)
136+
```
137+
### Liquidity Protection
138+
139+
Since the add liquidity process involves swapping then the Liquidity protection procedure must be applied.
140+
141+
## References
142+
143+
Detailed derivation of formulas https://hackmd.io/NjvaZY1qQiS17s_uEgZmTw?both

0 commit comments

Comments
 (0)