> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eco.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditional transfer and small programs

> Short SauceScript programs from the compiler documentation and the example sources shipped in @eco-incorp/sauce: a conditional native transfer, arithmetic with an input check, a Uniswap V3 factory lookup, an ERC-20-like transfer with storage and events, and Fibonacci.

Short programs that each show one part of the language. The first is from the compiler's introduction; the rest ship as example sources under `dev-tools/sauce/ts/` inside `@eco-incorp/sauce`. Compile them with `@eco-incorp/sauce/compiler` and run them through your Pot as in [Getting started](/programmable-transactions/sauce/getting-started); the package's older local-network scripts under `dev-tools/scripts` target an earlier contract interface and are not compatible with current deployments.

## Prerequisites

Complete [Sauce setup](/programmable-transactions/sauce/getting-started) and use the documented SDK and compiler versions. For execution, confirm the target deployment, Pot ownership, balances and Kitchen fee. Simulate the complete transaction and estimate its gas before submitting it.

## Conditional native transfer

Send native value to the supplied recipient only if the Pot holds enough.

```typescript theme={null}
function main(recipient, amount) {
  const balance = ctx.balance(ctx.self());
  if (balance >= amount) {
    evm.call(recipient, amount, hex``);
  }
  return balance;
}
```

## Arithmetic with an input check

```typescript example.ts theme={null}
// Add two values after requiring a positive first input.
function main(a: Uint256, b: Uint256): Uint256 {
  if (!(a > 0)) throw "a must be positive";
  return a + b;
}
```

## Addition

```typescript add.ts theme={null}
// Simple addition example
function main(a: Uint256, b: Uint256): Uint256 {
  return a + b;
}
```

## Contract call: Uniswap V3 factory lookup

Imports the factory ABI from an npm package and looks up a pool address.

```typescript call.ts theme={null}
// Uniswap V3 Pool Lookup
// Imports the factory ABI from @uniswap/v3-core and looks up a pool address
import { IUniswapV3Factory } from "@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Factory.sol/IUniswapV3Factory.json";

function main(factoryAddress: Address, token0: Address, token1: Address): Address {
  const factory = IUniswapV3Factory.at(factoryAddress);
  return factory.getPool(token0, token1, 3000);
}
```

The three `main` parameters (factory, `token0`, `token1`) arrive as ABI-encoded arguments appended to the compiled program. The import resolves through the compiler's `resolvePackage` hook, so `@uniswap/v3-core` must be installed in the project that compiles it.

## ERC-20-like transfer: storage, crypto, ABI encoding and events

```typescript erc20.ts theme={null}
// ERC20-like transfer operation
// Demonstrates: storage, crypto, abi encode/decode, events, ctx.msgSender()

function balanceSlot(account: Address): Uint256 {
  return crypto.keccak256(abi.encode(account, 1));
}

function main(to: Address, amount: Uint256): Uint256 {
  const from = ctx.msgSender();
  const fromSlot = balanceSlot(from);
  const fromBalance = evm.sload(fromSlot);
  if (fromBalance < amount) throw "insufficient balance";
  evm.sstore(fromSlot, fromBalance - amount);
  const toSlot = balanceSlot(to);
  evm.sstore(toSlot, evm.sload(toSlot) + amount);
  log(abi.encode(from, to, amount), crypto.keccak256("Transfer(address,address,uint256)"));
  return 1;
}
```

## Fibonacci: loops and locals

```typescript fibonacci.ts theme={null}
// Calculate fibonacci number
function main(n: Uint256): Uint256 {
  if (n <= 1) return n;

  let a: Uint256 = 0;
  let b: Uint256 = 1;
  for (let i: Uint256 = 2; i <= n; i++) {
    const temp: Uint256 = a + b;
    a = b;
    b = temp;
  }
  return b;
}
```

## Troubleshooting

If an import cannot be resolved, check your resolver and installed ABI package. EVM-specific calls and storage builtins in these examples require `target: "evm"`. Arithmetic overflow or a failed `require` or `throw` condition reverts execution.

## Next steps

Read the [SauceScript reference](/programmable-transactions/sauce/saucescript) or try the [best-pool swap](/cookbook/sauce/best-pool-swap).
