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

478 Views Asked by At

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 would call placebet on casino contract, he will automatically buys lotteryticket also. 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.

1

There are 1 best solutions below

3
On BEST ANSWER

If I understand correctly, you need to make the first method call another method from another contract.

Here is an example:

contract LotteryContract {
  function buyTicket() public {
    // code to buy ticket
  }
}

contract CasinoContract {
  function placeBet() public {
    // code to place bet
    LotteryContract.buyTicket()
  }
}

The basic idea in this example is that we call the LotteryContract's method in CasinoContract. In this case, when the user makes a bid via the placeBet method, the contract will call another buyTicket method

If you meant to make two calls to different contracts at once from the user, you can't do that. The user can only call one method from the contract, which will call other methods inside itself.