-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1227 from lavanet/CNS-869-iprpc-fund-tx
CNS-869: IPRPC part 3 - IPRPC fund TX
- Loading branch information
Showing
15 changed files
with
805 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cli | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lavanet/lava/x/rewards/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var _ = strconv.Itoa(0) | ||
|
||
func CmdFundIprpc() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "fund-iprpc [spec] [duration] [coins] --from <creator>", | ||
Short: `fund the IPRPC pool to a specific spec with ulava or IBC wrapped tokens. The tokens will be vested for <duration> months. | ||
Note that the amount of coins you put is the monthly quota (it's not for the total period of time). | ||
Also, the tokens must include duration*min_iprpc_cost of ulava tokens (min_iprpc_cost is shown with the show-iprpc-data command)`, | ||
Example: `lavad tx rewards fund-iprpc ETH1 4 100000ulava,50000ibctoken --from alice | ||
This command will transfer 4*100000ulava and 4*50000ibctoken to the IPRPC pool to be distributed for 4 months`, | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
spec := args[0] | ||
durationStr := args[1] | ||
duration, err := strconv.ParseUint(durationStr, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fundStr := args[2] | ||
fund, err := sdk.ParseCoinsNormalized(fundStr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := types.NewMsgFundIprpc( | ||
clientCtx.GetFromAddress().String(), | ||
spec, | ||
duration, | ||
fund, | ||
) | ||
if err := msg.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lavanet/lava/utils" | ||
"github.com/lavanet/lava/x/rewards/types" | ||
) | ||
|
||
func (k msgServer) FundIprpc(goCtx context.Context, msg *types.MsgFundIprpc) (*types.MsgFundIprpcResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
err := msg.ValidateBasic() | ||
if err != nil { | ||
return &types.MsgFundIprpcResponse{}, err | ||
} | ||
|
||
err = k.Keeper.FundIprpc(ctx, msg.Creator, msg.Duration, msg.Amounts, msg.Spec) | ||
if err == nil { | ||
logger := k.Keeper.Logger(ctx) | ||
details := map[string]string{ | ||
"spec": msg.Spec, | ||
"duration": strconv.FormatUint(msg.Duration, 10), | ||
"amounts": msg.Amounts.String(), | ||
} | ||
utils.LogLavaEvent(ctx, logger, types.FundIprpcEventName, details, "Funded IPRPC pool successfully") | ||
} | ||
|
||
return &types.MsgFundIprpcResponse{}, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package types | ||
|
||
import ( | ||
fmt "fmt" | ||
|
||
sdkerrors "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
legacyerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
const TypeMsgFundIprpc = "fund_iprpc" | ||
|
||
var _ sdk.Msg = &MsgFundIprpc{} | ||
|
||
func NewMsgFundIprpc(creator string, spec string, duration uint64, amounts sdk.Coins) *MsgFundIprpc { | ||
return &MsgFundIprpc{ | ||
Creator: creator, | ||
Spec: spec, | ||
Duration: duration, | ||
Amounts: amounts, | ||
} | ||
} | ||
|
||
func (msg *MsgFundIprpc) Route() string { | ||
return RouterKey | ||
} | ||
|
||
func (msg *MsgFundIprpc) Type() string { | ||
return TypeMsgFundIprpc | ||
} | ||
|
||
func (msg *MsgFundIprpc) GetSigners() []sdk.AccAddress { | ||
creator, err := sdk.AccAddressFromBech32(msg.Creator) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return []sdk.AccAddress{creator} | ||
} | ||
|
||
func (msg *MsgFundIprpc) GetSignBytes() []byte { | ||
bz := ModuleCdc.MustMarshalJSON(msg) | ||
return sdk.MustSortJSON(bz) | ||
} | ||
|
||
func (msg *MsgFundIprpc) ValidateBasic() error { | ||
_, err := sdk.AccAddressFromBech32(msg.Creator) | ||
if err != nil { | ||
return sdkerrors.Wrapf(legacyerrors.ErrInvalidAddress, "invalid creator address (%s)", err) | ||
} | ||
|
||
unique := map[string]struct{}{} | ||
for _, amount := range msg.Amounts { | ||
if !amount.IsValid() { | ||
return sdkerrors.Wrap(fmt.Errorf("invalid amount; invalid denom or negative amount. coin: %s", amount.String()), "") | ||
} | ||
if _, ok := unique[amount.Denom]; ok { | ||
return sdkerrors.Wrap(fmt.Errorf("invalid coins, duplicated denom: %s", amount.Denom), "") | ||
} | ||
unique[amount.Denom] = struct{}{} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lavanet/lava/testutil/sample" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFundIprpc_ValidateBasic(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
msg MsgFundIprpc | ||
valid bool | ||
}{ | ||
{ | ||
name: "valid", | ||
msg: MsgFundIprpc{ | ||
Creator: sample.AccAddress(), | ||
Duration: 2, | ||
Amounts: sdk.NewCoins(sdk.NewCoin("denom", math.OneInt())), | ||
Spec: "spec", | ||
}, | ||
valid: true, | ||
}, | ||
{ | ||
name: "invalid creator address", | ||
msg: MsgFundIprpc{ | ||
Creator: "invalid_address", | ||
Duration: 2, | ||
Amounts: sdk.NewCoins(sdk.NewCoin("denom", math.OneInt())), | ||
Spec: "spec", | ||
}, | ||
valid: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.msg.ValidateBasic() | ||
if tt.valid { | ||
require.NoError(t, err) | ||
return | ||
} | ||
require.Error(t, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.