erigon-pulse/cmd/rpcdaemon/commands/otterscan_has_code.go

37 lines
1000 B
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"github.com/ledgerwatch/erigon/common"
2022-12-21 04:27:14 +00:00
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
)
func (api *OtterscanAPIImpl) HasCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (bool, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return false, fmt.Errorf("hasCode cannot open tx: %w", err)
}
defer tx.Rollback()
blockNumber, _, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
if err != nil {
return false, err
}
2022-12-21 04:27:14 +00:00
chainConfig, err := api.chainConfig(tx)
if err != nil {
return false, err
}
2022-12-21 04:27:14 +00:00
reader := state.NewPlainState(tx, blockNumber, systemcontracts.SystemContractCodeLookup[chainConfig.ChainName])
acc, err := reader.ReadAccountData(address)
if acc == nil || err != nil {
return false, err
}
return !acc.IsEmptyCodeHash(), nil
}