Skip to content

Commit

Permalink
Replace discount with address (#960)
Browse files Browse the repository at this point in the history
* change discount values

* more changes

* get discount in amm:

* ifx tests

* fix tests

* fix

* tradeshield fix

* use tier structure

* update tests

* fixing tier keeper pointers (#958)

* fixing tier keeper pointers

* fixing tier keeper pointers

* fixing tier keeper pointers

* fix

* fix

* add discoumt to perpetual

* fix tests

* review changes

* use silver

* add address instead of discount

* replace discount field in perpetual

* fix

* handle empty

---------

Co-authored-by: Abhinav Kumar <[email protected]>
  • Loading branch information
amityadav0 and avkr003 authored Nov 18, 2024
1 parent 2e4870d commit 697f3c4
Show file tree
Hide file tree
Showing 34 changed files with 477 additions and 592 deletions.
5 changes: 1 addition & 4 deletions proto/elys/amm/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,7 @@ message QuerySwapEstimationByDenomRequest {
cosmos.base.v1beta1.Coin amount = 1 [ (gogoproto.nullable) = false ];
string denom_in = 2;
string denom_out = 3;
string discount = 4 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string address = 4;
}

message QuerySwapEstimationByDenomResponse {
Expand Down
12 changes: 2 additions & 10 deletions proto/elys/amm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ message MsgSwapExactAmountIn {
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string discount = 5 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string recipient = 6 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string recipient = 5 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message MsgSwapExactAmountInResponse {
Expand Down Expand Up @@ -128,11 +124,7 @@ message MsgSwapExactAmountOut {
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string discount = 5 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string recipient = 6 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string recipient = 5 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message MsgSwapExactAmountOutResponse {
Expand Down
5 changes: 1 addition & 4 deletions proto/elys/perpetual/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ message QueryOpenEstimationRequest {
];
string trading_asset = 3;
cosmos.base.v1beta1.Coin collateral = 4 [ (gogoproto.nullable) = false ];
string discount = 5 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string address = 5;
string take_profit_price = 6 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
Expand Down
7 changes: 4 additions & 3 deletions x/amm/client/cli/query_swap_estimation.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cli

import (
sdkmath "cosmossdk.io/math"
"errors"
"strconv"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -41,7 +42,7 @@ func CmdSwapEstimation() *cobra.Command {
})
}

discountStr, err := cmd.Flags().GetString(FlagDiscount)
discountStr, err := cmd.Flags().GetString(FlagAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -74,7 +75,7 @@ func CmdSwapEstimation() *cobra.Command {

flags.AddQueryFlagsToCmd(cmd)

cmd.Flags().String(FlagDiscount, "0.0", "discount to apply to the swap fee")
cmd.Flags().String(FlagAddress, "0.0", "discount to apply to the swap fee")

return cmd
}
11 changes: 3 additions & 8 deletions x/amm/client/cli/query_swap_estimation_by_denom.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -23,11 +22,7 @@ func CmdSwapEstimationByDenom() *cobra.Command {
reqDenomIn := args[1]
reqDenomOut := args[2]

discountStr, err := cmd.Flags().GetString(FlagDiscount)
if err != nil {
return err
}
discount, err := sdkmath.LegacyNewDecFromStr(discountStr)
address, err := cmd.Flags().GetString(FlagAddress)
if err != nil {
return err
}
Expand All @@ -43,7 +38,7 @@ func CmdSwapEstimationByDenom() *cobra.Command {
Amount: reqAmount,
DenomIn: reqDenomIn,
DenomOut: reqDenomOut,
Discount: discount,
Address: address,
}

res, err := queryClient.SwapEstimationByDenom(cmd.Context(), params)
Expand All @@ -57,7 +52,7 @@ func CmdSwapEstimationByDenom() *cobra.Command {

flags.AddQueryFlagsToCmd(cmd)

cmd.Flags().String(FlagDiscount, "0.0", "discount to apply to the swap fee")
cmd.Flags().String(FlagAddress, "", "address of the account making swap")

return cmd
}
2 changes: 1 addition & 1 deletion x/amm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
FlagDiscount = "discount"
FlagAddress = "address"
FlagRecipient = "recipient"
listSeparator = ","
)
Expand Down
4 changes: 2 additions & 2 deletions x/amm/client/cli/tx_swap_by_denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func CmdSwapByDenom() *cobra.Command {
}
}

discountStr, err := cmd.Flags().GetString(FlagDiscount)
discountStr, err := cmd.Flags().GetString(FlagAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func CmdSwapByDenom() *cobra.Command {

cmd.Flags().String(FlagMinAmount, "", "minimum amount of tokens to receive")
cmd.Flags().String(FlagMaxAmount, "", "maximum amount of tokens to send")
cmd.Flags().String(FlagDiscount, "0.0", "discount to apply to the swap fee (only smart contract broker can apply the discount)")
cmd.Flags().String(FlagAddress, "0.0", "discount to apply to the swap fee (only smart contract broker can apply the discount)")
cmd.Flags().String(FlagRecipient, "", "optional recipient field for the tokens swapped to be sent to")

return cmd
Expand Down
4 changes: 2 additions & 2 deletions x/amm/client/cli/tx_swap_exact_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CmdSwapExactAmountIn() *cobra.Command {
}
argSwapRouteDenoms := strings.Split(args[3], listSeparator)

discountStr, err := cmd.Flags().GetString(FlagDiscount)
discountStr, err := cmd.Flags().GetString(FlagAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func CmdSwapExactAmountIn() *cobra.Command {

flags.AddTxFlagsToCmd(cmd)

cmd.Flags().String(FlagDiscount, "0.0", "discount to apply to the swap fee (only smart contract broker can apply the discount)")
cmd.Flags().String(FlagAddress, "", "address of the account making the swap")
cmd.Flags().String(FlagRecipient, "", "optional recipient field for the tokens swapped to be sent to")

return cmd
Expand Down
4 changes: 2 additions & 2 deletions x/amm/client/cli/tx_swap_exact_amount_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CmdSwapExactAmountOut() *cobra.Command {
}
argSwapRouteDenoms := strings.Split(args[3], listSeparator)

discountStr, err := cmd.Flags().GetString(FlagDiscount)
discountStr, err := cmd.Flags().GetString(FlagAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func CmdSwapExactAmountOut() *cobra.Command {

flags.AddTxFlagsToCmd(cmd)

cmd.Flags().String(FlagDiscount, "0.0", "discount to apply to the swap fee (only smart contract broker can apply the discount)")
cmd.Flags().String(FlagAddress, "", "address of the account making the swap")
cmd.Flags().String(FlagRecipient, "", "optional recipient field for the tokens swapped to be sent to")

return cmd
Expand Down
4 changes: 2 additions & 2 deletions x/amm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (k Keeper) ApplySwapRequest(ctx sdk.Context, msg sdk.Msg) error {
if err != nil {
recipient = sender
}
_, _, _, err = k.RouteExactAmountIn(ctx, sender, recipient, msg.Routes, msg.TokenIn, msg.TokenOutMinAmount, msg.Discount)
_, _, _, err = k.RouteExactAmountIn(ctx, sender, recipient, msg.Routes, msg.TokenIn, msg.TokenOutMinAmount)
if err != nil {
return err
}
Expand All @@ -46,7 +46,7 @@ func (k Keeper) ApplySwapRequest(ctx sdk.Context, msg sdk.Msg) error {
if err != nil {
recipient = sender
}
_, _, _, err = k.RouteExactAmountOut(ctx, sender, recipient, msg.Routes, msg.TokenInMaxAmount, msg.TokenOut, msg.Discount)
_, _, _, err = k.RouteExactAmountOut(ctx, sender, recipient, msg.Routes, msg.TokenInMaxAmount, msg.TokenOut)
if err != nil {
return err
}
Expand Down
15 changes: 0 additions & 15 deletions x/amm/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
},
expSwapOrder: []uint64{0},
Expand All @@ -54,7 +53,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Expand All @@ -66,7 +64,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.BaseCurrency, 8000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
},
expSwapOrder: []uint64{1, 0},
Expand All @@ -85,7 +82,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.Elys, 11000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Expand All @@ -97,7 +93,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.BaseCurrency, 8000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Expand All @@ -109,7 +104,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.BaseCurrency, 1000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
},
expSwapOrder: []uint64{1, 0, 2},
Expand All @@ -128,7 +122,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.Elys, 11000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountOut{
Sender: sender.String(),
Expand All @@ -140,7 +133,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenOut: sdk.NewInt64Coin(ptypes.Elys, 8000),
TokenInMaxAmount: sdkmath.NewInt(1000000),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Expand All @@ -152,7 +144,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.BaseCurrency, 1000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
},
expSwapOrder: []uint64{2, 1, 0},
Expand All @@ -171,7 +162,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.Elys, 11000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountOut{
Sender: sender.String(),
Expand All @@ -183,7 +173,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenOut: sdk.NewInt64Coin(ptypes.Elys, 8000),
TokenInMaxAmount: sdkmath.NewInt(1000000),
Discount: sdkmath.LegacyZeroDec(),
},
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Expand All @@ -199,7 +188,6 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
},
TokenIn: sdk.NewInt64Coin(ptypes.BaseCurrency, 1000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
},
},
expSwapOrder: []uint64{2, 1, 0},
Expand Down Expand Up @@ -367,7 +355,6 @@ func (suite *AmmKeeperTestSuite) TestAbci() {
},
TokenOut: sdk.NewInt64Coin(ptypes.Elys, 8000),
TokenInMaxAmount: sdkmath.NewInt(1000000),
Discount: sdkmath.LegacyZeroDec(),
}

poolId := suite.app.AmmKeeper.FirstPoolId(msg)
Expand Down Expand Up @@ -418,7 +405,6 @@ func (suite *AmmKeeperTestSuite) TestAbci() {
},
TokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000),
TokenOutMinAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
}

err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg)
Expand Down Expand Up @@ -457,7 +443,6 @@ func (suite *AmmKeeperTestSuite) TestAbci() {
},
TokenOut: sdk.NewInt64Coin(ptypes.Elys, 10000),
TokenInMaxAmount: sdkmath.ZeroInt(),
Discount: sdkmath.LegacyZeroDec(),
}

err := suite.app.AmmKeeper.ApplySwapRequest(suite.ctx, msg)
Expand Down
14 changes: 10 additions & 4 deletions x/amm/keeper/calc_swap_estimation_by_denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (k Keeper) CalcSwapEstimationByDenom(
denomIn string,
denomOut string,
baseCurrency string,
discount sdkmath.LegacyDec,
address string,
overrideSwapFee sdkmath.LegacyDec,
decimals uint64,
) (
Expand All @@ -33,10 +33,16 @@ func (k Keeper) CalcSwapEstimationByDenom(
impactedPrice sdkmath.LegacyDec
)

addr, err := sdk.AccAddressFromBech32(address)
if err != nil {
addr = sdk.AccAddress{}
}
_, tier := k.tierKeeper.GetMembershipTier(ctx, addr)

// Initialize return variables
inRoute, outRoute = nil, nil
outAmount, availableLiquidity = sdk.Coin{}, sdk.Coin{}
spotPrice, swapFeeOut, discountOut, weightBonus, priceImpact = sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), discount, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec()
spotPrice, swapFeeOut, discountOut, weightBonus, priceImpact = sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), tier.Discount, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec()

// Determine the correct route based on the amount's denom
if amount.Denom == denomIn {
Expand All @@ -54,9 +60,9 @@ func (k Keeper) CalcSwapEstimationByDenom(

// Calculate final spot price and other outputs
if amount.Denom == denomIn {
spotPrice, impactedPrice, outAmount, swapFeeOut, _, availableLiquidity, slippage, weightBonus, err = k.CalcInRouteSpotPrice(ctx, amount, inRoute, discount, overrideSwapFee)
spotPrice, impactedPrice, outAmount, swapFeeOut, _, availableLiquidity, slippage, weightBonus, err = k.CalcInRouteSpotPrice(ctx, amount, inRoute, tier.Discount, overrideSwapFee)
} else {
spotPrice, impactedPrice, outAmount, swapFeeOut, _, availableLiquidity, slippage, weightBonus, err = k.CalcOutRouteSpotPrice(ctx, amount, outRoute, discount, overrideSwapFee)
spotPrice, impactedPrice, outAmount, swapFeeOut, _, availableLiquidity, slippage, weightBonus, err = k.CalcOutRouteSpotPrice(ctx, amount, outRoute, tier.Discount, overrideSwapFee)
}

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions x/amm/keeper/calc_swap_estimation_by_denom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (suite *AmmKeeperTestSuite) TestCalcSwapEstimationByDenom() {
suite.ctx,
amount,
ptypes.Elys, "uusda", ptypes.BaseCurrency,
sdkmath.LegacyZeroDec(),
"",
sdkmath.LegacyZeroDec(),
1,
)
Expand All @@ -110,7 +110,7 @@ func (suite *AmmKeeperTestSuite) TestCalcSwapEstimationByDenom() {
suite.ctx,
amount,
ptypes.Elys, "uusda", ptypes.BaseCurrency,
sdkmath.LegacyZeroDec(),
"",
sdkmath.LegacyZeroDec(),
1,
)
Expand All @@ -125,7 +125,7 @@ func (suite *AmmKeeperTestSuite) TestCalcSwapEstimationByDenom() {
_, _, _, _, _, _, _, _, _, _, err = suite.app.AmmKeeper.CalcSwapEstimationByDenom(
suite.ctx, amount,
ptypes.Elys, "uusda", ptypes.BaseCurrency,
sdkmath.LegacyZeroDec(),
"",
sdkmath.LegacyZeroDec(),
1,
)
Expand Down
Loading

0 comments on commit 697f3c4

Please sign in to comment.