-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
solana_transfer_controller.go
81 lines (71 loc) · 2.53 KB
/
solana_transfer_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
package web
import (
"math/big"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/chains"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
solanamodels "github.com/smartcontractkit/chainlink/v2/core/store/models/solana"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// SolanaTransfersController can send LINK tokens to another address
type SolanaTransfersController struct {
App chainlink.Application
}
// Create sends SOL and other native coins from the Chainlink's account to a specified address.
func (tc *SolanaTransfersController) Create(c *gin.Context) {
relayers := tc.App.GetRelayers().List(chainlink.FilterRelayersByType(relay.Solana))
if relayers == nil {
jsonAPIError(c, http.StatusBadRequest, ErrSolanaNotEnabled)
return
}
var tr solanamodels.SendRequest
if err := c.ShouldBindJSON(&tr); err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if tr.SolanaChainID == "" {
jsonAPIError(c, http.StatusBadRequest, errors.New("missing solanaChainID"))
return
}
if tr.From.IsZero() {
jsonAPIError(c, http.StatusUnprocessableEntity, errors.Errorf("source address is missing: %v", tr.From))
return
}
if tr.Amount == 0 {
jsonAPIError(c, http.StatusBadRequest, errors.New("amount must be greater than zero"))
return
}
amount := new(big.Int).SetUint64(tr.Amount)
relayerID := relay.ID{Network: relay.Solana, ChainID: relay.ChainID(tr.SolanaChainID)}
relayer, err := relayers.Get(relayerID)
if err != nil {
if errors.Is(err, chainlink.ErrNoSuchRelayer) {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
err = relayer.Transact(c, tr.From.String(), tr.To.String(), amount, !tr.AllowHigherAmounts)
if err != nil {
if errors.Is(err, chains.ErrNotFound) || errors.Is(err, chains.ErrChainIDEmpty) {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
resource := presenters.NewSolanaMsgResource("sol_transfer_"+uuid.New().String(), tr.SolanaChainID)
resource.Amount = tr.Amount
resource.From = tr.From.String()
resource.To = tr.To.String()
tc.App.GetAuditLogger().Audit(audit.SolanaTransactionCreated, map[string]interface{}{
"solanaTransactionResource": resource,
})
jsonAPIResponse(c, resource, "solana_tx")
}