> ## Documentation Index
> Fetch the complete documentation index at: https://neardocs-docs-2854-nep-621-tokenized-vault.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Production Builds

> Reduce contract size and create reproducible builds that users can verify against the deployed code.

export const Github = ({url, start, end, fname, language, withSourceLink = true}) => {
  const [code, setCode] = useState(null);
  function toRaw(ref) {
    const fullUrl = ref.slice(ref.indexOf('https'));
    const [url] = fullUrl.split('#');
    const [org, repo, , branch, ...pathSeg] = new URL(url).pathname.split('/').slice(1);
    return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${pathSeg.join('/')}`;
  }
  async function fetchCode(url, fromLine, toLine) {
    let res;
    if (typeof window !== 'undefined') {
      const validUntil = localStorage.getItem(`${url}-until`);
      if (validUntil && Number(validUntil) > Date.now()) {
        res = localStorage.getItem(url);
      }
    }
    if (!res) {
      try {
        res = await (await fetch(url)).text();
        if (typeof window !== 'undefined') {
          localStorage.setItem(url, res);
          localStorage.setItem(`${url}-until`, String(Date.now() + 60000));
        }
      } catch {
        return 'Error fetching code, please try reloading';
      }
    }
    let body = res.split('\n');
    const from = fromLine ? Number(fromLine) - 1 : 0;
    const to = toLine ? Number(toLine) : body.length;
    body = body.slice(from, to);
    const precedingSpace = body.reduce((prev, line) => {
      if (line.length === 0) return prev;
      const spaces = line.match(/^\s+/);
      if (spaces) return Math.min(prev, spaces[0].length);
      return 0;
    }, Infinity);
    return body.map(line => line.slice(precedingSpace === Infinity ? 0 : precedingSpace)).join('\n');
  }
  function buildSourceUrl(url, start, end) {
    const base = url.split('#')[0];
    if (start && end) return `${base}#L${start}-L${end}`;
    if (start) return `${base}#L${start}`;
    return base;
  }
  useEffect(() => {
    const rawUrl = toRaw(url);
    fetchCode(rawUrl, start, end).then(res => setCode(res));
  }, [url, start, end]);
  const sourceUrl = buildSourceUrl(url, start, end);
  const fileName = fname ?? sourceUrl.split('/').pop();
  return <div className="my-5">
      {code === null ? <div>Loading...</div> : <CodeBlock language={language} filename={fileName} lines>
          {code}
        </CodeBlock>}
      {withSourceLink && <div className="flex justify-end" style={{
    marginTop: "-1rem"
  }}>
          <a href={sourceUrl} target="_blank" rel="noreferrer noopener" className="text-[0.6875rem] font-medium text-[#656d76] no-underline hover:text-[#1f2328] dark:text-[#8b949e] dark:hover:text-[#e6edf3]">
            See code on GitHub
          </a>
        </div>}
    </div>;
};

In this page, we will explore strategies for reducing the size of smart contracts on NEAR. This is particularly useful for developers who want to optimize their contracts for deployment, especially in scenarios where contract size limits are a concern.

# Reducing a contract's size

## Advice & examples

This page is made for developers familiar with lower-level concepts who wish to reduce their contract size significantly, perhaps at the expense of code readability.

Some common scenarios where this approach may be helpful:

* contracts intended to be tied to one's account management
* contracts deployed using a factory
* future advancements similar to the EVM on NEAR

There have been a few items that may add unwanted bytes to a contract's size when compiled. Some of these may be more easily swapped for other approaches while others require more internal knowledge about system calls.

## Small wins

### Using flags

