mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 04:57:17 +00:00
parent
8971624ec6
commit
f11f960814
@ -37,6 +37,7 @@ func splitAndTrim(input string) []string {
|
||||
type EthAPI interface {
|
||||
BlockNumber(ctx context.Context) (hexutil.Uint64, error)
|
||||
GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error)
|
||||
GetBalance(_ context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error)
|
||||
}
|
||||
|
||||
// APIImpl is implementation of the EthAPI interface based on remote Db access
|
||||
|
44
cmd/rpcdaemon/commands/get_balance.go
Normal file
44
cmd/rpcdaemon/commands/get_balance.go
Normal file
@ -0,0 +1,44 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/ledgerwatch/turbo-geth/common"
|
||||
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
||||
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
||||
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
||||
"github.com/ledgerwatch/turbo-geth/ethdb"
|
||||
"github.com/ledgerwatch/turbo-geth/rpc"
|
||||
)
|
||||
|
||||
func (api *APIImpl) GetBalance(_ context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) {
|
||||
var blockNumber uint64
|
||||
|
||||
hash, ok := blockNrOrHash.Hash()
|
||||
if !ok {
|
||||
blockNumber = uint64(blockNrOrHash.BlockNumber.Int64())
|
||||
} else {
|
||||
block := rawdb.ReadBlockByHash(api.dbReader, hash)
|
||||
if block == nil {
|
||||
return nil, fmt.Errorf("block %x not found", hash)
|
||||
}
|
||||
blockNumber = block.NumberU64()
|
||||
|
||||
if blockNrOrHash.RequireCanonical && rawdb.ReadCanonicalHash(api.dbReader, blockNumber) != hash {
|
||||
return nil, fmt.Errorf("hash %q is not currently canonical", hash.String())
|
||||
}
|
||||
}
|
||||
|
||||
acc, err := GetAccount(api.db, blockNumber, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cant get a balance for account %q for block %v", address.String(), blockNumber)
|
||||
}
|
||||
|
||||
return (*hexutil.Big)(acc.Balance.ToBig()), nil
|
||||
}
|
||||
|
||||
func GetAccount(chainKV ethdb.KV, blockNumber uint64, address common.Address) (*accounts.Account, error) {
|
||||
reader := NewStateReader(chainKV, blockNumber)
|
||||
return reader.ReadAccountData(address)
|
||||
}
|
Loading…
Reference in New Issue
Block a user