0

Let's say I have smart contract A and smart contract B. I want to transfer the ownership of smart contract B to another account ONLY IF a transaction against smart contract A has been successful.

I understand I have to build both Tx-A and Tx-B and send them to the chain as part of the same transaction.

How can I do this?

1 Answer 1

2

You need to write a third contract, smart contract C, that tries to execute Tx-A and then, if that is successful, run Tx-B.

import {contractA} from "<path to contract A>";
import {contractB} from "<path to contract B>";
contract C {
    function execute() external {
        contractA A = contractA(<address of A>);
    
        bool success = A.functionYouWantToCall();

        require(success, "functionYouWantToCall not successful");

        contractB B = contractB(<address of B>);

        B.transferOwnership(<new address>);
    }

}

Now you can run C.execute() to achieve your goal.

Instead of writing your own contract, there may already be a contract on chain that lets you assemble function calls without having to write your own contract. For instance, many DEXs have a router that allows for "multicalls". See the test example which shows two functions being called in one transaction: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/a28aafdc85a592776544f7978c6b1a462d28ede2/test/utils/Multicall.test.js#L45-L51

A list of multicall contract addresses by chain is at https://www.multicall3.com/deployments

Your Answer

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

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