github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/fiat/currency/history/client.go (about)

     1  package currencyhistory
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	grpc2 "github.com/NpoolPlatform/go-service-framework/pkg/grpc"
     8  
     9  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    10  	currencymwpb "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat/currency"
    11  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat/currency/history"
    12  
    13  	servicename "github.com/NpoolPlatform/chain-middleware/pkg/servicename"
    14  )
    15  
    16  var timeout = 10 * time.Second
    17  
    18  type handler func(context.Context, npool.MiddlewareClient) (cruder.Any, error)
    19  
    20  func withCRUD(ctx context.Context, handler handler) (cruder.Any, error) {
    21  	_ctx, cancel := context.WithTimeout(ctx, timeout)
    22  	defer cancel()
    23  
    24  	conn, err := grpc2.GetGRPCConn(servicename.ServiceDomain, grpc2.GRPCTAG)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	defer conn.Close()
    30  
    31  	cli := npool.NewMiddlewareClient(conn)
    32  
    33  	return handler(_ctx, cli)
    34  }
    35  
    36  func GetCurrencies(ctx context.Context, conds *npool.Conds, offset, limit int32) ([]*currencymwpb.Currency, uint32, error) {
    37  	total := uint32(0)
    38  
    39  	infos, err := withCRUD(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
    40  		resp, err := cli.GetCurrencies(ctx, &npool.GetCurrenciesRequest{
    41  			Conds:  conds,
    42  			Offset: offset,
    43  			Limit:  limit,
    44  		})
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  
    49  		total = resp.Total
    50  
    51  		return resp.Infos, nil
    52  	})
    53  	if err != nil {
    54  		return nil, 0, err
    55  	}
    56  	return infos.([]*currencymwpb.Currency), total, nil
    57  }