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
|
|
|
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NetworkManagerInterface interface {
|
|
|
|
GetActiveNetworks() ([]*params.Network, error)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func GetSupportedChainIDs(networkManager NetworkManagerInterface) ([]uint64, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDefaultChainID(networkManager NetworkManagerInterface) (uint64, error) {
|
|
|
|
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
|
|
|
|
}
|