diff --git a/internal/quaiapi/quai_api.go b/internal/quaiapi/quai_api.go index 305d5bb38c..decf0318c8 100644 --- a/internal/quaiapi/quai_api.go +++ b/internal/quaiapi/quai_api.go @@ -491,6 +491,24 @@ func (s *PublicBlockChainQuaiAPI) EstimateGas(ctx context.Context, args Transact } } +// GetContractSize gives the size of the contract at the block hash or number +func (s *PublicBlockChainQuaiAPI) GetContractSize(ctx context.Context, address common.AddressBytes, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) { + addr := common.Bytes20ToAddress(address, s.b.NodeLocation()) + if addr.IsInQuaiLedgerScope() { + state, _, err := s.b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash) + if state == nil || err != nil { + return nil, err + } + internal, err := addr.InternalAndQuaiAddress() + if err != nil { + return nil, err + } + return (*hexutil.Big)(state.GetSize(internal)), state.Error() + } else { + return nil, errors.New("getContractSize cannot be called on a Qi Address") + } +} + // BaseFee returns the base fee for a tx to be included in the next block. // If txType is set to "true" returns the Quai base fee in units of Wei. // If txType is set to "false" returns the Qi base fee in units of Qit. diff --git a/quaiclient/ethclient/ethclient.go b/quaiclient/ethclient/ethclient.go index 13f3d2005c..8562cf5c58 100644 --- a/quaiclient/ethclient/ethclient.go +++ b/quaiclient/ethclient/ethclient.go @@ -338,6 +338,12 @@ func (ec *Client) BalanceAt(ctx context.Context, account common.MixedcaseAddress return (*big.Int)(&result), err } +func (ec *Client) ContractSizeAt(ctx context.Context, account common.MixedcaseAddress, blockNumber *big.Int) (*big.Int, error) { + var result hexutil.Big + err := ec.c.CallContext(ctx, &result, "quai_getContractSize", account.Original(), toBlockNumArg(blockNumber)) + return (*big.Int)(&result), err +} + func (ec *Client) GetOutpointsByAddress(ctx context.Context, address common.MixedcaseAddress) (map[string]*types.OutpointAndDenomination, error) { var outpoints map[string]*types.OutpointAndDenomination err := ec.c.CallContext(ctx, &outpoints, "quai_getOutpointsByAddress", address.Original())