Skip to main content
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:
Here is the parameters we use for the most examples in Cargo.toml:
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. You may also reference this Shrinking .wasm Size 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:
Adds unnecessary bloat
when it could be:
  1. When using the Rust SDK, you may override the default JSON serialization to use Borsh instead. See overriding interface serialization for more information and an example.
  2. When using assertions or guards, avoid using the standard assert macros like assert!, assert_eq!, or assert_ne! 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:
Adds unnecessary bloat
when it could be:
Example of removing expect:
Adds unnecessary bloat
when it could be:
Example of changing standard panic!():
Adds unnecessary bloat
when it could be:

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. 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 binaryen and wabt on your system. For Ubuntu and other Debian based Linux distributions run:
Minification could be rather aggressive, so you must test the contract after minification. Standalone NEAR runtime could be helpful here.

Lower-level approach

For a no_std approach to minimal contracts, observe the following examples:
Information on system calls

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, cargo-near, Docker, and SourceScan.
You need Docker to use the reproducible-build workflow.
When you initialize a project with cargo near new, its Cargo.toml includes the build environment and repository metadata:
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:

Verify and publish

To verify and publish the contract’s code, open its account page in NearBlocks, select the Contract tab, and use Verify and Publish. After verification, the source and metadata are available under Contract → Contract Code. NearBlocks interface for verifying a reproducible contract build For a complete walkthrough, see the SourceScan verification guide.