refactor(clients): improve readability

* add some error cases

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
Julian Strobl 2024-11-20 12:55:55 +01:00
parent 2ceba95685
commit d27fa4883b
No known key found for this signature in database
GPG Key ID: E0A8F9AD733499A7
3 changed files with 41 additions and 20 deletions

View File

@ -2,6 +2,8 @@ package claim
import ( import (
"context" "context"
"errors"
"fmt"
"net/http" "net/http"
"github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/config"
@ -9,22 +11,38 @@ import (
"github.com/rddl-network/rddl-claim-service/types" "github.com/rddl-network/rddl-claim-service/types"
) )
var ClaimServiceClient client.IRCClient var (
RCClient client.IRCClient
)
func lazyLoad() client.IRCClient { func lazyLoad() client.IRCClient {
if ClaimServiceClient != nil { if RCClient != nil {
return ClaimServiceClient return RCClient
} }
cfg := config.GetConfig() cfg := config.GetConfig()
ClaimServiceClient = client.NewRCClient(cfg.ClaimHost, &http.Client{}) RCClient = client.NewRCClient(cfg.ClaimHost, &http.Client{})
return ClaimServiceClient return RCClient
} }
func PostClaim(ctx context.Context, beneficiary string, amount uint64, id uint64) (txID string, err error) { func PostClaim(ctx context.Context, beneficiary string, amount uint64, id uint64) (txID string, err error) {
if beneficiary == "" {
return txID, errors.New("beneficiary cannot be empty")
}
if amount == 0 {
return txID, errors.New("amount must be greater than 0")
}
req := types.PostClaimRequest{
Beneficiary: beneficiary,
Amount: amount,
ClaimID: int(id),
}
client := lazyLoad() client := lazyLoad()
res, err := client.PostClaim(ctx, types.PostClaimRequest{Beneficiary: beneficiary, Amount: amount, ClaimID: int(id)}) resp, err := client.PostClaim(ctx, req)
if err != nil { if err != nil {
return return txID, fmt.Errorf("failed to post claim: %w", err)
} }
return res.TxID, nil return resp.TxID, nil
} }

View File

@ -2,14 +2,16 @@ package coordinator
import ( import (
"context" "context"
"log" "errors"
"github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/config"
"github.com/rddl-network/go-utils/tls" "github.com/rddl-network/go-utils/tls"
"github.com/rddl-network/shamir-coordinator-service/client" "github.com/rddl-network/shamir-coordinator-service/client"
) )
var ShamirCoordinatorServiceClient client.IShamirCoordinatorClient var (
ShamirCoordinatorServiceClient client.IShamirCoordinatorClient
)
func lazyLoad() client.IShamirCoordinatorClient { func lazyLoad() client.IShamirCoordinatorClient {
if ShamirCoordinatorServiceClient != nil { if ShamirCoordinatorServiceClient != nil {
@ -18,7 +20,8 @@ func lazyLoad() client.IShamirCoordinatorClient {
cfg := config.GetConfig() cfg := config.GetConfig()
httpsClient, err := tls.Get2WayTLSClient(cfg.CertsPath) httpsClient, err := tls.Get2WayTLSClient(cfg.CertsPath)
if err != nil { if err != nil {
defer log.Fatal("fatal error setting up mutual tls client for shamir coordinator") err := errors.New("fatal error setting up mutual tls client for shamir coordinator")
panic(err)
} }
ShamirCoordinatorServiceClient = client.NewShamirCoordinatorClient(cfg.IssuerHost, httpsClient) ShamirCoordinatorServiceClient = client.NewShamirCoordinatorClient(cfg.IssuerHost, httpsClient)
return ShamirCoordinatorServiceClient return ShamirCoordinatorServiceClient
@ -26,27 +29,27 @@ func lazyLoad() client.IShamirCoordinatorClient {
func SendTokens(ctx context.Context, recipient string, amount string, asset string) (txID string, err error) { func SendTokens(ctx context.Context, recipient string, amount string, asset string) (txID string, err error) {
client := lazyLoad() client := lazyLoad()
res, err := client.SendTokens(ctx, recipient, amount, asset) resp, err := client.SendTokens(ctx, recipient, amount, asset)
if err != nil { if err != nil {
return return
} }
return res.TxID, nil return resp.TxID, nil
} }
func ReIssueAsset(ctx context.Context, asset string, amount string) (txID string, err error) { func ReIssueAsset(ctx context.Context, asset string, amount string) (txID string, err error) {
client := lazyLoad() client := lazyLoad()
res, err := client.ReIssueAsset(ctx, asset, amount) resp, err := client.ReIssueAsset(ctx, asset, amount)
if err != nil { if err != nil {
return return
} }
return res.TxID, nil return resp.TxID, nil
} }
func IssueNFTAsset(ctx context.Context, name string, machineAddress string, domain string) (assetID string, contract string, hexTx string, err error) { func IssueNFTAsset(ctx context.Context, name string, machineAddress string, domain string) (assetID string, contract string, hexTx string, err error) {
client := lazyLoad() client := lazyLoad()
res, err := client.IssueMachineNFT(ctx, name, machineAddress, domain) resp, err := client.IssueMachineNFT(ctx, name, machineAddress, domain)
if err != nil { if err != nil {
return return
} }
return res.Asset, res.Contract, res.HexTX, nil return resp.Asset, resp.Contract, resp.HexTX, nil
} }

View File

@ -56,7 +56,7 @@ func Load(t *testing.T, configs ...Config) *Network {
claimMock.EXPECT().PostClaim(gomock.Any(), gomock.Any()).AnyTimes().Return(rcctypes.PostClaimResponse{ claimMock.EXPECT().PostClaim(gomock.Any(), gomock.Any()).AnyTimes().Return(rcctypes.PostClaimResponse{
TxID: "0000000000000000000000000000000000000000000000000000000000000000", TxID: "0000000000000000000000000000000000000000000000000000000000000000",
}, nil) }, nil)
claim.ClaimServiceClient = claimMock claim.RCClient = claimMock
shamirMock := clientmocks.NewMockIShamirCoordinatorClient(ctrl) shamirMock := clientmocks.NewMockIShamirCoordinatorClient(ctrl)
shamirMock.EXPECT().SendTokens(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(scctypes.SendTokensResponse{ shamirMock.EXPECT().SendTokens(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(scctypes.SendTokensResponse{