refactor(rpc): streamline RPC node selection and remove hardcoded defaults
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/assimon/luuu/config"
|
||||
tron "github.com/assimon/luuu/crypto"
|
||||
"github.com/assimon/luuu/model/data"
|
||||
"github.com/assimon/luuu/model/mdb"
|
||||
@@ -26,19 +25,21 @@ import (
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// tronNodeDefault is the fallback when no rpc_nodes row exists for TRON.
|
||||
const tronNodeDefault = "https://api.trongrid.io"
|
||||
|
||||
// resolveTronNode returns (baseURL, apiKey) for the TRON HTTP RPC node.
|
||||
// It reads the first healthy (or any enabled) row from the rpc_nodes table;
|
||||
// if none exists it falls back to the hard-coded default URL and the
|
||||
// tron_grid_api_key value from .env (config.TRON_GRID_API_KEY).
|
||||
func resolveTronNode() (string, string) {
|
||||
// It reads the first healthy (or any enabled) row from the rpc_nodes table.
|
||||
func resolveTronNode() (string, string, error) {
|
||||
node, err := data.SelectRpcNode(mdb.NetworkTron, mdb.RpcNodeTypeHttp)
|
||||
if err == nil && node != nil && node.ID > 0 {
|
||||
return strings.TrimRight(node.Url, "/"), node.ApiKey
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return tronNodeDefault, config.TRON_GRID_API_KEY
|
||||
if node == nil || node.ID == 0 {
|
||||
return "", "", fmt.Errorf("no enabled %s %s RPC node configured in rpc_nodes", mdb.NetworkTron, mdb.RpcNodeTypeHttp)
|
||||
}
|
||||
rpcURL := strings.TrimRight(strings.TrimSpace(node.Url), "/")
|
||||
if rpcURL == "" {
|
||||
return "", "", fmt.Errorf("rpc_nodes id=%d has empty url", node.ID)
|
||||
}
|
||||
return rpcURL, node.ApiKey, nil
|
||||
}
|
||||
|
||||
func Trc20CallBack(address string, wg *sync.WaitGroup) {
|
||||
@@ -79,7 +80,11 @@ func checkTrxTransfers(address string, wg *sync.WaitGroup) {
|
||||
client := http_client.GetHttpClient()
|
||||
startTime := carbon.Now().AddHours(-24).TimestampMilli()
|
||||
endTime := carbon.Now().TimestampMilli()
|
||||
tronBaseURL, tronAPIKey := resolveTronNode()
|
||||
tronBaseURL, tronAPIKey, err := resolveTronNode()
|
||||
if err != nil {
|
||||
log.Sugar.Errorf("[TRX][%s] resolve rpc_nodes err=%v", address, err)
|
||||
return
|
||||
}
|
||||
url := fmt.Sprintf("%s/v1/accounts/%s/transactions", tronBaseURL, address)
|
||||
|
||||
resp, err := client.R().SetQueryParams(map[string]string{
|
||||
@@ -217,7 +222,11 @@ func checkTrc20Transfers(address string, wg *sync.WaitGroup) {
|
||||
client := http_client.GetHttpClient()
|
||||
startTime := carbon.Now().AddHours(-24).TimestampMilli()
|
||||
endTime := carbon.Now().TimestampMilli()
|
||||
tronBaseURL, tronAPIKey := resolveTronNode()
|
||||
tronBaseURL, tronAPIKey, err := resolveTronNode()
|
||||
if err != nil {
|
||||
log.Sugar.Errorf("[TRC20][%s] resolve rpc_nodes err=%v", address, err)
|
||||
return
|
||||
}
|
||||
url := fmt.Sprintf("%s/v1/accounts/%s/transactions/trc20", tronBaseURL, address)
|
||||
|
||||
resp, err := client.R().SetQueryParams(map[string]string{
|
||||
|
||||
Reference in New Issue
Block a user