From 980f526afde7712eb7d1330c88f1c310e3ae841c Mon Sep 17 00:00:00 2001 From: Artem Poltorzhitskiy Date: Fri, 10 Feb 2023 14:56:29 +0400 Subject: [PATCH] API: add stats flag to contracts endpoint (#966) --- cmd/api/handlers/contract.go | 21 +++++++++++++++++---- cmd/api/handlers/requests.go | 9 +++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd/api/handlers/contract.go b/cmd/api/handlers/contract.go index b28068985..f45bf20db 100644 --- a/cmd/api/handlers/contract.go +++ b/cmd/api/handlers/contract.go @@ -32,6 +32,11 @@ func GetContract() gin.HandlerFunc { return } + var args withStatsRequest + if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusNotFound) { + return + } + contract, err := ctx.Contracts.Get(req.Address) if err != nil { if ctx.Storage.IsRecordNotFound(err) { @@ -44,11 +49,19 @@ func GetContract() gin.HandlerFunc { ctxs := c.MustGet("contexts").(config.Contexts) - res, err := contractWithStatsPostprocessing(ctxs, ctx, contract) - if handleError(c, ctx.Storage, err, 0) { - return + if args.HasStats() { + res, err := contractWithStatsPostprocessing(ctxs, ctx, contract) + if handleError(c, ctx.Storage, err, 0) { + return + } + c.SecureJSON(http.StatusOK, res) + } else { + res, err := contractPostprocessing(ctx, contract) + if handleError(c, ctx.Storage, err, 0) { + return + } + c.SecureJSON(http.StatusOK, res) } - c.SecureJSON(http.StatusOK, res) } } diff --git a/cmd/api/handlers/requests.go b/cmd/api/handlers/requests.go index 9b62ce0dc..1c64df3b4 100644 --- a/cmd/api/handlers/requests.go +++ b/cmd/api/handlers/requests.go @@ -33,6 +33,15 @@ type getContractCodeRequest struct { Level int64 `form:"level,omitempty"` } +type withStatsRequest struct { + Stats *bool `form:"stats,omitempty" binding:"omitempty"` +} + +// HasStats - +func (req withStatsRequest) HasStats() bool { + return req.Stats == nil || *req.Stats +} + type networkQueryRequest struct { Network string `form:"network,omitempty" binding:"omitempty,network"` }