mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-07-08 13:42:29 +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 (
|
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) {
|
||||||
client := lazyLoad()
|
if beneficiary == "" {
|
||||||
res, err := client.PostClaim(ctx, types.PostClaimRequest{Beneficiary: beneficiary, Amount: amount, ClaimID: int(id)})
|
return txID, errors.New("beneficiary cannot be empty")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
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 (
|
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
|
||||||
}
|
}
|
||||||
|
@ -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{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user