feat: merge favourites with saved address

Remove Favourites APIs and update the saved address APIs

Added up migration scripts that move the favourites from the old table
to the saved_addresses table with true flag and then drop the favourites table.

Required by #6546
This commit is contained in:
Stefan 2022-08-18 16:51:13 +02:00 committed by Stefan Dunca
parent 00aa103788
commit aa4d95917c
8 changed files with 16 additions and 79 deletions

View File

@ -0,0 +1,5 @@
ALTER TABLE saved_addresses ADD COLUMN favourite BOOLEAN NOT NULL DEFAULT FALSE;
INSERT OR REPLACE INTO saved_addresses(address, name, favourite, network_id) SELECT address, name, "TRUE", "1" FROM favourites;
DROP TABLE favourites;

View File

@ -256,20 +256,6 @@ func (api *API) WatchTransactionByChainID(ctx context.Context, chainID uint64, t
return api.s.transactionManager.watch(ctx, transactionHash, chainClient) return api.s.transactionManager.watch(ctx, transactionHash, chainClient)
} }
func (api *API) GetFavourites(ctx context.Context) ([]Favourite, error) {
log.Debug("call to get favourites")
rst, err := api.s.favouriteManager.GetFavourites()
log.Debug("result from database for favourites", "len", len(rst))
return rst, err
}
func (api *API) AddFavourite(ctx context.Context, favourite Favourite) error {
log.Debug("call to create or update favourites")
err := api.s.favouriteManager.AddFavourite(favourite)
log.Debug("result from database for create or update favourites", "err", err)
return err
}
func (api *API) GetCryptoOnRamps(ctx context.Context) ([]CryptoOnRamp, error) { func (api *API) GetCryptoOnRamps(ctx context.Context) ([]CryptoOnRamp, error) {
return api.s.cryptoOnRampManager.Get() return api.s.cryptoOnRampManager.Get()
} }

View File

@ -1,46 +0,0 @@
package wallet
import (
"database/sql"
"github.com/ethereum/go-ethereum/common"
)
type Favourite struct {
Address common.Address `json:"address"`
Name string `json:"name"`
}
type FavouriteManager struct {
db *sql.DB
}
func (fm *FavouriteManager) GetFavourites() ([]Favourite, error) {
rows, err := fm.db.Query(`SELECT address, name FROM favourites`)
if err != nil {
return nil, err
}
defer rows.Close()
var rst []Favourite
for rows.Next() {
favourite := Favourite{}
err := rows.Scan(&favourite.Address, &favourite.Name)
if err != nil {
return nil, err
}
rst = append(rst, favourite)
}
return rst, nil
}
func (fm *FavouriteManager) AddFavourite(favourite Favourite) error {
insert, err := fm.db.Prepare("INSERT OR REPLACE INTO favourites (address, name) VALUES (?, ?)")
if err != nil {
return err
}
_, err = insert.Exec(favourite.Address, favourite.Name)
return err
}

View File

