Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: percentage based fee on stream payment #21

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func NewStreamPayApp(
app.DistrKeeper,
govModAddress,
)
streampayModule := streampay.NewAppModule(appCodec, app.StreamPayKeeper, app.GetSubspace(streampaytypes.ModuleName))
streampayModule := streampay.NewAppModule(appCodec, app.StreamPayKeeper)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
cosmossdk.io/errors v1.0.0
cosmossdk.io/math v1.1.2
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
Expand Down Expand Up @@ -32,7 +33,6 @@ require (
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/log v1.2.1 // indirect
cosmossdk.io/math v1.1.2 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,6 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
Expand Down
6 changes: 3 additions & 3 deletions proto/OmniFlix/streampay/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ option (gogoproto.goproto_getters_all) = false;


message Params {
cosmos.base.v1beta1.Coin stream_payment_fee = 1 [
(gogoproto.moretags) = "yaml:\"stream_payment_fee\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
string stream_payment_fee_percentage = 1 [
(gogoproto.moretags) = "yaml:\"stream_payment_fee_percentage\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
4 changes: 0 additions & 4 deletions proto/OmniFlix/streampay/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ message MsgStreamSend {
StreamType stream_type = 5 [(gogoproto.moretags) = "yaml:\"stream_type\""];
repeated Period periods = 6 [(gogoproto.nullable) = true];
bool cancellable = 7;
cosmos.base.v1beta1.Coin fee = 8 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
}

message MsgStreamSendResponse {
Expand Down
2 changes: 0 additions & 2 deletions x/streampay/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const (
FlagDelayed = "delayed"
FlagStreamPeriodsFile = "stream-periods-file"
FlagCancellable = "cancellable"
FlagStreamPaymentFee = "stream-payment-fee"
)

var FsStreamSend = flag.NewFlagSet("", flag.ContinueOnError)
Expand All @@ -19,5 +18,4 @@ func init() {
FsStreamSend.Bool(FlagDelayed, false, "use to set delayed stream")
FsStreamSend.String(FlagStreamPeriodsFile, "", "stream periods json file")
FsStreamSend.Bool(FlagCancellable, false, "use to set cancellable stream")
FsStreamSend.String(FlagStreamPaymentFee, "", "stream payment fee")
}
10 changes: 0 additions & 10 deletions x/streampay/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ func GetCmdStreamSend() *cobra.Command {
if err != nil {
return err
}
feeStr, err := cmd.Flags().GetString(FlagStreamPaymentFee)
if err != nil {
return err
}
fee, err := sdk.ParseCoinNormalized(feeStr)
if err != nil {
return err
}

msg := types.NewMsgStreamSend(
sender.String(),
Expand All @@ -140,7 +132,6 @@ func GetCmdStreamSend() *cobra.Command {
duration,
periods,
cancellable,
fee,
)

if err := msg.ValidateBasic(); err != nil {
Expand All @@ -153,7 +144,6 @@ func GetCmdStreamSend() *cobra.Command {

cmd.Flags().AddFlagSet(FsStreamSend)
_ = cmd.MarkFlagRequired(FlagDuration)
_ = cmd.MarkFlagRequired(FlagStreamPaymentFee)
flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
14 changes: 0 additions & 14 deletions x/streampay/exported/stream_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package exported
import (
"time"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -17,15 +15,3 @@ type StreamPaymentI interface {
GetEndTime() time.Time
GetStreamedAmount() sdk.Coin
}

type (
ParamSet = paramtypes.ParamSet

// Subspace defines an interface that implements the legacy x/params Subspace
// type.
//
// NOTE: This is used solely for migration of x/params managed parameters.
Subspace interface {
GetParamSet(ctx sdk.Context, ps ParamSet)
}
)
17 changes: 7 additions & 10 deletions x/streampay/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ func TestKeeperTestSuite(t *testing.T) {
func (suite *KeeperTestSuite) SetupTest() {
suite.Setup()
fundAccsAmount := sdk.NewCoins(
sdk.NewCoin(
types.DefaultParams().StreamPaymentFee.Denom,
types.DefaultParams().StreamPaymentFee.Amount.MulRaw(1000)))
sdk.NewCoin("uspay", sdk.NewInt(10_000_000_000)),
)
for _, acc := range suite.TestAccs {
suite.FundAcc(acc, fundAccsAmount)
}
Expand All @@ -42,12 +41,11 @@ func (suite *KeeperTestSuite) CreateDefaultStreamPayment(cancellable bool) {
res, _ := suite.msgServer.StreamSend(ctx, &types.MsgStreamSend{
Sender: suite.TestAccs[0].String(),
Recipient: suite.TestAccs[1].String(),
Amount: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 100_000_000),
Amount: sdk.NewInt64Coin("uspay", 100_000_000),
StreamType: types.TypeContinuous,
Duration: 100,
Periods: nil,
Cancellable: cancellable,
Fee: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 10_000_000),
})
suite.defaultStreamPaymentId = res.StreamId
}
Expand All @@ -57,12 +55,11 @@ func (suite *KeeperTestSuite) CreateStreamPayment(streamType types.StreamType, c
res, _ := suite.msgServer.StreamSend(ctx, &types.MsgStreamSend{
Sender: suite.TestAccs[0].String(),
Recipient: suite.TestAccs[1].String(),
Amount: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 100_000_000),
Amount: sdk.NewInt64Coin("uspay", 100_000_000),
StreamType: streamType,
Duration: 100,
Periods: nil,
Cancellable: cancellable,
Fee: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 10_000_000),
})
return res.StreamId
}
Expand All @@ -76,21 +73,21 @@ func (suite *KeeperTestSuite) TestParams() {
{
name: "set invalid fee",
input: types.Params{
StreamPaymentFee: sdk.Coin{},
StreamPaymentFeePercentage: sdk.NewDec(1),
},
expectErr: true,
},
{
name: "set invalid fee",
input: types.Params{
StreamPaymentFee: sdk.NewCoin("foo", sdk.ZeroInt()),
StreamPaymentFeePercentage: sdk.NewDec(-1),
},
expectErr: true,
},
{
name: "set full valid params",
input: types.Params{
StreamPaymentFee: types.DefaultStreamPaymentFee,
StreamPaymentFeePercentage: types.DefaultStreamPaymentFeePercentage,
},
expectErr: false,
},
Expand Down
28 changes: 0 additions & 28 deletions x/streampay/keeper/migrator.go

This file was deleted.

16 changes: 6 additions & 10 deletions x/streampay/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/OmniFlix/streampay/v2/x/streampay/types"
Expand Down Expand Up @@ -47,21 +46,18 @@ func (m msgServer) StreamSend(goCtx context.Context, msg *types.MsgStreamSend) (
if err != nil {
return nil, err
}
fee := m.Keeper.GetStreamPaymentFee(ctx)
if !msg.Fee.Equal(fee) {
return nil, errorsmod.Wrapf(
types.ErrInvalidFee,
fmt.Sprintf("fee must be %s", fee.String()),
)
}
if err := m.distributionKeeper.FundCommunityPool(ctx, sdk.NewCoins(msg.Fee), sender); err != nil {
feePercentage := m.Keeper.GetStreamPaymentFeePercentage(ctx)
feeAmount := sdk.NewCoin(msg.Amount.Denom, msg.Amount.Amount.ToLegacyDec().Mul(feePercentage).TruncateInt())
amountToSend := msg.Amount.SubAmount(feeAmount.Amount)

if err := m.distributionKeeper.FundCommunityPool(ctx, sdk.NewCoins(feeAmount), sender); err != nil {
return nil, err
}

streamPaymentId, err := m.Keeper.CreateStreamPayment(
ctx,
sender, recipient,
msg.Amount,
amountToSend,
msg.StreamType,
msg.Duration,
msg.Periods,
Expand Down
29 changes: 6 additions & 23 deletions x/streampay/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,35 @@ func (suite *KeeperTestSuite) TestStreamSendMsg() {
duration time.Duration
periods []*types.Period
cancellable bool
fee sdk.Coin
valid bool
expectedMessageEvents int
}{
{
sender: suite.TestAccs[0].String(),
recipient: suite.TestAccs[1].String(),
amount: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 100_000_000),
amount: sdk.NewInt64Coin("uspay", 100_000_000),
streamType: types.TypeContinuous,
duration: time.Second * 100,
periods: nil,
cancellable: false,
fee: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 10_000_000),
valid: true,
expectedMessageEvents: 1,
},
{
sender: suite.TestAccs[0].String(),
recipient: suite.TestAccs[1].String(),
amount: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 10_000_000),
streamType: types.TypeContinuous,
duration: time.Second * 100,
periods: nil,
cancellable: false,
fee: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 1_000_000),
valid: false,
expectedMessageEvents: 0,
},
{
sender: suite.TestAccs[0].String(),
recipient: suite.TestAccs[1].String(),
amount: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 100_000_000),
amount: sdk.NewInt64Coin("uspay", 100_000_000),
streamType: types.TypeDelayed,
duration: time.Second * 100,
periods: nil,
cancellable: false,
fee: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 10_000_000),
valid: true,
expectedMessageEvents: 1,
},
{
sender: suite.TestAccs[0].String(),
recipient: suite.TestAccs[1].String(),
amount: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 100_000_000),
amount: sdk.NewInt64Coin("uspay", 100_000_000),
streamType: types.TypePeriodic,
duration: time.Second * 100,
periods: []*types.Period{
Expand Down Expand Up @@ -106,7 +91,6 @@ func (suite *KeeperTestSuite) TestStreamSendMsg() {
},
},
cancellable: true,
fee: sdk.NewInt64Coin(types.DefaultParams().StreamPaymentFee.Denom, 10_000_000),
valid: true,
expectedMessageEvents: 1,
},
Expand All @@ -125,7 +109,6 @@ func (suite *KeeperTestSuite) TestStreamSendMsg() {
tc.duration,
tc.periods,
tc.cancellable,
tc.fee,
),
)
if tc.valid {
Expand Down Expand Up @@ -246,7 +229,7 @@ func (suite *KeeperTestSuite) TestUpdateParams() {
request: &types.MsgUpdateParams{
Authority: suite.App.StreamPayKeeper.GetAuthority(),
Params: types.Params{
StreamPaymentFee: sdk.NewCoin("foo", sdk.ZeroInt()),
StreamPaymentFeePercentage: sdk.NewDec(1),
},
},
expectErr: true,
Expand All @@ -256,7 +239,7 @@ func (suite *KeeperTestSuite) TestUpdateParams() {
request: &types.MsgUpdateParams{
Authority: suite.App.StreamPayKeeper.GetAuthority(),
Params: types.Params{
StreamPaymentFee: sdk.Coin{},
StreamPaymentFeePercentage: sdk.NewDecWithPrec(-5, 2),
},
},
expectErr: true,
Expand All @@ -266,7 +249,7 @@ func (suite *KeeperTestSuite) TestUpdateParams() {
request: &types.MsgUpdateParams{
Authority: suite.App.StreamPayKeeper.GetAuthority(),
Params: types.Params{
StreamPaymentFee: types.DefaultStreamPaymentFee,
StreamPaymentFeePercentage: types.DefaultStreamPaymentFeePercentage,
},
},
expectErr: false,
Expand Down
6 changes: 3 additions & 3 deletions x/streampay/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
return nil
}

// GetStreamPaymentFee returns the current stream payment fee.
func (k Keeper) GetStreamPaymentFee(ctx sdk.Context) sdk.Coin {
// GetStreamPaymentFeePercentage returns the current stream payment fee.
func (k Keeper) GetStreamPaymentFeePercentage(ctx sdk.Context) sdk.Dec {
params := k.GetParams(ctx)
return params.StreamPaymentFee
return params.StreamPaymentFeePercentage
}
Loading
Loading