2021-09-09 14:28:54 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
)
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
const baseQuery = "SELECT chain_id, chain_name, rpc_url, block_explorer_url, icon_url, native_currency_name, native_currency_symbol, native_currency_decimals, is_test, layer, enabled FROM networks"
|
|
|
|
|
|
|
|
func newNetworksQuery() *networksQuery {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
buf.WriteString(baseQuery)
|
|
|
|
return &networksQuery{buf: buf}
|
|
|
|
}
|
|
|
|
|
|
|
|
type networksQuery struct {
|
|
|
|
buf *bytes.Buffer
|
|
|
|
args []interface{}
|
|
|
|
added bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nq *networksQuery) andOrWhere() {
|
|
|
|
if nq.added {
|
|
|
|
nq.buf.WriteString(" AND")
|
|
|
|
} else {
|
|
|
|
nq.buf.WriteString(" WHERE")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nq *networksQuery) filterEnabled(enabled bool) *networksQuery {
|
|
|
|
nq.andOrWhere()
|
|
|
|
nq.added = true
|
|
|
|
nq.buf.WriteString(" enabled = ?")
|
|
|
|
nq.args = append(nq.args, enabled)
|
|
|
|
return nq
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nq *networksQuery) filterChainID(chainID uint64) *networksQuery {
|
|
|
|
nq.andOrWhere()
|
|
|
|
nq.added = true
|
|
|
|
nq.buf.WriteString(" chain_id = ?")
|
|
|
|
nq.args = append(nq.args, chainID)
|
|
|
|
return nq
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
func (nq *networksQuery) exec(db *sql.DB) ([]*params.Network, error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
rows, err := db.Query(nq.buf.String(), nq.args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-12 20:04:43 +00:00
|
|
|
var res []*params.Network
|
2021-09-09 14:28:54 +00:00
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
2022-01-12 20:04:43 +00:00
|
|
|
network := params.Network{}
|
2021-09-09 14:28:54 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&network.ChainID, &network.ChainName, &network.RPCURL, &network.BlockExplorerURL, &network.IconURL,
|
|
|
|
&network.NativeCurrencyName, &network.NativeCurrencySymbol, &network.NativeCurrencyDecimals,
|
|
|
|
&network.IsTest, &network.Layer, &network.Enabled,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, &network)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type Manager struct {
|
2021-09-22 17:49:20 +00:00
|
|
|
db *sql.DB
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 17:49:20 +00:00
|
|
|
func NewManager(db *sql.DB) *Manager {
|
2021-09-09 14:28:54 +00:00
|
|
|
return &Manager{
|
2021-09-22 17:49:20 +00:00
|
|
|
db: db,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
func (nm *Manager) Init(networks []params.Network) error {
|
2021-09-22 17:49:20 +00:00
|
|
|
if networks == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
currentNetworks, _ := nm.Get(false)
|
|
|
|
if len(currentNetworks) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range networks {
|
|
|
|
err := nm.Upsert(&networks[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
func (nm *Manager) Upsert(network *params.Network) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
_, err := nm.db.Exec(
|
|
|
|
"INSERT OR REPLACE INTO networks (chain_id, chain_name, rpc_url, block_explorer_url, icon_url, native_currency_name, native_currency_symbol, native_currency_decimals, is_test, layer, enabled) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
network.ChainID, network.ChainName, network.RPCURL, network.BlockExplorerURL, network.IconURL,
|
|
|
|
network.NativeCurrencyName, network.NativeCurrencySymbol, network.NativeCurrencyDecimals,
|
|
|
|
network.IsTest, network.Layer, network.Enabled,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nm *Manager) Delete(chainID uint64) error {
|
|
|
|
_, err := nm.db.Exec("DELETE FROM networks WHERE chain_id = ?", chainID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
func (nm *Manager) Find(chainID uint64) *params.Network {
|
2021-09-09 14:28:54 +00:00
|
|
|
networks, err := newNetworksQuery().filterChainID(chainID).exec(nm.db)
|
|
|
|
if len(networks) != 1 || err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return networks[0]
|
|
|
|
}
|
|
|
|
|
2022-01-12 20:04:43 +00:00
|
|
|
func (nm *Manager) Get(onlyEnabled bool) ([]*params.Network, error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
query := newNetworksQuery()
|
|
|
|
if onlyEnabled {
|
|
|
|
query.filterEnabled(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.exec(nm.db)
|
|
|
|
}
|