Skip to content

Commit

Permalink
Fix lp logic, add test cases (#1708)
Browse files Browse the repository at this point in the history
Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mattverse and ValarDragon authored Jun 8, 2022
1 parent 840466a commit 94a3878
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/gamm/pool-models/internal/cfmm_common/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func MaximalExactRatioJoin(p types.PoolI, ctx sdk.Context, tokensIn sdk.Coins) (
continue
}

usedAmount := minShareRatio.MulInt(coin.Amount).Ceil().TruncateInt()
usedAmount := minShareRatio.MulInt(poolLiquidity.AmountOfNoDenomValidation(coin.Denom)).Ceil().TruncateInt()
newAmt := coin.Amount.Sub(usedAmount)
// if newAmt is non-zero, add to RemCoins. (It could be zero due to rounding)
if !newAmt.IsZero() {
Expand Down
69 changes: 69 additions & 0 deletions x/gamm/pool-models/internal/cfmm_common/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,72 @@ func TestCalcExitPool(t *testing.T) {
}
}
}

func TestMaximalExactRatioJoin(t *testing.T) {
emptyContext := sdk.Context{}

balancerPoolAsset := []balancer.PoolAsset{
{Token: sdk.NewInt64Coin("foo", 100), Weight: sdk.NewIntFromUint64(5)},
{Token: sdk.NewInt64Coin("bar", 100), Weight: sdk.NewIntFromUint64(5)},
}

tests := []struct {
name string
pool func() gammtypes.PoolI
tokensIn sdk.Coins
expNumShare sdk.Int
expRemCoin sdk.Coins
}{
{
name: "two asset pool, same tokenIn ratio",
pool: func() gammtypes.PoolI {
balancerPool, err := balancer.NewBalancerPool(
1,
balancer.PoolParams{SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec()},
balancerPoolAsset,
"",
time.Now(),
)
require.NoError(t, err)
return &balancerPool
},
tokensIn: sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10)), sdk.NewCoin("bar", sdk.NewInt(10))),
expNumShare: sdk.NewIntFromUint64(10000000000000000000),
expRemCoin: sdk.Coins{},
},
{
name: "two asset pool, different tokenIn ratio with pool",
pool: func() gammtypes.PoolI {
balancerPool, err := balancer.NewBalancerPool(
1,
balancer.PoolParams{SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec()},
balancerPoolAsset,
"",
time.Now(),
)
require.NoError(t, err)
return &balancerPool
},
tokensIn: sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(10)), sdk.NewCoin("bar", sdk.NewInt(11))),
expNumShare: sdk.NewIntFromUint64(10000000000000000000),
expRemCoin: sdk.NewCoins(sdk.NewCoin("bar", sdk.NewIntFromUint64(1))),
},
}

for _, test := range tests {
balancerPool, err := balancer.NewBalancerPool(
1,
balancer.PoolParams{SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec()},
balancerPoolAsset,
"",
time.Now(),
)
require.NoError(t, err)

numShare, remCoins, err := cfmm_common.MaximalExactRatioJoin(&balancerPool, emptyContext, test.tokensIn)

require.NoError(t, err)
require.Equal(t, test.expNumShare, numShare)
require.Equal(t, test.expRemCoin, remCoins)
}
}

0 comments on commit 94a3878

Please sign in to comment.