Hardhat – What are the strange calls to my smart contract?

Viewed 782

I am using Hardhat hackathon boilerplate and calling my smart contract's (address 1c0, addresses shortened for clarity) function MyContract#addProduct from the UI via ethers.js.

The problem: I see bunch of weird function calls before and after I call my addProduct function. An account (266) doing these weird calls is a first address generated by HH upon starting. MyContract is deployed at 1c0.

Below is the log from Hardhat node terminal with my comments after //

What are these calls and where do they originate?

UPDATE: A suggestion from my friend that it can be MetaMask querying a smart contract for whatever purposes. Still gonna investigate it.

// I don't know who's calling it
eth_call 
  Contract call:       MyContract#<unrecognized-selector>
  From:                266
  To:                  1c0

// My fallback is logging
  console.log:
    MyContract: Fallback called 

// I don't know who's calling it
eth_call
  Contract call:       MyContract#symbol
  From:                266
  To:                  1c0

// I don't know who's calling it
eth_blockNumber
eth_getBalance (3)
eth_call
  WARNING: Calling an account which is not a contract
  From:                266
  To:                  e86

// This is what I call from UI
eth_getTransactionCount
eth_blockNumber
eth_sendRawTransaction 
  Contract call:       MyContract#addProduct
  Transaction:         740
  From:                5ab
  To:                  1c0
  Value:               0 ETH
  Gas used:            414218 of 414218
  Block #23:           925

  console.log:
    MyContract:addProduct()

eth_getTransactionReceipt
eth_blockNumber
eth_getTransactionReceipt
eth_blockNumber
eth_getBlockByHash

// I don't know who's calling it
eth_call
  Contract call:       MyContract#<unrecognized-selector>
  From:                266
  To:                  1c0

  console.log:
    MyContract: Fallback called

// I don't know who's calling it
eth_call 
  Contract call:       MyContract#<unrecognized-selector>
  From:               266
  To:                 1c0

  console.log:
    MyContract: Fallback called
3 Answers

Upon further investigation I believe metamask is automatically calling the EIP165 "supportsInterface" function.

I added some console.log to my contract and found they matched checks that my contracts supported the ERC721 interface, ERC721 Metadata interface and ERC1155 interface.

It's very annoying and a bit unnerving to these these unhandled calls in my local hardhat development, and I don't fully understand why metamask needs to make them at all.

The "Hardhat hackathon boilerplate" frontend app defines a periodic check - querying a token balance each second.

Assuming your contract doesn't implement the balanceOf(address) function, the call redirects to the fallback() function.

Adding

    receive() external payable {} // to support receiving ETH by default
    fallback() external payable {}

to the contract will make those logs go away.

Hardhat in your case, or in general anyone can call the contract for functions you have not defined and fallback will trap them.

fallback function is executed if the caller meant to call a function that is not available. You can also use its input params to see what is being called and then take certain action. Documentation here:

https://docs.soliditylang.org/en/v0.8.12/contracts.html#fallback-function

Related