Skip to content

Task/andrin/str 154 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
204eceb
AddInstruments working as expected, CreateEntity test requires a mock…
ocasta181 Oct 19, 2022
54f25e9
have instrument add working
ocasta181 Oct 19, 2022
470128c
cleanup entities_test.go
ocasta181 Oct 19, 2022
6cb68d8
resolve rebase conflicts
ocasta181 Oct 20, 2022
2603537
have instrument add working
ocasta181 Oct 19, 2022
4302a87
cleanup entities_test.go
ocasta181 Oct 19, 2022
dae2910
more merge conflicts
ocasta181 Oct 20, 2022
01ba44c
somehow missed a rebase conflict
ocasta181 Oct 20, 2022
bfb21ca
adding customData
ocasta181 Oct 21, 2022
bb2a8c1
resolve rebase conflicts
ocasta181 Oct 20, 2022
b608c66
have instrument add working
ocasta181 Oct 19, 2022
3413f3c
cleanup entities_test.go
ocasta181 Oct 19, 2022
3920e97
merge conflicts
ocasta181 Oct 25, 2022
47ff378
have instrument add working
ocasta181 Oct 19, 2022
079c663
somehow missed a rebase conflict
ocasta181 Oct 20, 2022
edf0616
adding customData
ocasta181 Oct 21, 2022
7b99fc6
another merge...
ocasta181 Oct 25, 2022
08bd81e
make db gets more precicely named; add getDigitalData() to unit21
ocasta181 Oct 25, 2022
7a86f81
add user_platform table and update unit21 entity endpoint
ocasta181 Oct 26, 2022
cda9e18
add user_platform and contract_platform repos; rename usercontact to …
ocasta181 Oct 26, 2022
a0e9ed8
round out entity api with update and customData
ocasta181 Oct 26, 2022
d99d59e
tests running
ocasta181 Oct 27, 2022
e4c39a2
Merge branch 'develop' into task/andrin/str-154
Oct 27, 2022
12442e4
cleanup; instrument progress; entity completion
ocasta181 Oct 27, 2022
9e5482c
Merge branch 'task/andrin/str-154' of github.com:String-xyz/string-ap…
ocasta181 Oct 27, 2022
6cdeb04
cleanup debugging logs
ocasta181 Oct 27, 2022
eeeebec
expose constructors
ocasta181 Oct 27, 2022
a447329
another constructor reference
ocasta181 Oct 27, 2022
5e5026e
add statusCode checking
ocasta181 Oct 27, 2022
8bb200b
entity and instrument tests passing
ocasta181 Oct 28, 2022
c490427
normalized TX vs Tx across the codebase; mostly complete structure fo…
ocasta181 Oct 28, 2022
4f9bcb9
transaction event tests passing
ocasta181 Oct 29, 2022
c84f13d
instrument tests closer to fully working
ocasta181 Oct 29, 2022
464f2f9
adding more data for entity; testing non-persistence of data to u21
ocasta181 Oct 31, 2022
03904bd
unit21 can't handle empty string values
ocasta181 Nov 1, 2022
c9790d0
remove debugging error thrown and commented out code
ocasta181 Nov 1, 2022
ffb6097
remove logs and comments
ocasta181 Nov 1, 2022
4950730
move out repos into own struct
ocasta181 Nov 2, 2022
3c1982b
fix quote typo
ocasta181 Nov 2, 2022
e6be36e
remove todo comment from entity model
ocasta181 Nov 2, 2022
2e16357
resolve merge conflicts
ocasta181 Nov 2, 2022
3b201c9
GetId to GetById
ocasta181 Nov 2, 2022
638243e
ensure verification endpoint is properly routed
ocasta181 Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ REDIS_PORT=6379
JWT_SECRET_KEY=hskdhfjfkdhsgafgwterurorhfh
UNIT21_API_KEY=
UNIT21_URL=https://sandbox2-api.unit21.com/v1/
UNIT21_ORG_NAME=String
TWILIO_ACCOUNT_SID=AC034879a536d54325687e48544403cb4d
TWILIO_AUTH_TOKEN=
TWILIO_SMS_SID=MG367a4f51ea6f67a28db4d126eefc734f
Expand Down
6 changes: 3 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func baseMiddleware(logger *zerolog.Logger, e *echo.Echo) {
func authRoute(config APIConfig, e *echo.Echo) service.Auth {
a := repository.NewAuth(config.Redis, config.DB)
u := repository.NewUser(config.DB)
c := repository.NewUserContact(config.DB)
c := repository.NewContact(config.DB)
service := service.NewAuth(a, u, c)
handler := handler.NewAuth(service)
handler.RegisterRoutes(e.Group("/auth"))
Expand All @@ -57,7 +57,7 @@ func authRoute(config APIConfig, e *echo.Echo) service.Auth {
func platformRoute(config APIConfig, e *echo.Echo) {
p := repository.NewPlatform(config.DB)
a := repository.NewAuth(config.Redis, config.DB)
c := repository.NewUserContact(config.DB)
c := repository.NewContact(config.DB)
service := service.NewPlatform(p, c, a)
handler := handler.NewPlatform(service)
handler.RegisterRoutes(e.Group("/platform"), middleware.BearerAuth())
Expand Down Expand Up @@ -95,6 +95,6 @@ func verificationRoute(config APIConfig, e *echo.Echo) {
Instrument: repository.NewInstrument(config.DB),
}
service := service.NewUser(repos)
handler := handler.NewUser(e, service)
handler := handler.NewVerification(e, service)
handler.RegisterRoutes(e.Group("/verification"))
}
10 changes: 5 additions & 5 deletions migrations/0001_string-user_platform_asset_network.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,26 @@ CREATE INDEX network_gas_token_id_fk ON network (gas_token_id);
-- ASSET ----------------------------------------------------------------
DROP TRIGGER IF EXISTS update_asset_updated_at ON asset;
DROP INDEX IF EXISTS network_gas_token_id_fk;
DROP TABLE asset;
DROP TABLE IF EXISTS asset;

-------------------------------------------------------------------------
-- NETWORK --------------------------------------------------------------
DROP TRIGGER IF EXISTS update_network_updated_at ON network;
DROP TABLE network;
DROP TABLE IF EXISTS network;

-------------------------------------------------------------------------
-- PLATFORM -------------------------------------------------------------
DROP TRIGGER IF EXISTS update_platform_updated_at ON platfom;
DROP TABLE platform;
DROP TABLE IF EXISTS platform;

-------------------------------------------------------------------------
-- STRING_USER ----------------------------------------------------------
DROP TRIGGER IF EXISTS update_string_user_updated_at ON string_user;
DROP TABLE string_user;
DROP TABLE IF EXISTS string_user;

-------------------------------------------------------------------------
-- UPDATE_UPDATED_AT_COLUMN() -------------------------------------------
DROP FUNCTION update_updated_at_column;
DROP FUNCTION IF EXISTS update_updated_at_column;

-------------------------------------------------------------------------
-- UUID EXTENSION -------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
-------------------------------------------------------------------------
-- +goose Up

-------------------------------------------------------------------------
-- USER_PLATFORM --------------------------------------------------------
CREATE TABLE user_platform (
user_id UUID REFERENCES string_user (id),
platform_id UUID REFERENCES platform (id)
);

CREATE UNIQUE INDEX user_platform_user_id_platform_id_idx ON user_platform(user_id, platform_id);

-------------------------------------------------------------------------
-- DEVICE ---------------------------------------------------------------
CREATE TABLE device (
Expand All @@ -13,7 +22,7 @@ CREATE TABLE device (
type TEXT DEFAULT '', -- enum: to be defined at struct level in Go
description TEXT DEFAULT '',
fingerprint TEXT DEFAULT '',
ip_addresses JSONB DEFAULT '[]'::JSONB,
ip_addresses TEXT[] DEFAULT NULL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be JSONB, since we are dealing with it as a string array at the Go level

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the change that @saito-sv and I discussed at some length. It is the 2nd change that will need to be made to all arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@auroter, we can discuss further in office hours.

user_id UUID NOT NULL REFERENCES string_user (id)
);

Expand Down Expand Up @@ -96,19 +105,23 @@ EXECUTE PROCEDURE update_updated_at_column();
-------------------------------------------------------------------------
-- INSTRUMENT -----------------------------------------------------------
DROP TRIGGER IF EXISTS update_instrument_updated_at ON instrument;
DROP TABLE instrument;
DROP TABLE IF EXISTS instrument;

-------------------------------------------------------------------------
-- LOCATION -----------------------------------------------------------
DROP TRIGGER IF EXISTS update_location_updated_at ON location;
DROP TABLE location;
DROP TABLE IF EXISTS location;

-------------------------------------------------------------------------
-- CONTACT --------------------------------------------------------------
DROP TRIGGER IF EXISTS update_contact_updated_at ON contact;
DROP TABLE contact;
DROP TABLE IF EXISTS contact;

-------------------------------------------------------------------------
-- DEVICE ---------------------------------------------------------------
DROP TRIGGER IF EXISTS update_device_updated_at ON device;
DROP TABLE device;
DROP TABLE IF EXISTS device;

-------------------------------------------------------------------------
-- USER_PLATFORM -----------------------------------------------------
DROP TABLE IF EXISTS user_platform;
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ EXECUTE PROCEDURE update_updated_at_column();
-------------------------------------------------------------------------
-- TRANSACTION ----------------------------------------------------------
DROP TRIGGER IF EXISTS update_transaction_updated_at ON transaction;
DROP TABLE transaction;
DROP TABLE IF EXISTS transaction;

-------------------------------------------------------------------------
-- TX_LEG ---------------------------------------------------------------
DROP TRIGGER IF EXISTS update_tx_leg_updated_at ON tx_leg;
DROP TABLE tx_leg;
DROP TABLE IF EXISTS tx_leg;

-------------------------------------------------------------------------
-- DEVICE_INSTRUMENT ----------------------------------------------------
DROP TABLE device_instrument;
DROP TABLE IF EXISTS device_instrument;

-------------------------------------------------------------------------
-- CONTACT_PLATFORM -----------------------------------------------------
DROP TABLE contact_platform;
DROP TABLE IF EXISTS contact_platform;
2 changes: 1 addition & 1 deletion migrations/0004_auth_strategy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ EXECUTE PROCEDURE update_updated_at_column();

-- +goose Down
DROP TRIGGER IF EXISTS update_auth_strategy_updated_at ON auth_strategy;
DROP TABLE auth_strategy;
DROP TABLE IF EXISTS auth_strategy;
14 changes: 14 additions & 0 deletions pkg/internal/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package common
import (
"crypto/sha256"
"encoding/hex"
"log"
"math"
"reflect"
"strconv"

"github.com/ethereum/go-ethereum/accounts"
ethcomm "github.com/ethereum/go-ethereum/common"
Expand All @@ -29,6 +32,17 @@ func RecoverAddress(message string, signature string) (ethcomm.Address, error) {
return crypto.PubkeyToAddress(*recovered), nil
}

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)
err = StringError(err)
return
}
floatReturn = floatReturn * math.Pow(10, -float64(decimals))
return
}

func isNil(i interface{}) bool {
if i == nil {
return true
Expand Down
9 changes: 9 additions & 0 deletions pkg/internal/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"testing"

"github.com/String-xyz/string-api/pkg/model"
"github.com/stretchr/testify/assert"
)

Expand All @@ -12,3 +13,11 @@ func TestRecoverSignature(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC", addr.Hex())
}

func TestKeysAndValues(t *testing.T) {
mType := "type"
m := model.ContactUpdates{Type: &mType}
names, vals := KeysAndValues(m)
assert.Len(t, names, 1)
assert.Len(t, vals, 1)
}
97 changes: 45 additions & 52 deletions pkg/internal/unit21/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package unit21
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand All @@ -12,25 +13,10 @@ import (
"github.com/String-xyz/string-api/pkg/internal/common"
)

// type Unit21Data interface {
// Create(any) (unit21Id string, err error)
// Update(id string, updates any) (err error)
// }

// type unit21Data struct {
// repository repository.Transactable
// }

// func newUnit21Data(repo repository.Transactable) Unit21Data {
// return &unit21Data{repository: repo}
// }

// u21type == entities
// jsonBody = MapUserToEntity(user)
func create(datatype string, jsonBody any) (body []byte, err error) {
apiKey := os.Getenv("UNIT21_API_KEY")
url := os.Getenv("UNIT21_URL") + datatype + "/create"
log.Printf("url: %s", url)

reqBodyBytes, err := json.Marshal(jsonBody)
if err != nil {
log.Printf("Could not encode %s to bytes: %s", datatype, err)
Expand All @@ -54,8 +40,6 @@ func create(datatype string, jsonBody any) (body []byte, err error) {
res, err := client.Do(req)
if err != nil {
log.Printf("Request failed to create %s: %s", datatype, err)
//handle 409 on update that is not allowed
//handle 423, 500, 503 for retries
return nil, common.StringError(err)
}

Expand All @@ -69,52 +53,61 @@ func create(datatype string, jsonBody any) (body []byte, err error) {

log.Printf("String of body from response: %s", string(body))

if res.StatusCode != 200 {
log.Printf("Request failed to create %s: %s", datatype, fmt.Sprint(res.StatusCode))
err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body)))
return
}

return body, nil
}

func update(datatype string, id string, updates any) (body []byte, err error) {
// apiKey := os.Getenv("UNIT21_API_KEY")
// url := os.Getenv("UNIT21_URL") + "/" + datatype + "/" + id + "/update"
func update(datatype string, id string, jsonBody any) (body []byte, err error) {
apiKey := os.Getenv("UNIT21_API_KEY")
orgName := os.Getenv("UNIT21_ORG_NAME")
url := os.Getenv("UNIT21_URL") + orgName + "/" + datatype + "/" + id + "/update"

// names, keyToUpdate := common.KeysAndValues(updates)
reqBodyBytes, err := json.Marshal(jsonBody)
if err != nil {
log.Printf("Could not encode %s to bytes: %s", datatype, err)
return nil, common.StringError(err)
}

// reqBodyBytes, err := json.Marshal(jsonBody)
// if err != nil {
// log.Printf("Could not encode %s to bytes: %s", datatype, err)
// return
// }
bodyReader := bytes.NewReader(reqBodyBytes)

// bodyReader := bytes.NewReader(reqBodyBytes)
req, err := http.NewRequest(http.MethodPut, url, bodyReader)
if err != nil {
log.Printf("Could not create request for %s: %s", datatype, err)
return nil, common.StringError(err)
}

// req, err := http.NewRequest(http.MethodPost, url, bodyReader)
// if err != nil {
// log.Printf("Could not create request for %s: %s", datatype, err)
// return
// }
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("u21-key", apiKey)

// req.Header.Add("accept", "application/json")
// req.Header.Add("content-type", "application/json")
// req.Header.Add("u21-key", apiKey)
client := http.Client{Timeout: 10 * time.Second}

// client := http.Client{Timeout: 10 * time.Second}
res, err := client.Do(req)
if err != nil {
log.Printf("Request failed to update %s: %s", datatype, err)
return nil, common.StringError(err)
}

// res, err := client.Do(req)
// if err != nil {
// log.Printf("Request failed to create %s: %s", datatype, err)
// //handle 409 on update that is not allowed
// //handle 423, 500, 503 for retries
// return
// }
defer res.Body.Close()

// defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Error extracting body from %s update request: %s", datatype, err)
return nil, common.StringError(err)
}

// body, err = ioutil.ReadAll(res.Body)
// if err != nil {
// log.Printf("Error extracting body from %s create request: %s", datatype, err)
// return
// }
log.Printf("String of body from response: %s", string(body))

// log.Printf("String of body from response: %s", string(body))
if res.StatusCode != 200 {
log.Printf("Request failed to update %s: %s", datatype, fmt.Sprint(res.StatusCode))
err = common.StringError(fmt.Errorf("request failed with status code %s and return body: %s", fmt.Sprint(res.StatusCode), string(body)))
return
}

return
return body, nil
}
Loading