2022-11-03 04:32:15 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
|
2022-11-03 04:32:15 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
|
|
|
)
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func (api *OtterscanAPIImpl) HasCode(ctx context.Context, address libcommon.Address, blockNrOrHash rpc.BlockNumberOrHash) (bool, error) {
|
2022-11-03 04:32:15 +00:00
|
|
|
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-11-03 04:32:15 +00:00
|
|
|
|
2023-01-24 05:43:04 +00:00
|
|
|
reader, err := rpchelper.CreateHistoryStateReader(tx, blockNumber, 0, api.historyV3(tx), chainConfig.ChainName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-11-03 04:32:15 +00:00
|
|
|
acc, err := reader.ReadAccountData(address)
|
|
|
|
if acc == nil || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return !acc.IsEmptyCodeHash(), nil
|
|
|
|
}
|