When compiling a contract make sure to pass flag `-C link-arg=-s` to the rust compiler:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
RUSTFLAGS='-C link-arg=-s' cargo build --target wasm32-unknown-unknown --release
```

Here is the parameters we use for the most examples in `Cargo.toml`:

```toml theme={"theme":{"light":"github-light","dark":"github-dark"}}
[profile.release]
codegen-units = 1
opt-level = "s"
lto = true
debug = false
panic = "abort"
overflow-checks = true
```

You may want to experiment with using `opt-level = "z"` instead of `opt-level = "s"` to see if generates a smaller binary. See more details on this in [The Cargo Book Profiles section](https://doc.rust-lang.org/cargo/reference/profiles.html#opt-level). You may also reference this [Shrinking .wasm Size](https://rustwasm.github.io/book/reference/code-size.html#tell-llvm-to-optimize-for-size-instead-of-speed) resource.

### Removing `rlib` from the manifest

Ensure that your manifest (`Cargo.toml`) doesn't contain `rlib` unless it needs to. Some NEAR examples have included this:

<Warning>
  **Adds unnecessary bloat**

  ```toml theme={"theme":{"light":"github-light","dark":"github-dark"}}
  [lib]
  crate-type = ["cdylib", "rlib"]
  ```
</Warning>

when it could be:

<Tip>
  ```toml theme={"theme":{"light":"github-light","dark":"github-dark"}}
  [lib]
  crate-type = ["cdylib"]
  ```
</Tip>

3. When using the Rust SDK, you may override the default JSON serialization to use [Borsh](https://borsh.io) instead. See [overriding interface serialization](/smart-contracts/anatomy/serialization#overriding-interface-serialization) for more information and an example.
4. When using assertions or guards, avoid using the standard `assert` macros like [`assert!`](https://doc.rust-lang.org/std/macro.assert.html), [`assert_eq!`](https://doc.rust-lang.org/std/macro.assert_eq.html), or [`assert_ne!`](https://doc.rust-lang.org/std/macro.assert_ne.html) as these may add bloat for information regarding the line number of the error. There are similar issues with `unwrap`, `expect`, and Rust's `panic!()` macro.

Example of a standard assertion:

<Warning>
  **Adds unnecessary bloat**

  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  assert_eq!(contract_owner, predecessor_account, "ERR_NOT_OWNER");
  ```
</Warning>

when it could be:

<Tip>
  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  if contract_owner != predecessor_account {
    env::panic(b"ERR_NOT_OWNER");
  }
  ```
</Tip>

Example of removing `expect`:

<Warning>
  **Adds unnecessary bloat**

  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  let owner_id = self.owner_by_id.get(&token_id).expect("Token not found");
  ```
</Warning>

when it could be:

<Tip>
  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  fn expect_token_found<T>(option: Option<T>) -> T {
    option.unwrap_or_else(|| env::panic_str("Token not found"))
  }
  let owner_id = expect_token_found(self.owner_by_id.get(&token_id));
  ```
</Tip>

Example of changing standard `panic!()`:

<Warning>
  **Adds unnecessary bloat**

  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  panic!("ERR_MSG_HERE");
  ```
</Warning>

when it could be:

<Tip>
  ```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
  env::panic_str("ERR_MSG_HERE");
  ```
</Tip>

## Ready to use script

