-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
solana_transfer_controller.go
135 lines (115 loc) · 3.73 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package web
import (
"net/http"
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
solanaGo "github.com/gagliardetto/solana-go"
"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) {
solanaChains := tc.App.GetChains().Solana
if solanaChains == 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
}
chain, err := solanaChains.Chain(c.Request.Context(), tr.SolanaChainID)
switch err {
case chains.ErrNotFound, chains.ErrChainIDEmpty:
jsonAPIError(c, http.StatusBadRequest, err)
return
case nil:
break
default:
jsonAPIError(c, http.StatusInternalServerError, err)
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
}
txm := chain.TxManager()
var reader client.Reader
reader, err = chain.Reader()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, errors.Errorf("chain unreachable: %v", err))
return
}
blockhash, err := reader.LatestBlockhash()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, errors.Errorf("failed to get latest block hash: %v", err))
return
}
tx, err := solanaGo.NewTransaction(
[]solanaGo.Instruction{
system.NewTransferInstruction(
tr.Amount,
tr.From,
tr.To,
).Build(),
},
blockhash.Value.Blockhash,
solanaGo.TransactionPayer(tr.From),
)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, errors.Errorf("failed to create tx: %v", err))
return
}
if !tr.AllowHigherAmounts {
if err = solanaValidateBalance(reader, tr.From, tr.Amount, tx.Message.ToBase64()); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, errors.Errorf("failed to validate balance: %v", err))
return
}
}
err = txm.Enqueue("", tx)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, errors.Errorf("transaction failed: %v", 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")
}
func solanaValidateBalance(reader client.Reader, from solanaGo.PublicKey, amount uint64, msg string) error {
balance, err := reader.Balance(from)
if err != nil {
return err
}
fee, err := reader.GetFeeForMessage(msg)
if err != nil {
return err
}
if balance < (amount + fee) {
return errors.Errorf("balance %d is too low for this transaction to be executed: amount %d + fee %d", balance, amount, fee)
}
return nil
}