2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-09-19 18:24:06 +00:00
|
|
|
"context"
|
2024-07-18 15:30:10 +00:00
|
|
|
"database/sql"
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
"github.com/status-im/status-go/rpc/network"
|
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-09-19 18:24:06 +00:00
|
|
|
NetworkManager *network.Manager
|
2024-07-26 16:18:01 +00:00
|
|
|
Db *sql.DB
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func (c *ChainIDCommand) Execute(ctx context.Context, 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
|
|
|
}
|