1

So program derived addresses import { Pda } from "@metaplex-foundation/js"; seem to be Solana addresses like any other, except they have a bump associated with them - I can see then when writing unit tests and wanting to compare one Pda with an expected Pda.

What is the bump?

new Pda("2TQ464nFCwPs45wYsXXYpkhY7wgUyEVzpecWp1RBMeDU", 255),

2 Answers 2

4

I will refer to Solana web3.js but there is no difference with the metaplex SDK about the concept of PDAs.

A PDA corresponds to 1 address + 1 bump. You can have different bumps for a given address. Imagine, you have an address AbCdEf. Bumps provide variations to this address. For example:

  • AbCd, 255
  • AbCd, 253
  • etc...

Note that bump 255 does not always exist, nor does the next bump has to be 254 or 253. That's why you need to run a function called findProgramAddress to "find" the PDA, and this function goes like this:

function findProgramAddress(...) {
  let bump = 255

  while (bump != 0) {
    const address = tryCreateProgramAddressWithBump(..., bump)

    if (isValidPDA(address) {
      // We found the valid combination of address + bump
      // -> This is a valid pda 👍
      return [address, bump]
    } else {
      // that's not a valid combination 👎
      // let's try again with next bump 254, then 253, etc.
      bump--
    }
  }
}

The real source code of the function can be found here

As to what is a valid PDA, you can read more about it here or Rohan's answer

TLDR:

A PDA is the address + the variation. I picture them like colors, a PDA is the address AbCdEF and bumps are colors (variations) of that address.

So referring to a PDA without the bump would be incomplete. For example, if you tell me:

  • "the PDA is AbCd",
  • I would ask you, "which AbCd, is that AbCd 255 (blue) or AbCd 252 (red)?

I guess that's the case you are in now, you are trying to initialize a PDA but you don't know what bump to use, if you do need a PDA then:

  1. Solution 1: you need to know the bump in advance.
  2. Solution 2: you don't know the bump. Then you need to "find back" that combination of pda + seed by running findProgramAddress. Meaning you need to know what are the seeds and the program address to feed to findProgramAddress(seeds, programId);
1
  • 1
    Thanks! Why start from 255 instead of 0 though? Aug 19 at 16:38
3

Source reference for text + attached image: https://solanacookbook.com/core-concepts/pdas.html#generating-pdas

Highly recommend giving that website a look.

"To understand the concept behind PDAs, it may be helpful to consider that PDAs are not technically created, but rather found. PDAs are generated from a combination of seeds (such as the string “vote_account”) and a program id. This combination of seeds and program id is then run through a sha256 hash function to see whether or not they generate a public key that lies on the ed25519 elliptic curve.

In running our program id and seeds through a hash function, there is a ~50% chance that we actually end up with a valid public key that does lie on the elliptic curve. In this case, we simply add something to fudge our input a little bit and try again. The technical term for this fudge factor is a bump. In Solana, we start with bump = 255 and simply iterate down through bump = 254, bump = 253, etc. until we get an address that is not on the elliptic curve. This may seem rudimentary, but once found it gives us a deterministic way of deriving the same PDA over and over again."

PDA

1
  • 2
    These are both great answers - one high level with a wide audience, the other one more specific and cryptog focused. I chose the other one as it may appeal to more people but upvoted this too - thanks for the solid explanation! Aug 19 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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