mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-11 00:26:38 +00:00

* refactor: align variable name with rest of the code Signed-off-by: Julian Strobl <jmastr@mailbox.org>
44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package keeper
|
|
|
|
import (
|
|
"context"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/planetmint/planetmint-go/config"
|
|
"github.com/planetmint/planetmint-go/x/dao/types"
|
|
)
|
|
|
|
func (k msgServer) MintToken(goCtx context.Context, msg *types.MsgMintToken) (*types.MsgMintTokenResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
conf := config.GetConfig()
|
|
|
|
_, found := k.GetMintRequestByHash(ctx, msg.GetMintRequest().GetLiquidTxHash())
|
|
if found {
|
|
return nil, errorsmod.Wrapf(types.ErrAlreadyMinted, "liquid tx hash %s has already been minted", msg.GetMintRequest().GetLiquidTxHash())
|
|
}
|
|
|
|
amt := msg.GetMintRequest().GetAmount()
|
|
beneficiary := msg.GetMintRequest().GetBeneficiary()
|
|
beneficiaryAddr, err := sdk.AccAddressFromBech32(beneficiary)
|
|
if err != nil {
|
|
return nil, errorsmod.Wrapf(types.ErrInvalidAddress, "for provided address %s", beneficiary)
|
|
}
|
|
|
|
coin := sdk.NewCoin(conf.TokenDenom, sdk.NewIntFromUint64(amt))
|
|
coins := sdk.NewCoins(coin)
|
|
err = k.bankKeeper.MintCoins(ctx, types.ModuleName, coins)
|
|
if err != nil {
|
|
return nil, errorsmod.Wrapf(types.ErrMintFailed, "error while minting %v token for address %s", amt, beneficiary)
|
|
}
|
|
|
|
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, beneficiaryAddr, coins)
|
|
if err != nil {
|
|
return nil, errorsmod.Wrapf(types.ErrTransferFailed, "error while transferring %v token to address %s", amt, beneficiary)
|
|
}
|
|
|
|
k.StoreMintRequest(ctx, *msg.MintRequest)
|
|
|
|
return &types.MsgMintTokenResponse{}, nil
|
|
}
|