From aa4d95917c7dcf9b4bc2c21d945634b927f58967 Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 18 Aug 2022 16:51:13 +0200 Subject: [PATCH] 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 --- ..._move_favourites_to_saved_addresses.up.sql | 5 ++ ...l => 1662972194_add_keypairs_table.up.sql} | 0 services/wallet/api.go | 14 ------ services/wallet/favourite.go | 46 ------------------- services/wallet/reader.go | 7 --- services/wallet/saved_addresses.go | 13 +++--- services/wallet/saved_addresses_test.go | 7 +-- services/wallet/service.go | 3 -- 8 files changed, 16 insertions(+), 79 deletions(-) create mode 100644 appdatabase/migrations/sql/1662460056_move_favourites_to_saved_addresses.up.sql rename appdatabase/migrations/sql/{1662447680_add_keypairs_table.up.sql => 1662972194_add_keypairs_table.up.sql} (100%) delete mode 100644 services/wallet/favourite.go diff --git a/appdatabase/migrations/sql/1662460056_move_favourites_to_saved_addresses.up.sql b/appdatabase/migrations/sql/1662460056_move_favourites_to_saved_addresses.up.sql new file mode 100644 index 000000000..c491fb507 --- /dev/null +++ b/appdatabase/migrations/sql/1662460056_move_favourites_to_saved_addresses.up.sql @@ -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; \ No newline at end of file diff --git a/appdatabase/migrations/sql/1662447680_add_keypairs_table.up.sql b/appdatabase/migrations/sql/1662972194_add_keypairs_table.up.sql similarity index 100% rename from appdatabase/migrations/sql/1662447680_add_keypairs_table.up.sql rename to appdatabase/migrations/sql/1662972194_add_keypairs_table.up.sql diff --git a/services/wallet/api.go b/services/wallet/api.go index 1af6a3dc6..224db7154 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -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() } diff --git a/services/wallet/favourite.go b/services/wallet/favourite.go deleted file mode 100644 index b7319437c..000000000 --- a/services/wallet/favourite.go +++ /dev/null @@ -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 -} diff --git a/services/wallet/reader.go b/services/wallet/reader.go index 62589908b..98897ef3a 100644 --- a/services/wallet/reader.go +++ b/services/wallet/reader.go @@ -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, diff --git a/services/wallet/saved_addresses.go b/services/wallet/saved_addresses.go index e17c733bc..4e1323a85 100644 --- a/services/wallet/saved_addresses.go +++ b/services/wallet/saved_addresses.go @@ -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 } diff --git a/services/wallet/saved_addresses_test.go b/services/wallet/saved_addresses_test.go index 3010fca28..0d0569a09 100644 --- a/services/wallet/saved_addresses_test.go +++ b/services/wallet/saved_addresses_test.go @@ -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) diff --git a/services/wallet/service.go b/services/wallet/service.go index 50c52c88e..fd127c480 100644 --- a/services/wallet/service.go +++ b/services/wallet/service.go @@ -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