/
erc20.go
60 lines (51 loc) · 1.65 KB
/
erc20.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
package checkbalance_erc20
import (
"errors"
"math/big"
"net/http"
"github.com/TheLazarusNetwork/go-helpers/httpo"
"github.com/TheLazarusNetwork/go-helpers/logo"
"github.com/Weareflexable/Superiad/models/user"
"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("/erc20")
{
g.POST("", erc20CheckBalance)
}
}
func erc20CheckBalance(c *gin.Context) {
var req CheckErc20BalanceRequest
err := c.ShouldBindJSON(&req)
if err != nil {
httpo.NewErrorResponse(http.StatusBadRequest, "body is not valid").Send(c, http.StatusBadRequest)
return
}
network := "matic"
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 with id %v, err %s", req.UserId, err)
return
}
var balance *big.Int
balance, err = polygon.GetERC20Balance(mnemonic, common.HexToAddress(req.ContractAddr))
if err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to get balance").SendD(c)
logo.Errorf("failed to get ERC20 balance of wallet of userId: %v , network: %v, contractAddr: %v , error: %s", req.UserId,
network, req.ContractAddr, err)
return
}
payload := CheckErc20BalancePayload{
Balance: balance.String(),
}
httpo.NewSuccessResponseP(200, "balance successfully fetched", payload).SendD(c)
}