-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cosmos_transfer_controller.go
93 lines (81 loc) · 3.2 KB
/
cosmos_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
package web
import (
"fmt"
"net/http"
"slices"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
coscfg "github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/config"
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/db"
"github.com/smartcontractkit/chainlink-cosmos/pkg/cosmos/denom"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
cosmosmodels "github.com/smartcontractkit/chainlink/v2/core/store/models/cosmos"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// CosmosTransfersController can send LINK tokens to another address
type CosmosTransfersController struct {
App chainlink.Application
}
// Create sends native coins from the Chainlink's account to a specified address.
func (tc *CosmosTransfersController) Create(c *gin.Context) {
relayers := tc.App.GetRelayers().List(chainlink.FilterRelayersByType(relay.Cosmos))
if relayers == nil {
jsonAPIError(c, http.StatusBadRequest, ErrSolanaNotEnabled)
return
}
var tr cosmosmodels.SendRequest
if err := c.ShouldBindJSON(&tr); err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if tr.CosmosChainID == "" {
jsonAPIError(c, http.StatusBadRequest, errors.New("missing cosmosChainID"))
return
}
if tr.FromAddress.Empty() {
jsonAPIError(c, http.StatusUnprocessableEntity, errors.Errorf("withdrawal source address is missing: %v", tr.FromAddress))
return
}
relayerID := relay.ID{Network: relay.Cosmos, ChainID: tr.CosmosChainID}
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
}
var gasToken string
cfgs := tc.App.GetConfig().CosmosConfigs()
i := slices.IndexFunc(cfgs, func(config *coscfg.TOMLConfig) bool { return *config.ChainID == tr.CosmosChainID })
if i == -1 {
jsonAPIError(c, http.StatusInternalServerError, fmt.Errorf("no config for chain id: %s", tr.CosmosChainID))
return
}
gasToken = cfgs[i].GasToken()
//TODO move this inside?
coin, err := denom.ConvertDecCoinToDenom(sdk.NewDecCoinFromDec(tr.Token, tr.Amount), gasToken)
if err != nil {
jsonAPIError(c, http.StatusBadRequest, errors.Errorf("unable to convert %s to %s: %v", tr.Token, gasToken, err))
return
} else if !coin.Amount.IsPositive() {
jsonAPIError(c, http.StatusBadRequest, errors.Errorf("amount must be greater than zero: %s", coin.Amount))
return
}
err = relayer.Transact(c, tr.FromAddress.String(), tr.DestinationAddress.String(), coin.Amount.BigInt(), !tr.AllowHigherAmounts)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, errors.Errorf("failed to send transaction: %v", err))
return
}
resource := presenters.NewCosmosMsgResource("cosmos_transfer_"+uuid.New().String(), tr.CosmosChainID, "")
resource.State = string(db.Unstarted)
tc.App.GetAuditLogger().Audit(audit.CosmosTransactionCreated, map[string]interface{}{
"cosmosTransactionResource": resource,
})
jsonAPIResponse(c, resource, "cosmos_msg")
}