If your contract exceeds the size limit for a single deployment:
{{#include ../../../examples/contracts/src/lib.rs:show_contract_is_too_big}}
you can deploy it in segments using a partitioned approach:
{{#include ../../../examples/contracts/src/lib.rs:deploy_via_loader}}
When you convert a standard contract into a loader contract, the following changes occur:
- The original contract code is replaced with the loader contract code.
- The original contract code is split into blobs, which will be deployed via blob transactions before deploying the contract itself.
- The new loader code, when invoked, loads these blobs into memory and executes your original contract.
After deploying the loader contract, you can interact with it just as you would with a standard contract:
{{#include ../../../examples/contracts/src/lib.rs:use_loader}}
A helper function is available to deploy your contract normally if it is within the size limit, or as a loader contract if it exceeds the limit:
{{#include ../../../examples/contracts/src/lib.rs:auto_convert_to_loader}}
You also have the option to separate the blob upload from the contract deployment for more granular control:
{{#include ../../../examples/contracts/src/lib.rs:upload_blobs_then_deploy}}
Alternatively, you can manually split your contract code into blobs and then create and deploy a loader:
{{#include ../../../examples/contracts/src/lib.rs:manual_blobs_then_deploy}}
Or you can upload the blobs yourself and proceed with just the loader deployment:
{{#include ../../../examples/contracts/src/lib.rs:manual_blob_upload_then_deploy}}
The size of a Blob transaction is constrained by three factors:
- The maximum size of a single transaction:
{{#include ../../../examples/contracts/src/lib.rs:show_max_tx_size}}
- The maximum gas usage for a single transaction:
{{#include ../../../examples/contracts/src/lib.rs:show_max_tx_gas}}
- The maximum HTTP body size accepted by the Fuel node.
To estimate an appropriate size for your blobs, you can run:
{{#include ../../../examples/contracts/src/lib.rs:estimate_max_blob_size}}
However, keep in mind the following limitations:
- The estimation only considers the maximum transaction size, not the max gas usage or HTTP body limit.
- It does not account for any size increase that may occur after the transaction is funded.
Therefore, it is advisable to make your blobs a few percent smaller than the estimated maximum size.