Skip to content

Commit

Permalink
Merge pull request #57 from CheesecakeLabs/feat_supply_chart
Browse files Browse the repository at this point in the history
Feat supply chart
  • Loading branch information
lucasmagnus authored Sep 28, 2023
2 parents ec12d69 + 9bc7be7 commit 03fb0dc
Show file tree
Hide file tree
Showing 23 changed files with 2,288 additions and 149 deletions.
798 changes: 757 additions & 41 deletions backend/docs/docs.go

Large diffs are not rendered by default.

798 changes: 757 additions & 41 deletions backend/docs/swagger.json

Large diffs are not rendered by default.

512 changes: 496 additions & 16 deletions backend/docs/swagger.yaml

Large diffs are not rendered by default.

52 changes: 36 additions & 16 deletions backend/internal/controller/http/v1/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,39 @@ type CreateAssetRequest struct {
}

type BurnAssetRequest struct {
Id string `json:"id" binding:"required" example:"001"`
SponsorId int `json:"sponsor_id" example:"2"`
Amount string `json:"amount" binding:"required" example:"1000"`
Id string `json:"id" binding:"required" example:"001"`
SponsorId int `json:"sponsor_id" example:"2"`
Amount string `json:"amount" binding:"required" example:"1000"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type MintAssetRequest struct {
Id string `json:"id" binding:"required" example:"12"`
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
Id string `json:"id" binding:"required" example:"12"`
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type ClawbackAssetRequest struct {
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
From string `json:"from" binding:"required" example:"GDKIJJIKXLOM2NRMPNQZUUYK24ZPVFC6426GZAICZ6E5PQG2MIPIMB2L"`
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
From string `json:"from" binding:"required" example:"GDKIJJIKXLOM2NRMPNQZUUYK24ZPVFC6426GZAICZ6E5PQG2MIPIMB2L"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type TransferAssetRequest struct {
SourceWalletID int `json:"source_wallet_id" binding:"required" example:"1"`
SponsorId int `json:"sponsor_id" example:"2"`
DestinationWalletPK string `json:"destination_wallet_pk" binding:"required" example:"GABCD...."`
AssetID string `json:"asset_id" binding:"required" example:"12"`
Amount string `json:"amount" binding:"required" example:"12"`
SourceWalletID int `json:"source_wallet_id" binding:"required" example:"1"`
SponsorId int `json:"sponsor_id" example:"2"`
DestinationWalletPK string `json:"destination_wallet_pk" binding:"required" example:"GABCD...."`
AssetID string `json:"asset_id" binding:"required" example:"12"`
Amount string `json:"amount" binding:"required" example:"12"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type UpdateAuthFlagsRequest struct {
Expand Down Expand Up @@ -321,12 +329,15 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
if err != nil {
amount = 0
}

err = r.l.CreateLogTransaction(entity.LogTransaction{
Asset: asset,
Amount: amount,
TransactionTypeID: entity.MintAsset,
UserID: userID,
Description: createLogDescription(entity.MintAsset, asset.Code, nil, nil),
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down Expand Up @@ -415,6 +426,8 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {
Amount: amount,
UserID: userID,
Description: createLogDescription(entity.BurnAsset, asset.Code, nil, nil),
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down Expand Up @@ -485,6 +498,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

token := c.Request.Header.Get("Authorization")
Expand All @@ -511,6 +525,8 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
Description: createLogDescription(entity.TransferAsset, asset.Code, nil, nil),
OriginPK: &sourceWallet.Key.PublicKey,
DestinationPK: &request.DestinationWalletPK,
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down Expand Up @@ -575,6 +591,7 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

token := c.Request.Header.Get("Authorization")
Expand All @@ -587,6 +604,7 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
userID, err := strconv.Atoi(user.ID)
if err != nil {
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

amount, err := strconv.ParseFloat(request.Amount, 64)
Expand All @@ -599,6 +617,8 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
Amount: amount,
UserID: userID,
Description: createLogDescription(entity.ClawbackAsset, asset.Code, nil, nil),
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down
45 changes: 45 additions & 0 deletions backend/internal/controller/http/v1/log_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ func newLogTransactionsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase,
h.GET("/assets/:asset_id/type/:transaction_type_id/sum/:time_range/:time_frame", r.sumAmountsByAssetID)
h.GET("/assets/sum/:time_range/:time_frame", r.sumAmountsForAllAssets)
h.GET("/last-transactions/:transaction_type_id", r.getLastLogTransactions)
h.POST("/supply/:asset_id", r.supplyByAssetID)

}
}

type FiltersRequest struct {
TimeRange string `json:"time_range" example:"hour"`
PeriodInitial string `json:"period_initial" example:"24 hours"`
Interval string `json:"interval" example:"1 hour"`
}

// @Summary Get all transactions logs
// @Description Get all transactions logs within a specific time range
// @Tags Log Transactions
Expand Down Expand Up @@ -244,3 +252,40 @@ func (r *logTransactionsRoutes) getLastLogTransactions(c *gin.Context) {

c.JSON(http.StatusOK, logTransactions)
}

// @Summary Get sum of supply by Asset ID within a specific time frame
// @Description Get sum of supply for a specific asset, grouped by a specified time frame (e.g., '1h' for 1 hour)
// @Tags Log Transactions
// @Accept json
// @Produce json
// @Param asset_id path int true "Asset ID"
// @Param time_range path string true "Time range for the query (e.g., '24h' or '1d' '7d' '30d')"
// @Param time_frame path string true "Time frame for the query (e.g., '1h' '2h' '24h' '36h')"
// @Security ApiKeyAuth
// @Success 200 {object} entity.SumLogTransaction
// @Failure 400 {string} string "Invalid time_frame format"
// @Failure 500 {string} string "Internal server error"
// @Router /log_transactions/supply/{asset_id}/{time_range}/{time_frame} [get]
func (r *logTransactionsRoutes) supplyByAssetID(c *gin.Context) {
assetIDStr := c.Param("asset_id")

var request FiltersRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

assetID, err := strconv.Atoi(assetIDStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid asset ID: %s", err.Error()), err)
return
}

sum, err := r.l.LogTransactionsSupplyByAssetID(assetID, request.TimeRange, request.PeriodInitial, request.Interval)
if err != nil {
errorResponse(c, http.StatusInternalServerError, fmt.Sprintf("error getting log transactions: %s", err.Error()), err)
return
}

c.JSON(http.StatusOK, sum)
}
8 changes: 4 additions & 4 deletions backend/internal/controller/http/v1/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newRoleRoutes(handler *gin.RouterGroup, roleUseCase usecase.RoleUseCase, me
// @Tags Role
// @Accept json
// @Produce json
// @Param type query string true "Type"
// @Param type query string true "Type"
// @Success 200 {object} []entity.Role
// @Router /role [get]
func (r *role) list(c *gin.Context) {
Expand All @@ -49,7 +49,7 @@ func (r *role) list(c *gin.Context) {
// @Tags Role
// @Accept json
// @Produce json
// @Param request body RoleRequest true "Role info"
// @Param request body entity.RoleRequest true "Role info"
// @Success 200 {object} entity.RoleRequest
// @Failure 400 {object} response
// @Failure 404 {object} response
Expand Down Expand Up @@ -80,7 +80,7 @@ func (r *role) createRole(c *gin.Context) {
// @Accept json
// @Produce json
// @Param id path string true "Role ID" Format(uuid)
// @Param request body RoleRequest true "Role info"
// @Param request body entity.RoleRequest true "Role info"
// @Success 200 {object} entity.Role "Updated role information"
// @Failure 400 {object} response "Bad Request: Invalid input data"
// @Failure 404 {object} response "Not Found: Role not found"
Expand Down Expand Up @@ -130,7 +130,7 @@ func (r *role) updateRole(c *gin.Context) {
// @Accept json
// @Produce json
// @Param id path string true "Role ID" Format(uuid)
// @Param request body RoleRequest true "Role info"
// @Param request body entity.RoleRequest true "Role info"
// @Success 200 {object} entity.RoleDelete "Updated role information"
// @Failure 400 {object} response "Bad Request: Invalid input data"
// @Failure 404 {object} response "Not Found: Role not found"
Expand Down
5 changes: 2 additions & 3 deletions backend/internal/controller/http/v1/role_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ func (r *rolePermissions) permissions(c *gin.Context) {
// @Tags RolesPermissions
// @Accept json
// @Produce json
// @Param request body RolePermissionRequest
// @Success 200 {object} entity.RolePermissionRequest "Updated roles permissions information"
// @Param request body RolePermissionRequest true "Roles permissions information"
// @Success 200 {object} RolePermissionRequest "Updated roles permissions information"
// @Failure 400 {object} response "Bad Request: Invalid input data"
// @Failure 500 {object} response "Internal Server Error: Failed to update roles permissions"
// @Router /role-permission [put]
func (r *rolePermissions) updateRolesPermissions(c *gin.Context) {

var request []RolePermissionRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
Expand Down
6 changes: 3 additions & 3 deletions backend/internal/controller/http/v1/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *usersRoutes) logout(c *gin.Context) {
// @Accept json
// @Produce json
// @Success 200 {object} []entity.UserResponse
// @Router /users [get]
// @Router /users/list-users [get]
func (r *usersRoutes) getAllUsers(c *gin.Context) {
users, err := r.t.GetAllUsers()
fmt.Println(users)
Expand All @@ -190,7 +190,7 @@ func (r *usersRoutes) getAllUsers(c *gin.Context) {
// @Produce json
// @Success 200 {object} entity.UserRole
// @Failure 500 {object} response
// @Router /user/create [post]
// @Router /users/edit-users-role [post]
func (r *usersRoutes) editUsersRole(c *gin.Context) {
var userRole entity.UserRole
if err := c.ShouldBindJSON(&userRole); err != nil {
Expand All @@ -217,7 +217,7 @@ func (r *usersRoutes) editUsersRole(c *gin.Context) {
// @Accept json
// @Produce json
// @Success 200 {object} entity.UserResponse
// @Router /users [get]
// @Router /users/profile [get]
func (r *usersRoutes) getProfile(c *gin.Context) {
token := c.GetHeader("Authorization")
profile, err := r.t.GetProfile(token)
Expand Down
6 changes: 3 additions & 3 deletions backend/internal/controller/http/v1/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newVaultRoutes(handler *gin.RouterGroup, m HTTPControllerMessenger, a useca
type CreateVaultRequest struct {
Name string `json:"name" binding:"required" example:"Treasury"`
VaultCategoryId int `json:"vault_category_id" binding:"required" example:"1"`
AssetsId []int `json:"assets_id" binding:"required" example:"[1,2]"`
AssetsId []int `json:"assets_id" binding:"required"`
}

type UpdateVaultCategoryRequest struct {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (r *vaultRoutes) createVault(c *gin.Context) {
// @Produce json
// @Success 200 {object} []entity.Vault
// @Failure 500 {object} response
// @Router / [get]
// @Router /vault/list [get]
func (r *vaultRoutes) getAllVaults(c *gin.Context) {
vault, err := r.v.GetAll()
if err != nil {
Expand All @@ -191,7 +191,7 @@ func (r *vaultRoutes) getAllVaults(c *gin.Context) {
// @Produce json
// @Success 200 {object} entity.Vault
// @Failure 500 {object} response
// @Router / [get]
// @Router /vault/{id} [get]
func (r *vaultRoutes) getVaultById(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/controller/http/v1/vault_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *vaultCategoryRoutes) createVaultCategory(c *gin.Context) {
// @Produce json
// @Success 200 {object} []entity.VaultCategory
// @Failure 500 {object} response
// @Router / [get]
// @Router /vault-category [get]
func (r *vaultCategoryRoutes) getAllVaultCategories(c *gin.Context) {
vaultCategories, err := r.vc.GetAll()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions backend/internal/entity/role.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package entity

type Role struct {
Id int `json:"id" example:"1"`
Name string `json:"name" example:"Admin"`
Id int `json:"id" example:"1"`
Name string `json:"name" example:"Admin"`
Admin int `json:"admin" example:"1"`
}

type RoleRequest struct {
Expand Down
35 changes: 24 additions & 11 deletions backend/internal/entity/transaction_log.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package entity

type LogTransaction struct {
LogID int `json:"log_id" example:"1"`
UserID int `json:"user_id" example:"42"`
TransactionTypeID int `json:"transaction_type_id" example:"1"`
Asset Asset `json:"asset"`
Date string `json:"date" example:"2023-08-10T14:30:00Z"`
Amount float64 `json:"amount" example:"100000"`
Description string `json:"description" example:"Mint Asset"`
OriginPK *string `json:"origin_pk" example:"GSDC..."`
DestinationPK *string `json:"destination_pk" example:"GSDC..."`
CurrentSupply *int `json:"current_supply" example:"1000"`
CurrentMainVault *int `json:"current_main_vault" example:"1000"`
LogID int `json:"log_id" example:"1"`
UserID int `json:"user_id" example:"42"`
TransactionTypeID int `json:"transaction_type_id" example:"1"`
Asset Asset `json:"asset"`
Date string `json:"date" example:"2023-08-10T14:30:00Z"`
Amount float64 `json:"amount" example:"100000"`
Description string `json:"description" example:"Mint Asset"`
OriginPK *string `json:"origin_pk" example:"GSDC..."`
DestinationPK *string `json:"destination_pk" example:"GSDC..."`
CurrentSupply *float64 `json:"current_supply" example:"1000"`
CurrentMainVault *float64 `json:"current_main_vault" example:"1000"`
}

type SumLogTransaction struct {
Expand All @@ -21,6 +21,19 @@ type SumLogTransaction struct {
Quantity []int `json:"quantity" example:"1"`
}

type SumLogTransactionSupply struct {
Asset Asset `json:"asset"`
CurrentSupply []*float64 `json:"current_supply" example:"1000"`
CurrentyMainVault []*float64 `json:"current_main_vault" example:"1000"`
Date []string `json:"date" example:"2023-08-10T14:30:00Z"`
}

type LogTransactionSupply struct {
CurrentSupply []*float64 `json:"current_supply" example:"1000"`
CurrentyMainVault []*float64 `json:"current_main_vault" example:"1000"`
Date []string `json:"date" example:"2023-08-10T14:30:00Z"`
}

const (
CreateAsset int = iota + 1 // CreateAsset = 1
MintAsset // MintAsset = 2
Expand Down
2 changes: 2 additions & 0 deletions backend/internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,7 @@ type (
SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration, transactionType int) (entity.SumLogTransaction, error)
SumLogTransactions(timeRange string, timeFrame time.Duration) ([]entity.SumLogTransaction, error)
GetLastLogTransactions(transactionTypeID int) ([]entity.LogTransaction, error)
SumLogTransactionSupply(timeRange string, timeFrame time.Duration) ([]entity.SumLogTransactionSupply, error)
LogTransactionSupplyByAssetID(assetID int, timeRange string, periodInitial string, interval string) (entity.LogTransactionSupply, error)
}
)
16 changes: 16 additions & 0 deletions backend/internal/usecase/log_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ func (l *LogTransactionUseCase) GetLastLogTransactions(transactionTypeID int) ([
}
return logTransactions, nil
}

func (l *LogTransactionUseCase) SumLogTransactionsSupply(timeRange string, timeFrame time.Duration) ([]entity.SumLogTransactionSupply, error) {
sum, err := l.lRepo.SumLogTransactionSupply(timeRange, timeFrame)
if err != nil {
return []entity.SumLogTransactionSupply{}, err
}
return sum, nil
}

func (l *LogTransactionUseCase) LogTransactionsSupplyByAssetID(assetID int, timeRange string, periodInitial string, interval string) (entity.LogTransactionSupply, error) {
sum, err := l.lRepo.LogTransactionSupplyByAssetID(assetID, timeRange, periodInitial, interval)
if err != nil {
return entity.LogTransactionSupply{}, err
}
return sum, nil
}
Loading

0 comments on commit 03fb0dc

Please sign in to comment.