forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial airdrop support (cosmos#94)
- Loading branch information
1 parent
1227bf3
commit a59a3d4
Showing
31 changed files
with
5,510 additions
and
144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,35 @@ | ||
syntax = "proto3"; | ||
package cosmos.airdrop.v1beta1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "cosmos_proto/cosmos.proto"; | ||
import "cosmos/base/v1beta1/coin.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/airdrop/types"; | ||
|
||
// Fund defines a structure for a fund that is being distributed to network stakers | ||
message Fund { | ||
option (gogoproto.equal) = true; | ||
|
||
// The amount of fund that is remaining | ||
cosmos.base.v1beta1.Coin amount = 1 [(gogoproto.moretags) = "yaml:\"amount\""]; | ||
|
||
// The amount of funds that should be removed from the fund every block | ||
string drip_amount = 2 [ | ||
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", | ||
(gogoproto.nullable) = false, | ||
(gogoproto.moretags) = "yaml:\"drip_amount\"" | ||
]; | ||
} | ||
|
||
// ActiveFund describes an active fund on the network | ||
message ActiveFund { | ||
string sender = 1 [(gogoproto.moretags) = "yaml:\"sender\""]; | ||
Fund fund = 2 [(gogoproto.moretags) = "yaml:\"blocks_remaining\""]; | ||
} | ||
|
||
// Params define the module parameters | ||
message Params { | ||
// The set of addresses which are allowed to create are drop funds | ||
repeated string allow_list = 1 [(gogoproto.moretags) = "yaml:\"allow_list\""]; | ||
} |
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,16 @@ | ||
syntax = "proto3"; | ||
package cosmos.airdrop.v1beta1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "cosmos/airdrop/v1beta1/airdrop.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/airdrop/types"; | ||
|
||
// GenesisState defines the bank module's genesis state. | ||
message GenesisState { | ||
// params defines all the parameters of the module. | ||
Params params = 1 [(gogoproto.nullable) = false]; | ||
|
||
// balances is an array containing the balances of all the accounts. | ||
repeated ActiveFund funds = 2 [(gogoproto.nullable) = false]; | ||
} |
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,57 @@ | ||
syntax = "proto3"; | ||
package cosmos.airdrop.v1beta1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "google/api/annotations.proto"; | ||
import "cosmos/base/query/v1beta1/pagination.proto"; | ||
import "cosmos/airdrop/v1beta1/airdrop.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/airdrop/types"; | ||
|
||
// Query defines the gRPC querier service. | ||
service Query { | ||
|
||
// AllFunds queries all active airdrop funds | ||
rpc AllFunds(QueryAllFundsRequest) returns (QueryAllFundsResponse) { | ||
option (google.api.http).get = "/cosmos/airdrop/v1beta1/funds"; | ||
} | ||
|
||
// Fund queries a specific airdrop fund | ||
rpc Fund(QueryFundRequest) returns (QueryFundResponse) { | ||
option (google.api.http).get = "/cosmos/airdrop/v1beta1/funds/{address}"; | ||
} | ||
|
||
// Params queries the current modules parameters | ||
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { | ||
option (google.api.http).get = "/cosmos/airdrop/v1beta1/params"; | ||
} | ||
} | ||
|
||
// QueryAllFundsRequest defines the request for querying all the funds | ||
message QueryAllFundsRequest { | ||
cosmos.base.query.v1beta1.PageRequest pagination = 1; | ||
} | ||
|
||
// QueryAllFundsResponse defines the response for querying all the funds | ||
message QueryAllFundsResponse { | ||
repeated cosmos.airdrop.v1beta1.ActiveFund funds = 1; | ||
cosmos.base.query.v1beta1.PageResponse pagination = 2; | ||
} | ||
|
||
// QueryFundRequest defines the request for querying a specific fund | ||
message QueryFundRequest { | ||
string address = 1 [(gogoproto.nullable) = true]; | ||
} | ||
|
||
// QueryFundResponse defines the response for querying a specific fund | ||
message QueryFundResponse { | ||
cosmos.airdrop.v1beta1.Fund fund = 1; | ||
} | ||
|
||
// QueryParamsRequest defines the request type for querying x/airdrop parameters. | ||
message QueryParamsRequest {} | ||
|
||
// QueryParamsResponse defines the response type for querying x/airdrop parameters. | ||
message QueryParamsResponse { | ||
Params params = 1 [(gogoproto.nullable) = false]; | ||
} |
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,26 @@ | ||
syntax = "proto3"; | ||
package cosmos.airdrop.v1beta1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "cosmos/airdrop/v1beta1/airdrop.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/airdrop/types"; | ||
|
||
// Msg defines the airdrop Msg service. | ||
service Msg { | ||
|
||
// AirDrop defines a method for sending coins to the airdrop module for distribution | ||
rpc AirDrop(MsgAirDrop) returns (MsgAirDropResponse); | ||
} | ||
|
||
// MsgAirDrop represents a message to create an airdrop fund for distribution | ||
message MsgAirDrop { | ||
option (gogoproto.equal) = false; | ||
option (gogoproto.goproto_getters) = false; | ||
|
||
string from_address = 1 [(gogoproto.moretags) = "yaml:\"from_address\""]; | ||
Fund fund = 2 [(gogoproto.moretags) = "yaml:\"fund\""]; | ||
} | ||
|
||
// MsgAirDropResponse represents a message for the response | ||
message MsgAirDropResponse {} |
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,18 @@ | ||
package airdrop | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/telemetry" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/airdrop/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/airdrop/types" | ||
"time" | ||
) | ||
|
||
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { | ||
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) | ||
|
||
_, err := k.DripAllFunds(ctx) | ||
if err != nil { | ||
ctx.Logger().Error("Unable to perform airdrop drip", "err", err.Error()) | ||
} | ||
} |
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,70 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/version" | ||
"github.com/cosmos/cosmos-sdk/x/airdrop/types" | ||
"github.com/spf13/cobra" | ||
"strings" | ||
) | ||
|
||
func GetQueryCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: types.ModuleName, | ||
Short: "Querying commands for the bank module", | ||
DisableFlagParsing: true, | ||
SuggestionsMinimumDistance: 2, | ||
RunE: client.ValidateCmd, | ||
} | ||
|
||
cmd.AddCommand( | ||
GetFunds(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func GetFunds() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "funds", | ||
Short: "Query the currently active airdrop funds", | ||
Long: strings.TrimSpace( | ||
fmt.Sprintf(`Query the currently active airdrop funds. | ||
Example: | ||
$ %s query %s funds | ||
`, | ||
version.AppName, types.ModuleName, | ||
), | ||
), | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
pageReq, err := client.ReadPageRequest(cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
params := types.NewQueryAllFundsRequest(pageReq) | ||
|
||
res, err := queryClient.AllFunds(cmd.Context(), params) | ||
if err != nil { | ||
return err | ||
} | ||
return clientCtx.PrintProto(res) | ||
}, | ||
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
flags.AddPaginationFlagsToCmd(cmd, "all funds") | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"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" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/version" | ||
"github.com/cosmos/cosmos-sdk/x/airdrop/types" | ||
"github.com/spf13/cobra" | ||
"strings" | ||
) | ||
|
||
func GetTxCmd() *cobra.Command { | ||
txCmd := &cobra.Command{ | ||
Use: types.ModuleName, | ||
Short: "Airdrop transaction subcommands", | ||
DisableFlagParsing: true, | ||
SuggestionsMinimumDistance: 2, | ||
RunE: client.ValidateCmd, | ||
} | ||
|
||
txCmd.AddCommand(NewCreateTxCmd()) | ||
|
||
return txCmd | ||
} | ||
|
||
func NewCreateTxCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create [from_key_or_address] [amount] [drip_amount]", | ||
Short: `Creates a new airdrop fund. Note, the '--from' flag is | ||
ignored as it is implied from [from_key_or_address].`, | ||
Long: strings.TrimSpace( | ||
fmt.Sprintf(`Creates a new airdrop fund. | ||
When creating an airdrop fund, the sender transfers a specified amount of funds to the block chain along with | ||
the drip amount. Every block up to a maximum of drip_amount of funds are added to the rewards of the current block. | ||
The maximum duration of the airdrop is therefore calculated as amount / drip_amount blocks. | ||
Example: | ||
$ %s tx %s create [address] [amount] [drip_amount] | ||
$ %s tx %s create fetch1se8mjg4mtvy8zaf4599m84xz4atn59dlqmwhnl 200000000000000000000afet 2000000000000000000 | ||
`, | ||
version.AppName, types.ModuleName, version.AppName, types.ModuleName, | ||
), | ||
), | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.Flags().Set(flags.FlagFrom, args[0]) | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
coin, err := sdk.ParseCoinNormalized(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dripAmount, okay := sdk.NewIntFromString(args[2]) | ||
if !okay || !dripAmount.IsPositive() { | ||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s is not a valid drip rate", args[2]) | ||
} | ||
|
||
msg := types.NewMsgAirDrop(clientCtx.GetFromAddress(), coin, dripAmount) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package airdrop | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/airdrop/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/airdrop/types" | ||
) | ||
|
||
func NewHandler(k keeper.Keeper) sdk.Handler { | ||
msgServer := keeper.NewMsgServerImpl(k) | ||
|
||
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { | ||
ctx = ctx.WithEventManager(sdk.NewEventManager()) | ||
|
||
switch msg := msg.(type) { | ||
case *types.MsgAirDrop: | ||
res, err := msgServer.AirDrop(sdk.WrapSDKContext(ctx), msg) | ||
return sdk.WrapServiceResult(ctx, res, err) | ||
|
||
default: | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized airdrop message type: %T", msg) | ||
} | ||
} | ||
} |
Oops, something went wrong.