From bf865cbe22899e518754171a0f3764121d4eabcb Mon Sep 17 00:00:00 2001 From: Shubham Prajapati Date: Sat, 21 Sep 2024 11:52:18 +0530 Subject: [PATCH] "Added new API endpoint for delegate ticket creation for base network and updated gin mode configuration" --- .../delegatenftcreation.go | 50 ++++++++++++++++++- app/stage/apprun/apprun.go | 5 ++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/api/v1/matic/delegateticketcreation/delegatenftcreation/delegatenftcreation.go b/api/v1/matic/delegateticketcreation/delegatenftcreation/delegatenftcreation.go index b2300c8..00e40f6 100644 --- a/api/v1/matic/delegateticketcreation/delegatenftcreation/delegatenftcreation.go +++ b/api/v1/matic/delegateticketcreation/delegatenftcreation/delegatenftcreation.go @@ -19,8 +19,8 @@ import ( func ApplyRoutes(r *gin.RouterGroup) { g := r.Group("/delegateTicketCreation") { - g.POST("", delegateTicketCreation) + g.POST("/base", delegateTicketCreationForBase) } } @@ -72,6 +72,54 @@ func delegateTicketCreation(c *gin.Context) { } sendSuccessResponse(c, hash, req.UserId) } +func delegateTicketCreationForBase(c *gin.Context) { + network := "base" + var req DelegateTicketCreationRequest + if err := c.ShouldBindJSON(&req); err != nil { + logo.Errorf("invalid request %s", err) + httpo.NewErrorResponse(http.StatusBadRequest, "body is invalid").SendD(c) + return + } + mnemonic, err := user.GetMnemonic(req.UserId) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + httpo.NewErrorResponse(httpo.UserNotFound, "user not found").Send(c, 404) + + return + } + + httpo.NewErrorResponse(http.StatusInternalServerError, "failed to fetch user").SendD(c) + logo.Errorf("failed to fetch user mnemonic for userId: %v, error: %s", + req.UserId, err) + return + } + + contractDetails, err := contracts.GetContract(req.ContractAddress) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + httpo.NewErrorResponse(404, "contract not found in database").SendD(c) + return + } + httpo.NewErrorResponse(http.StatusInternalServerError, "failed to fetch contract details from database").SendD(c) + logo.Errorf("failed to fetch contract details from database, error: %s", err) + return + } + if contractDetails.ContractType != contracts.ERC721 { + httpo.NewErrorResponse(http.StatusBadRequest, "contract is not ERC721").SendD(c) + return + } + + flexableNFTContractAddr := common.HexToAddress(req.ContractAddress) + var hash string + hash, err = polygon.DelegateNFTCreation(mnemonic, flexableNFTContractAddr, req.MetadataURI) + if err != nil { + httpo.NewErrorResponse(http.StatusInternalServerError, "failed to tranfer").SendD(c) + logo.Errorf("failed to delegate erc721 to wallet of userId: %v , network: %v, contractAddr: %v, error: %s", + req.UserId, network, req.ContractAddress, err) + return + } + sendSuccessResponse(c, hash, req.UserId) +} func sendSuccessResponse(c *gin.Context, hash string, userId string) { payload := DelegateTicketCreationPayload{ diff --git a/app/stage/apprun/apprun.go b/app/stage/apprun/apprun.go index c56cf32..afb7657 100644 --- a/app/stage/apprun/apprun.go +++ b/app/stage/apprun/apprun.go @@ -14,6 +14,11 @@ import ( func Run() { ginApp := gin.Default() + if envconfig.EnvVars.GIN_MODE == "debug" { + gin.SetMode(gin.DebugMode) + } else { + gin.SetMode(gin.ReleaseMode) + } corsM := cors.New(cors.Config{AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},