From cc82b7741e6987d93db4170a72a92510dfe8eb20 Mon Sep 17 00:00:00 2001 From: "mark.cherepovskyi" Date: Mon, 14 Aug 2023 10:35:43 +0300 Subject: [PATCH] hot fix: update db call and modify response --- config.yaml | 2 +- docs/spec/components/schemas/TxBlob.yaml | 7 +++++-- internal/config/networks.go | 2 +- internal/data/pg/master.go | 4 ++-- internal/data/pg/transactions.go | 6 +----- internal/service/api/handlers/get_tx_lists.go | 21 ++++++++++++------- internal/service/core/listener/listenr.go | 3 +-- internal/service/core/rarimo/api.go | 6 +++--- resources/model_tx_blob.go | 9 ++++---- 9 files changed, 32 insertions(+), 28 deletions(-) diff --git a/config.yaml b/config.yaml index 1ef133c..120ba48 100644 --- a/config.yaml +++ b/config.yaml @@ -9,7 +9,7 @@ listener: addr: :8000 networks: - list: + keys: - name: 'Ethereum' rpc: "https://mainnet.infura.io/v3/5afc5eb6b4424ad39f8c0db22125074a" - name: 'Polygon' diff --git a/docs/spec/components/schemas/TxBlob.yaml b/docs/spec/components/schemas/TxBlob.yaml index fc5e8e8..c34c8ed 100644 --- a/docs/spec/components/schemas/TxBlob.yaml +++ b/docs/spec/components/schemas/TxBlob.yaml @@ -3,7 +3,8 @@ required: - payment_id - tx_hash - recipient - - network + - network_from + - network_to properties: payment_id: type: string @@ -11,5 +12,7 @@ properties: type: string tx_hash: type: string - network: + network_from: + type: string + network_to: type: string diff --git a/internal/config/networks.go b/internal/config/networks.go index c705e55..a974b6b 100644 --- a/internal/config/networks.go +++ b/internal/config/networks.go @@ -12,7 +12,7 @@ type NetworkConfiger interface { } type Network struct { - NetInfoList []NetInfo `fig:"list"` + NetInfoList []NetInfo `fig:"keys"` } type NetInfo struct { diff --git a/internal/data/pg/master.go b/internal/data/pg/master.go index 24f7b39..a331cec 100644 --- a/internal/data/pg/master.go +++ b/internal/data/pg/master.go @@ -12,12 +12,12 @@ type masterQ struct { func NewMasterQ(db *pgdb.DB) data.MasterQ { return &masterQ{ - db: db.Clone(), + db: db, } } func (q *masterQ) New() data.MasterQ { - return NewMasterQ(q.db) + return NewMasterQ(q.db.Clone()) } func (q *masterQ) TransactionsQ() data.TransactionsQ { diff --git a/internal/data/pg/transactions.go b/internal/data/pg/transactions.go index 1f073af..614e93e 100644 --- a/internal/data/pg/transactions.go +++ b/internal/data/pg/transactions.go @@ -1,7 +1,6 @@ package pg import ( - "database/sql" "fmt" sq "github.com/Masterminds/squirrel" "github.com/dl-only-tokens/back-listener/internal/data" @@ -14,7 +13,7 @@ const transactionTableName = "transactions" const ( idField = "id" - addressField = "address" + addressField = "recipient" ) func NewTransactionsQ(db *pgdb.DB) data.TransactionsQ { @@ -36,9 +35,6 @@ func (q *TransactionsQ) New() data.TransactionsQ { func (q *TransactionsQ) Select() ([]data.Transactions, error) { var result []data.Transactions err := q.db.Select(&result, q.sql) - if err == sql.ErrNoRows { - return nil, nil - } if err != nil { return nil, errors.Wrap(err, "failed to select txs") } diff --git a/internal/service/api/handlers/get_tx_lists.go b/internal/service/api/handlers/get_tx_lists.go index 72852fb..f8e0022 100644 --- a/internal/service/api/handlers/get_tx_lists.go +++ b/internal/service/api/handlers/get_tx_lists.go @@ -1,6 +1,8 @@ package handlers import ( + "database/sql" + "errors" "github.com/dl-only-tokens/back-listener/internal/data" "github.com/dl-only-tokens/back-listener/internal/service/api/requests" "github.com/dl-only-tokens/back-listener/resources" @@ -19,15 +21,16 @@ func GetTxLists(w http.ResponseWriter, r *http.Request) { txs, err := MasterQ(r).TransactionsQ().New().FilterByAddress(req.Address).Select() if err != nil { + if errors.Is(err, sql.ErrNoRows) { + Log(r).WithError(err).Error("failed to empty select list") + ape.Render(w, resources.GetTxListResponse{}) + return + } + Log(r).WithError(err).Error("failed to select txs by address") ape.RenderErr(w, problems.InternalError()) return } - if txs == nil { - Log(r).WithError(err).Error("failed to empty select list") - ape.RenderErr(w, problems.InternalError()) - return - } ape.Render(w, prepareResponse(txs)) } @@ -36,9 +39,11 @@ func prepareResponse(txs []data.Transactions) resources.GetTxListResponse { txBlobs := make([]resources.TxBlob, 0) for _, tx := range txs { blob := resources.TxBlob{ - PaymentId: tx.PaymentID, - Recipient: tx.Recipient, - TxHash: tx.TxHash, + NetworkFrom: tx.NetworkFrom, + NetworkTo: tx.NetworkTo, + PaymentId: tx.PaymentID, + Recipient: tx.Recipient, + TxHash: tx.TxHash, } txBlobs = append(txBlobs, blob) } diff --git a/internal/service/core/listener/listenr.go b/internal/service/core/listener/listenr.go index 6d4fb8d..6509736 100644 --- a/internal/service/core/listener/listenr.go +++ b/internal/service/core/listener/listenr.go @@ -119,10 +119,8 @@ func (l *ListenData) Run() { func (l *ListenData) getTxIntputsOnBlock(txHashes []RecipientInfo, block *types.Block) map[string][]string { result := make(map[string][]string) - for _, info := range txHashes { tx := block.Transaction(info.TxHash) - l.log.Debug(hex.EncodeToString(tx.Data())) parsedData, err := l.parsePayloadOnInput(hex.EncodeToString(tx.Data()), l.txMetaData.Header, l.txMetaData.Footer) //todo header if err != nil { @@ -135,6 +133,7 @@ func (l *ListenData) getTxIntputsOnBlock(txHashes []RecipientInfo, block *types. return result } +// parse some encoded info(payment id, network from and network to ) from tx data func (l *ListenData) parsePayloadOnInput(input string, header string, footer string) ([]string, error) { //move to another class index := strings.Index(input, header) if index == -1 { diff --git a/internal/service/core/rarimo/api.go b/internal/service/core/rarimo/api.go index 0cc5f69..f262ba9 100644 --- a/internal/service/core/rarimo/api.go +++ b/internal/service/core/rarimo/api.go @@ -27,14 +27,14 @@ func (h RarimoApi) GetContractsAddresses() ([]config.NetInfo, error) { return nil, errors.Wrap(err, "failed to send request") } - if resp.StatusCode >= 300 && resp.StatusCode < 500 { - return nil, errors.New("bad response code") + if resp.StatusCode >= 300 { + return nil, errors.New("failed get contract addresses via rarimo api: bad response code") } decodedResponse := new(NetworkListResponse) if err := json.NewDecoder(resp.Body).Decode(&decodedResponse); err != nil { - return nil, errors.New("failed to decode response ") + return nil, errors.Wrap(err, "failed to decode response ") } return h.parseResponse(decodedResponse.Data), nil } diff --git a/resources/model_tx_blob.go b/resources/model_tx_blob.go index 9eef77c..ce972cf 100644 --- a/resources/model_tx_blob.go +++ b/resources/model_tx_blob.go @@ -5,8 +5,9 @@ package resources type TxBlob struct { - Network string `json:"network"` - PaymentId string `json:"payment_id"` - Recipient string `json:"recipient"` - TxHash string `json:"tx_hash"` + NetworkFrom string `json:"network_from"` + NetworkTo string `json:"network_to"` + PaymentId string `json:"payment_id"` + Recipient string `json:"recipient"` + TxHash string `json:"tx_hash"` }