2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2024-07-26 16:18:01 +00:00
|
|
|
"github.com/status-im/status-go/services/connector/chainutils"
|
2024-07-18 15:30:10 +00:00
|
|
|
persistence "github.com/status-im/status-go/services/connector/database"
|
|
|
|
walletCommon "github.com/status-im/status-go/services/wallet/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ChainIDCommand struct {
|
2024-07-26 16:18:01 +00:00
|
|
|
NetworkManager NetworkManagerInterface
|
|
|
|
Db *sql.DB
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 07:43:31 +00:00
|
|
|
func (c *ChainIDCommand) Execute(request RPCRequest) (interface{}, error) {
|
2024-07-18 15:30:10 +00:00
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dApp, err := persistence.SelectDAppByUrl(c.Db, request.URL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-08-15 11:49:29 +00:00
|
|
|
var chainId uint64
|
2024-07-18 15:30:10 +00:00
|
|
|
if dApp == nil {
|
2024-08-15 11:49:29 +00:00
|
|
|
chainId, err = chainutils.GetDefaultChainID(c.NetworkManager)
|
2024-07-26 16:18:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-08-15 11:49:29 +00:00
|
|
|
} else {
|
|
|
|
chainId = dApp.ChainID
|
2024-07-26 20:00:12 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 11:49:29 +00:00
|
|
|
chainIdHex, err := chainutils.GetHexChainID(walletCommon.ChainID(chainId).String())
|
2024-07-26 20:00:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 11:49:29 +00:00
|
|
|
return chainIdHex, nil
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|