How can I combine my 2 smart contract functions in one transaction?

I am building two smart contracts, one is casino contract and the other one is lottery contract(planning to deploy it separately). I want to combine both the placebet(casino) function and buyticket(lottery) function in one call. Once user call placebet on the casino contract, he will automatically buys lotteryticket also in lotterycontract. Any help will be appreciated. Thank you.

Note: (Pls. respect) I am just a newbie, no formal coding education, I am just studying how to make dapp.

There are many ways, for example, you can write a function contains these two actions.

function combine() public {
    ContractA.functionA();
    ContractB.functionB();
}
1 Like

Thanks for the answer.
Does it mean I need to deploy a 3rd contract where I can call the combine function of contract 1 and contract 2?
My plan is to deploy the lotterycontract first, and then next is the casino contract where I will be adding the lotterycontract to it and call the combine function on the main casino contract.

You can also add the combine function to the casino contract and instead of the ContractA.functionA() you just call your functionA() from the casino contract.

2 Likes

I think this is ok, you can have a try.

I have tried to do this option. had created combined function to call both casino and lottery contracts, firstly, had deployed the lotterycontract, and then took the sc address and added on the casinocontract constructor upon deploying the 2nd contract.
Was able to combine it in one call but the problem is, the transaction to lottery contract is an internal transaction coming from the casinocontract.
What I want is the msg.sender will send the transaction directly to the lotterycontract, not the casino contract forwarding the payment.
Is there any ways to do both the msg.sender will interact directly to both casinocontract and lotterycontract simultaneously in one call function?

this is what i did on the combined call.

constructor(address _lotteryAddr) {
 lotteryTicketPrice = 0.0001 ether;
lotteryAddr = payable(_lotteryAddr);

function combined() public payable{
placeBet();   //-->>this one is the casinocontract main function.
buyTicket(); //-->> the lotterycontract function i created inside casinocontract to call lotterycontract.
}
function buyTicket() public payable {
    //require is used to assure gas is maximized by the users.
    lotteryTicketPrice = 0.0001 ether; 
    (bool sent,) = lotteryAddr.call{value: lotteryTicketPrice}("");
    require(sent, "Failed to send Ether");
}

thats not possible the way you suggest from the casino contract itself. the msg.sender will then be the casino contract when you call the lotery.

the tx.origin wil contain the original msg.sender, however its not recommended to use this.

So if you want the casino & lottery function both receive the msg.sender then your client interface would need to make 2 calls. Its not possible to do that from the casino contract directly.

Thank you for your answer. I have to change the plan.

@everyone, An update for this project. kindly head over on this page if you want to contribute or be hired. spot still open. I need someone to verify and publish my smart contract on bscscan and fix any sc errors.(paid)