TransWikia.com

Smart contract multiple functions call

Ethereum Asked by Anarz on November 4, 2021

My question is mainly to know if what I want to achieve is possible or not, unfortunately I cannot find (understand?) my answer elsewhere.

I have no experience with Solidity, and very little with Ethereum, I just managed to call a function from a smart contract in a transaction with an ETH client (go-ethereum). I have not deployed the contract, it is a public DeFi smart contract.

So far, I’ve successfully called a function from the smart contract with its parameters and get its response.
The goal is to call several functions, simultaneously but in a defined order, each with its own parameters, from the same smart contract, and ideally in the same transaction.

  • Is it possible to call multiple functions (with parameters) and get all responses within one single Ethereum transaction ?

  • If not, is it possible to do it by another way, like deploy my own smart contract to do the job ?

  • Is there a way to guarantee the order in which these functions will be executed by the smart contract (I mean the final target smart contract, not mine if I ever need to deploy one) ?

  • To reach this goal, is there a directly related question or potential solution that I would not have thought of ?

2 Answers

One transaction is always one call from an EOA (Externally Owned Account) to either another EOA (Ether transfer) or to one single smart contract.

So what you can do is create your own contract which does what you need. So your contract creates a reference to other contracts and calls them in specific order. Here are some references on how to do that: https://medium.com/@blockchain101/calling-the-function-of-another-contract-in-solidity-f9edfa921f4c and https://www.reddit.com/r/ethereum/comments/58x16k/how_do_i_call_a_deployed_contracts_method_in/ (note that the syntax of the code changes quite fast so the syntax is the examples might not be fully correct anymore)

The order of execution is strictly defined in a single transaction - there is no parallelism. So first the first line of code is executed, then the second and so on. If the first line fails the second line (mostly) doesn't even get executed.

Answered by Lauri Peltonen on November 4, 2021

To batch different call in one transaction you will need to use smart contract.

For your smart contract, you have two possibilities.

On chain batch definition

Define the set of actions in your contract.

import "./Target1.sol";
import "./Target2.sol";

contract Test {
    Target1 target1;
    Target2 target2;
    
    constructor(address addr1) {
        target1 = Target1(addr1);
        target2 = Target2(addr2);
    }
    function myFunc() public {
        target1.func1();
        target2.func1();
        target1.func2();
    }
}

Off chain batch construction

Define a batch function in your contract and build the calls offchain.

contract Test {
    struct Call {
        address to;
        uint256 value;
        bytes data;
    }

    function batch(Call[] memory calls)
        public
    {
        for (uint i = 0; i < calls.length; i++) {
            assembly {
                success := call(
                    gasleft(),
                    calls[i].to,
                    calls[i].value,
                    add(data, 0x20),
                    mload(calls[i].data),
                    0,
                    0
                )
            }
        }
    }
}

Answered by Tclairet on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP