Since an auction contract hosts a single auction, each time you would like to host a new auction you will need to deploy a new contract. Rather than finding the compiled WASM file, creating a new account, deploying the contract, and then initializing it each time, you can use a factory contract to do this for you. Luckily for us, there is already a factory contract example! We will fork this example and slightly modify it to suit our use case. If you would like to learn more about how the factory contract works, you can take a look at the associated documentation. The factory example only comes in rust since, currently, the JavaScript SDK does not allow you to embed the WASM file in the contract. This is a limitation of the SDK and not the blockchain itself.Documentation Index
Fetch the complete documentation index at: https://docs.near.org/llms.txt
Use this file to discover all available pages before exploring further.
Changing the default contract
In the current example, the factory contract deploys the donation contract example. We will change this to deploy our auction contract instead. Firstly, we’ll need the compiled auction contract WASM file. You can get this by running the following command in 03-bid-with-fts ofcontract-rs
target/near; copy this file and use it to replace the WASM of the donation contract in the factory contract’s source folder. Now edit the auction contract changing the path to the auction contract.
On initialization, the factory will add the auction contracts WASM, as bytes, to the factory’s state. It is more efficient to not store the WASM in the factory’s state, however, we may want to update the auction contract if we find a bug or want to add new features. The factory implements a method to update the auction contract - we’ll change the name to update_auction_contract as this factory will only deploy auction contracts.
Modifying deploy method
The method to deploy a new contract is specific to the contract being deployed (in the case the contract has custom initialization parameters). We will modify the method to take in the auction contract’s initialization parameters. In this fork, we have also removed the option to add an access key to the contract account since, as discussed earlier, we want auctions to be locked.Using the factory
Build and deploy the factory like you would any other contract, this time without any initialization parameters.Deposit and storage costs
Note that we attach 1.6
$NEAR to the call to cover the storage costs of deploying the new auction. The storage cost on NEAR is 1 $NEAR per 100 kb, and our auction contract is around 140 kb, but we’ll add a little to cover the storage used on initialization.new-auction.auction-factory.testnet.