2019-06-14 10:16:30 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2019-07-02 07:28:57 +00:00
|
|
|
var (
|
|
|
|
// ErrServiceNotInitialized returned when wallet is not initialized/started,.
|
|
|
|
ErrServiceNotInitialized = errors.New("wallet service is not initialized")
|
|
|
|
)
|
|
|
|
|
2019-06-14 10:16:30 +00:00
|
|
|
func NewAPI(s *Service) *API {
|
|
|
|
return &API{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
// API is class with methods available over RPC.
|
|
|
|
type API struct {
|
|
|
|
s *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransfers returns transfers in range of blocks. If `end` is nil all transfers from `start` will be returned.
|
|
|
|
// TODO(dshulyak) benchmark loading many transfers from database. We can avoid json unmarshal/marshal if we will
|
|
|
|
// read header, tx and receipt as a raw json.
|
2019-07-15 11:16:07 +00:00
|
|
|
func (api *API) GetTransfers(ctx context.Context, start, end *hexutil.Big) ([]TransferView, error) {
|
2019-06-14 10:16:30 +00:00
|
|
|
log.Debug("call to get transfers", "start", start, "end", end)
|
|
|
|
if start == nil {
|
|
|
|
return nil, errors.New("start of the query must be provided. use 0 if you want to load all transfers")
|
|
|
|
}
|
|
|
|
if api.s.db == nil {
|
2019-07-02 07:28:57 +00:00
|
|
|
return nil, ErrServiceNotInitialized
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
rst, err := api.s.db.GetTransfers((*big.Int)(start), (*big.Int)(end))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Debug("result from database for transfers", "start", start, "end", end, "len", len(rst))
|
2019-07-15 11:16:07 +00:00
|
|
|
return castToTransferViews(rst), nil
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransfersByAddress returns transfers for a single address between two blocks.
|
2019-07-15 11:16:07 +00:00
|
|
|
func (api *API) GetTransfersByAddress(ctx context.Context, address common.Address, start, end *hexutil.Big) ([]TransferView, error) {
|
2019-06-14 10:16:30 +00:00
|
|
|
log.Debug("call to get transfers for an address", "address", address, "start", start, "end", end)
|
|
|
|
if start == nil {
|
|
|
|
return nil, errors.New("start of the query must be provided. use 0 if you want to load all transfers")
|
|
|
|
}
|
|
|
|
if api.s.db == nil {
|
2019-07-02 07:28:57 +00:00
|
|
|
return nil, ErrServiceNotInitialized
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
rst, err := api.s.db.GetTransfersByAddress(address, (*big.Int)(start), (*big.Int)(end))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Debug("result from database for address", "address", address, "start", start, "end", end, "len", len(rst))
|
2019-07-15 11:16:07 +00:00
|
|
|
return castToTransferViews(rst), nil
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
2019-07-02 07:28:57 +00:00
|
|
|
|
|
|
|
// GetTokensBalances return mapping of token balances for every account.
|
|
|
|
func (api *API) GetTokensBalances(ctx context.Context, accounts, tokens []common.Address) (map[common.Address]map[common.Address]*big.Int, error) {
|
|
|
|
if api.s.client == nil {
|
|
|
|
return nil, ErrServiceNotInitialized
|
|
|
|
}
|
|
|
|
return GetTokensBalances(ctx, api.s.client, accounts, tokens)
|
|
|
|
}
|