|
| 1 | +// x/scavenge/keeper/msg_server_submit_scavenge.go |
| 2 | + |
1 | 3 | package keeper |
2 | 4 |
|
3 | 5 | import ( |
4 | 6 | "context" |
5 | 7 |
|
6 | 8 | sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 10 | + "github.com/tendermint/tendermint/crypto" |
| 11 | + |
7 | 12 | "scavenge/x/scavenge/types" |
8 | 13 | ) |
9 | 14 |
|
10 | 15 | func (k msgServer) SubmitScavenge(goCtx context.Context, msg *types.MsgSubmitScavenge) (*types.MsgSubmitScavengeResponse, error) { |
| 16 | + // get context that contains information about the environment, such as block height |
11 | 17 | ctx := sdk.UnwrapSDKContext(goCtx) |
12 | 18 |
|
13 | | - // TODO: Handling the message |
14 | | - _ = ctx |
| 19 | + // create a new scavenge from the data in the MsgSubmitScavenge message |
| 20 | + var scavenge = types.Scavenge{ |
| 21 | + Index: msg.SolutionHash, |
| 22 | + Description: msg.Description, |
| 23 | + SolutionHash: msg.SolutionHash, |
| 24 | + Reward: msg.Reward, |
| 25 | + } |
| 26 | + |
| 27 | + // try getting a scavenge from the store using the solution hash as the key |
| 28 | + _, isFound := k.GetScavenge(ctx, scavenge.SolutionHash) |
| 29 | + |
| 30 | + // return an error if a scavenge already exists in the store |
| 31 | + if isFound { |
| 32 | + return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Scavenge with that solution hash already exists") |
| 33 | + } |
| 34 | + |
| 35 | + // get address of the Scavenge module account |
| 36 | + moduleAcct := sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleName))) |
| 37 | + |
| 38 | + // convert the message creator address from a string into sdk.AccAddress |
| 39 | + scavenger, err := sdk.AccAddressFromBech32(msg.Creator) |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + |
| 44 | + // convert tokens from string into sdk.Coins |
| 45 | + reward, err := sdk.ParseCoinsNormalized(scavenge.Reward) |
| 46 | + if err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + |
| 50 | + // send tokens from the scavenge creator to the module account |
| 51 | + sdkError := k.bankKeeper.SendCoins(ctx, scavenger, moduleAcct, reward) |
| 52 | + if sdkError != nil { |
| 53 | + return nil, sdkError |
| 54 | + } |
15 | 55 |
|
| 56 | + // write the scavenge to the store |
| 57 | + k.SetScavenge(ctx, scavenge) |
16 | 58 | return &types.MsgSubmitScavengeResponse{}, nil |
17 | 59 | } |
0 commit comments