update_global_contract_id method. The factory creates sub-accounts of itself and deploys corresponding contract on them.
You can learn more about global contracts on NEAR here.
Overview
The factory is a smart contract that:- Creates sub-accounts of itself and deploys its contract on them (
deploy) - Can update the contract it deploys
Quickstart
- Make sure you have installed rust.
- Install the
NEAR CLI - Install the
cargo nearextension.
Build and Deploy the Factory
You can automatically compile and deploy the contract in the NEAR testnet by running:Deploy the Stored Contract Into a Sub-Account
Methoddeploy will create a sub-account of the factory and deploy contract on it using stored global contract id. It also asserts that the attached deposit is at least the minimum required deposit stored in the factory.
Technically, there is no need to attach any deposit just to deploy a contract using global contracts. But to initialize it further, you will need some NEAR tokens to cover the storage cost. Since initiliazing method can’t accept deposit, it makes sense to attach some minimum amount of tokens during creating account and deploying a contract.
sub.<factory-account>, which will have a global contract deployed on it.
ft.globals.primitives.testnet. To initilize the contract, you can call its new_default_meta method:
ft_metadata method to verify that the contract is deployed and initialized correctly:
Update the Stored Contract
update_global_contract_id enables to change the global contract id that the factory stores.
The method is interesting because it has no declared parameters, and yet it takes
an input: the new contract to store as a stream of bytes.
To use it, we need to pass the contract id we want to store. It can be in form of account id or global contract hash. What is the difference between them you can read in Global Contracts section.
Factories - Concepts & Limitations
Factories are an interesting concept, here we further explain some of their implementation aspects, as well as their limitations.Automatically Creating Accounts
NEAR accounts can only create sub-accounts of itself, therefore, thefactory can only create and
deploy contracts on its own sub-accounts.
This means that the factory:
- Can create
sub.factory.testnetand deploy a contract on it. - Cannot create sub-accounts of the
predecessor. - Can create new accounts (e.g.
account.testnet), but cannot deploy contracts on them.
factory.testnet can create sub.factory.testnet, it has
no control over it after its creation.
The Deploy Method
During the creation of a sub-account we add signers public key to the new sub-account as a full access key. It means that the predecessor will be able to control the new sub-account after its creation. Also, in the tutorial, we attach deposit to thedeploy call which goes straight to the new sub-account. When we deploy a new contract using global contracts, we need to initialize it after deployment. That tokens will cover the storage after the deployed contract is initialized.