[wallet] Set initial blocks range for a new empty account
This commit is contained in:
parent
0a686bba4c
commit
d21cd6aba1
|
@ -24,6 +24,11 @@ type API struct {
|
|||
s *Service
|
||||
}
|
||||
|
||||
// SetInitialBlocksRange sets initial blocks range
|
||||
func (api *API) SetInitialBlocksRange(ctx context.Context) error {
|
||||
return api.s.SetInitialBlocksRange(api.s.db.network)
|
||||
}
|
||||
|
||||
// GetTransfersByAddress returns transfers for a single address
|
||||
func (api *API) GetTransfersByAddress(ctx context.Context, address common.Address, toBlock, limit *hexutil.Big) ([]TransferView, error) {
|
||||
log.Debug("[WalletAPI:: GetTransfersByAddress] get transfers for an address", "address", address, "block", toBlock, "limit", limit)
|
||||
|
|
|
@ -789,6 +789,16 @@ func insertBlocksWithTransactions(creator statementCreator, account common.Addre
|
|||
return nil
|
||||
}
|
||||
|
||||
func (db *Database) UpsertRange(account common.Address, network uint64, from *big.Int, to *big.Int) error {
|
||||
log.Debug("upsert blocks range", "account", account, "network id", network, "from", from, "to", to)
|
||||
insert, err := db.db.Prepare("INSERT INTO blocks_ranges (network_id, address, blk_from, blk_to) VALUES (?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = insert.Exec(network, account, (*SQLBigInt)(from), (*SQLBigInt)(to))
|
||||
return err
|
||||
}
|
||||
|
||||
func upsertRange(creator statementCreator, account common.Address, network uint64, from *big.Int, to *big.Int) (err error) {
|
||||
update, err := creator.Prepare(`UPDATE blocks_ranges
|
||||
SET blk_to = ?
|
||||
|
|
|
@ -3,6 +3,7 @@ package wallet
|
|||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
|
@ -183,3 +184,26 @@ func mapToList(m map[common.Address]struct{}) []common.Address {
|
|||
func (s *Service) IsStarted() bool {
|
||||
return s.started
|
||||
}
|
||||
|
||||
func (s *Service) SetInitialBlocksRange(network uint64) error {
|
||||
accountsDB := accounts.NewDB(s.db.db)
|
||||
watchAddress, err := accountsDB.GetWalletAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
from := big.NewInt(0)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
header, err := s.client.HeaderByNumber(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.db.UpsertRange(common.Address(watchAddress), network, from, header.Number)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue