mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-07-08 05:32:30 +00:00
refactor(clients): improve readability
* add some error cases Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
2ceba95685
commit
d27fa4883b
@ -2,6 +2,8 @@ package claim
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
@ -9,22 +11,38 @@ import (
|
||||
"github.com/rddl-network/rddl-claim-service/types"
|
||||
)
|
||||
|
||||
var ClaimServiceClient client.IRCClient
|
||||
var (
|
||||
RCClient client.IRCClient
|
||||
)
|
||||
|
||||
func lazyLoad() client.IRCClient {
|
||||
if ClaimServiceClient != nil {
|
||||
return ClaimServiceClient
|
||||
if RCClient != nil {
|
||||
return RCClient
|
||||
}
|
||||
cfg := config.GetConfig()
|
||||
ClaimServiceClient = client.NewRCClient(cfg.ClaimHost, &http.Client{})
|
||||
return ClaimServiceClient
|
||||
RCClient = client.NewRCClient(cfg.ClaimHost, &http.Client{})
|
||||
return RCClient
|
||||
}
|
||||
|
||||
func PostClaim(ctx context.Context, beneficiary string, amount uint64, id uint64) (txID string, err error) {
|
||||
client := lazyLoad()
|
||||
res, err := client.PostClaim(ctx, types.PostClaimRequest{Beneficiary: beneficiary, Amount: amount, ClaimID: int(id)})
|
||||
if err != nil {
|
||||
return
|
||||
if beneficiary == "" {
|
||||
return txID, errors.New("beneficiary cannot be empty")
|
||||
}
|
||||
return res.TxID, nil
|
||||
|
||||
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()
|
||||
resp, err := client.PostClaim(ctx, req)
|
||||
if err != nil {
|
||||
return txID, fmt.Errorf("failed to post claim: %w", err)
|
||||
}
|
||||
return resp.TxID, nil
|
||||
}
|
||||
|
@ -2,14 +2,16 @@ package coordinator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"errors"
|
||||
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
"github.com/rddl-network/go-utils/tls"
|
||||
"github.com/rddl-network/shamir-coordinator-service/client"
|
||||
)
|
||||
|
||||
var ShamirCoordinatorServiceClient client.IShamirCoordinatorClient
|
||||
var (
|
||||
ShamirCoordinatorServiceClient client.IShamirCoordinatorClient
|
||||
)
|
||||
|
||||
func lazyLoad() client.IShamirCoordinatorClient {
|
||||
if ShamirCoordinatorServiceClient != nil {
|
||||
@ -18,7 +20,8 @@ func lazyLoad() client.IShamirCoordinatorClient {
|
||||
cfg := config.GetConfig()
|
||||
httpsClient, err := tls.Get2WayTLSClient(cfg.CertsPath)
|
||||
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)
|
||||
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) {
|
||||
client := lazyLoad()
|
||||
res, err := client.SendTokens(ctx, recipient, amount, asset)
|
||||
resp, err := client.SendTokens(ctx, recipient, amount, asset)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return res.TxID, nil
|
||||
return resp.TxID, nil
|
||||
}
|
||||
|
||||
func ReIssueAsset(ctx context.Context, asset string, amount string) (txID string, err error) {
|
||||
client := lazyLoad()
|
||||
res, err := client.ReIssueAsset(ctx, asset, amount)
|
||||
resp, err := client.ReIssueAsset(ctx, asset, amount)
|
||||
if err != nil {
|
||||
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) {
|
||||
client := lazyLoad()
|
||||
res, err := client.IssueMachineNFT(ctx, name, machineAddress, domain)
|
||||
resp, err := client.IssueMachineNFT(ctx, name, machineAddress, domain)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return res.Asset, res.Contract, res.HexTX, nil
|
||||
return resp.Asset, resp.Contract, resp.HexTX, nil
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func Load(t *testing.T, configs ...Config) *Network {
|
||||
claimMock.EXPECT().PostClaim(gomock.Any(), gomock.Any()).AnyTimes().Return(rcctypes.PostClaimResponse{
|
||||
TxID: "0000000000000000000000000000000000000000000000000000000000000000",
|
||||
}, nil)
|
||||
claim.ClaimServiceClient = claimMock
|
||||
claim.RCClient = claimMock
|
||||
|
||||
shamirMock := clientmocks.NewMockIShamirCoordinatorClient(ctrl)
|
||||
shamirMock.EXPECT().SendTokens(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(scctypes.SendTokensResponse{
|
||||
|
Loading…
x
Reference in New Issue
Block a user