Skip to content

Commit

Permalink
hot fix: update response
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Aug 30, 2023
1 parent c86b904 commit 7b80f4f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 43 deletions.
16 changes: 3 additions & 13 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ networks:
keys:
- name: 'Goerli'
chain_id: 5
start_block: 9600199
rpc: "https://goerli.infura.io/v3/"
- name: 'Fuji'
chain_id: 43113
rpc: "https://avalanche-fuji.infura.io/v3/"
- name: 'BSC Testnet'
chain_id: 97
start_block: 9600199
rpc: "https://data-seed-prebsc-2-s3.binance.org:8545/"
- name: 'Ethereum'
chain_id: 1
rpc: "https://mainnet.infura.io/v3/"
- name: 'Avalanche'
chain_id: 43114
rpc: "https://avalanche-mainnet.infura.io/v3/"
- name: 'Sepolia'
chain_id: 11155111
rpc: "https://sepolia.infura.io/v3/"



meta_data:
Expand All @@ -36,7 +27,6 @@ meta_data:

chain_listener:
pause_time: 10000
abi_path: "/fgo/src/github.com/dl-only-tokens/back-listener/pkg/contract/erc20/erc20.abi"

cop:
disabled: true
Expand Down
43 changes: 25 additions & 18 deletions internal/data/pg/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
const transactionTableName = "transactions"

const (
idField = "id"
recipientField = "recipient"
paymentIDField = "payment_id"
txHashFromField = "tx_hash_from"
txHashToField = "tx_hash_to"
RecipientField = "recipient"
PaymentIDField = "payment_id"
TxHashFromField = "tx_hash_from"
TxHashToField = "tx_hash_to"
TimestampField = "timestamp_to"
)

func NewTransactionsQ(db *pgdb.DB) data.TransactionsQ {
Expand Down Expand Up @@ -67,44 +67,51 @@ func (q *TransactionsQ) Insert(value *data.Transactions) error {
}

func (q *TransactionsQ) FilterByRecipient(address string) data.TransactionsQ {
q.sql = q.sql.Where(sq.Eq{recipientField: address})
q.upd = q.upd.Where(sq.Eq{recipientField: address})
q.sql = q.sql.Where(sq.Eq{RecipientField: address})
q.upd = q.upd.Where(sq.Eq{RecipientField: address})

return q
}

func (q *TransactionsQ) FilterByPaymentID(paymentID string) data.TransactionsQ {
q.sql = q.sql.Where(sq.Eq{paymentIDField: paymentID})
q.upd = q.upd.Where(sq.Eq{paymentIDField: paymentID})
q.sql = q.sql.Where(sq.Eq{PaymentIDField: paymentID})
q.upd = q.upd.Where(sq.Eq{PaymentIDField: paymentID})

return q
}

func (q *TransactionsQ) FilterByReady() data.TransactionsQ {
q.sql = q.sql.Where(sq.NotEq{txHashToField: ""})
q.upd = q.upd.Where(sq.NotEq{txHashToField: ""})
q.sql = q.sql.Where(sq.NotEq{TxHashToField: ""})
q.upd = q.upd.Where(sq.NotEq{TxHashToField: ""})

q.sql = q.sql.Where(sq.NotEq{txHashFromField: ""})
q.upd = q.upd.Where(sq.NotEq{txHashFromField: ""})
q.sql = q.sql.Where(sq.NotEq{TxHashFromField: ""})
q.upd = q.upd.Where(sq.NotEq{TxHashFromField: ""})

return q
}

func (q *TransactionsQ) FilterByNotReady() data.TransactionsQ {
q.sql = q.sql.Where(sq.Eq{txHashToField: ""})
q.upd = q.upd.Where(sq.Eq{txHashToField: ""})
q.sql = q.sql.Where(sq.Eq{TxHashToField: ""})
q.upd = q.upd.Where(sq.Eq{TxHashToField: ""})

return q
}
func (q *TransactionsQ) FilterByNetworkFrom(networkFrom int32) data.TransactionsQ {
q.sql = q.sql.Where(sq.Eq{txHashFromField: networkFrom})
q.upd = q.upd.Where(sq.Eq{txHashFromField: networkFrom})
q.sql = q.sql.Where(sq.Eq{TxHashFromField: networkFrom})
q.upd = q.upd.Where(sq.Eq{TxHashFromField: networkFrom})

return q
}

func (q *TransactionsQ) OrderBy(column, order string) data.TransactionsQ {
q.sql = q.sql.OrderBy(fmt.Sprintf("%s %s", column, order))
q.upd = q.upd.OrderBy(fmt.Sprintf("%s %s", column, order))

return q
}

func (q *TransactionsQ) Page(pageParams pgdb.OffsetPageParams) data.TransactionsQ {
q.sql = pageParams.ApplyTo(q.sql, idField)
q.sql = pageParams.ApplyTo(q.sql, PaymentIDField)

return q
}
1 change: 1 addition & 0 deletions internal/data/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TransactionsQ interface {
FilterByNetworkFrom(networkFrom int32) TransactionsQ
FilterByRecipient(address string) TransactionsQ
FilterByPaymentID(paymentID string) TransactionsQ
OrderBy(column, order string) TransactionsQ
}

type Transactions struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/service/api/handlers/get_tx_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"database/sql"
"errors"
"github.com/dl-only-tokens/back-listener/internal/data"
"github.com/dl-only-tokens/back-listener/internal/data/pg"
"github.com/dl-only-tokens/back-listener/internal/service/api/requests"
"github.com/dl-only-tokens/back-listener/resources"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
"gitlab.com/distributed_lab/kit/pgdb"
"net/http"
)

