status-go/services/wallet/saved_addresses.go

56 lines
1.4 KiB
Go
Raw Normal View History

2021-09-10 18:08:22 +00:00
package wallet
import (
"database/sql"
"github.com/ethereum/go-ethereum/common"
)
type SavedAddress struct {
Address common.Address `json:"address"`
// TODO: Add Emoji and Networks
// Emoji string `json:"emoji"`
Name string `json:"name"`
Favourite bool `json:"favourite"`
ChainID uint64 `json:"chainId"`
2021-09-10 18:08:22 +00:00
}
type SavedAddressesManager struct {
db *sql.DB
}
2022-05-10 07:48:05 +00:00
func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddress, error) {
rows, err := sam.db.Query("SELECT address, name, favourite, network_id FROM saved_addresses WHERE network_id = ?", chainID)
2021-09-10 18:08:22 +00:00
if err != nil {
return nil, err
}
defer rows.Close()
2022-05-10 07:48:05 +00:00
var rst []SavedAddress
2021-09-10 18:08:22 +00:00
for rows.Next() {
2022-05-10 07:48:05 +00:00
sa := SavedAddress{}
err := rows.Scan(&sa.Address, &sa.Name, &sa.Favourite, &sa.ChainID)
2021-09-10 18:08:22 +00:00
if err != nil {
return nil, err
}
rst = append(rst, sa)
}
return rst, nil
}
func (sam *SavedAddressesManager) AddSavedAddress(sa SavedAddress) error {
insert, err := sam.db.Prepare("INSERT OR REPLACE INTO saved_addresses (network_id, address, name, favourite) VALUES (?, ?, ?, ?)")
2021-09-10 18:08:22 +00:00
if err != nil {
return err
}
_, err = insert.Exec(sa.ChainID, sa.Address, sa.Name, sa.Favourite)
2021-09-10 18:08:22 +00:00
return err
}
func (sam *SavedAddressesManager) DeleteSavedAddress(chainID uint64, address common.Address) error {
_, err := sam.db.Exec(`DELETE FROM saved_addresses WHERE address = ? AND network_id = ?`, address, chainID)
return err
}