Skip to main content
Since an auction contract hosts a single auction, each time you would like to host a new auction you will need a new contract account. A factory can create that account and initialize the auction for you. The repository includes an auction factory that creates a locked account for each auction. Each account uses a shared global contract, so we publish the auction code once and let new auctions reference it.

Publishing the auction contract

First, build the FT/NFT auction contract from contract-rs/03-bid-with-fts:
Publish the resulting WASM as a global contract using an existing funded account:
Choose testnet and the publisher account’s signing credentials when prompted. Publishing the code has a one-time cost; each auction then pays for its own state and global contract reference.
An account-ID global contract follows code updates published by that account. Updating the code at <global-auction.testnet> changes the implementation used by existing auctions too. Use a publisher account you control and trust. Removing an auction account’s keys does not prevent updates through this global reference.

Initializing the factory

The factory stores the account ID of the published global contract. Its new method takes this account ID as global_contract_id.
lib.rs
Create a funded factory account using near account create-account. Then, from the repository’s factory directory, build, deploy, and initialize the factory:
Choose zero attached deposit for initialization, testnet, and the factory account as signer when prompted.
If you deployed the earlier factory that stored auction WASM, use a new factory account for this version. Its state and initialization API have changed.

Creating an auction account

The deploy_new_auction method takes a name for the new subaccount and the auction’s initialization parameters. It checks that the name is a valid single subaccount label, that the auction ends in the future, and that the attached deposit covers the required funding.
deploy.rs
The factory then creates the account, transfers the attached deposit, references the global contract, and calls init in one atomic batch. The auction’s initialization function is public so the factory can call it. A successfully created auction is already initialized and has no access keys, as discussed earlier.
deploy.rs
A callback returns true on success. If account creation, the global contract reference, or initialization fails, it returns false and refunds the attached deposit to the caller.

Using the factory

First, choose an end time in nanoseconds. This command prints a timestamp two minutes from now:
Use the timestamp and your account IDs to prepare the auction parameters. The starting_price is expressed in the FT’s smallest units.
Before creating the auction, query get_required with the same initialization parameters, leaving out name and wrapping them in args:
The result is the required deposit in yoctoNEAR, returned as a string. It includes a conservative estimate for account, state, and global reference storage, plus 0.1 NEAR for outgoing FT/NFT calls. Long token IDs increase the requirement. The entire attached deposit is transferred to the new auction account.
deploy.rs
For ordinary short arguments, 0.2 NEAR covers the requirement. Call deploy_new_auction with your prepared JSON and at least the required deposit:
Choose your signer and testnet when prompted. On success, the auction is ready at new-auction.<factory.testnet>. Before accepting bids, transfer the NFT to this account and register the auction, bidders, and auctioneer on the FT contract using storage_deposit, as covered in the FT section.

Conclusion

In this part of the tutorial, you have learned how to publish an auction as a global contract and configure a factory to create initialized auction accounts that share its code. If you’re feeling adventurous you could create a frontend to interact with the factory contract to make it even easier to deploy new auctions. If you do so feel free to share it in our developer Telegram or Discord channels!