Expand All @@ -19,7 +21,7 @@ func GetTxLists(w http.ResponseWriter, r *http.Request) {
return
}

txs, err := MasterQ(r).TransactionsQ().New().FilterByRecipient(req.Address).Select()
txs, err := MasterQ(r).TransactionsQ().New().FilterByRecipient(req.Address).OrderBy(pg.TimestampField, pgdb.OrderTypeDesc).Select()
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Log(r).WithError(err).Error("failed to empty select list")
Expand Down
3 changes: 2 additions & 1 deletion internal/service/api/requests/get_tx_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/pkg/errors"
"gitlab.com/distributed_lab/urlval"
"net/http"
"strings"
)

const (
Expand All @@ -20,7 +21,7 @@ func NewGetTXsListRequest(r *http.Request) (GetTXsListRequest, error) {
if err := urlval.Decode(r.URL.Query(), &request); err != nil {
return request, errors.Wrap(err, "failed to decode query")
}
request.Address = chi.URLParam(r, AddressPathParam)
request.Address = strings.ToLower(chi.URLParam(r, AddressPathParam))

return request, nil
}
29 changes: 19 additions & 10 deletions internal/service/core/listener/listenr.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (l *ListenData) prepareDataToInsert(inputs map[string][]string) ([]data.Tra
response = append(response, tx)
}

return response, nil
return l.txsToLowerCase(response), nil
}

func (l *ListenData) insertTxs(txs []data.Transactions, blockTime uint64) error {
Expand Down Expand Up @@ -275,14 +275,6 @@ func (l *ListenData) insertTxs(txs []data.Transactions, blockTime uint64) error
return nil
}

func (l *ListenData) interfaceToTx(any interface{}) (*[]data.Transactions, error) {
if convertedData, ok := any.([]data.Transactions); ok {
return &convertedData, nil
}

return nil, errors.New("data cant be converted to TX")
}

func (l *ListenData) filteringTx(block *types.Block) (map[string][]string, error) {
result := make(map[string][]string)
for _, evmTx := range block.Transactions() {
Expand All @@ -299,7 +291,6 @@ func (l *ListenData) filteringTx(block *types.Block) (map[string][]string, error
}

func (l *ListenData) packTX(firstTX data.Transactions, secondTX data.Transactions) *data.Transactions {

result := data.Transactions{}

result.TxHashTo = secondTX.TxHashTo
Expand All @@ -323,6 +314,24 @@ func (l *ListenData) packTX(firstTX data.Transactions, secondTX data.Transaction
return &result
}

func (l *ListenData) txsToLowerCase(data []data.Transactions) []data.Transactions {
for _, tx := range data {
if len(tx.Sender) > 0 {
strings.ToLower(tx.Sender)
}
if len(tx.Recipient) > 0 {
strings.ToLower(tx.Recipient)
}
if len(tx.TxHashTo) > 0 {
strings.ToLower(tx.TxHashTo)
}
if len(tx.TxHashFrom) > 0 {
strings.ToLower(tx.TxHashFrom)
}
}
return data
}

func (l *ListenData) stringToInt32(str string) (int32, error) {
var n int32
if _, err := fmt.Sscan(str, &n); err != nil {
Expand Down

0 comments on commit 7b80f4f

Please sign in to comment.