planetmint-go/x/dao/keeper/msg_server_mint_token.go
Julian Strobl 6d05f85cbd
Use lib in rest e2e machine test (#263)
* refactor: align variable name with rest of the code

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2024-01-09 10:14:54 +01:00

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
}