github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/api/tx/create.go (about)

     1  //nolint:nolintlint,dupl
     2  package tran
     3  
     4  import (
     5  	"context"
     6  
     7  	tx1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/tx"
     8  	"github.com/NpoolPlatform/go-service-framework/pkg/logger"
     9  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/tx"
    10  
    11  	"google.golang.org/grpc/codes"
    12  	"google.golang.org/grpc/status"
    13  )
    14  
    15  func (s *Server) CreateTx(ctx context.Context, in *npool.CreateTxRequest) (*npool.CreateTxResponse, error) {
    16  	req := in.GetInfo()
    17  	handler, err := tx1.NewHandler(
    18  		ctx,
    19  		tx1.WithID(req.ID, false),
    20  		tx1.WithEntID(req.EntID, false),
    21  		tx1.WithCoinTypeID(req.CoinTypeID, true),
    22  		tx1.WithFromAccountID(req.FromAccountID, true),
    23  		tx1.WithToAccountID(req.ToAccountID, true),
    24  		tx1.WithAmount(req.Amount, true),
    25  		tx1.WithFeeAmount(req.FeeAmount, true),
    26  		tx1.WithChainTxID(req.ChainTxID, false),
    27  		tx1.WithExtra(req.Extra, true),
    28  		tx1.WithType(req.Type, true),
    29  	)
    30  	if err != nil {
    31  		logger.Sugar().Errorw(
    32  			"CreateTx",
    33  			"In", in,
    34  			"Error", err,
    35  		)
    36  		return &npool.CreateTxResponse{}, status.Error(codes.InvalidArgument, err.Error())
    37  	}
    38  
    39  	info, err := handler.CreateTx(ctx)
    40  	if err != nil {
    41  		logger.Sugar().Errorw(
    42  			"CreateTx",
    43  			"In", in,
    44  			"Error", err,
    45  		)
    46  		return &npool.CreateTxResponse{}, status.Error(codes.Internal, err.Error())
    47  	}
    48  
    49  	return &npool.CreateTxResponse{
    50  		Info: info,
    51  	}, nil
    52  }
    53  
    54  func (s *Server) CreateTxs(ctx context.Context, in *npool.CreateTxsRequest) (*npool.CreateTxsResponse, error) {
    55  	handler, err := tx1.NewHandler(
    56  		ctx,
    57  		tx1.WithReqs(in.GetInfos(), true),
    58  	)
    59  	if err != nil {
    60  		logger.Sugar().Errorw(
    61  			"CreateTxs",
    62  			"In", in,
    63  			"Error", err,
    64  		)
    65  		return &npool.CreateTxsResponse{}, status.Error(codes.InvalidArgument, err.Error())
    66  	}
    67  
    68  	infos, err := handler.CreateTxs(ctx)
    69  	if err != nil {
    70  		logger.Sugar().Errorw(
    71  			"CreateTxs",
    72  			"In", in,
    73  			"Error", err,
    74  		)
    75  		return &npool.CreateTxsResponse{}, status.Error(codes.Internal, err.Error())
    76  	}
    77  
    78  	return &npool.CreateTxsResponse{
    79  		Infos: infos,
    80  	}, nil
    81  }