A Look at Predicates in Sway

Brandon Surh
5 min readFeb 8, 2023

What are Predicates?

In functional programming, predicates are functions that return a Boolean value of True or False. This returning value indicates if the function’s given condition was met or not. Predicates can be very simple functions such as checking if the input is a character or not or more complex and check if a specific list of conditions have been met.

For better understanding, here are some examples of typical predicate uses:

  • Checking states of variables in object-oriented programing
  • Filtering lists given a specified condition
  • Sorting lists given a specified condition

Here is an example in Java:

public class predicate {

public static void main(String[] args) {

// char variables
char ch1, ch2;

// assign values to ch1, ch2
ch1 = 'A';
ch2 = '1';

// boolean variables that will hold predicate return values
boolean b1, b2;

// ask predicate function if ch1 and ch2 are characters
b1 = Character.isLetter(ch1);
b2 = Character.isLetter(ch2);


String str1 = "Is " + ch1 + " a letter? The predicate returns " + b1;
String str2 = "Is " + ch2 + " a letter? The predicate returns " + b2;

// print b1, b2 values
System.out.println("Output:");
System.out.println(str1);
System.out.println(str2);
}

}

Output:

Is A a letter?    The predicate returns true
Is 1 a letter? The predicate returns false

Whether simple in it’s construction or not, a predicate can be described as a conditional execution meaning something will only happen if the condition is satisfied. This is a way to abstract the utility of predicates and better understand their use.

Conceptually, predicates are nothing new to programmers. However, Fuel takes them and forms them into an intuitive interaction between predicates and the user.

Predicates in Fuel

In the context of Fuel, predicates in concept are just as expected. They are pure functions that evaluate to True or False given a specified condition. However, there are more things worth noting and some characteristics that are unique to Fuel. Predicates are stateless, they cannot read or write to any contract’s state, they cannot access contract storage, and they cannot emit logs. Since predicates can exist on their own, this allows much freedom in their implementation.

In Fuel, the bytecode of a predicate is taken and hashed into a unique string resembling an address that then represents the predicate. This is called the predicate root or the bytecode root. This predicate root can be sent coin UTXOs which are then spendable not on the action of a valid signature, but if a transaction’s supplied predicate has a root that matches their owner and evaluates to True.

It is important to note, these predicate roots are not to be looked at in the same way as an address to an account. Rather, predicates are inputs to transactions similar to coins. They do not operate like a wallet account but function as a way to sign a transaction.

With functionality such as this, some features such as trading and coin storage don’t need to be held in a smart contract, and thus, don’t need to be deployed. Deploying a smart contract can be costly and solidify them as existing permanently. Predicates, however, are not permanent and disappear once satisfied.

Over-the-Counter (OTC) Swap Application

One of the more easily demonstrated applications is over-the-counter trades. In the finance world, over-the-counter (OTC) trades are trades that occur between two parties with no middleman or centralized entity. Everything happens directly between the individuals that buy and individuals that sell.

With Fuel, the OTC Swap application makes use of this concept while minimizing risk in a trustless nature. The predicates act as over-the-counter orders that anyone can fill while being able to take in runtime arguments. A user can transfer coins to the predicate root which can then be unlocked and spendable by any transaction that has an output of True, thus satisfying the order.

Source: Freepik

For example, imagine there is a predicate where it returns True if a specified amount of token is sent to a specified receiver. If there is a transaction that satisfies this condition then the predicate would return True and it would be considered a spending transaction. This allows the predicate to sign the transaction and perform the other end of the trade.

The condition within the predicate is hardcoded so that there is no mistake on how the bytecode will act making the entire process trustless. Lastly, orders cannot be partially filled as the predicate operates on a Boolean system. In this case, the order “taker” must pay the entire ask amount to satisfy the predicate’s specific conditions and complete the trade.

This application allows for over-the-counter trades simple or complex with the ability to swap the signature function to whatever you want it to be, granting freedom to the user.

Looking Ahead

With examples like OTC Swap, it is easy to see the vast possibilities that predicates allow. The beauty of these constructs in Fuel is that you can define the set of conditions that permit a transaction to be signed. This is in comparison to the single cryptographic condition of spending coins through the validation of a specified wallet which heavily limits modern protocols. The introduction of predicates opens many doors for innovation and unexplored approaches to the way that transactions can be utilized.

Conclusion

Fuel’s use of predicates help lay down an important foundation for development amongst programmers and experience for users, while displaying it’s perceptive and well-researched outlooks on the current blockchain landscape. It achieves this in it’s insightful and efficiently designed constructs that shift the protocol paradigm. Predicates are building blocks to those wishing to develop the space for an intuitive and smarter future.

Learn More

--

--