Yawlle
Yawlle

Reputation: 15

How to call multiple functions at once in a smart contract?

Good morning! I have here three smart contracts that interact with each other. To deploy two of them, they call the address of the first, and then they are linked. I use Remix, which is an IDE that has buttons where we can interact with contracts after deploying. Problem is, they have multiple functions, and to go through all the operation I need to perform, I need to keep calling function by function, and that's kind of tiring and error-prone. Please, do you know any alternative for me to call a series of functions in a certain order without having to call them one by one? Like a main contract that could call all the functions I need from the three other contracts. Thank you!

Example: function 1 of contract 1 / function 2 of contract 2 / function 1 of contract 3 / function 2 of contract 2 /

I don't want to do this manually anymore by clicking the Remix buttons. I wanted him to do this step by step alone.

Upvotes: 0

Views: 1310

Answers (2)

Neo Z
Neo Z

Reputation: 1

You can use Brownie in python, take your solidity code and compile it using a script, then you can interact with them using .transact() and .call() (Though you need not explicitly state them) after creating an instance of the object.

Here's an example, a contract from the documentation itself:

       pragma solidity >0.5.0;
           contract Greeter {
                string public greeting;
                constructor() public {
                    greeting = 'Hello';
                }
                function setGreeting(string memory _greeting) public {
                    greeting = _greeting;
                }
                function greet() view public returns (string memory) {
                    return greeting;
                }
         }

So we will save it in a file named Greeter.sol

Now the script for brownie in ganache virtual env:

    from brownie import accounts, Greeter
    
    def interactions():
        account = accounts[0]
        contractObj = Greeter.deploy({"from": account})
        transaction = contractObj.setGreeting(Hola, {"from": account})
        transaction.wait(1)
        updatedGreeter = contractObj.greet()
        print(updatedGreeter)
    
    def main()
        interactions()

So, these tools are pretty great and powerful once you clear the basics with remix.

Upvotes: 0

Márcio
Márcio

Reputation: 181

You can interact with contracts directly on CLI using some tools like Hardhat or Truffle. A lot of developers are preferring to use Hardhat, it seems very practical, stable, and can do what you need.

You can even create script tests on Hardhat with various functions being called in any order you want.

Upvotes: 1

Related Questions