From 2298ee4a668a02ae65d37dcdb5cd6a9d94631496 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 14:04:05 -0600 Subject: [PATCH 01/19] get instrument types sending properly again --- pkg/internal/unit21/base.go | 2 +- pkg/internal/unit21/instrument.go | 4 +++- pkg/internal/unit21/types.go | 6 +++++- pkg/service/transaction.go | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 822595de..66df125b 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -97,7 +97,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { return nil, common.StringError(err) } - log.Info().Str("body", string(body)).Msgf("Strinb of body grom response") + log.Info().Str("body", string(body)).Msgf("String of body from response") if res.StatusCode != 200 { log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update") diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index e9ae797c..23a1bbd9 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -172,7 +172,9 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum } for _, device := range devices { - digitalData.IpAddresses = append(digitalData.IpAddresses, device.IpAddresses...) + for _, ip := range device.IpAddresses { + digitalData.IpAddresses = append(digitalData.IpAddresses, ipAddress{IpAddress: ip}) + } } return } diff --git a/pkg/internal/unit21/types.go b/pkg/internal/unit21/types.go index 072e2d9b..ea44b6ea 100644 --- a/pkg/internal/unit21/types.go +++ b/pkg/internal/unit21/types.go @@ -87,8 +87,12 @@ type instrumentEntity struct { EntityType string `json:"entity_type,omitempty"` } +type ipAddress struct { + IpAddress string `json:"ip_address"` +} + type instrumentDigitalData struct { - IpAddresses []string `json:"ip_addresses,omitempty"` + IpAddresses []ipAddress `json:"ip_addresses,omitempty"` } type instrumentLocationData struct { diff --git a/pkg/service/transaction.go b/pkg/service/transaction.go index 4912c44b..28ad06ec 100644 --- a/pkg/service/transaction.go +++ b/pkg/service/transaction.go @@ -510,7 +510,7 @@ func (t transaction) addWalletInstrumentIdIfNew(address string, id string) (stri } // Create a new instrument - instrument = model.Instrument{Type: "CryptoWallet", Status: "external", Network: "ethereum", PublicKey: address, UserID: id} // No locationID or userID because this wallet was not registered with the user and is some other recipient + instrument = model.Instrument{Type: "Crypto Wallet", Status: "external", Network: "ethereum", PublicKey: address, UserID: id} // No locationID or userID because this wallet was not registered with the user and is some other recipient instrument, err = t.repos.Instrument.Create(instrument) if err != nil { return "", common.StringError(err) From 9ada89850b8a27944f9664ecf0bb68cae0e4aeec Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 14:56:55 -0600 Subject: [PATCH 02/19] remove CustomData and empty LocationData --- pkg/internal/unit21/instrument.go | 37 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 23a1bbd9..39729a22 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -179,7 +179,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum return } -func (i instrument) getLocationData(locationId string) (locationData instrumentLocationData, err error) { +func (i instrument) getLocationData(locationId string) (locationData *instrumentLocationData, err error) { if locationId == "" { log.Warn().Msg("No locationId defined") return @@ -191,23 +191,24 @@ func (i instrument) getLocationData(locationId string) (locationData instrumentL err = common.StringError(err) return } - - locationData = instrumentLocationData{ - Type: location.Type, - BuildingNumber: location.BuildingNumber, - UnitNumber: location.UnitNumber, - StreetName: location.StreetName, - City: location.City, - State: location.State, - PostalCode: location.PostalCode, - Country: location.Country, - VerifiedOn: int(location.CreatedAt.Unix()), + if location.CreatedAt.Unix() != 0 { + locationData = &instrumentLocationData{ + Type: location.Type, + BuildingNumber: location.BuildingNumber, + UnitNumber: location.UnitNumber, + StreetName: location.StreetName, + City: location.City, + State: location.State, + PostalCode: location.PostalCode, + Country: location.Country, + VerifiedOn: int(location.CreatedAt.Unix()), + } } return locationData, nil } -func mapToUnit21Instrument(instrument model.Instrument, source string, entityData instrumentEntity, digitalData instrumentDigitalData, locationData instrumentLocationData) *u21Instrument { +func mapToUnit21Instrument(instrument model.Instrument, source string, entityData instrumentEntity, digitalData instrumentDigitalData, locationData *instrumentLocationData) *u21Instrument { var instrumentTagArr []string if instrument.Tags != nil { for key, value := range instrument.Tags { @@ -227,12 +228,10 @@ func mapToUnit21Instrument(instrument model.Instrument, source string, entityDat RegisteredAt: int(instrument.CreatedAt.Unix()), ParentInstrumentId: "", Entities: entityArray, - CustomData: &instrumentCustomData{ - None: nil, - }, - DigitalData: &digitalData, - LocationData: &locationData, - Tags: instrumentTagArr, + CustomData: nil, //TODO: include platform in customData + DigitalData: &digitalData, + LocationData: locationData, + Tags: instrumentTagArr, // Options: &options, } From 53668437162d8490fb84266cbd91fee30c9df564 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 15:28:45 -0600 Subject: [PATCH 03/19] Turns out they lied... --- pkg/internal/unit21/instrument.go | 4 +--- pkg/internal/unit21/types.go | 6 +----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 39729a22..1921f052 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -172,9 +172,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum } for _, device := range devices { - for _, ip := range device.IpAddresses { - digitalData.IpAddresses = append(digitalData.IpAddresses, ipAddress{IpAddress: ip}) - } + digitalData.IpAddresses = append(digitalData.IpAddresses, device.IpAddresses...) } return } diff --git a/pkg/internal/unit21/types.go b/pkg/internal/unit21/types.go index ea44b6ea..072e2d9b 100644 --- a/pkg/internal/unit21/types.go +++ b/pkg/internal/unit21/types.go @@ -87,12 +87,8 @@ type instrumentEntity struct { EntityType string `json:"entity_type,omitempty"` } -type ipAddress struct { - IpAddress string `json:"ip_address"` -} - type instrumentDigitalData struct { - IpAddresses []ipAddress `json:"ip_addresses,omitempty"` + IpAddresses []string `json:"ip_addresses,omitempty"` } type instrumentLocationData struct { From e5d543d21b31055397149895a8dc0f36b737cc33 Mon Sep 17 00:00:00 2001 From: akfoster Date: Wed, 15 Feb 2023 15:01:36 -0600 Subject: [PATCH 04/19] replace ioutil.ReadAll with io.ReadAll (#122) --- pkg/internal/common/json.go | 6 +++--- pkg/internal/common/util.go | 4 ++-- pkg/internal/unit21/base.go | 6 +++--- pkg/service/geofencing.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/internal/common/json.go b/pkg/internal/common/json.go index 18c4cf56..8abc28e1 100644 --- a/pkg/internal/common/json.go +++ b/pkg/internal/common/json.go @@ -2,7 +2,7 @@ package common import ( "encoding/json" - "io/ioutil" + "io" "net/http" "reflect" "time" @@ -18,7 +18,7 @@ func GetJson(url string, target interface{}) error { return StringError(err) } defer response.Body.Close() - jsonData, err := ioutil.ReadAll(response.Body) + jsonData, err := io.ReadAll(response.Body) if err != nil { return StringError(err) } @@ -41,7 +41,7 @@ func GetJsonGeneric(url string, target interface{}) error { return StringError(err) } defer response.Body.Close() - jsonData, err := ioutil.ReadAll(response.Body) + jsonData, err := io.ReadAll(response.Body) if err != nil { return StringError(err) } diff --git a/pkg/internal/common/util.go b/pkg/internal/common/util.go index 15041d88..1139d7e7 100644 --- a/pkg/internal/common/util.go +++ b/pkg/internal/common/util.go @@ -6,7 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "math" "os" @@ -107,7 +107,7 @@ func BetterStringify(jsonBody any) (betterString string, err error) { bodyReader := bytes.NewReader(bodyBytes) - betterBytes, err := ioutil.ReadAll(bodyReader) + betterBytes, err := io.ReadAll(bodyReader) betterString = string(betterBytes) if err != nil { return betterString, StringError(err) diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 1f0e6c89..143467f1 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -45,7 +45,7 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { defer res.Body.Close() - body, err = ioutil.ReadAll(res.Body) + body, err = io.ReadAll(res.Body) if err != nil { log.Printf("Error extracting body from %s update request: %s", url, err) return nil, common.StringError(err) @@ -92,7 +92,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { defer res.Body.Close() - body, err = ioutil.ReadAll(res.Body) + body, err = io.ReadAll(res.Body) if err != nil { log.Printf("Error extracting body from %s update response: %s", url, err) return nil, common.StringError(err) diff --git a/pkg/service/geofencing.go b/pkg/service/geofencing.go index d5b8f7b0..d6a31f7a 100644 --- a/pkg/service/geofencing.go +++ b/pkg/service/geofencing.go @@ -2,7 +2,7 @@ package service import ( "encoding/json" - "io/ioutil" + "io" "net/http" "os" @@ -95,7 +95,7 @@ func getLocationFromAPI(ip string) (GeoLocation, error) { } // read the response body - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return GeoLocation{}, common.StringError(err) From 8e0348f54135586dcaf2caa79b75d1e4c1b8b035 Mon Sep 17 00:00:00 2001 From: saito-sv <7920256+saito-sv@users.noreply.github.com> Date: Mon, 20 Feb 2023 16:56:41 -0800 Subject: [PATCH 05/19] fix go build on CI (#123) * fixed go build * Update .github/workflows/dev-deploy.yml * Update .github/workflows/dev-deploy.yml --- .github/workflows/dev-deploy.yml | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/.github/workflows/dev-deploy.yml b/.github/workflows/dev-deploy.yml index 9665865f..f073a769 100644 --- a/.github/workflows/dev-deploy.yml +++ b/.github/workflows/dev-deploy.yml @@ -2,7 +2,7 @@ name: deploy to development permissions: id-token: write contents: read -on: +on: push: branches: [develop] jobs: @@ -23,12 +23,10 @@ jobs: go-version-file: go.mod cache: true cache-dependency-path: go.sum - - name: install deps and build - ## TODO: Move all building into the docker container + - name: install deps run: | go mod download - GOOS=linux GOARCH=amd64 go build -o ./cmd/app/main ./cmd/app/main.go - + - name: configure aws credentials uses: aws-actions/configure-aws-credentials@v1.7.0 with: @@ -39,13 +37,14 @@ jobs: id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - - name: tag and push to Amazon ECR + - name: build,tag and push to Amazon ECR env: ECR_REPO: ${{ secrets.AWS_ACCT }}.dkr.ecr.us-west-2.amazonaws.com SERVICE: string-api IMAGE_TAG: latest run: | - docker build -t $ECR_REPO/$SERVICE:$IMAGE_TAG ./cmd/app/ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./cmd/app/main ./cmd/app/main.go + docker build --platform linux/amd64 -t $ECR_REPO/$SERVICE:$IMAGE_TAG ./cmd/app/ docker push $ECR_REPO/$SERVICE:$IMAGE_TAG - name: deploy @@ -54,5 +53,4 @@ jobs: SERVICE: string-api AWS_REGION: us-west-2 run: | - aws ecs --region $AWS_REGION update-service --cluster $CLUSTER --service $SERVICE --force-new-deployment - + aws ecs --region $AWS_REGION update-service --cluster $CLUSTER --service $SERVICE --force-new-deployment \ No newline at end of file From fa72e18251f84cbde627cd37e2ad64393abcd5a5 Mon Sep 17 00:00:00 2001 From: saito-sv <7920256+saito-sv@users.noreply.github.com> Date: Tue, 21 Feb 2023 13:38:23 -0800 Subject: [PATCH 06/19] use zero logs instead of log (#124) --- pkg/internal/common/util.go | 6 ++-- pkg/internal/unit21/action.go | 12 +++---- pkg/internal/unit21/base.go | 27 ++++++++------- pkg/internal/unit21/entity.go | 34 +++++++++---------- pkg/internal/unit21/instrument.go | 46 +++++++++++++------------- pkg/internal/unit21/transaction.go | 44 ++++++++++++------------- pkg/service/transaction.go | 53 +++++++++++++++--------------- 7 files changed, 109 insertions(+), 113 deletions(-) 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) } From c1d061835524960e3793eb0f8f8f19d4a85827b7 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 15:38:20 -0600 Subject: [PATCH 07/19] merge conflicts --- pkg/internal/common/util.go | 1 + pkg/internal/unit21/base.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pkg/internal/common/util.go b/pkg/internal/common/util.go index 238cfc95..4e769922 100644 --- a/pkg/internal/common/util.go +++ b/pkg/internal/common/util.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "io" + "math" "os" "reflect" diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 822595de..61e35ca9 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "net/http" "os" "time" From 42f6d31d005b08872fd0ef3df4a4835f1f821458 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 14:04:05 -0600 Subject: [PATCH 08/19] get instrument types sending properly again --- pkg/internal/unit21/base.go | 2 +- pkg/internal/unit21/instrument.go | 4 +++- pkg/internal/unit21/types.go | 6 +++++- pkg/service/transaction.go | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 61e35ca9..9085c7e2 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -98,7 +98,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) { return nil, common.StringError(err) } - log.Info().Str("body", string(body)).Msgf("Strinb of body grom response") + log.Info().Str("body", string(body)).Msgf("String of body from response") if res.StatusCode != 200 { log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update") diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index e9ae797c..23a1bbd9 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -172,7 +172,9 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum } for _, device := range devices { - digitalData.IpAddresses = append(digitalData.IpAddresses, device.IpAddresses...) + for _, ip := range device.IpAddresses { + digitalData.IpAddresses = append(digitalData.IpAddresses, ipAddress{IpAddress: ip}) + } } return } diff --git a/pkg/internal/unit21/types.go b/pkg/internal/unit21/types.go index 072e2d9b..ea44b6ea 100644 --- a/pkg/internal/unit21/types.go +++ b/pkg/internal/unit21/types.go @@ -87,8 +87,12 @@ type instrumentEntity struct { EntityType string `json:"entity_type,omitempty"` } +type ipAddress struct { + IpAddress string `json:"ip_address"` +} + type instrumentDigitalData struct { - IpAddresses []string `json:"ip_addresses,omitempty"` + IpAddresses []ipAddress `json:"ip_addresses,omitempty"` } type instrumentLocationData struct { diff --git a/pkg/service/transaction.go b/pkg/service/transaction.go index 4912c44b..28ad06ec 100644 --- a/pkg/service/transaction.go +++ b/pkg/service/transaction.go @@ -510,7 +510,7 @@ func (t transaction) addWalletInstrumentIdIfNew(address string, id string) (stri } // Create a new instrument - instrument = model.Instrument{Type: "CryptoWallet", Status: "external", Network: "ethereum", PublicKey: address, UserID: id} // No locationID or userID because this wallet was not registered with the user and is some other recipient + instrument = model.Instrument{Type: "Crypto Wallet", Status: "external", Network: "ethereum", PublicKey: address, UserID: id} // No locationID or userID because this wallet was not registered with the user and is some other recipient instrument, err = t.repos.Instrument.Create(instrument) if err != nil { return "", common.StringError(err) From e44da5c5831aced1f096af106b197a05cde5039d Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 14:56:55 -0600 Subject: [PATCH 09/19] remove CustomData and empty LocationData --- pkg/internal/unit21/instrument.go | 37 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 23a1bbd9..39729a22 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -179,7 +179,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum return } -func (i instrument) getLocationData(locationId string) (locationData instrumentLocationData, err error) { +func (i instrument) getLocationData(locationId string) (locationData *instrumentLocationData, err error) { if locationId == "" { log.Warn().Msg("No locationId defined") return @@ -191,23 +191,24 @@ func (i instrument) getLocationData(locationId string) (locationData instrumentL err = common.StringError(err) return } - - locationData = instrumentLocationData{ - Type: location.Type, - BuildingNumber: location.BuildingNumber, - UnitNumber: location.UnitNumber, - StreetName: location.StreetName, - City: location.City, - State: location.State, - PostalCode: location.PostalCode, - Country: location.Country, - VerifiedOn: int(location.CreatedAt.Unix()), + if location.CreatedAt.Unix() != 0 { + locationData = &instrumentLocationData{ + Type: location.Type, + BuildingNumber: location.BuildingNumber, + UnitNumber: location.UnitNumber, + StreetName: location.StreetName, + City: location.City, + State: location.State, + PostalCode: location.PostalCode, + Country: location.Country, + VerifiedOn: int(location.CreatedAt.Unix()), + } } return locationData, nil } -func mapToUnit21Instrument(instrument model.Instrument, source string, entityData instrumentEntity, digitalData instrumentDigitalData, locationData instrumentLocationData) *u21Instrument { +func mapToUnit21Instrument(instrument model.Instrument, source string, entityData instrumentEntity, digitalData instrumentDigitalData, locationData *instrumentLocationData) *u21Instrument { var instrumentTagArr []string if instrument.Tags != nil { for key, value := range instrument.Tags { @@ -227,12 +228,10 @@ func mapToUnit21Instrument(instrument model.Instrument, source string, entityDat RegisteredAt: int(instrument.CreatedAt.Unix()), ParentInstrumentId: "", Entities: entityArray, - CustomData: &instrumentCustomData{ - None: nil, - }, - DigitalData: &digitalData, - LocationData: &locationData, - Tags: instrumentTagArr, + CustomData: nil, //TODO: include platform in customData + DigitalData: &digitalData, + LocationData: locationData, + Tags: instrumentTagArr, // Options: &options, } From a400a2555ed3b1b07084b7dbbe56061421fc6120 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 15:28:45 -0600 Subject: [PATCH 10/19] Turns out they lied... --- pkg/internal/unit21/instrument.go | 4 +--- pkg/internal/unit21/types.go | 6 +----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 39729a22..1921f052 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -172,9 +172,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum } for _, device := range devices { - for _, ip := range device.IpAddresses { - digitalData.IpAddresses = append(digitalData.IpAddresses, ipAddress{IpAddress: ip}) - } + digitalData.IpAddresses = append(digitalData.IpAddresses, device.IpAddresses...) } return } diff --git a/pkg/internal/unit21/types.go b/pkg/internal/unit21/types.go index ea44b6ea..072e2d9b 100644 --- a/pkg/internal/unit21/types.go +++ b/pkg/internal/unit21/types.go @@ -87,12 +87,8 @@ type instrumentEntity struct { EntityType string `json:"entity_type,omitempty"` } -type ipAddress struct { - IpAddress string `json:"ip_address"` -} - type instrumentDigitalData struct { - IpAddresses []ipAddress `json:"ip_addresses,omitempty"` + IpAddresses []string `json:"ip_addresses,omitempty"` } type instrumentLocationData struct { From 662cfd32a18933cdcccc363bb52804516104cc85 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 15:43:17 -0600 Subject: [PATCH 11/19] remove new lines --- pkg/internal/common/util.go | 1 - pkg/internal/unit21/base.go | 1 - 2 files changed, 2 deletions(-) diff --git a/pkg/internal/common/util.go b/pkg/internal/common/util.go index 4e769922..238cfc95 100644 --- a/pkg/internal/common/util.go +++ b/pkg/internal/common/util.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "math" "os" "reflect" diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 9085c7e2..66df125b 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "net/http" "os" "time" From b35c081c27512675af1f6f2c33a2da7152c71c19 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 16:11:39 -0600 Subject: [PATCH 12/19] pull from master --- pkg/internal/unit21/base.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 66df125b..7753df49 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -62,6 +62,8 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { func u21Post(url string, jsonBody any) (body []byte, err error) { apiKey := os.Getenv("UNIT21_API_KEY") + jsonInstrument, _ := common.BetterStringify(jsonBody) + log.Printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>create data passed to Unit21: %+v\n", jsonInstrument) reqBodyBytes, err := json.Marshal(jsonBody) if err != nil { From afed4d6b6668b4a17cf09c28c21b32dbf844acc2 Mon Sep 17 00:00:00 2001 From: akfoster Date: Wed, 15 Feb 2023 15:01:36 -0600 Subject: [PATCH 13/19] replace ioutil.ReadAll with io.ReadAll (#122) --- pkg/internal/common/util.go | 1 - pkg/internal/unit21/base.go | 1 - 2 files changed, 2 deletions(-) diff --git a/pkg/internal/common/util.go b/pkg/internal/common/util.go index 4e769922..238cfc95 100644 --- a/pkg/internal/common/util.go +++ b/pkg/internal/common/util.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "math" "os" "reflect" diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 9085c7e2..66df125b 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "net/http" "os" "time" From 85648b1ae02254efb64a3d30ce7df4cf17103dd7 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 14:04:05 -0600 Subject: [PATCH 14/19] get instrument types sending properly again --- pkg/internal/unit21/instrument.go | 4 +++- pkg/internal/unit21/types.go | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 1921f052..39729a22 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -172,7 +172,9 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum } for _, device := range devices { - digitalData.IpAddresses = append(digitalData.IpAddresses, device.IpAddresses...) + for _, ip := range device.IpAddresses { + digitalData.IpAddresses = append(digitalData.IpAddresses, ipAddress{IpAddress: ip}) + } } return } diff --git a/pkg/internal/unit21/types.go b/pkg/internal/unit21/types.go index 072e2d9b..ea44b6ea 100644 --- a/pkg/internal/unit21/types.go +++ b/pkg/internal/unit21/types.go @@ -87,8 +87,12 @@ type instrumentEntity struct { EntityType string `json:"entity_type,omitempty"` } +type ipAddress struct { + IpAddress string `json:"ip_address"` +} + type instrumentDigitalData struct { - IpAddresses []string `json:"ip_addresses,omitempty"` + IpAddresses []ipAddress `json:"ip_addresses,omitempty"` } type instrumentLocationData struct { From 0235995c390162ece578a6822cbfdc4ef46afb19 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 15:28:45 -0600 Subject: [PATCH 15/19] Turns out they lied... --- pkg/internal/unit21/instrument.go | 4 +--- pkg/internal/unit21/types.go | 6 +----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 39729a22..1921f052 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -172,9 +172,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum } for _, device := range devices { - for _, ip := range device.IpAddresses { - digitalData.IpAddresses = append(digitalData.IpAddresses, ipAddress{IpAddress: ip}) - } + digitalData.IpAddresses = append(digitalData.IpAddresses, device.IpAddresses...) } return } diff --git a/pkg/internal/unit21/types.go b/pkg/internal/unit21/types.go index ea44b6ea..072e2d9b 100644 --- a/pkg/internal/unit21/types.go +++ b/pkg/internal/unit21/types.go @@ -87,12 +87,8 @@ type instrumentEntity struct { EntityType string `json:"entity_type,omitempty"` } -type ipAddress struct { - IpAddress string `json:"ip_address"` -} - type instrumentDigitalData struct { - IpAddresses []ipAddress `json:"ip_addresses,omitempty"` + IpAddresses []string `json:"ip_addresses,omitempty"` } type instrumentLocationData struct { From 886e8f109e98d9b0337733822a4cc1cf118a5f60 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Thu, 23 Feb 2023 16:11:39 -0600 Subject: [PATCH 16/19] pull from master --- pkg/internal/unit21/base.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 66df125b..7753df49 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -62,6 +62,8 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { func u21Post(url string, jsonBody any) (body []byte, err error) { apiKey := os.Getenv("UNIT21_API_KEY") + jsonInstrument, _ := common.BetterStringify(jsonBody) + log.Printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>create data passed to Unit21: %+v\n", jsonInstrument) reqBodyBytes, err := json.Marshal(jsonBody) if err != nil { From 1d838447480291f992ba5a97c96235d8cf330bf0 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Fri, 24 Feb 2023 11:53:54 -0600 Subject: [PATCH 17/19] allow nil device IPAddress --- pkg/model/common.go | 12 ------------ pkg/model/entity.go | 2 +- pkg/service/device.go | 12 ++++++++---- pkg/service/fingerprint.go | 15 ++++++++++++--- 4 files changed, 21 insertions(+), 20 deletions(-) delete mode 100644 pkg/model/common.go diff --git a/pkg/model/common.go b/pkg/model/common.go deleted file mode 100644 index 2ec67411..00000000 --- a/pkg/model/common.go +++ /dev/null @@ -1,12 +0,0 @@ -package model - -type FPVisitor struct { - VisitorID string - Country string - State string - IPAddress string - Timestamp int64 - Confidence float64 - Type string - UserAgent string -} diff --git a/pkg/model/entity.go b/pkg/model/entity.go index 57d460fc..0c8f0229 100644 --- a/pkg/model/entity.go +++ b/pkg/model/entity.go @@ -82,7 +82,7 @@ type Device struct { Type string `json:"type" db:"type"` Description string `json:"description" db:"description"` Fingerprint string `json:"fingerprint" db:"fingerprint"` - IpAddresses pq.StringArray `json:"ipAddresses" db:"ip_addresses"` + IpAddresses pq.StringArray `json:"ipAddresses,omitempty" db:"ip_addresses"` UserID string `json:"userId" db:"user_id"` } diff --git a/pkg/service/device.go b/pkg/service/device.go index de5ca877..b4b5d87f 100644 --- a/pkg/service/device.go +++ b/pkg/service/device.go @@ -27,22 +27,26 @@ func NewDevice(repos repository.Repositories, f Fingerprint) Device { return &device{repos, f} } -func (d device) createDevice(userID string, visitor model.FPVisitor, description string) (model.Device, error) { +func (d device) createDevice(userID string, visitor FPVisitor, description string) (model.Device, error) { + addresses := pq.StringArray{} + if visitor.IPAddress.String != "" { + addresses = pq.StringArray{visitor.IPAddress.String} + } + return d.repos.Device.Create(model.Device{ UserID: userID, Fingerprint: visitor.VisitorID, Type: visitor.Type, - IpAddresses: pq.StringArray{visitor.IPAddress}, + IpAddresses: addresses, Description: description, LastUsedAt: time.Now(), }) } func (d device) CreateUnknownDevice(userID string) (model.Device, error) { - visitor := model.FPVisitor{ + visitor := FPVisitor{ VisitorID: "unknown", Type: "unknown", - IPAddress: "unknown", UserAgent: "unknown", } device, err := d.createDevice(userID, visitor, "an unknown device") diff --git a/pkg/service/fingerprint.go b/pkg/service/fingerprint.go index 6b90fb59..bbcecdcd 100644 --- a/pkg/service/fingerprint.go +++ b/pkg/service/fingerprint.go @@ -1,16 +1,25 @@ package service import ( + "database/sql" "errors" "github.com/String-xyz/string-api/pkg/internal/common" - "github.com/String-xyz/string-api/pkg/model" ) type FPClient common.FingerprintClient type HTTPConfig common.HTTPConfig type HTTPClient common.HTTPClient -type FPVisitor = model.FPVisitor +type FPVisitor struct { + VisitorID string + Country string + State string + IPAddress sql.NullString + Timestamp int64 + Confidence float64 + Type string + UserAgent string +} func NewHTTPClient(config HTTPConfig) HTTPClient { return common.NewHTTPClient(common.HTTPConfig(config)) @@ -59,7 +68,7 @@ func (f fingerprint) hydrateVisitor(visitor common.FPVisitor) (FPVisitor, error) VisitorID: visitor.ID, Country: visit.IPLocation.Coutry.Code, State: state, - IPAddress: visit.IP, + IPAddress: sql.NullString{String: visit.IP}, Timestamp: visit.Timestamp, Confidence: visit.IPLocation.Confidence.Score, Type: visit.BrowserDetails.Device, From b7b451e24aef62a50ee09fd828b1ff0ccfc2b155 Mon Sep 17 00:00:00 2001 From: Sean Date: Fri, 24 Feb 2023 11:18:59 -0700 Subject: [PATCH 18/19] await unit21CreateInstrument --- pkg/service/transaction.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/service/transaction.go b/pkg/service/transaction.go index 28ad06ec..b014a5b6 100644 --- a/pkg/service/transaction.go +++ b/pkg/service/transaction.go @@ -497,7 +497,7 @@ func (t transaction) addCardInstrumentIdIfNew(p transactionProcessingData) (stri if err != nil { return "", common.StringError(err) } - go t.unit21CreateInstrument(instrument) + t.unit21CreateInstrument(instrument) return instrument.ID, nil } @@ -515,7 +515,7 @@ func (t transaction) addWalletInstrumentIdIfNew(address string, id string) (stri if err != nil { return "", common.StringError(err) } - go t.unit21CreateInstrument(instrument) + t.unit21CreateInstrument(instrument) return instrument.ID, nil } From fbe79eecd3ddb9e15f28988d3423f51ad994d807 Mon Sep 17 00:00:00 2001 From: Ocasta Date: Fri, 24 Feb 2023 15:14:34 -0600 Subject: [PATCH 19/19] refactor unit21 for dependency injection; create wallet instrument on user creation --- api/config.go | 5 +- pkg/internal/unit21/action.go | 12 +--- pkg/internal/unit21/base.go | 3 - pkg/internal/unit21/instrument.go | 25 +++++--- pkg/internal/unit21/transaction.go | 18 +++--- pkg/service/base.go | 1 + pkg/service/transaction.go | 96 ++++++++---------------------- pkg/service/unit21.go | 29 +++++++++ pkg/service/user.go | 44 +++----------- 9 files changed, 93 insertions(+), 140 deletions(-) create mode 100644 pkg/service/unit21.go diff --git a/api/config.go b/api/config.go index b785b175..9bfebccf 100644 --- a/api/config.go +++ b/api/config.go @@ -31,6 +31,7 @@ func NewRepos(config APIConfig) repository.Repositories { * Not every service needs access to all of the repos, so we can pass in only the ones it needs. This will make it easier to test */ func NewServices(config APIConfig, repos repository.Repositories) service.Services { + unit21 := service.NewUnit21(repos) httpClient := service.NewHTTPClient(service.HTTPConfig{Timeout: time.Duration(30) * time.Second}) client := service.NewFingerprintClient(httpClient) fingerprint := service.NewFingerprint(client) @@ -52,8 +53,8 @@ func NewServices(config APIConfig, repos repository.Repositories) service.Servic platformRepos := repository.Repositories{Auth: repos.Auth, Platform: repos.Platform} platform := service.NewPlatform(platformRepos) - transaction := service.NewTransaction(repos, config.Redis) - user := service.NewUser(repos, auth, fingerprint, device) + transaction := service.NewTransaction(repos, config.Redis, unit21) + user := service.NewUser(repos, auth, fingerprint, device, unit21) return service.Services{ Auth: auth, diff --git a/pkg/internal/unit21/action.go b/pkg/internal/unit21/action.go index d4b17cea..b816bec2 100644 --- a/pkg/internal/unit21/action.go +++ b/pkg/internal/unit21/action.go @@ -6,7 +6,6 @@ import ( "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" ) @@ -17,18 +16,11 @@ type Action interface { eventSubtype string) (unit21Id string, err error) } -type ActionRepo struct { - User repository.User - Device repository.Device - Location repository.Location -} - type action struct { - repo ActionRepo } -func NewAction(r ActionRepo) Action { - return &action{repo: r} +func NewAction() Action { + return &action{} } func (a action) Create( diff --git a/pkg/internal/unit21/base.go b/pkg/internal/unit21/base.go index 7753df49..9131b60e 100644 --- a/pkg/internal/unit21/base.go +++ b/pkg/internal/unit21/base.go @@ -62,10 +62,7 @@ func u21Put(url string, jsonBody any) (body []byte, err error) { func u21Post(url string, jsonBody any) (body []byte, err error) { apiKey := os.Getenv("UNIT21_API_KEY") - jsonInstrument, _ := common.BetterStringify(jsonBody) - log.Printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>create data passed to Unit21: %+v\n", jsonInstrument) reqBodyBytes, err := json.Marshal(jsonBody) - if err != nil { log.Err(err).Msg("Could not encode into bytes") return nil, common.StringError(err) diff --git a/pkg/internal/unit21/instrument.go b/pkg/internal/unit21/instrument.go index 1921f052..91c93f2a 100644 --- a/pkg/internal/unit21/instrument.go +++ b/pkg/internal/unit21/instrument.go @@ -15,18 +15,19 @@ type Instrument interface { Update(instrument model.Instrument) (unit21Id string, err error) } -type InstrumentRepo struct { +type InstrumentRepos struct { User repository.User Device repository.Device Location repository.Location } type instrument struct { - repo InstrumentRepo + action Action + repos InstrumentRepos } -func NewInstrument(r InstrumentRepo) Instrument { - return &instrument{repo: r} +func NewInstrument(r InstrumentRepos, a Action) Instrument { + return &instrument{repos: r, action: a} } func (i instrument) Create(instrument model.Instrument) (unit21Id string, err error) { @@ -70,6 +71,14 @@ func (i instrument) Create(instrument model.Instrument) (unit21Id string, err er } log.Info().Str("Unit21Id", u21Response.Unit21Id).Send() + + // Log create instrument action w/ Unit21 + _, err = i.action.Create(instrument, "Creation", u21Response.Unit21Id, "Creation") + if err != nil { + log.Err(err).Msg("Error creating a new instrument action in Unit21") + return u21Response.Unit21Id, common.StringError(err) + } + return u21Response.Unit21Id, nil } @@ -125,7 +134,7 @@ func (i instrument) getSource(userId string) (source string, err error) { log.Warn().Msg("No userId defined") return } - user, err := i.repo.User.GetById(userId) + user, err := i.repos.User.GetById(userId) if err != nil { log.Err(err).Msg("Failed go get user contacts") return "", common.StringError(err) @@ -143,7 +152,7 @@ func (i instrument) getEntities(userId string) (entity instrumentEntity, err err return } - user, err := i.repo.User.GetById(userId) + user, err := i.repos.User.GetById(userId) if err != nil { log.Err(err).Msg("Failed go get user contacts") err = common.StringError(err) @@ -164,7 +173,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum return } - devices, err := i.repo.Device.ListByUserId(userId, 100, 0) + devices, err := i.repos.Device.ListByUserId(userId, 100, 0) if err != nil { log.Err(err).Msg("Failed to get user devices") err = common.StringError(err) @@ -183,7 +192,7 @@ func (i instrument) getLocationData(locationId string) (locationData *instrument return } - location, err := i.repo.Location.GetById(locationId) + location, err := i.repos.Location.GetById(locationId) if err != nil { log.Err(err).Msg("Failed go get instrument location") err = common.StringError(err) diff --git a/pkg/internal/unit21/transaction.go b/pkg/internal/unit21/transaction.go index 1614aeb7..9a03ef44 100644 --- a/pkg/internal/unit21/transaction.go +++ b/pkg/internal/unit21/transaction.go @@ -16,18 +16,18 @@ type Transaction interface { Update(transaction model.Transaction) (unit21Id string, err error) } -type TransactionRepo struct { - TxLeg repository.TxLeg +type TransactionRepos struct { User repository.User + TxLeg repository.TxLeg Asset repository.Asset } type transaction struct { - repo TransactionRepo + repos TransactionRepos } -func NewTransaction(r TransactionRepo) Transaction { - return &transaction{repo: r} +func NewTransaction(r TransactionRepos) Transaction { + return &transaction{repos: r} } func (t transaction) Evaluate(transaction model.Transaction) (pass bool, err error) { @@ -118,28 +118,28 @@ func (t transaction) Update(transaction model.Transaction) (unit21Id string, err } func (t transaction) getTransactionData(transaction model.Transaction) (txData transactionData, err error) { - senderData, err := t.repo.TxLeg.GetById(transaction.OriginTxLegID) + senderData, err := t.repos.TxLeg.GetById(transaction.OriginTxLegID) if err != nil { log.Err(err).Msg("Failed go get origin transaction leg") err = common.StringError(err) return } - receiverData, err := t.repo.TxLeg.GetById(transaction.DestinationTxLegID) + receiverData, err := t.repos.TxLeg.GetById(transaction.DestinationTxLegID) if err != nil { log.Err(err).Msg("Failed go get origin transaction leg") err = common.StringError(err) return } - senderAsset, err := t.repo.Asset.GetById(senderData.AssetID) + senderAsset, err := t.repos.Asset.GetById(senderData.AssetID) if err != nil { log.Err(err).Msg("Failed go get transaction sender asset") err = common.StringError(err) return } - receiverAsset, err := t.repo.Asset.GetById(receiverData.AssetID) + receiverAsset, err := t.repos.Asset.GetById(receiverData.AssetID) if err != nil { log.Err(err).Msg("Failed go get transaction receiver asset") err = common.StringError(err) diff --git a/pkg/service/base.go b/pkg/service/base.go index ee055faf..8c3a04cf 100644 --- a/pkg/service/base.go +++ b/pkg/service/base.go @@ -14,4 +14,5 @@ type Services struct { User User Verification Verification Device Device + Unit21 Unit21 } diff --git a/pkg/service/transaction.go b/pkg/service/transaction.go index 28ad06ec..4a6fc427 100644 --- a/pkg/service/transaction.go +++ b/pkg/service/transaction.go @@ -10,7 +10,6 @@ import ( "time" "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" @@ -45,9 +44,14 @@ type InternalIds struct { } type transaction struct { - repos repository.Repositories - redis store.RedisStore - ids InternalIds + repos repository.Repositories + redis store.RedisStore + ids InternalIds + unit21 Unit21 +} + +func NewTransaction(repos repository.Repositories, redis store.RedisStore, unit21 Unit21) Transaction { + return &transaction{repos: repos, redis: redis, unit21: unit21} } type transactionProcessingData struct { @@ -67,10 +71,6 @@ type transactionProcessingData struct { trueGas *uint64 } -func NewTransaction(repos repository.Repositories, redis store.RedisStore) Transaction { - return &transaction{repos: repos, redis: redis} -} - func (t transaction) Quote(d model.TransactionRequest) (model.ExecutionRequest, error) { // TODO: use prefab service to parse d and fill out known params res := model.ExecutionRequest{TransactionRequest: d} @@ -349,8 +349,14 @@ func (t transaction) safetyCheck(p transactionProcessingData) (transactionProces } // Validate Transaction through Real Time Rules engine - // RTR is not released for Unit21 Production (slated for Late February 2023) - evaluation, err := t.unit21Evaluate(p.transactionModel.ID) + txModel, err := t.repos.Transaction.GetById(p.transactionModel.ID) + if err != nil { + log.Err(err).Msg("error getting tx model in unit21 Tx Evalute") + return p, common.StringError(err) + } + + evaluation, err := t.unit21.Transaction.Evaluate(txModel) + if err != nil { // If Unit21 Evaluate fails, just log, but otherwise continue with the transaction log.Err(err).Msg("Error evaluating transaction in Unit21") @@ -477,7 +483,8 @@ func (t transaction) addCardInstrumentIdIfNew(p transactionProcessingData) (stri if err != nil && !strings.Contains(err.Error(), "not found") { // because we are wrapping error and care about its value return "", common.StringError(err) } else if err == nil && instrument.UserID != "" { - return instrument.ID, nil // instrument already exists + go t.unit21.Instrument.Update(instrument) // if instrument already exists, update it anyways + return instrument.ID, nil // return if instrument already exists } // We should gather type from the payment processor @@ -497,7 +504,8 @@ func (t transaction) addCardInstrumentIdIfNew(p transactionProcessingData) (stri if err != nil { return "", common.StringError(err) } - go t.unit21CreateInstrument(instrument) + + go t.unit21.Instrument.Create(instrument) return instrument.ID, nil } @@ -506,7 +514,8 @@ func (t transaction) addWalletInstrumentIdIfNew(address string, id string) (stri if err != nil && !strings.Contains(err.Error(), "not found") { return "", common.StringError(err) } else if err == nil && instrument.PublicKey == address { - return instrument.ID, nil + go t.unit21.Instrument.Update(instrument) // if instrument already exists, update it anyways + return instrument.ID, nil // return if instrument already exists } // Create a new instrument @@ -515,7 +524,8 @@ func (t transaction) addWalletInstrumentIdIfNew(address string, id string) (stri if err != nil { return "", common.StringError(err) } - go t.unit21CreateInstrument(instrument) + + go t.unit21.Instrument.Create(instrument) return instrument.ID, nil } @@ -774,37 +784,6 @@ func floatToFixedString(value float64, decimals int) string { return strconv.FormatUint(uint64(value*(math.Pow10(decimals-1))), 10) } -func (t transaction) unit21CreateInstrument(instrument model.Instrument) (err error) { - u21InstrumentRepo := unit21.InstrumentRepo{ - User: t.repos.User, - Device: t.repos.Device, - Location: t.repos.Location, // empty until fingerprint integration - } - - u21Instrument := unit21.NewInstrument(u21InstrumentRepo) - u21InstrumentId, err := u21Instrument.Create(instrument) - if err != nil { - log.Err(err).Msg("Error creating new instrument in Unit21") - return common.StringError(err) - } - - // Log create instrument action w/ Unit21 - u21ActionRepo := unit21.ActionRepo{ - User: t.repos.User, - Device: t.repos.Device, - Location: t.repos.Location, // empty until fingerprint integration - } - - u21Action := unit21.NewAction(u21ActionRepo) - _, err = u21Action.Create(instrument, "Creation", u21InstrumentId, "Creation") - if err != nil { - log.Err(err).Msg("Error creating a new instrument action in Unit21") - return common.StringError(err) - } - - return -} - func (t transaction) unit21CreateTransaction(transactionId string) (err error) { txModel, err := t.repos.Transaction.GetById(transactionId) if err != nil { @@ -812,14 +791,7 @@ func (t transaction) unit21CreateTransaction(transactionId string) (err error) { return common.StringError(err) } - u21Repo := unit21.TransactionRepo{ - TxLeg: t.repos.TxLeg, - User: t.repos.User, - Asset: t.repos.Asset, - } - - u21Tx := unit21.NewTransaction(u21Repo) - _, err = u21Tx.Create(txModel) + _, err = t.unit21.Transaction.Create(txModel) if err != nil { log.Err(err).Msg("Error updating unit21 in Tx Postprocess") return common.StringError(err) @@ -828,24 +800,6 @@ func (t transaction) unit21CreateTransaction(transactionId string) (err error) { return nil } -func (t transaction) unit21Evaluate(transactionId string) (evaluation bool, err error) { - //Check transaction in Unit21 - txModel, err := t.repos.Transaction.GetById(transactionId) - if err != nil { - log.Err(err).Msg("error getting tx model in unit21 Tx Evalute") - return evaluation, common.StringError(err) - } - - u21Repo := unit21.TransactionRepo{ - TxLeg: t.repos.TxLeg, - User: t.repos.User, - Asset: t.repos.Asset, - } - - u21Tx := unit21.NewTransaction(u21Repo) - return u21Tx.Evaluate(txModel) -} - func (t transaction) updateTransactionStatus(status string, transactionId string) (err error) { updateDB := &model.TransactionUpdates{Status: &status} err = t.repos.Transaction.Update(transactionId, updateDB) diff --git a/pkg/service/unit21.go b/pkg/service/unit21.go new file mode 100644 index 00000000..01c3d831 --- /dev/null +++ b/pkg/service/unit21.go @@ -0,0 +1,29 @@ +package service + +import ( + "github.com/String-xyz/string-api/pkg/internal/unit21" + "github.com/String-xyz/string-api/pkg/repository" +) + +type Unit21 struct { + Action unit21.Action + Entity unit21.Entity + Instrument unit21.Instrument + Transaction unit21.Transaction +} + +func NewUnit21(repos repository.Repositories) Unit21 { + action := unit21.NewAction() + entityRepos := unit21.EntityRepos{Device: repos.Device, Contact: repos.Contact, UserToPlatform: repos.UserToPlatform} + entity := unit21.NewEntity(entityRepos) + instrumentRepos := unit21.InstrumentRepos{User: repos.User, Device: repos.Device, Location: repos.Location} + instrument := unit21.NewInstrument(instrumentRepos, action) + transactionRepos := unit21.TransactionRepos{User: repos.User, TxLeg: repos.TxLeg, Asset: repos.Asset} + transaction := unit21.NewTransaction(transactionRepos) + return Unit21{ + Action: action, + Entity: entity, + Instrument: instrument, + Transaction: transaction, + } +} diff --git a/pkg/service/user.go b/pkg/service/user.go index 27f81231..94408d0f 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -4,11 +4,9 @@ import ( "os" "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/pkg/errors" - "github.com/rs/zerolog/log" ) type UserRequest = model.UserRequest @@ -38,10 +36,11 @@ type user struct { auth Auth fingerprint Fingerprint device Device + unit21 Unit21 } -func NewUser(repos repository.Repositories, auth Auth, fprint Fingerprint, device Device) User { - return &user{repos, auth, fprint, device} +func NewUser(repos repository.Repositories, auth Auth, fprint Fingerprint, device Device, unit21 Unit21) User { + return &user{repos, auth, fprint, device, unit21} } func (u user) GetStatus(userID string) (model.UserOnboardingStatus, error) { @@ -110,7 +109,7 @@ func (u user) Create(request model.WalletSignaturePayloadSigned) (UserCreateResp } // deviceService.RegisterNewUserDevice() - go u.createUnit21Entity(user) + go u.unit21.Entity.Create(user) return UserCreateResponse{JWT: jwt, User: user}, nil } @@ -136,11 +135,12 @@ func (u user) createUserData(addr string) (model.User, error) { u.repos.Instrument.Rollback() return user, common.StringError(err) } - if err := u.repos.User.Commit(); err != nil { return user, common.StringError(errors.New("error commiting transaction")) } + go u.unit21.Instrument.Create(instrument) + return user, nil } @@ -151,37 +151,7 @@ func (u user) Update(userID string, request UserUpdates) (model.User, error) { return user, common.StringError(err) } - go u.updateUnit21Entity(user) + go u.unit21.Entity.Update(user) return user, nil } - -func (u user) createUnit21Entity(user model.User) { - // Createing a User Entity in Unit21 - u21Repo := unit21.EntityRepos{ - Device: u.repos.Device, - Contact: u.repos.Contact, - UserToPlatform: u.repos.UserToPlatform, - } - - u21Entity := unit21.NewEntity(u21Repo) // TODO: Make it an injected dependency - _, err := u21Entity.Create(user) - if err != nil { - log.Err(err).Msg("Error creating Entity in Unit21") - } -} - -func (u user) updateUnit21Entity(user model.User) { - // Createing a User Entity in Unit21 - u21Repo := unit21.EntityRepos{ - Device: u.repos.Device, - Contact: u.repos.Contact, - UserToPlatform: u.repos.UserToPlatform, - } - - u21Entity := unit21.NewEntity(u21Repo) - _, err := u21Entity.Update(user) - if err != nil { - log.Err(err).Msg("Error updating Entity in Unit21") - } -}