> ## 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.

# sauce compile

> Compile a SauceScript program or an actions document for one chain and print its bytecode

`sauce compile` compiles a SauceScript program for one chain and prints its bytecode.

## Synopsis

```bash theme={null}
sauce compile <file | -> [--chain <slug|id>] [options]
```

## Description

`sauce compile` reads one program file, or the program on stdin when the file is `-`, and compiles it as a standalone program for the chain `--chain` names. On that chain the program's token symbols (`USDC`), protocol namespaces (`UniswapV3.Factory`), the chain id, the `@sauce/token` functions and the token writes (`USDC.transfer`, `Token(address).approve`) resolve without an import or an address. One file therefore compiles for every chain whose names it uses. A name the chain does not have is refused, naming the chain.

Relative imports are read from beside the file. A program read from stdin resolves them against the current directory, or the directory `--source-dir` names.

Without `--chain`, the chain comes from the file's header: the `//` comment lines it starts with may hold `// @chain <slug>` and `// @target <evm|svm>`. `--chain` and `--target` take precedence over them, and a target that disagrees with the chain is an error. With neither a flag nor a header line, the command exits `2` and asks for `--chain` or a `// @chain <slug>` line. The other commands that compile a file read the same header.

An [actions document](https://docs.eco.com/sauce-sdk/cli/actions.md) compiles too: give its `.json` file in place of a `.sauce.ts` file.

`--args` and `--args-json` give `main`'s arguments. Each value is checked against its parameter's type before anything is compiled, and the compile reports the payload size with the arguments appended. The arguments do not change the bytecode `sauce compile` prints: they are appended to it when a cook is built ([`sauce tx`](https://docs.eco.com/sauce-sdk/cli/tx), [`sauce run`](https://docs.eco.com/sauce-sdk/cli/run)).

Compiling works on every chain in the registry, preview chains included. It builds no transaction and signs nothing.

## Options

| Option | Description |
| - | - |
| `--chain <slug\|id>` | The chain the program runs on: a slug, an alias or a chain id (`base`, `8453`, `solana`) |
| `--target <evm\|svm>` | Checked against the chain. `svm` with no `--chain` means Solana |
| `--source-dir <dir>` | The directory the program's relative imports resolve from. Default: beside the file; for stdin, the current directory |
| `--args-json <json\|file>` | `main`'s arguments as one JSON array, inline or in a file |
| `--account NAME=<address>` | Solana: an account `main` takes (an address, `pot`, `owner`, `authority`, `pot:USDC`, `@to:USDC`, `mint:USDC`, `program:USDC`), used to size the cook |
| `--via <kitchen\|engine>` | Solana: size the Kitchen's cook (default) or the engine's direct cook |
| `--rpc-url <url>` | Your own RPC endpoint for the chain. Default: a built-in public one |
| `--config <file>` | A file of your own endpoints: `{ "rpcUrls": { "<chain id>": "<url>" } }` |
| `--offline` | Read no chain: no endpoint, built-in ones included |
| `--out <path>` | Write the output to a file instead of stdout |
| `--json` | Print JSON: sizes, the names and contracts the compile applied, `main`'s parameters, the engine |
| `-h, --help` | Show the help |
| `--no-update-check` | Print no notice of a newer `sauce`, and read no registry for one |
| `--args <value>…` | `main`'s arguments in order: an integer as digits, a negative one too (`-5`), JSON for an array or a tuple. Put `--` before a word that starts like a flag |

### Low-level options

`--help` lists these apart, with the note "a program's inputs are main's parameters, given with --args".

| Option | Description |
| - | - |
| `--define NAME=VALUE` | A compile-time value for a name the program leaves undeclared: an integer, `0x` hex, `true` or `false`; base58 on Solana |
| `--defines <file.json>` | Compile-time values from a JSON object of name to value. `--define` takes precedence |

A `--define` the program never reads is named on stderr; it changes nothing.

## Output

Without `--json`, stdout carries the bytecode as one `0x` hex string. Notes go to stderr.

With `--json`, stdout carries one object:

| Field | Contents |
| - | - |
| `bytecode` | The compiled program, `0x` hex |
| `size` | The program's size in bytes |
| `payload` | `size` and `argumentsSize` of the program with `main`'s arguments appended; `null` without `--args` or `--args-json` |
| `entryEncoding` | How `main`'s arguments are encoded after the program: always `abi-v1` |
| `entryParams` | `main`'s parameters, each with `name` and `type`, in the order `--args` takes them |
| `returnType` | The type `main` declares it returns, or `null` |
| `defines` | Each name the chain gave the program (a token symbol, a protocol contract, the chain id), with its value, `kind` and `chain` |
| `contracts` | Each protocol contract the program calls or reads, as written and as resolved on the chain |
| `compiler`, `engine` | The compiler that compiled it and the engine release the bytecode pairs with ([deployments](https://docs.eco.com/sauce-sdk/reference/deployments.md)) |
| `manifest`, `cook` | Solana only: the accounts the program takes, and the cook's transaction size against the 1232-byte limit |

Exit codes: `0` when the program compiles, `1` when it does not, `2` for a usage error such as a malformed argument.

## Examples

### Compile for Base

```ts pay.sauce.ts theme={null}
// Pays `amount` of USDC from your Pot to `to`, and reverts if the Pot holds less.
//
//   sauce simulate pay.sauce.ts --chain base --owner <owner> --assume-balance USDC:5 \
//     --args <to> <amount>
//
//   to: Address       who receives the USDC, an address on the chain it runs on
//   amount: Uint256   how much, in USDC's smallest unit: 1000000 is 1 USDC
function main(to: Address, amount: Uint256) {
  require(pot.USDC >= amount, "not enough USDC");
  USDC.transfer(to, amount);
}
```

```bash theme={null}
sauce compile pay.sauce.ts --chain base
```

```text theme={null}
0xa502011fd1514702000eb19000b6d00200df01209554e10200ff01209554010014833589fcd6edb6e08f4c7c32d4…e1b0
```

The bytecode is 223 bytes. Compiled with `--chain ethereum`, it differs only in the `USDC` address it embeds.

### Report what the compile applied

```bash theme={null}
sauce compile pay.sauce.ts --chain base --json \
  --args 0x2222222222222222222222222222222222222222 1000000
```

```json theme={null}
{
  "chain": "base",
  "chainId": 8453,
  "target": "evm",
  "bytecode": "0xa502011fd151…e1b0",
  "size": 223,
  "payload": { "size": 287, "argumentsSize": 64 },
  "entryEncoding": "abi-v1",
  "entryParams": [
    { "name": "to", "type": "Address" },
    { "name": "amount", "type": "Uint256" }
  ],
  "returnType": null,
  "defines": [
    {
      "name": "USDC",
      "value": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
      "source": "chain",
      "kind": "token",
      "chain": "base"
    }
  ],
  "contracts": [],
  "compiler": { "name": "@eco-incorp/sauce-compiler", "version": "<version>" },
  "engine": { "version": "<version>", "address": "<engine address>" }
}
```

The two arguments add 64 bytes, one ABI word each.

### Write the bytecode to a file

```bash theme={null}
sauce compile pay.sauce.ts --chain arbitrum --out pay.arbitrum.hex
```

The command prints nothing on stdout and writes the hex to `pay.arbitrum.hex`. Arbitrum is a preview chain: the program compiles there, and `sauce tx` and `sauce run` refuse it.

### Read a compile error

A program that uses `to` without declaring it:

```bash theme={null}
sauce compile broken.sauce.ts --chain base
```

```text theme={null}
sauce compile: broken.sauce.ts does not compile for base
compiling the program failed
…
    ╰╴error: `to` is not a declared variable
       --> broken.sauce.ts:3:17
        |
      3 |   USDC.transfer(to, amount);
        |                 ^^
hint: if to is a value you supply, make it a parameter of main (`to: Uint256`, or `Address`) and pass it with --args
```

The command exits `1`.

### Read a rejected argument

```bash theme={null}
sauce compile pay.sauce.ts --chain base --args 0x12 1
```

```text theme={null}
sauce compile: --args: argument 1 (to: Address): '0x12' is not an address: 20 bytes of 0x-prefixed hex
Run `sauce compile --help` for usage.
```

The command exits `2`.

## Next steps

* [`sauce simulate`](https://docs.eco.com/sauce-sdk/cli/simulate): run the compiled program against a chain's current state.
* [`sauce actions`](https://docs.eco.com/sauce-sdk/cli/actions): compile a JSON actions document.
* [Names](https://docs.eco.com/sauce/names.md): how `USDC` and `UniswapV3.Factory` resolve on each chain.
* [Bytecode](https://docs.eco.com/sauce/bytecode.md): what the compiled program is.
* [Chains and endpoints](https://docs.eco.com/sauce-sdk/cli/chains-and-endpoints.md): `--chain`, `--rpc-url`, `--config` and preview chains.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.