Skip to content

Commit

Permalink
hot fix: update db call and modify response
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Aug 14, 2023
1 parent da74e01 commit cc82b77
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ listener:
addr: :8000

networks:
list:
keys:
- name: 'Ethereum'
rpc: "https://mainnet.infura.io/v3/5afc5eb6b4424ad39f8c0db22125074a"
- name: 'Polygon'
Expand Down
7 changes: 5 additions & 2 deletions docs/spec/components/schemas/TxBlob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ required:
- payment_id
- tx_hash
- recipient
- network
- network_from
- network_to
properties:
payment_id:
type: string
recipient:
type: string
tx_hash:
type: string
network:
network_from:
type: string
network_to:
type: string
2 changes: 1 addition & 1 deletion internal/config/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type NetworkConfiger interface {
}

type Network struct {
NetInfoList []NetInfo `fig:"list"`
NetInfoList []NetInfo `fig:"keys"`
}

type NetInfo struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/data/pg/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions internal/data/pg/transactions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pg

import (
"database/sql"
"fmt"
sq "github.com/Masterminds/squirrel"
"github.com/dl-only-tokens/back-listener/internal/data"
Expand All @@ -14,7 +13,7 @@ const transactionTableName = "transactions"

const (
idField = "id"
addressField = "address"
addressField = "recipient"
)

func NewTransactionsQ(db *pgdb.DB) data.TransactionsQ {
Expand All @@ -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")
}
Expand Down
21 changes: 13 additions & 8 deletions internal/service/api/handlers/get_tx_lists.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))
}
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/service/core/listener/listenr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions internal/service/core/rarimo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions resources/model_tx_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit cc82b77

Please sign in to comment.