Skip to content

Commit

Permalink
resolve unit tests, apply discount update, parameter add authority ch…
Browse files Browse the repository at this point in the history
…eck, add broker address update msg
  • Loading branch information
jelysn committed Dec 1, 2023
1 parent 9df8ee2 commit 551161f
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 163 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ func NewElysApp(
appCodec,
keys[parametermoduletypes.StoreKey],
keys[parametermoduletypes.MemStoreKey],
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// add capability keeper and ScopeToModule for ibc module
Expand Down
2 changes: 2 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87112,6 +87112,8 @@ definitions:
type: string
format: uint64
description: QueryParamsResponse is response type for the Query/Params RPC method.
elys.parameter.MsgUpdateBrokerAddressResponse:
type: object
elys.parameter.MsgUpdateMaxVotingPowerResponse:
type: object
elys.parameter.MsgUpdateMinCommissionResponse:
Expand Down
9 changes: 8 additions & 1 deletion proto/elys/parameter/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ service Msg {
rpc UpdateMinCommission(MsgUpdateMinCommission) returns (MsgUpdateMinCommissionResponse);
rpc UpdateMaxVotingPower(MsgUpdateMaxVotingPower) returns (MsgUpdateMaxVotingPowerResponse);
rpc UpdateMinSelfDelegation(MsgUpdateMinSelfDelegation) returns (MsgUpdateMinSelfDelegationResponse);
rpc UpdateBrokerAddress(MsgUpdateBrokerAddress) returns (MsgUpdateBrokerAddressResponse);
}

message MsgUpdateMinCommission {
Expand All @@ -26,4 +27,10 @@ message MsgUpdateMinSelfDelegation {
string creator = 1;
string min_self_delegation = 3;
}
message MsgUpdateMinSelfDelegationResponse {}
message MsgUpdateMinSelfDelegationResponse {}

message MsgUpdateBrokerAddress {
string creator = 1;
string broker_address = 3;
}
message MsgUpdateBrokerAddressResponse {}
3 changes: 3 additions & 0 deletions testutil/keeper/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/elys-network/elys/x/parameter/keeper"
"github.com/elys-network/elys/x/parameter/types"
"github.com/stretchr/testify/require"
Expand All @@ -33,6 +35,7 @@ func ParameterKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
cdc,
storeKey,
memStoreKey,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
23 changes: 2 additions & 21 deletions x/amm/keeper/apply_discount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,11 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/elys-network/elys/x/amm/types"
)

// ApplyDiscount applies discount to swap fee if applicable
func (k Keeper) ApplyDiscount(ctx sdk.Context, swapFee sdk.Dec, discount sdk.Dec, sender string) (sdk.Dec, sdk.Dec, error) {
// if discount is nil, return swap fee and zero discount
if discount.IsNil() {
return swapFee, sdk.ZeroDec(), nil
}

// if discount is zero, return swap fee and zero discount
if discount.IsZero() {
return swapFee, sdk.ZeroDec(), nil
}

// check if discount is positive and signer address is broker address otherwise throw an error
brokerAddress := k.parameterKeeper.GetParams(ctx).BrokerAddress
if discount.IsPositive() && sender != brokerAddress {
return sdk.ZeroDec(), sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrInvalidDiscount, "discount %s is positive and signer address %s is not broker address %s", discount, sender, brokerAddress)
}

func ApplyDiscount(swapFee sdk.Dec, discount sdk.Dec) sdk.Dec {
// apply discount percentage to swap fee
swapFee = swapFee.Mul(sdk.OneDec().Sub(discount))

return swapFee, discount, nil
return swapFee
}
61 changes: 9 additions & 52 deletions x/amm/keeper/apply_discount_test.go
Original file line number Diff line number Diff line change
@@ -1,103 +1,60 @@
package keeper_test

import (
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/keeper"
)

func (suite *KeeperTestSuite) TestApplyDiscount() {
k, ctx := suite.app.AmmKeeper, suite.ctx
brokerAddress := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()).String()
params := suite.app.ParameterKeeper.GetParams(ctx)
params.BrokerAddress = brokerAddress
suite.app.ParameterKeeper.SetParams(ctx, params)

// Define test cases
tests := []struct {
name string
swapFee types.Dec
discount types.Dec
sender string
wantFee types.Dec
wantError bool
name string
swapFee types.Dec
discount types.Dec
wantFee types.Dec
}{
{
name: "Zero discount",
swapFee: types.NewDecWithPrec(100, 2), // 1.00 as an example
discount: types.ZeroDec(),
sender: "testSender",
wantFee: types.NewDecWithPrec(100, 2),
},
{
name: "Positive discount with valid broker address",
swapFee: types.NewDecWithPrec(100, 2),
discount: types.NewDecWithPrec(10, 2), // 0.10 (10%)
sender: brokerAddress,
wantFee: types.NewDecWithPrec(90, 2), // 0.90 after discount
},
{
name: "Positive discount with invalid broker address",
swapFee: types.NewDecWithPrec(100, 2),
discount: types.NewDecWithPrec(10, 2),
sender: "invalidBroker",
wantError: true,
},
{
name: "Boundary value for discount",
swapFee: types.NewDecWithPrec(100, 2),
discount: types.NewDecWithPrec(9999, 4), // 0.9999 (99.99%)
sender: brokerAddress,
wantFee: types.NewDecWithPrec(1, 4), // 0.01 after discount
wantFee: types.NewDecWithPrec(1, 4), // 0.01 after discount
},
{
name: "Discount greater than swap fee",
swapFee: types.NewDecWithPrec(50, 2), // 0.50
discount: types.NewDecWithPrec(75, 2), // 0.75
sender: brokerAddress,
wantFee: types.NewDecWithPrec(125, 3),
},
{
name: "Invalid swap fee",
swapFee: types.NewDecWithPrec(-100, 2), // -1.00 (invalid)
discount: types.NewDecWithPrec(10, 2),
sender: "testSender",
wantError: true,
},
{
name: "Zero swap fee with valid discount",
swapFee: types.ZeroDec(),
discount: types.NewDecWithPrec(10, 2),
sender: brokerAddress,
wantFee: types.ZeroDec(),
},
{
name: "Large discount with valid broker address",
swapFee: types.NewDecWithPrec(100, 2),
discount: types.NewDecWithPrec(9000, 4), // 0.90 (90%)
sender: brokerAddress,
wantFee: types.NewDecWithPrec(10, 2), // 0.10 after discount
},
{
name: "Large discount with invalid broker address",
swapFee: types.NewDecWithPrec(100, 2),
discount: types.NewDecWithPrec(9000, 4), // 0.90
sender: "invalidBroker",
wantError: true,
wantFee: types.NewDecWithPrec(10, 2), // 0.10 after discount
},
}

for _, tc := range tests {
suite.Run(tc.name, func() {
fee, discount, err := k.ApplyDiscount(ctx, tc.swapFee, tc.discount, tc.sender)

if tc.wantError {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
suite.Require().Equal(tc.wantFee, fee)
suite.Require().Equal(tc.discount, discount)
}
fee := keeper.ApplyDiscount(tc.swapFee, tc.discount)
suite.Require().Equal(tc.wantFee, fee)
})
}
}
8 changes: 2 additions & 6 deletions x/amm/keeper/calc_in_route_spot_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ func (k Keeper) CalcInRouteSpotPrice(ctx sdk.Context, tokenIn sdk.Coin, routes [
// Get Pool swap fee
swapFee := pool.GetPoolParams().SwapFee

// Apply discount to swap fee if applicable
brokerAddress := k.parameterKeeper.GetParams(ctx).BrokerAddress
swapFee, _, err := k.ApplyDiscount(ctx, swapFee, discount, brokerAddress)
if err != nil {
return sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, err
}
// Apply discount to swap fee
swapFee = ApplyDiscount(swapFee, discount)

// Calculate the total discounted swap fee
totalDiscountedSwapFee = totalDiscountedSwapFee.Add(swapFee)
Expand Down
8 changes: 2 additions & 6 deletions x/amm/keeper/calc_out_route_spot_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ func (k Keeper) CalcOutRouteSpotPrice(ctx sdk.Context, tokenOut sdk.Coin, routes
// Get Pool swap fee
swapFee := pool.GetPoolParams().SwapFee

// Apply discount to swap fee if applicable
brokerAddress := k.parameterKeeper.GetParams(ctx).BrokerAddress
swapFee, _, err := k.ApplyDiscount(ctx, swapFee, discount, brokerAddress)
if err != nil {
return sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, err
}
// Apply discount
swapFee = ApplyDiscount(swapFee, discount)

// Calculate the total discounted swap fee
totalDiscountedSwapFee = totalDiscountedSwapFee.Add(swapFee)
Expand Down
11 changes: 8 additions & 3 deletions x/amm/keeper/route_exact_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/elys-network/elys/x/amm/types"
)

Expand Down Expand Up @@ -73,10 +74,14 @@ func (k Keeper) RouteExactAmountIn(
}

// Apply discount to swap fee if applicable
swapFee, _, err = k.ApplyDiscount(ctx, swapFee, discount, sender.String())
if err != nil {
return math.Int{}, sdk.ZeroDec(), sdk.ZeroDec(), err
brokerAddress := k.parameterKeeper.GetParams(ctx).BrokerAddress
if discount.IsNil() {
discount = sdk.ZeroDec()
}
if discount.IsPositive() && sender.String() != brokerAddress {
return math.Int{}, sdk.ZeroDec(), sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrInvalidDiscount, "discount %s is positive and signer address %s is not broker address %s", discount, sender, brokerAddress)
}
swapFee = ApplyDiscount(swapFee, discount)

// Calculate the total discounted swap fee
totalDiscountedSwapFee = totalDiscountedSwapFee.Add(swapFee)
Expand Down
11 changes: 8 additions & 3 deletions x/amm/keeper/route_exact_amount_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/elys-network/elys/x/amm/types"
)

Expand Down Expand Up @@ -99,10 +100,14 @@ func (k Keeper) RouteExactAmountOut(ctx sdk.Context,
}

// Apply discount to swap fee if applicable
swapFee, discount, err = k.ApplyDiscount(ctx, swapFee, discount, sender.String())
if err != nil {
return math.Int{}, sdk.ZeroDec(), sdk.ZeroDec(), err
brokerAddress := k.parameterKeeper.GetParams(ctx).BrokerAddress
if discount.IsNil() {
discount = sdk.ZeroDec()
}
if discount.IsPositive() && sender.String() != brokerAddress {
return math.Int{}, sdk.ZeroDec(), sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrInvalidDiscount, "discount %s is positive and signer address %s is not broker address %s", discount, sender, brokerAddress)
}
swapFee = ApplyDiscount(swapFee, discount)

// Calculate the total discounted swap fee
totalDiscountedSwapFee = totalDiscountedSwapFee.Add(swapFee)
Expand Down
Loading

0 comments on commit 551161f

Please sign in to comment.