Skip to content

Commit

Permalink
Fix: contract stats query (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jan 28, 2023
1 parent 377849e commit 7659ffe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
1 change: 0 additions & 1 deletion cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ type Screenshot struct {
// AccountInfo -
type AccountInfo struct {
Address string `json:"address"`
Network string `json:"network"`
Alias string `json:"alias,omitempty" extensions:"x-nullable"`
Balance int64 `json:"balance"`
TxCount int64 `json:"tx_count"`
Expand Down
24 changes: 17 additions & 7 deletions internal/postgres/operation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ func (storage *Storage) EventsCount(accountID int64) (int, error) {
Where("kind = 7").Count()
}

const cteContractStatsTemplate = `with last_operations as (
SELECT timestamp
FROM mainnet.operations AS "operation"
WHERE ((destination_id = ?) OR (source_id = ?))
order by timestamp desc
FETCH NEXT 20 ROWS ONLY
)
select max(timestamp) as timestamp from last_operations`

type lastTimestampResult struct {
Timestamp time.Time `pg:"timestamp"`
}

// ContractStats -
func (storage *Storage) ContractStats(address string) (stats operation.ContractStats, err error) {
var accountID int64
Expand All @@ -338,14 +351,11 @@ func (storage *Storage) ContractStats(address string) (stats operation.ContractS
return
}

if err = storage.DB.Model((*operation.Operation)(nil)).ColumnExpr("max(timestamp)").
WhereGroup(
func(q *orm.Query) (*orm.Query, error) {
return q.Where("destination_id = ?", accountID).WhereOr("source_id = ?", accountID), nil
},
).Select(&stats.LastAction); err != nil {
return
var result lastTimestampResult
if _, err := storage.DB.QueryOne(&result, cteContractStatsTemplate, accountID, accountID); err != nil {
return stats, err
}
stats.LastAction = result.Timestamp

count, err := storage.DB.Model((*operation.Operation)(nil)).WhereGroup(
func(q *orm.Query) (*orm.Query, error) {
Expand Down

0 comments on commit 7659ffe

Please sign in to comment.