4

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
TechGeorgii
  • 414
  • 4
  • 14
  • I have started noticing these calls as well when testing locally deployed contract on hardhat through metamask. It's very strange and I'll be staying keyed in here in case somebody knows. – T_R_U_T_H May 14 '22 at 00:57

3 Answers3

4

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.

T_R_U_T_H
  • 475
  • 3
  • 12
  • I'm having issues with this too. If you implement an ERC721 metamask calls SupportsInterface, Symbol and Name. However, if you're not implementing ERC721, metamask calls supportsInterface, symbol and one other that I can't figure out right now. Any idea? – Alex Lacayo May 15 '22 at 09:21
  • Actually it's attempting to call "decimals" (from ERC20) too. – Alex Lacayo May 15 '22 at 09:40
1

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

user566245
  • 4,011
  • 1
  • 30
  • 36
0

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.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Thank you for the answer, Petr. However, this is not the case for a number of reasons: (1) A periodic check functionality is removed long ago. (2) These weird call do not appear periodically. They accompany my call to actual smart contract function, they appear before and after call to MyContract#addProduct. (3) For testing, I've implemented all ERC-20 functions in MyContract (including balanceOf) and put logging up there. I was able to verify no one of these functions are called. – TechGeorgii May 04 '22 at 08:11