1

I'm a complete beginner at programming, but I have been a blockchain enthusiast for quite some time, so a lot of the concepts and ideas are familiar to me already, except these more technical ones. Bare with me if I say nonsense.

Right now i'm doing a tutorial to learn how to make a blockchain game, with Solidity, mostly for fun and learning. There's a part that got me thinking and I still can't grasp it even after checking other threads.

The tutorial:

"In Solidity, functions are public by default. This means anyone (or any other contract) can call your contract's function and execute its code.

uint[] numbers;

function _addToArray(uint _number) private {
  numbers.push(_number);
}

This means only other functions within our contract will be able to call this function and add to the numbers array."

So inside a program, I have various functions right? some are private, some are public. What my question is, in a broader sense, how exactly are these functions called by other smart contracts?? And what kind of stuff could they do?

What I think it is (and I may be totally wrong), is that I could make a Smart Contract X with public functions. Then someone else makes Smart Contract Y, and in that smart contract, they call out some of the functions I made in X. So someone can make a Smart Contract based on many other Smart Contracts essentially. And because they are in the blockchain, forever and immutable, the new Smart Contract is guaranteed to run every time successfully since its base is never going to be altered or corrupted.

I have no idea. Could someone give me examples of how this "calling" could work. Please, analogy or practical examples would make me understand much better!

2 Answers 2

0

There are several contracts you might wish to interact with inside a smart contract, for example: AAvee contracts, uniswap contracts, etc. It will always depend on what are you trying to do.

Lets say you want to implement a contract A that interacts with a ERC20Token contract B, lets say that for example you just want to check if the user has a balance of X amount of tokens to be able to perform an operation, you would have to import the contract and also initialize it with the deployment address.

Code of that:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract A {
   ERC20 B;//Declare the contract B

   constructor(address _token){
      B = _token;//Initialize token
   }
   function example() public {
      if(B.name() == "My Token"){//Then B function name is a public one so you can call it from here
          ...
      }
 //This following line of code would not run since _transfer is an internal function on contract B
 B._transfer();
   }
}
0

In order to execute a function from a contract we can think it as a message is being passed to the contract. The message contains the function invoked and the parameters needed. The contract has a "dispatcher" that will decode the message and call the function requested.

This is the message passed for an ERC20 transfer

0x
a9059cbb
0000000000000000000000009cb92027488321573b6c91c097b5242c132bd979
000000000000000000000000000000000000000000000000002386f26fc10000
  • a9059cbb is the function signature
  • 0000000000000000000000009cb92027488321573b6c91c097b5242c132bd979 is the recipient address (the extra zeros is because it is padded to 32 bytes)
  • 000000000000000000000000000000000000000000000000002386f26fc10000 is the amount using the full decimals, 0x2386f26fc10000 = 10**16 = 0.01 * 10**18.

The "dispatcher" controls which function can be executed. Internal and private function aren't included in the dispatcher table so they cannot be invoked by an external entity, only the contract knows the location in the bytecode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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