algorand / algorand/pyteal

Add lazy operators

Aperta
#187 0 commenti 1 reazione 0 assegnatari Vedi su GitHub
external contribution new-feature-request Team Scytale
Lingua principale
Python
Stelle
288
Fork
138
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

## Problem

When writing a smart contract, it is very often useful to check if a series of conditions hold. In a lot of cases it is enough to use the `and` opcode, however, in many other cases this creates some problems.

Consider the common scenario in which we want to check if the first argument of an application call is equal to some value, but crucially, only if the first argument actually exists (might not exist for example if the transaction that we're checking is not an application call, or maybe a call with no arguments).

If this check is done incorrectly, in some executions the contract might crash (instead of just returning false in the `and` expression), as we might be doing some invalid checks.

## Solution

A pattern to solve this problem is a "lazy and", which in TEAL could be written as follows:

```teal
check1
bz label_false
check2
bz label_false
check3
bz label_false
check4
b lazy_end:

label_false:
int 0
lazy_end:
```

As of right now, the only way (afaik) to do this in pyteal, is by using a sequence of nested ifs, but this results in a more expensive teal code, and also a much uglier pyteal code.

The same could also be done for the `or` opcode, although less importantly.

## Dependencies

Don't think it has any.

## Urgency

Not urgent, but would be nice, both for optimization and as a useful quality of life feature

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.