Skip to main content
A factory is a smart contract that creates sub-accounts and deploys contracts onto them. Our factory example creates fungible tokens using a shared global contract. The factory stores the account ID of the global FT implementation. Each new token account references that implementation and keeps its own owner, balance, and metadata in state.

Overview

The factory exposes two methods for creating tokens:
  1. get_required quotes the deposit needed for the token’s initial storage.
  2. create_token creates a sub-account, attaches the global FT implementation, and initializes the token in one atomic batch.
The factory is initialized with the account ID of the global contract it will use:
lib.rs
The creation method transfers the attached deposit to the new account, references the global code, and calls the FT contract’s new method:
deploy.rs

Quickstart

  1. Make sure you have installed Rust.
  2. Install the NEAR CLI.
  3. Install cargo-near.
  4. Clone the example and navigate to its directory:

Build and Deploy the Factory

Compile and deploy the factory to a funded account on testnet:
During deployment, initialize the factory by calling new with these arguments:
Use ft.globals.primitives.near for mainnet. The factory requires initialization before it can create tokens.

Prepare the Token Arguments

Both get_required and create_token take an args object containing the owner, total supply, and token metadata. Save the following as token-args.json, replacing the owner with your account:
The supply is expressed in the token’s smallest units. With six decimal places, 100000000 represents 100 tokens. The symbol determines the sub-account name: EXAMPLE creates example.<factory-account>. Symbols must contain only ASCII letters and digits, and the resulting account ID must be valid.

Quote the Deposit and Create the Token

Call get_required with the arguments you saved:
The result is a deposit in yoctoNEAR, encoded as a JSON string. Attach that amount when calling create_token with the same arguments:
The new token account is initialized in the same batch that creates it, and the owner receives the full initial supply. The method returns true on success. If account creation or initialization fails, the callback returns false and refunds the attached deposit to the caller. You can query the new token’s metadata to verify the result:
Users register separately through the FT’s storage_deposit method before receiving transfers. Their future storage is not included in the factory’s initial deposit quote.

Factories - Concepts & Limitations

Automatically Creating Accounts

The factory creates direct sub-accounts of itself. For example, factory.testnet can create example.factory.testnet and initialize a token on it. The created token account has no access keys. Its owner_id identifies the token owner in contract state; it does not give that owner a full access key to the account.

Storage Costs

The deposit quote includes the global reference and the initial state of the published FT implementation:
deploy.rs
For an initial footprint of at most 770 bytes, the quote is zero. Above that limit, it funds the entire initial storage footprint. Gas is paid separately. If you change the global FT implementation, review the storage estimate against that implementation’s layout.

Global Contract Updates

This factory references global code by account ID. When the publisher updates that global implementation, token accounts referencing it use the updated code. Choose a publisher you trust. See global contract references for more details.