-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
solana_transfer_controller.go
73 lines (64 loc) · 2.14 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
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"
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) {
relayer := tc.App.GetChains().Solana
if relayer == 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)
err := relayer.SendTx(c, tr.SolanaChainID, tr.From.String(), tr.To.String(), amount, !tr.AllowHigherAmounts)
if err != nil {
switch err {
case chains.ErrNotFound, chains.ErrChainIDEmpty:
jsonAPIError(c, http.StatusBadRequest, err)
return
case nil:
break
default:
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")
}