2024-07-26 16:18:01 +00:00
|
|
|
package chainutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-07-26 20:00:12 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-07-26 16:18:01 +00:00
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
"github.com/status-im/status-go/rpc/network"
|
2024-07-26 16:18:01 +00:00
|
|
|
)
|
|
|
|
|
2024-07-26 20:00:12 +00:00
|
|
|
var (
|
|
|
|
ErrNoActiveNetworks = errors.New("no active networks available")
|
|
|
|
ErrUnsupportedNetwork = errors.New("unsupported network")
|
|
|
|
)
|
2024-07-26 16:18:01 +00:00
|
|
|
|
|
|
|
// GetSupportedChainIDs retrieves the chain IDs from the provided NetworkManager.
|
2024-09-19 18:24:06 +00:00
|
|
|
func GetSupportedChainIDs(networkManager *network.Manager) ([]uint64, error) {
|
2024-07-26 16:18:01 +00:00
|
|
|
activeNetworks, err := networkManager.GetActiveNetworks()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(activeNetworks) < 1 {
|
|
|
|
return nil, ErrNoActiveNetworks
|
|
|
|
}
|
|
|
|
|
|
|
|
chainIDs := make([]uint64, len(activeNetworks))
|
|
|
|
for i, network := range activeNetworks {
|
|
|
|
chainIDs[i] = network.ChainID
|
|
|
|
}
|
|
|
|
|
|
|
|
return chainIDs, nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func GetDefaultChainID(networkManager *network.Manager) (uint64, error) {
|
2024-07-26 16:18:01 +00:00
|
|
|
chainIDs, err := GetSupportedChainIDs(networkManager)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return chainIDs[0], nil
|
|
|
|
}
|
2024-07-26 20:00:12 +00:00
|
|
|
|
|
|
|
func GetHexChainID(decimalStr string) (string, error) {
|
|
|
|
decimalValue, err := strconv.ParseInt(decimalStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", ErrUnsupportedNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
hexStr := fmt.Sprintf(`0x%s`, strconv.FormatInt(decimalValue, 16))
|
|
|
|
|
|
|
|
return hexStr, nil
|
|
|
|
}
|