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:
parent
00aa103788
commit
aa4d95917c
|
@ -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;
|
|
@ -256,20 +256,6 @@ func (api *API) WatchTransactionByChainID(ctx context.Context, chainID uint64, t
|
|||
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) {
|
||||
return api.s.cryptoOnRampManager.Get()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -37,7 +37,6 @@ type ReaderAccount struct {
|
|||
|
||||
type Wallet struct {
|
||||
Accounts []ReaderAccount `json:"accounts"`
|
||||
Favorites []Favourite `json:"favorites"`
|
||||
OnRamp []CryptoOnRamp `json:"onRamp"`
|
||||
SavedAddresses map[uint64][]SavedAddress `json:"savedAddresses"`
|
||||
Tokens map[uint64][]*Token `json:"tokens"`
|
||||
|
@ -199,11 +198,6 @@ func (r *Reader) GetWallet(ctx context.Context, chainIDs []uint64) (*Wallet, err
|
|||
return nil, err
|
||||
}
|
||||
|
||||
favorites, err := r.s.favouriteManager.GetFavourites()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pendingTransactions := make(map[uint64][]*PendingTransaction)
|
||||
for _, chainID := range chainIDs {
|
||||
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{
|
||||
Accounts: readerAccounts,
|
||||
Favorites: favorites,
|
||||
OnRamp: onRamp,
|
||||
SavedAddresses: savedAddressesMap,
|
||||
Tokens: tokensMap,
|
||||
|
|
|
@ -10,8 +10,9 @@ type SavedAddress struct {
|
|||
Address common.Address `json:"address"`
|
||||
// TODO: Add Emoji and Networks
|
||||
// Emoji string `json:"emoji"`
|
||||
Name string `json:"name"`
|
||||
ChainID uint64 `json:"chainId"`
|
||||
Name string `json:"name"`
|
||||
Favourite bool `json:"favourite"`
|
||||
ChainID uint64 `json:"chainId"`
|
||||
}
|
||||
|
||||
type SavedAddressesManager struct {
|
||||
|
@ -19,7 +20,7 @@ type SavedAddressesManager struct {
|
|||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -28,7 +29,7 @@ func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddr
|
|||
var rst []SavedAddress
|
||||
for rows.Next() {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -40,11 +41,11 @@ func (sam *SavedAddressesManager) GetSavedAddresses(chainID uint64) ([]SavedAddr
|
|||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
_, err = insert.Exec(sa.ChainID, sa.Address, sa.Name)
|
||||
_, err = insert.Exec(sa.ChainID, sa.Address, sa.Name, sa.Favourite)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -32,9 +32,10 @@ func TestSavedAddresses(t *testing.T) {
|
|||
require.Nil(t, rst)
|
||||
|
||||
sa := SavedAddress{
|
||||
Address: common.Address{1},
|
||||
Name: "Zilliqa",
|
||||
ChainID: 777,
|
||||
Address: common.Address{1},
|
||||
Name: "Zilliqa",
|
||||
Favourite: true,
|
||||
ChainID: 777,
|
||||
}
|
||||
|
||||
err = manager.AddSavedAddress(sa)
|
||||
|
|
|
@ -33,14 +33,12 @@ func NewService(
|
|||
tokenManager := &TokenManager{db: db, RPCClient: rpcClient, networkManager: rpcClient.NetworkManager}
|
||||
savedAddressesManager := &SavedAddressesManager{db: db}
|
||||
transactionManager := &TransactionManager{db: db, transactor: transactor, gethManager: gethManager, config: config, accountsDB: accountsDB}
|
||||
favouriteManager := &FavouriteManager{db: db}
|
||||
transferController := transfer.NewTransferController(db, rpcClient, accountFeed)
|
||||
|
||||
return &Service{
|
||||
db: db,
|
||||
accountsDB: accountsDB,
|
||||
rpcClient: rpcClient,
|
||||
favouriteManager: favouriteManager,
|
||||
tokenManager: tokenManager,
|
||||
savedAddressesManager: savedAddressesManager,
|
||||
transactionManager: transactionManager,
|
||||
|
@ -60,7 +58,6 @@ type Service struct {
|
|||
savedAddressesManager *SavedAddressesManager
|
||||
tokenManager *TokenManager
|
||||
transactionManager *TransactionManager
|
||||
favouriteManager *FavouriteManager
|
||||
cryptoOnRampManager *CryptoOnRampManager
|
||||
transferController *transfer.Controller
|
||||
feesManager *FeeManager
|
||||
|
|
Loading…
Reference in New Issue