6 min read
Overview
Programs are Solana's equivalent to smart contracts. Solana Programs are mutable by default since they can be updated by the Program's "update authority," a public key that the Program's deployer defines. Immutability in a Solana Program is a standard best practice as it assures users that a program will never be updated or modified by a bad actor. In this guide, you will deploy a local Solana Anchor Program and learn how to make it immutable forever.
Make any Solana Program immutable by removing the update authority with this command:
solana program set-upgrade-authority <PROGRAM_ID> -u <YOUR_QUICKNODE_ENDPOINT> --final
WARNING This process is irreversible. Once you remove the update authority, the program can never be changed.
What You Will Do
- Run a local validator
- Deploy a Solana Program to your localhost using Anchor
- Remove the update authority of a Solana Program
What You Will Need
Either:
Deploy a Solana Program to your Localhost using Anchor
In this section, you will deploy a Solana Program to your localhost using Anchor (You can use the same steps to deploy a Solana Program to Devnet or Mainnet).
Create a New Anchor Project
Anchor is a popular development framework for building Programs on Solana. To get started, check out our Intro to Anchor Guide.
Create a new project directory in your terminal with the following:
mkdir immutable-demo
cd immutable-demo
Create a new Anchor project using the following command:
anchor init solana-immutable-program
Because we are just testing how to update a program's authority, we will not change the default 'Initialize' program inside lib.rs
.
Update Program Config
Before deploying your program, we will need to update the program's config. Open the Anchor.toml
file and update the provider.cluster
field to localhost
.
[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"
Double-check that the path to your wallet is correct. You can use any .json key (Check out our Guide on Creating Solana Vanity Addresses).
Go ahead and build your program. In your terminal, enter:
cd solana-immutable-program
anchor build
The first build may take a couple of minutes. Be patient.
You should see the following output:
Compiling anchor-lang v0.26.0
Compiling solana-immutable-program v0.1.0 (~/immutable-demo/solana-immutable-program/programs/solana-immutable-program)
warning: unused variable: `ctx`
--> programs/solana-immutable-program/src/lib.rs:9:23
|
9 | pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
| ^^^ help: if this is intentional, prefix it with an underscore: `_ctx`
|
= note: `#[warn(unused_variables)]` on by default
warning: `solana-immutable-program` (lib) generated 1 warning
Finished release [optimized] target(s) in 1m 22s
No worries about the warnings, as they will not impact what we are demonstrating here.
Great job. You now have a compiled program that you can deploy to the localhost.
Start a Local Validator
Before deploying your program, we will need to start a local validator. In a separate terminal, start a local validator by using the following command:
solana-test-validator -r
the -r
flag will reset the ledger to genesis if it exists (instead of resuming from a previous instance)
For more information on running a local validator, check out the Solana CLI docs, or you can enter the help menu by typing the following command in your terminal:
solana-test-validator --help
You should see your validator start producing blocks:
Set Your Solana CLI Config
Before deploying your program, you will need to set your Solana CLI config to localhost and the same wallet referenced in Anchor.toml
. You can set your Solana CLI config using the following command:
solana config set --url localhost --keypair ~/.config/solana/id.json
You should see the following output:
Config File: ~/.config/solana/cli/config.yml
RPC URL: http://localhost:8899
WebSocket URL: ws://localhost:8900/ (computed)
Keypair Path: ~/.config/solana/id.json
Commitment: confirmed
Finally, let's be sure your wallet has some SOL on your local cluster. You can airdrop some SOL by using the following command:
solana airdrop 100
Note: local host airdrops are not limited like Devnet and Testnet servers, so you can airdrop more than 1 or 2 SOL at a time.
We should be good to go! Let's deploy our program to localhost.
Deploy the Program
With your local validator running, you can deploy your program using the following command:
anchor deploy
You should see the following output:
quicknode solana-immutable-program % anchor deploy
Deploying workspace: http://localhost:8899
Upgrade authority: ~/.config/solana/id.json
Deploying program "solana-immutable-program"...
Program path: <PATH>/solana-immutable-program/target/deploy/solana-immutable-program.so...
Program Id: TESTxAHSs72DNFzhxmWhD9cVJjYqcgH2kHuDsq2NzEz
Deploy success
Great job! You have successfully deployed your program to the localhost. Make sure to copy that Program Id, as we need it in the next step.
Remove the Update Authority of a Solana Program
Our Program is currently mutable. To change it to be immutable indefinitely, you can remove the update authority using the following command (makes sure to replace the Program Id with the one you copied in the previous step):
solana program set-upgrade-authority <PROGRAM_ID> -u localhost --final
A couple of notes about our command:
PROGRAM_ID
is the program id of the program you want to make immutable-u localhost
sets the url of the cluster we are working on to localhost (this is the same as the--url localhost
flag). You do not need to set the URL if you have already set your Solana CLI config to localhost. However, we like to do this when making major changes like this to ensure we are working on the correct cluster.--final
is the flag that removes the update authority (makes the program immutable)
You should see the following output:
Account Type: Program
Authority: none
Awesome! Nice work. You have successfully removed the update authority of a Solana Program and made it immutable. If you would like to verify that the program is immutable, you can try to update the program using the following command:
anchor upgrade --program-id <PROGRAM_ID> <PATH_TO/solana-immutable-program>
Make sure to use the same Program Id you used in the previous step and the correct path to your program.
You should see the following output:
Error: Program <PROGRAM_ID> is no longer upgradeable
There was a problem deploying: Output { status: ExitStatus(unix_wait_status(256)), stdout: "", stderr: "" }.
You can also check your program's authority using Solana Explorer. Replace your Program_ID in the following URL and open it in your browser:
https://explorer.solana.com/address/<PROGRAM_ID>?cluster=custom&customUrl=http://localhost:8899
You should see that the program's 'Upgradeable' field is set to 'No':
Deploying to Mainnet with QuickNode
When ready, you can apply the same steps to your Program on Mainnet. Be careful! Once you remove the update authority of a program, it is immutable. You cannot update the program once the update authority is removed.
Deploying programs to mainnet is an intensive process--if you'd like 8x faster response times, you can leave the heavy lifting to us. See why over 50% of projects on Solana choose QuickNode and sign up for a free account here. To use your QuickNode endpoint with this guide, simply replace localhost
with your QuickNode endpoint in the -u and --url
flags above, e.g.:
solana program set-upgrade-authority <PROGRAM_ID> -u <YOUR_QUICKNODE_ENDPOINT> --final
Wrap Up
Great job! You have successfully deployed an immutable Solana program to localhost. You can shut down your local validator by entering ctrl (or cmd) + c
in your validator terminal.
Got questions? Or just want to share what you are working on? Drop us a line in the QuickNode Discord or on Twitter, and we will be happy to help you out!
We <3 Feedback!
If you have any feedback or questions on this guide, let us know here! We'd love to hear from you!