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

# Smart Contracts

> Programs that run on NEXUS — live on Zcash today, expanding to Bitcoin and Dogecoin

## What is a Smart Contract?

A smart contract is a **program that lives on-chain**. Once deployed, it runs exactly as written — no company or person controls it. Anyone can call it, and the outcome is transparent and verifiable.

NEXUS smart contracts run on **Zcash today**. The same contract code will work identically when Bitcoin and Dogecoin join the unified execution layer.

On NEXUS, smart contracts can:

* **Hold and transfer** vZEC (live), vSAT and vDOGE (coming soon)
* **Run any logic** — token balances, swap pools, lending, NFTs
* **Call other contracts** — composable DeFi
* **Read Zcash L1 data** — e.g. block height, deposit confirmations

## How it Works (No Code Required)

```
You write logic in Rust
        ↓
Compile to WebAssembly (WASM) — a compact, fast binary format
        ↓
Deploy to NEXUS — gets a permanent address on-chain
        ↓
Anyone calls your contract — NEXUS runs it, results are final
```

WASM is the same technology used in modern web browsers — it is fast, portable, and sandboxed.

## What Can Contracts Do?

<AccordionGroup>
  <Accordion title="Store data permanently">
    Contracts have persistent storage — like a database that lives on-chain forever.
    A token contract stores balances. An AMM stores liquidity pool reserves.
    No server, no database bill — it just exists.
  </Accordion>

  <Accordion title="Move funds">
    Contracts can hold and move vSAT (Bitcoin), vZEC (Zcash), and vDOGE (Dogecoin).
    A swap pool holds both sides of a trading pair. A lending protocol holds collateral.
    Funds only move when contract logic says they can.
  </Accordion>

  <Accordion title="Call other contracts">
    Contracts can call each other — this is how DeFi composes.
    A yield vault can call an AMM to swap, which calls a token to transfer.
    Everything happens atomically in one transaction.
  </Accordion>

  <Accordion title="Emit events">
    Contracts emit events when things happen (e.g. "Transfer: Alice sent 10 to Bob").
    Wallets, explorers, and indexers listen for these events off-chain.
  </Accordion>

  <Accordion title="Read L1 chain data">
    Contracts can read Bitcoin block height, Zcash confirmations, and Dogecoin data directly.
    This enables time-locks, oracle-free conditions, and cross-chain logic.
  </Accordion>
</AccordionGroup>

## Gas: Paying for Computation

Every operation in a contract costs **gas** — a small fee proportional to how much work the network does. Gas prevents runaway loops and spam. You set a gas limit when submitting a transaction; if your contract uses more, it reverts.

| Operation             | Relative Cost         |
| --------------------- | --------------------- |
| Read stored data      | Low                   |
| Write stored data     | Medium                |
| Cryptographic hash    | Very low              |
| Call another contract | Medium + subcall cost |

## Built-in Protections

NEXUS contracts have safeguards built into the SDK:

* **Reentrancy guard** — prevents the classic DeFi reentrancy attack automatically
* **Safe math** — arithmetic overflows revert the transaction instead of wrapping
* **Require checks** — any failed condition reverts the entire transaction (no partial state)

## Ready-Made Templates

You don't have to start from scratch. The **[nexus-contract-template](https://github.com/yattacorp/nexus-contract-template)** includes production-ready examples:

| Example  | What it gives you                         |
| -------- | ----------------------------------------- |
| Counter  | Minimal working contract with `nexus_fn!` |
| NEP-20   | Fungible token (like ERC-20)              |
| WVZEC    | Wrapped native vZEC for AMM compatibility |
| AMM Pair | Core swap logic (SatsAMM)                 |

```bash theme={null}
git clone https://github.com/yattacorp/nexus-contract-template
cd nexus-contract-template
cargo build --target wasm32-unknown-unknown --release
```

## For Developers

If you want to write contracts, see:

* [Contracts Overview](/contracts/overview) — full SDK reference with code examples
* [Deploy a Contract](/guides/deploy-contract) — step-by-step deployment guide
* [NEP-20 Token Standard](/contracts/nep20) — fungible token reference
