/
erc721.go
63 lines (53 loc) · 1.8 KB
/
erc721.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package getstatus_erc721
import (
"errors"
"math/big"
"net/http"
"github.com/TheLazarusNetwork/go-helpers/httpo"
"github.com/TheLazarusNetwork/go-helpers/logo"
"github.com/Weareflexable/Superiad/models/contracts"
"github.com/Weareflexable/Superiad/pkg/network/polygon"
"github.com/ethereum/go-ethereum/common"
"gorm.io/gorm"
"github.com/gin-gonic/gin"
)
// ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/erc721")
{
g.POST("", getStatusErc721)
}
}
func getStatusErc721(c *gin.Context) {
network := "matic"
var req GetStatusRequest
if err := c.ShouldBindJSON(&req); err != nil {
logo.Errorf("invalid request %s", err)
httpo.NewErrorResponse(http.StatusBadRequest, "body is invalid").SendD(c)
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
}
status, err := polygon.GetStatus(common.HexToAddress(req.ContractAddress), big.NewInt(req.TokenId))
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to get status of token").SendD(c)
logo.Errorf("failed to get status of token with id: %d, network: %v, contractAddr: %v, error: %s",
req.TokenId, network, req.ContractAddress, err)
return
}
httpo.NewSuccessResponseP(200, "status fetched", GetStatusPayload{
Status: status,
}).SendD(c)
}