-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
evm_forwarders_controller.go
89 lines (71 loc) · 2.7 KB
/
evm_forwarders_controller.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package web
import (
"net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/forwarders"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/smartcontractkit/chainlink/v2/core/utils/stringutils"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
"github.com/gin-gonic/gin"
)
// EVMForwardersController manages EVM forwarders.
type EVMForwardersController struct {
App chainlink.Application
}
// Index lists EVM forwarders.
func (cc *EVMForwardersController) Index(c *gin.Context, size, page, offset int) {
orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig())
fwds, count, err := orm.FindForwarders(0, size)
if err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
var resources []presenters.EVMForwarderResource
for _, fwd := range fwds {
resources = append(resources, presenters.NewEVMForwarderResource(fwd))
}
paginatedResponse(c, "forwarder", size, page, resources, count, err)
}
// TrackEVMForwarderRequest is a JSONAPI request for creating an EVM forwarder.
type TrackEVMForwarderRequest struct {
EVMChainID *utils.Big `json:"chainID"`
Address common.Address `json:"address"`
}
// Track adds a new EVM forwarder.
func (cc *EVMForwardersController) Track(c *gin.Context) {
request := &TrackEVMForwarderRequest{}
if err := c.ShouldBindJSON(&request); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig())
fwd, err := orm.CreateForwarder(request.Address, *request.EVMChainID)
if err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
cc.App.GetAuditLogger().Audit(audit.ForwarderCreated, map[string]interface{}{
"forwarderID": fwd.ID,
"forwarderAddress": fwd.Address,
"forwarderEVMChainID": fwd.EVMChainID,
})
jsonAPIResponseWithStatus(c, presenters.NewEVMForwarderResource(fwd), "forwarder", http.StatusCreated)
}
// Delete removes an EVM Forwarder.
func (cc *EVMForwardersController) Delete(c *gin.Context) {
id, err := stringutils.ToInt32(c.Param("fwdID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig())
err = orm.DeleteForwarder(id)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
cc.App.GetAuditLogger().Audit(audit.ForwarderDeleted, map[string]interface{}{"id": id})
jsonAPIResponseWithStatus(c, nil, "forwarder", http.StatusNoContent)
}