9

I want to deployed a program to main net would like to figure out before hand how much it will cost. For accounts I can just calculate it by counting the bytes to be rent exempt but how do I do that for programs? Thank you

Edit: I want be be able to calculate the size of the Program Executable Data Account that needs to be created to save the byte code of a new programm so that I can calculate how much the rent for that account will cost. For the hello world example that is 0.62905176 Sol and the .so file is 45Kb which should result in only about half with cost 0.3216077 sol.

2 Answers 2

11

What About Deployments Costs SOL?

When you deploy a program, you're actually initializing two accounts.

  1. The program data account, whose address came from the key created in target/debug/<name-of-your-program>-keypair.json the first time you ran anchor build. This account stores information about your program on-chain, but not the program itself. For example, this account stores your program's upgrade authority. It also stores a reference to the program buffer account.
  2. The program buffer account. This is where your actual .so file data lives. Unless you deploy using really low-level command-line legos, then this buffer account will be configured twice the size of the initially upload file. Why twice the size? This gives you ample headroom for upgrades.

The address in (1) is the "familiar" address of your program, and doesn't cost very many lamports. The address in (2) is the overwhelming majority of the cost of deployment.

Some Rough Math

First you need to know the size of your program. This command will tell you.

$ du -b target/deploy/<your-program-to-deploy>.so

That will spit out the size in bytes, let's call it n. Last I checked, a standard Anchor deployment actually sized the buffer account to 2n + 45 bytes.

Figure out that number, let's call it N. Run solana rent on it:

$ solana rent <N>

That will tell you the amount of SOL you need for your program's buffer account.

Real World Example

Assume a program of 363,960 bytes (approximately 363KB).

  1. Storing the Program –- 2(363,960) + 45 = 727,965 bytes.
$ solana rent 727965
Rent per byte-year: 0.00000348 SOL
Rent per epoch: 0.013874429 SOL
Rent-exempt minimum: 5.06752728 SOL
  1. Uploading the Program – The program is 363KB, but we can only send transactions of just over a kilobyte in size (I believe 1232 bytes maximum? someone please correct me if I'm wrong), so the actual program upload takes many transactions to perform. Let's assume 1KB per transaction which is generous because the remaining instruction data certainly shouldn't take up the remaining 230 bytes. So 363 transactions to upload, but let's round up to 400 transactions for easy math, so 400 * 5,000 = 2,000,000 lamports, or two Megaports, or 0.002 SOL for the transactions required to upload it.

  2. Referencing the Program – Another roughly 0.001 SOL for the program data account that we mentioned before. Let’s round that up to 0.002 as well.

So in total, to deploy this 363KB program will cost roughly 5.0679 SOL. I'm sure I probably forgot to account for some lamports somewhere, but nothing big.

Edit:

If you want to spare yourself some lamports by reducing the size of the buffer account, this is possible, but not easy. There is a bpf_loader_upgradeable::create_buffer instruction, but the Solana CLI doesn't expose it as its own command (instead, it just lumps the several program instructions related to deployment into one CLI command). You will have to roll your own code solution to issue that transaction, and then issue the write_buffer instruction after that. If you are intent on going down this path, I suggest reading the source code to the bpf_loader_upgradeable program here: https://github.com/solana-labs/solana/blob/master/sdk/program/src/bpf_loader_upgradeable.rs

1
  • 1
    Amazing answer, thank you. Can i configure the 2x headroom somehow? I dont want to spend 200dollar to upload my program. I am already not using anchor because it ads so much to the program size.
    – Jonas H.
    Commented Aug 16, 2022 at 7:54
3

Solana Instructions have a fixed fee of 5000 Lamports (currently) A Transaction can have multiple instructions.

But Data Allocation cost is calculated

Solana defines a constant cost as 1 byte per year and being rent exempt if you exceed 2 years. so

/// default rental rate in lamports/byte-year:
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR: u64 = 1_000_000_000 / 100 * 365 / (1024 * 1024);


/// default amount of time (in years) the balance has to include rent for:
pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;

Allocating 500 bytes will cost 500 * DEFAULT_LAMPORTS_PER_BYTES_YEAR * DEFAULT_EXEMPTION_THRESHOLD Lamports

Plus 5000 Lamports for the instruction

which is around ~0.007 Sol

you can do the same to calculate the fee for creating a token account (165 bytes) mint etc... You can automate this process with getMinimumBalanceForRentExemption


source

another example: How much sol is required to mint 1000 items using sugar candy machine?

3
  • My question was about the costs to deploy a program. Not about how rent exemption works. Do i need to calculate getMinimumBalanceForRentExemption for the size of the .so file of the program for maybe?
    – Jonas H.
    Commented Aug 14, 2022 at 11:04
  • 2
    Programs are accounts that are marked as "executable", the logic is the same. This blog explains program deploys in detail: jstarry.notion.site/…
    – C.OG
    Commented Aug 14, 2022 at 11:22
  • I know that everything is an account :D I guess my question was not clear enough sorry. How can i find out the size of the executable byte array of a programm account so i can calculate its rent exempt cost. My guess is its the size of the .so file?
    – Jonas H.
    Commented Aug 14, 2022 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.