We have prepared a simple `bash` script that can be used to minify `.wasm` contract file. You can find it [here](https://github.com/near/near-sdk-rs/blob/master/minifier/minify.sh).

The current approach to minification is the following:

1. Snip (i.e. just replace with unreachable instruction) few known fat functions from the standard library (such as float formatting and panic-related) with `wasm-snip`.
2. Run `wasm-gc` to eliminate all functions reachable from the snipped functions.
3. Strip unneeded sections, such as names with `wasm-strip`.
4. Run `binaryen wasm-opt`, which cleans up the rest.

### Requirements to run the script:

* install [wasm-snip](https://docs.rs/wasm-snip/0.4.0/wasm_snip/) and [wasm-gc](https://docs.rs/crate/wasm-gc/0.1.6) with Cargo:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
cargo install wasm-snip wasm-gc
```

* install [binaryen](https://github.com/WebAssembly/binaryen) and [wabt](https://github.com/WebAssembly/wabt) on your system. For Ubuntu and other Debian based Linux distributions run:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
apt install binaryen wabt
```

<Danger>
  Minification could be rather aggressive, so you must test the contract after minification. Standalone NEAR runtime could be helpful [here](https://github.com/nearprotocol/nearcore/tree/master/runtime/near-vm-runner).
</Danger>

## Lower-level approach

For a `no_std` approach to minimal contracts, observe the following examples:

* [Tiny contract](https://github.com/near/nearcore/tree/1e7c6613f65c23f87adf2c92e3d877f4ffe666ea/runtime/near-test-contracts/tiny-contract-rs)
* [NEAR ETH Gateway](https://github.com/ilblackdragon/near-eth-gateway/blob/master/proxy/src/lib.rs)
* [This YouTube video](https://youtu.be/Hy4VBSCqnsE) where Eugene demonstrates a fungible token in `no_std` mode. The code for this [example lives here](https://github.com/near/core-contracts/pull/88).
* [Examples using a project called `nesdie`](https://github.com/austinabell/nesdie/tree/main/examples).
* Note that Aurora has found success using [rjson](https://crates.io/crates/rjson) as a lightweight JSON serialization crate. It has a smaller footprint than [serde](https://crates.io/crates/serde) which is currently packaged with the Rust SDK. See [this example of rjson](https://github.com/aurora-is-near/aurora-engine/blob/65a1d11fcd16192cc1bda886c62005c603189a24/src/json.rs#L254) in an Aurora repository, although implementation details will have to be gleaned by the reader and won't be expanded upon here. [This nesdie example](https://github.com/austinabell/nesdie/blob/bb6beb77e32cd54077ac54bf028f262a9dfb6ad0/examples/multisig/src/utils/json/vector.rs#L26-L30) also uses the [miniserde crate](https://crates.io/crates/miniserde), which is another option to consider for folks who choose to avoid using the Rust SDK.

<Note>
  **Information on system calls**

  <Accordion title="Expand to see what's available from <code>sys.rs</code>">
    <Github language="rust" url="https://github.com/near/near-sdk-rs/blob/master/near-sdk/src/environment/env.rs" />
  </Accordion>
</Note>

***

## Reproducible builds

Reproducible builds let different people build the same program and get the exact same output. They help users verify that a deployed contract corresponds to its published source code.

Building the same contract on different machines can produce similar but non-identical binaries because the artifact can be affected by the locale, timezone, build path, and other parts of the build environment. NEAR addresses this with [NEP-330 source metadata](https://github.com/near/NEPs/blob/master/neps/nep-0330.md), [cargo-near](https://github.com/near/cargo-near), Docker, and SourceScan.

<Info>
  You need [Docker](https://docker.com) to use the reproducible-build workflow.
</Info>

When you initialize a project with `cargo near new`, its `Cargo.toml` includes the build environment and repository metadata:

```toml theme={"theme":{"light":"github-light","dark":"github-dark"}}
repository = "https://github.com/<organization>/<repository>"

[package.metadata.near.reproducible_build]
image = "sourcescan/cargo-near:0.13.5-rust-1.85.1"
image_digest = "sha256:3b0272ecdbb91465f3e7348330d7f2d031d27901f26fb25b4eaf1560a60c20f3"
passed_env = []
container_build_command = [
    "cargo",
    "near",
    "build",
    "non-reproducible-wasm",
    "--locked",
]
```

When you deploy with `cargo near deploy`, this information is used to clone the repository and compile the contract in a Docker container. The build adds a `contract_source_metadata` method without changing the contract's logic.

After deployment, inspect that metadata with:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
near view <contract-account> contract_source_metadata
```

## Verify and publish

To verify and publish the contract's code, open its account page in [NearBlocks](https://nearblocks.io), select the **Contract** tab, and use **Verify and Publish**. After verification, the source and metadata are available under **Contract → Contract Code**.

<img src="https://mintcdn.com/neardocs-docs-2854-nep-621-tokenized-vault/SqY2WhoKwVauryHq/assets/docs/smart-contracts/reproducible-build.png?fit=max&auto=format&n=SqY2WhoKwVauryHq&q=85&s=ae0daddd89399c813dac9daa4ef2a3c5" alt="NearBlocks interface for verifying a reproducible contract build" width="1388" height="562" data-path="assets/docs/smart-contracts/reproducible-build.png" />

For a complete walkthrough, see the [SourceScan verification guide](https://github.com/SourceScan/verification-guide).
