-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
evm_forwarders_controller.go
107 lines (87 loc) · 3.54 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package web
import (
"math/big"
"net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/forwarders"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"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().Database())
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:"evmChainId"`
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().Database())
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.ToInt64(c.Param("fwdID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
filterCleanup := func(tx pg.Queryer, evmChainID int64, addr common.Address) error {
chain, err2 := cc.App.GetRelayers().LegacyEVMChains().Get(big.NewInt(evmChainID).String())
if err2 != nil {
// If the chain id doesn't even exist, or logpoller is disabled, then there isn't any filter to clean up. Returning an error
// here could be dangerous as it would make it impossible to delete a forwarder with an invalid chain id
return nil
}
if chain.LogPoller() == logpoller.LogPollerDisabled {
// handle same as non-existent chain id
return nil
}
return chain.LogPoller().UnregisterFilter(forwarders.FilterName(addr), pg.WithQueryer(tx))
}
orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig().Database())
err = orm.DeleteForwarder(id, filterCleanup)
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)
}