diff --git a/pkg/internal/common/util.go b/pkg/internal/common/util.go index 1139d7e7..238cfc95 100644 --- a/pkg/internal/common/util.go +++ b/pkg/internal/common/util.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "log" "math" "os" "reflect" @@ -17,6 +16,7 @@ import ( ethcomm "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" + "github.com/rs/zerolog/log" ) func ToSha256(v string) string { @@ -40,7 +40,7 @@ func RecoverAddress(message string, signature string) (ethcomm.Address, error) { func BigNumberToFloat(bigNumber string, decimals uint64) (floatReturn float64, err error) { floatReturn, err = strconv.ParseFloat(bigNumber, 64) if err != nil { - log.Printf("Failed to convert bigNumber to float: %s", err) + log.Err(err).Msg("Failed to convert bigNumber to float") err = StringError(err) return } @@ -101,7 +101,7 @@ func IsLocalEnv() bool { func BetterStringify(jsonBody any) (betterString string, err error) { bodyBytes, err := json.Marshal(jsonBody) if err != nil { - log.Printf("Could not encode %+v to bytes: %s", jsonBody, err) + log.Err(err).Interface("body", jsonBody).Msg("Could not encode to bytes") return betterString, StringError(err) } diff --git a/pkg/internal/unit21/action.go b/pkg/internal/unit21/action.go index e313fe5f..d4b17cea 100644 --- a/pkg/internal/unit21/action.go +++ b/pkg/internal/unit21/action.go @@ -2,12 +2,12 @@ package unit21 import ( "encoding/json" - "log" "os" "github.com/String-xyz/string-api/pkg/internal/common" "github.com/String-xyz/string-api/pkg/model" "github.com/String-xyz/string-api/pkg/repository" + "github.com/rs/zerolog/log" ) type Action interface { @@ -48,18 +48,18 @@ func (a action) Create( url := "https://" + os.Getenv("UNIT21_ENV") + ".unit21.com/v1/events/create" body, err := u21Post(url, mapToUnit21ActionEvent(instrument, actionData, unit21InstrumentId, eventSubtype)) if err != nil { - log.Printf("Unit21 Action create failed: %s", err) + log.Err(err).Msg("Unit21 Action create failed") return "", common.StringError(err) } var u21Response *createEventResponse err = json.Unmarshal(body, &u21Response) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return "", common.StringError(err) } - log.Printf("Create Action Unit21Id: %s", u21Response.Unit21Id) + log.Info().Str("unit21Id", u21Response.Unit21Id).Msg("Create Action") return u21Response.Unit21Id, nil } @@ -90,10 +90,10 @@ func mapToUnit21ActionEvent(instrument model.Instrument, actionData actionData, actionBody, err := common.BetterStringify(jsonBody) if err != nil { - log.Printf("\nError creating action body\n") + log.Err(err).Msg("Error creating action body") return jsonBody } - log.Printf("\nCreate Action action body: %+v\n", actionBody) + log.Info().Str("body", actionBody).Msg("Create Action action body") return jsonBody } diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 143467f1..822595de 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -5,12 +5,12 @@ import ( "encoding/json" "fmt" "io" - "log" "net/http" "os" "time" "github.com/String-xyz/string-api/pkg/internal/common" + "github.com/rs/zerolog/log" ) func u21Put(url string, jsonBody any) (body []byte, err error) { @@ -18,16 +18,15 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { reqBodyBytes, err := json.Marshal(jsonBody) if err != nil { - log.Printf("Could not encode %+v to bytes: %s", jsonBody, err) + log.Err(err).Msg("Could not encode into bytes") return nil, common.StringError(err) } - log.Printf("reqBodyBytes: %s", reqBodyBytes) - + log.Info().Str("body", string(reqBodyBytes)).Send() bodyReader := bytes.NewReader(reqBodyBytes) req, err := http.NewRequest(http.MethodPut, url, bodyReader) if err != nil { - log.Printf("Could not create request for %s: %s", url, err) + log.Err(err).Str("url", url).Msg("Could not create request") return nil, common.StringError(err) } @@ -39,7 +38,7 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { res, err := client.Do(req) if err != nil { - log.Printf("Request failed to update %s: %s", url, err) + log.Err(err).Str("url", url).Msg("Request failed to update") return nil, common.StringError(err) } @@ -47,12 +46,12 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { body, err = io.ReadAll(res.Body) if err != nil { - log.Printf("Error extracting body from %s update request: %s", url, err) + log.Err(err).Str("url", url).Msg("Error extracting body") return nil, common.StringError(err) } if res.StatusCode != 200 { - log.Printf("Request failed to update %s: %s", url, fmt.Sprint(res.StatusCode)) + log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update") err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body))) return } @@ -66,7 +65,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { reqBodyBytes, err := json.Marshal(jsonBody) if err != nil { - log.Printf("Could not encode %+v to bytes: %s", jsonBody, err) + log.Err(err).Msg("Could not encode into bytes") return nil, common.StringError(err) } @@ -74,7 +73,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { req, err := http.NewRequest(http.MethodPost, url, bodyReader) if err != nil { - log.Printf("Could not create request for %s: %s", url, err) + log.Err(err).Str("url", url).Msg("Could not create request") return nil, common.StringError(err) } @@ -86,7 +85,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { res, err := client.Do(req) if err != nil { - log.Printf("Request failed to update %s: %s", url, err) + log.Err(err).Str("url", url).Msg("Request failed to update") return nil, common.StringError(err) } @@ -94,14 +93,14 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { body, err = io.ReadAll(res.Body) if err != nil { - log.Printf("Error extracting body from %s update response: %s", url, err) + log.Err(err).Str("url", url).Msg("Error extracting body from") return nil, common.StringError(err) } - log.Printf("String of body from response: %s", string(body)) + log.Info().Str("body", string(body)).Msgf("Strinb of body grom response") if res.StatusCode != 200 { - log.Printf("Request failed to update %s: %s", url, fmt.Sprint(res.StatusCode)) + log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update") err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body))) return } diff --git a/pkg/internal/unit21/entity.go b/pkg/internal/unit21/entity.go index e0c3820a..55210b0f 100644 --- a/pkg/internal/unit21/entity.go +++ b/pkg/internal/unit21/entity.go @@ -2,12 +2,12 @@ package unit21 import ( "encoding/json" - "log" "os" "github.com/String-xyz/string-api/pkg/internal/common" "github.com/String-xyz/string-api/pkg/model" "github.com/String-xyz/string-api/pkg/repository" + "github.com/rs/zerolog/log" ) type Entity interface { @@ -37,37 +37,37 @@ func (e entity) Create(user model.User) (unit21Id string, err error) { communications, err := e.getCommunications(user.ID) if err != nil { - log.Printf("Failed to gather Unit21 entity communications: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity communications") return "", common.StringError(err) } digitalData, err := e.getEntityDigitalData(user.ID) if err != nil { - log.Printf("Failed to gather Unit21 entity digitalData: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity digitalData") return "", common.StringError(err) } customData, err := e.getCustomData(user.ID) if err != nil { - log.Printf("Failed to gather Unit21 entity customData: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity customData") return "", common.StringError(err) } url := "https://" + os.Getenv("UNIT21_ENV") + ".unit21.com/v1/entities/create" body, err := u21Post(url, mapUserToEntity(user, communications, digitalData, customData)) if err != nil { - log.Printf("Unit21 Entity create failed: %s", err) + log.Err(err).Msg("Unit21 Entity create failed") return "", common.StringError(err) } var entity *createEntityResponse err = json.Unmarshal(body, &entity) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return "", common.StringError(err) } - log.Printf("Unit21Id: %s", entity.Unit21Id) + log.Info().Str("Unit21Id", entity.Unit21Id).Send() return entity.Unit21Id, nil } @@ -79,21 +79,21 @@ func (e entity) Update(user model.User) (unit21Id string, err error) { communications, err := e.getCommunications(user.ID) if err != nil { - log.Printf("Failed to gather Unit21 entity communications: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity communications") err = common.StringError(err) return } digitalData, err := e.getEntityDigitalData(user.ID) if err != nil { - log.Printf("Failed to gather Unit21 entity digitalData: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity digitalData") err = common.StringError(err) return } customData, err := e.getCustomData(user.ID) if err != nil { - log.Printf("Failed to gather Unit21 entity customData: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity customData") err = common.StringError(err) return } @@ -103,7 +103,7 @@ func (e entity) Update(user model.User) (unit21Id string, err error) { body, err := u21Put(url, mapUserToEntity(user, communications, digitalData, customData)) if err != nil { - log.Printf("Unit21 Entity create failed: %s", err) + log.Err(err).Msg("Unit21 Entity create failed") err = common.StringError(err) return } @@ -111,12 +111,12 @@ func (e entity) Update(user model.User) (unit21Id string, err error) { var entity *updateEntityResponse err = json.Unmarshal(body, &entity) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") err = common.StringError(err) return } - log.Printf("Unit21Id: %s", entity.Unit21Id) + log.Info().Str("Unit21Id", entity.Unit21Id).Send() return entity.Unit21Id, nil } @@ -130,7 +130,7 @@ func (e entity) AddInstruments(entityId string, instrumentIds []string) (err err _, err = u21Put(url, instruments) if err != nil { - log.Printf("Unit21 Entity Add Instruments failed: %s", err) + log.Err(err).Msg("Unit21 Entity Add Instruments failed") err = common.StringError(err) return } @@ -142,7 +142,7 @@ func (e entity) getCommunications(userId string) (communications entityCommunica // Get user contacts contacts, err := e.repo.Contact.ListByUserId(userId, 100, 0) if err != nil { - log.Printf("Failed to get user contacts: %s", err) + log.Err(err).Msg("Failed to get user contacts") err = common.StringError(err) return } @@ -161,7 +161,7 @@ func (e entity) getCommunications(userId string) (communications entityCommunica func (e entity) getEntityDigitalData(userId string) (deviceData entityDigitalData, err error) { devices, err := e.repo.Device.ListByUserId(userId, 100, 0) if err != nil { - log.Printf("Failed to get user devices: %s", err) + log.Err(err).Msg("Failed to get user devices") err = common.StringError(err) return } @@ -176,7 +176,7 @@ func (e entity) getEntityDigitalData(userId string) (deviceData entityDigitalDat func (e entity) getCustomData(userId string) (customData entityCustomData, err error) { devices, err := e.repo.UserToPlatform.ListByUserId(userId, 100, 0) if err != nil { - log.Printf("Failed to get user platforms: %s", err) + log.Err(err).Msg("Failed to get user platforms") err = common.StringError(err) return } diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index d4b72c7d..e9ae797c 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -2,12 +2,12 @@ package unit21 import ( "encoding/json" - "log" "os" "github.com/String-xyz/string-api/pkg/internal/common" "github.com/String-xyz/string-api/pkg/model" "github.com/String-xyz/string-api/pkg/repository" + "github.com/rs/zerolog/log" ) type Instrument interface { @@ -33,43 +33,43 @@ func (i instrument) Create(instrument model.Instrument) (unit21Id string, err er source, err := i.getSource(instrument.UserID) if err != nil { - log.Printf("Failed to gather Unit21 instrument source: %s", err) + log.Err(err).Msg("Failed to gather Unit21 instrument source") return "", common.StringError(err) } entities, err := i.getEntities(instrument.UserID) if err != nil { - log.Printf("Failed to gather Unit21 instrument entity: %s", err) + log.Err(err).Msg("Failed to gather Unit21 instrument entity") return "", common.StringError(err) } digitalData, err := i.getInstrumentDigitalData(instrument.UserID) if err != nil { - log.Printf("Failed to gather Unit21 entity digitalData: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity digitalData") return "", common.StringError(err) } locationData, err := i.getLocationData(instrument.LocationID.String) if err != nil { - log.Printf("Failed to gather Unit21 instrument location: %s", err) + log.Err(err).Msg("Failed to gather Unit21 instrument location") return "", common.StringError(err) } url := "https://" + os.Getenv("UNIT21_ENV") + ".unit21.com/v1/instruments/create" body, err := u21Post(url, mapToUnit21Instrument(instrument, source, entities, digitalData, locationData)) if err != nil { - log.Printf("Unit21 Instrument create failed: %s", err) + log.Err(err).Msg("Unit21 Instrument create failed") return "", common.StringError(err) } var u21Response *createInstrumentResponse err = json.Unmarshal(body, &u21Response) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return "", common.StringError(err) } - log.Printf("Unit21Id: %s", u21Response.Unit21Id) + log.Info().Str("Unit21Id", u21Response.Unit21Id).Send() return u21Response.Unit21Id, nil } @@ -77,25 +77,25 @@ func (i instrument) Update(instrument model.Instrument) (unit21Id string, err er source, err := i.getSource(instrument.UserID) if err != nil { - log.Printf("Failed to gather Unit21 instrument source: %s", err) + log.Err(err).Msg("Failed to gather Unit21 instrument source") return "", common.StringError(err) } entities, err := i.getEntities(instrument.UserID) if err != nil { - log.Printf("Failed to gather Unit21 instrument entity: %s", err) + log.Err(err).Msg("Failed to gather Unit21 instrument entity") return "", common.StringError(err) } digitalData, err := i.getInstrumentDigitalData(instrument.UserID) if err != nil { - log.Printf("Failed to gather Unit21 entity digitalData: %s", err) + log.Err(err).Msg("Failed to gather Unit21 entity digitalData") return "", common.StringError(err) } locationData, err := i.getLocationData(instrument.LocationID.String) if err != nil { - log.Printf("Failed to gather Unit21 instrument location: %s", err) + log.Err(err).Msg("Failed to gather Unit21 instrument location") return "", common.StringError(err) } @@ -104,30 +104,30 @@ func (i instrument) Update(instrument model.Instrument) (unit21Id string, err er body, err := u21Put(url, mapToUnit21Instrument(instrument, source, entities, digitalData, locationData)) if err != nil { - log.Printf("Unit21 Instrument create failed: %s", err) + log.Err(err).Msg("Unit21 Instrument create failed") return "", common.StringError(err) } var u21Response *updateInstrumentResponse err = json.Unmarshal(body, &u21Response) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return "", common.StringError(err) } - log.Printf("Unit21Id: %s", u21Response.Unit21Id) + log.Info().Str("Unit21Id", u21Response.Unit21Id).Send() return u21Response.Unit21Id, nil } func (i instrument) getSource(userId string) (source string, err error) { if userId == "" { - log.Printf("No userId defined") + log.Warn().Msg("No userId defined") return } user, err := i.repo.User.GetById(userId) if err != nil { - log.Printf("Failed go get user contacts: %s", err) + log.Err(err).Msg("Failed go get user contacts") return "", common.StringError(err) } @@ -139,13 +139,13 @@ func (i instrument) getSource(userId string) (source string, err error) { func (i instrument) getEntities(userId string) (entity instrumentEntity, err error) { if userId == "" { - log.Printf("No userId defined") + log.Warn().Msg("No userId defined") return } user, err := i.repo.User.GetById(userId) if err != nil { - log.Printf("Failed go get user contacts: %s", err) + log.Err(err).Msg("Failed go get user contacts") err = common.StringError(err) return } @@ -160,13 +160,13 @@ func (i instrument) getEntities(userId string) (entity instrumentEntity, err err func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrumentDigitalData, err error) { if userId == "" { - log.Printf("No userId defined") + log.Warn().Msg("No userId defined") return } devices, err := i.repo.Device.ListByUserId(userId, 100, 0) if err != nil { - log.Printf("Failed to get user devices: %s", err) + log.Err(err).Msg("Failed to get user devices") err = common.StringError(err) return } @@ -179,13 +179,13 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum func (i instrument) getLocationData(locationId string) (locationData instrumentLocationData, err error) { if locationId == "" { - log.Printf("No locationId defined") + log.Warn().Msg("No locationId defined") return } location, err := i.repo.Location.GetById(locationId) if err != nil { - log.Printf("Failed go get instrument location: %s", err) + log.Err(err).Msg("Failed go get instrument location") err = common.StringError(err) return } diff --git a/pkg/internal/unit21/transaction.go b/pkg/internal/unit21/transaction.go index 1858ff82..1614aeb7 100644 --- a/pkg/internal/unit21/transaction.go +++ b/pkg/internal/unit21/transaction.go @@ -2,12 +2,12 @@ package unit21 import ( "encoding/json" - "log" "os" "github.com/String-xyz/string-api/pkg/internal/common" "github.com/String-xyz/string-api/pkg/model" "github.com/String-xyz/string-api/pkg/repository" + "github.com/rs/zerolog/log" ) type Transaction interface { @@ -33,7 +33,7 @@ func NewTransaction(r TransactionRepo) Transaction { func (t transaction) Evaluate(transaction model.Transaction) (pass bool, err error) { transactionData, err := t.getTransactionData(transaction) if err != nil { - log.Printf("Failed to gather Unit21 transaction source: %s", err) + log.Err(err).Msg("Failed to gather Unit21 transaction source") return false, common.StringError(err) } @@ -44,7 +44,7 @@ func (t transaction) Evaluate(transaction model.Transaction) (pass bool, err err body, err := u21Post(url, mapToUnit21TransactionEvent(transaction, transactionData)) if err != nil { - log.Printf("Unit21 Transaction evaluate failed: %s", err) + log.Err(err).Msg("Unit21 Transaction evaluate failed") return false, common.StringError(err) } @@ -52,7 +52,7 @@ func (t transaction) Evaluate(transaction model.Transaction) (pass bool, err err var response evaluateEventResponse err = json.Unmarshal(body, &response) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return false, common.StringError(err) } @@ -69,33 +69,32 @@ func (t transaction) Create(transaction model.Transaction) (unit21Id string, err transactionData, err := t.getTransactionData(transaction) if err != nil { - log.Printf("Failed to gather Unit21 transaction source: %s", err) + log.Err(err).Msg("Failed to gather Unit21 transaction source") return "", common.StringError(err) } url := "https://" + os.Getenv("UNIT21_ENV") + ".unit21.com/v1/events/create" body, err := u21Post(url, mapToUnit21TransactionEvent(transaction, transactionData)) if err != nil { - log.Printf("Unit21 Transaction create failed: %s", err) + log.Err(err).Msg("Unit21 Transaction create failed") return "", common.StringError(err) } var u21Response *createEventResponse err = json.Unmarshal(body, &u21Response) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return "", common.StringError(err) } - log.Printf("Unit21Id: %s", u21Response.Unit21Id) - + log.Info().Str("unit21Id", u21Response.Unit21Id).Send() return u21Response.Unit21Id, nil } func (t transaction) Update(transaction model.Transaction) (unit21Id string, err error) { transactionData, err := t.getTransactionData(transaction) if err != nil { - log.Printf("Failed to gather Unit21 transaction source: %s", err) + log.Err(err).Msg("Failed to gather Unit21 transaction source") return "", common.StringError(err) } @@ -104,67 +103,66 @@ func (t transaction) Update(transaction model.Transaction) (unit21Id string, err body, err := u21Put(url, mapToUnit21TransactionEvent(transaction, transactionData)) if err != nil { - log.Printf("Unit21 Transaction create failed: %s", err) + log.Err(err).Msg("Unit21 Transaction create failed:") return "", common.StringError(err) } var u21Response *updateEventResponse err = json.Unmarshal(body, &u21Response) if err != nil { - log.Printf("Reading body failed: %s", err) + log.Err(err).Msg("Reading body failed") return "", common.StringError(err) } - - log.Printf("Unit21Id: %s", u21Response.Unit21Id) + log.Info().Str("unit21Id", u21Response.Unit21Id).Send() return u21Response.Unit21Id, nil } func (t transaction) getTransactionData(transaction model.Transaction) (txData transactionData, err error) { senderData, err := t.repo.TxLeg.GetById(transaction.OriginTxLegID) if err != nil { - log.Printf("Failed go get origin transaction leg: %s", err) + log.Err(err).Msg("Failed go get origin transaction leg") err = common.StringError(err) return } receiverData, err := t.repo.TxLeg.GetById(transaction.DestinationTxLegID) if err != nil { - log.Printf("Failed go get origin transaction leg: %s", err) + log.Err(err).Msg("Failed go get origin transaction leg") err = common.StringError(err) return } senderAsset, err := t.repo.Asset.GetById(senderData.AssetID) if err != nil { - log.Printf("Failed go get transaction sender asset: %s", err) + log.Err(err).Msg("Failed go get transaction sender asset") err = common.StringError(err) return } receiverAsset, err := t.repo.Asset.GetById(receiverData.AssetID) if err != nil { - log.Printf("Failed go get transaction receiver asset: %s", err) + log.Err(err).Msg("Failed go get transaction receiver asset") err = common.StringError(err) return } amount, err := common.BigNumberToFloat(senderData.Value, 6) if err != nil { - log.Printf("Failed to convert amount: %s", err) + log.Err(err).Msg("Failed to convert amount") err = common.StringError(err) return } senderAmount, err := common.BigNumberToFloat(senderData.Amount, senderAsset.Decimals) if err != nil { - log.Printf("Failed to convert senderAmount: %s", err) + log.Err(err).Msg("Failed to convert senderAmount") err = common.StringError(err) return } receiverAmount, err := common.BigNumberToFloat(receiverData.Amount, receiverAsset.Decimals) if err != nil { - log.Printf("Failed to convert receiverAmount: %s", err) + log.Err(err).Msg("Failed to convert receiverAmount") err = common.StringError(err) return } @@ -172,7 +170,7 @@ func (t transaction) getTransactionData(transaction model.Transaction) (txData t if transaction.StringFee != "" { stringFee, err = common.BigNumberToFloat(transaction.StringFee, 6) if err != nil { - log.Printf("Failed to convert stringFee: %s", err) + log.Err(err).Msg("Failed to convert stringFee") err = common.StringError(err) return } @@ -182,7 +180,7 @@ func (t transaction) getTransactionData(transaction model.Transaction) (txData t if transaction.ProcessingFee != "" { processingFee, err = common.BigNumberToFloat(transaction.ProcessingFee, 6) if err != nil { - log.Printf("Failed to convert processingFee: %s", err) + log.Err(err).Msg("Failed to convert processingFee") err = common.StringError(err) return } diff --git a/pkg/service/transaction.go b/pkg/service/transaction.go index d24be8ab..4912c44b 100644 --- a/pkg/service/transaction.go +++ b/pkg/service/transaction.go @@ -3,22 +3,21 @@ package service import ( "encoding/json" "fmt" - "log" "math" "math/big" "strconv" "strings" "time" - "github.com/checkout/checkout-sdk-go/payments" - "github.com/pkg/errors" - "github.com/String-xyz/string-api/pkg/internal/common" "github.com/String-xyz/string-api/pkg/internal/unit21" "github.com/String-xyz/string-api/pkg/model" "github.com/String-xyz/string-api/pkg/repository" "github.com/String-xyz/string-api/pkg/store" + "github.com/checkout/checkout-sdk-go/payments" "github.com/lib/pq" + "github.com/pkg/errors" + "github.com/rs/zerolog/log" ) type Transaction interface { @@ -145,7 +144,7 @@ func (t transaction) postProcess(p transactionProcessingData) { p.executor = &executor err := executor.Initialize(p.chain.RPC) if err != nil { - log.Printf("Failed to initialized executor in postProcess: %s", common.StringError(err)) + log.Err(err).Msg("Failed to initialized executor in postProcess") // TODO: Handle error instead of returning it } @@ -155,7 +154,7 @@ func (t transaction) postProcess(p transactionProcessingData) { updateDB.Status = &status err = t.repos.Transaction.Update(p.transactionModel.ID, updateDB) if err != nil { - log.Printf("Failed to update transaction repo with status 'Post Process RPC Dialed': %s", common.StringError(err)) + log.Err(err).Msg("Failed to update transaction repo with status 'Post Process RPC Dialed'") // TODO: Handle error instead of returning it } @@ -163,7 +162,7 @@ func (t transaction) postProcess(p transactionProcessingData) { trueGas, err := confirmTx(executor, *p.txId) p.trueGas = &trueGas if err != nil { - log.Printf("Failed to confirm transaction: %s", common.StringError(err)) + log.Err(err).Msg("Failed to confirm transaction") // TODO: Handle error instead of returning it } @@ -174,14 +173,14 @@ func (t transaction) postProcess(p transactionProcessingData) { updateDB.NetworkFee = &networkFee // geth uses uint64 for gas err = t.repos.Transaction.Update(p.transactionModel.ID, updateDB) if err != nil { - log.Printf("Failed to update transaction repo with status 'Tx Confirmed': %s", common.StringError(err)) + log.Err(err).Msg("Failed to update transaction repo with status 'Tx Confirmed'") // TODO: Handle error instead of returning it } // Get new string wallet balance after executing the transaction postBalance, err := executor.GetBalance() if err != nil { - log.Printf("Failed to get executor balance: %s", common.StringError(err)) + log.Err(err).Msg("Failed to get executor balance") // TODO: handle error instead of returning it } @@ -195,7 +194,7 @@ func (t transaction) postProcess(p transactionProcessingData) { msg := fmt.Sprintf("STRING-API: %s balance is < %.2f at %.2f", p.chain.OwlracleName, threshold, postBalance) err = MessageStaff(msg) if err != nil { - log.Printf("Failed to send staff with low balance threshold message: %s", common.StringError(err)) + log.Err(err).Msg("Failed to send staff with low balance threshold message") // Not seeing any e // TODO: handle error instead of returning it } @@ -205,7 +204,7 @@ func (t transaction) postProcess(p transactionProcessingData) { // TODO: factor request.processingFeeAsset in the event of crypto-to-usd profit, err := t.tenderTransaction(p) if err != nil { - log.Printf("Failed to tender transaction: %s", common.StringError(err)) + log.Err(err).Msg("Failed to tender transaction") // TODO: Handle error instead of returning it } stringFee := floatToFixedString(profit, 6) @@ -218,14 +217,14 @@ func (t transaction) postProcess(p transactionProcessingData) { updateDB.Status = &status err = t.repos.Transaction.Update(p.transactionModel.ID, updateDB) if err != nil { - log.Printf("Failed to update transaction repo with status 'Profit Tendered': %s", common.StringError(err)) + log.Err(err).Msg("Failed to update transaction repo with status 'Profit Tendered'") // TODO: Handle error instead of returning it } // charge the users CC err = t.chargeCard(p) if err != nil { - log.Printf("Error, failed to charge card: %+v", common.StringError(err)) + log.Err(err).Msg("failed to charge card") // TODO: Handle error instead of returning it } @@ -236,7 +235,7 @@ func (t transaction) postProcess(p transactionProcessingData) { // and use it to populate processing_fee and processing_fee_asset in the table err = t.repos.Transaction.Update(p.transactionModel.ID, updateDB) if err != nil { - log.Printf("Failed to update transaction repo with status 'Card Charged': %s", common.StringError(err)) + log.Err(err).Msg("Failed to update transaction repo with status 'Card Charged'") // TODO: Handle error instead of returning it } @@ -245,19 +244,19 @@ func (t transaction) postProcess(p transactionProcessingData) { updateDB.Status = &status err = t.repos.Transaction.Update(p.transactionModel.ID, updateDB) if err != nil { - log.Printf("Failed to update transaction repo with status 'Completed': %s", common.StringError(err)) + log.Err(err).Msg("Failed to update transaction repo with status 'Completed'") } // Create Transaction data in Unit21 err = t.unit21CreateTransaction(p.transactionModel.ID) if err != nil { - log.Printf("Error creating Unit21 transaction: %s", common.StringError(err)) + log.Err(err).Msg("Error creating Unit21 transaction") } // send email receipt err = t.sendEmailReceipt(p) if err != nil { - log.Printf("Error sending email receipt to user: %s", common.StringError(err)) + log.Err(err).Msg("Error sending email receipt to user") } } @@ -290,7 +289,7 @@ func (t transaction) transactionSetup(p transactionProcessingData) (transactionP } err = t.repos.Transaction.Update(transactionModel.ID, updateDB) if err != nil { - fmt.Printf("\nERROR = %+v", common.StringError(err)) + log.Err(err).Send() return p, common.StringError(err) } @@ -354,7 +353,7 @@ func (t transaction) safetyCheck(p transactionProcessingData) (transactionProces evaluation, err := t.unit21Evaluate(p.transactionModel.ID) if err != nil { // If Unit21 Evaluate fails, just log, but otherwise continue with the transaction - log.Printf("Error evaluating transaction in Unit21: %s", common.StringError(err)) + log.Err(err).Msg("Error evaluating transaction in Unit21") return p, nil // NOTE: intentionally returning nil here in order to continue the transaction } @@ -731,12 +730,12 @@ func (t transaction) chargeCard(p transactionProcessingData) error { func (t transaction) sendEmailReceipt(p transactionProcessingData) error { user, err := t.repos.User.GetById(*p.userId) if err != nil { - log.Printf("Error getting user from repo: %s", common.StringError(err)) + log.Err(err).Msg("Error getting user from repo") return common.StringError(err) } contact, err := t.repos.Contact.GetByUserId(user.ID) if err != nil { - log.Printf("Error getting user contact from repo: %s", common.StringError(err)) + log.Err(err).Msg("Error getting user contact from repo") return common.StringError(err) } name := user.FirstName // + " " + user.MiddleName + " " + user.LastName @@ -765,7 +764,7 @@ func (t transaction) sendEmailReceipt(p transactionProcessingData) error { } err = common.EmailReceipt(contact.Data, receiptParams, receiptBody) if err != nil { - log.Printf("Error sending email receipt to user: %s", common.StringError(err)) + log.Err(err).Msg("Error sending email receipt to user") return common.StringError(err) } return nil @@ -785,7 +784,7 @@ func (t transaction) unit21CreateInstrument(instrument model.Instrument) (err er u21Instrument := unit21.NewInstrument(u21InstrumentRepo) u21InstrumentId, err := u21Instrument.Create(instrument) if err != nil { - fmt.Printf("Error creating new instrument in Unit21") + log.Err(err).Msg("Error creating new instrument in Unit21") return common.StringError(err) } @@ -799,7 +798,7 @@ func (t transaction) unit21CreateInstrument(instrument model.Instrument) (err er u21Action := unit21.NewAction(u21ActionRepo) _, err = u21Action.Create(instrument, "Creation", u21InstrumentId, "Creation") if err != nil { - fmt.Printf("Error creating a new instrument action in Unit21") + log.Err(err).Msg("Error creating a new instrument action in Unit21") return common.StringError(err) } @@ -809,7 +808,7 @@ func (t transaction) unit21CreateInstrument(instrument model.Instrument) (err er func (t transaction) unit21CreateTransaction(transactionId string) (err error) { txModel, err := t.repos.Transaction.GetById(transactionId) if err != nil { - log.Printf("Error getting tx model in Unit21 in Tx Postprocess: %s", common.StringError(err)) + log.Err(err).Msg("Error getting tx model in Unit21 in Tx Postprocess") return common.StringError(err) } @@ -822,7 +821,7 @@ func (t transaction) unit21CreateTransaction(transactionId string) (err error) { u21Tx := unit21.NewTransaction(u21Repo) _, err = u21Tx.Create(txModel) if err != nil { - log.Printf("Error updating Unit21 in Tx Postprocess: %s", common.StringError(err)) + log.Err(err).Msg("Error updating unit21 in Tx Postprocess") return common.StringError(err) } @@ -833,7 +832,7 @@ func (t transaction) unit21Evaluate(transactionId string) (evaluation bool, err //Check transaction in Unit21 txModel, err := t.repos.Transaction.GetById(transactionId) if err != nil { - log.Printf("Error getting tx model in Unit21 in Tx Evaluate: %s", common.StringError(err)) + log.Err(err).Msg("error getting tx model in unit21 Tx Evalute") return evaluation, common.StringError(err) }