2

I want to know if it is possible to query (discover) smart contracts inside my smart contract. For example, I want to find a smart contract with some name and/or parameters and then call it. Is that possible?

1

It's possible for a contract to call another arbitrary contract, knowing its address and the function signature of the function you want to call.

It's also possible to make a contract that acts as a database of other contracts, and returns a contract address and function signature when sent some kind of search parameter.

Those two things together would satisfy the stated requirements. However, I don't know of anyone making the latter thing, and I can't imagine how it would be useful.

A common pattern for unspecified contracts that take known parameters, which may or may not solve whatever problem you're trying to solve, is:

  • Define an interface (a function signature), and make your contract able to call that for some particular task
  • Allow the user to specify the address and pass it in to your contract as a parameter, and have your contract call the other contract at the specified address using the expected interface.

For instance, a contract might allow you to sell songs for any ERC20 token. (An ERC token is a contract, with an interface that matches the ERC20 token standard.) The seller would look up the address of the contract for the token they wanted to sell their song for using the website of their choice, and pass it into the seller contract, along with the song ID and the price in that token. Later, when a buyer showed up, the seller contract would call its expected interface, in this case transferFrom() method, to complete the payment for the sale.

This pattern works because the user is able to browse the internet and use common sense to decide what would be a good contract to use (in this case as a token), so they don't need a smart contract to be able to do it for them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.