@ -37,7 +37,6 @@ type ReaderAccount struct {
type Wallet struct { type Wallet struct {
Accounts []ReaderAccount `json:"accounts"` Accounts []ReaderAccount `json:"accounts"`
Favorites []Favourite `json:"favorites"`
OnRamp []CryptoOnRamp `json:"onRamp"` OnRamp []CryptoOnRamp `json:"onRamp"`
SavedAddresses map[uint64][]SavedAddress `json:"savedAddresses"` SavedAddresses map[uint64][]SavedAddress `json:"savedAddresses"`
Tokens map[uint64][]*Token `json:"tokens"` Tokens map[uint64][]*Token `json:"tokens"`
@ -199,11 +198,6 @@ func (r *Reader) GetWallet(ctx context.Context, chainIDs []uint64) (*Wallet, err
return nil, err return nil, err
} }
favorites, err := r.s.favouriteManager.GetFavourites()
if err != nil {
return nil, err
}
pendingTransactions := make(map[uint64][]*PendingTransaction) pendingTransactions := make(map[uint64][]*PendingTransaction)
for _, chainID := range chainIDs { for _, chainID := range chainIDs {
pendingTx, err := r.s.transactionManager.getAllPendings([]uint64{chainID}) pendingTx, err := r.s.transactionManager.getAllPendings([]uint64{chainID})
@ -214,7 +208,6 @@ func (r *Reader) GetWallet(ctx context.Context, chainIDs []uint64) (*Wallet, err
} }
return &Wallet{ return &Wallet{
Accounts: readerAccounts, Accounts: readerAccounts,
Favorites: favorites,
OnRamp: onRamp, OnRamp: onRamp,
SavedAddresses: savedAddressesMap, SavedAddresses: savedAddressesMap,
Tokens: tokensMap, Tokens: tokensMap,

View File

@ -10,8 +10,9 @@ type SavedAddress struct {
Address common.Address `json:"address"` Address common.Address `json:"address"`
// TODO: Add Emoji and Networks // TODO: Add Emoji and Networks
// Emoji string `json:"emoji"` // Emoji string `json:"emoji"`
Name string `json:"name"` Name string `json:"name"`
ChainID uint64 `json:"chainId"` Favourite bool `json:"favourite"`
ChainID uint64 `json:"chainId"`
} }
type SavedAddressesManager struct { type SavedAddressesManager struct {
@ -19,7 +20,7 @@ type SavedAddressesManager struct {
} }
func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddress, error) { func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddress, error) {
rows, err := sam.db.Query("SELECT address, name, network_id FROM saved_addresses WHERE network_id = ?", chainID) rows, err := sam.db.Query("SELECT address, name, favourite, network_id FROM saved_addresses WHERE network_id = ?", chainID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -28,7 +29,7 @@ func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddr
var rst []SavedAddress var rst []SavedAddress
for rows.Next() { for rows.Next() {
sa := SavedAddress{} sa := SavedAddress{}
err := rows.Scan(&sa.Address, &sa.Name, &sa.ChainID) err := rows.Scan(&sa.Address, &sa.Name, &sa.Favourite, &sa.ChainID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -40,11 +41,11 @@ func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddr
} }
func (sam *SavedAddressesManager) AddSavedAddress(sa SavedAddress) error { func (sam *SavedAddressesManager) AddSavedAddress(sa SavedAddress) error {
insert, err := sam.db.Prepare("INSERT OR REPLACE INTO saved_addresses (network_id, address, name) VALUES (?, ?, ?)") insert, err := sam.db.Prepare("INSERT OR REPLACE INTO saved_addresses (network_id, address, name, favourite) VALUES (?, ?, ?, ?)")
if err != nil { if err != nil {
return err return err
} }
_, err = insert.Exec(sa.ChainID, sa.Address, sa.Name) _, err = insert.Exec(sa.ChainID, sa.Address, sa.Name, sa.Favourite)
return err return err
} }

View File

@ -32,9 +32,10 @@ func TestSavedAddresses(t *testing.T) {
require.Nil(t, rst) require.Nil(t, rst)
sa := SavedAddress{ sa := SavedAddress{
Address: common.Address{1}, Address: common.Address{1},
Name: "Zilliqa", Name: "Zilliqa",
ChainID: 777, Favourite: true,
ChainID: 777,
} }
err = manager.AddSavedAddress(sa) err = manager.AddSavedAddress(sa)

View File

@ -33,14 +33,12 @@ func NewService(
tokenManager := &TokenManager{db: db, RPCClient: rpcClient, networkManager: rpcClient.NetworkManager} tokenManager := &TokenManager{db: db, RPCClient: rpcClient, networkManager: rpcClient.NetworkManager}
savedAddressesManager := &SavedAddressesManager{db: db} savedAddressesManager := &SavedAddressesManager{db: db}
transactionManager := &TransactionManager{db: db, transactor: transactor, gethManager: gethManager, config: config, accountsDB: accountsDB} transactionManager := &TransactionManager{db: db, transactor: transactor, gethManager: gethManager, config: config, accountsDB: accountsDB}
favouriteManager := &FavouriteManager{db: db}
transferController := transfer.NewTransferController(db, rpcClient, accountFeed) transferController := transfer.NewTransferController(db, rpcClient, accountFeed)
return &Service{ return &Service{
db: db, db: db,
accountsDB: accountsDB, accountsDB: accountsDB,
rpcClient: rpcClient, rpcClient: rpcClient,
favouriteManager: favouriteManager,
tokenManager: tokenManager, tokenManager: tokenManager,
savedAddressesManager: savedAddressesManager, savedAddressesManager: savedAddressesManager,
transactionManager: transactionManager, transactionManager: transactionManager,
@ -60,7 +58,6 @@ type Service struct {
savedAddressesManager *SavedAddressesManager savedAddressesManager *SavedAddressesManager
tokenManager *TokenManager tokenManager *TokenManager
transactionManager *TransactionManager transactionManager *TransactionManager
favouriteManager *FavouriteManager
cryptoOnRampManager *CryptoOnRampManager cryptoOnRampManager *CryptoOnRampManager
transferController *transfer.Controller transferController *transfer.Controller
feesManager *FeeManager feesManager *FeeManager