Sway tx fields input_owner

i am reading the code of OTC predicate. I am confused about the input_owner() here. I am not very clear about how fuel tx is constructed.
what is the index 0 and 1 here for?
why does checking this input_owner can make sure someone send coins along with the tx?
fuel-book/sway-book does not talk much about the tx and fuel-specs only lists the specs.
Can someone ELI5? want to dig deeper into this and know better.

    // Check if the transaction contains a single input coin from the receiver, to cancel their own order (in addition to this predicate)
    if input_count() == 2u8 {
        if input_owner(0).unwrap() == RECEIVER
            || input_owner(1).unwrap() == RECEIVER
        {
            return true;
        };
    };

https://github.dev/FuelLabs/sway-applications/blob/4d497d25df0e88ee29d8b6e6c7055aac83c17842/OTC-swap-predicate/project/predicates/swap-predicate/src/main.sw#L32-L38

3 Likes

The idea here is that the address specified as the receiver of the proposed swap can cancel the swap and recover the funds. In order to do this, they can submit a simple transaction which spends the predicate.

This transaction will have exactly two inputs: an input coin (to pay for gas) and the coin belonging to the predicate itself. The input coin contains a signature of the owner over the transaction hash, and this is what allows input_owner() to deduce the transaction sender.

So this block of code says:

"If there are exactly two inputs, check if the first one (index 0) is the RECEIVER. If it isn’t, check if the second one (index 1) is the RECEIVER.

If either of the inputs belongs to the RECEIVER, the predicate returns true and is spendable"

A transaction to execute the swap must contain at least three inputs: a coin for gas, the requested coin for the swap, and the coin belonging to the predicate. So in this case, we don’t enter the above block of code, and instead the conditions of the swap are evaluated.

5 Likes

thank you for the detailed explanation! very helpful!

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.