project-serum / serum-dex

Project Serum Rust Monorepo
https://projectserum.com
Apache License 2.0
629 stars 329 forks source link

Non standard nonce usage for program derived address #107

Open armaniferrante opened 3 years ago

armaniferrante commented 3 years ago

A u64 used for the nonce/bump seed, rather than a u8. This is inconsistent with the rest of the solana tooling, creating confusion when integrating with the dex. See https://github.com/project-serum/serum-dex/blob/master/dex/src/state.rs#L1154.

armaniferrante commented 3 years ago

I think this is pretty important to do since it adds unnecessary boilerplate when composing with the dex, since the standard tooling for creating nonce (e.g., Pubkey.findProgramAddress) doesn't work and so one has to create their own nonce generation function, e.g.,

  async function getVaultOwnerAndNonce(market) {
    const nonce = new BN(0);
    while (true) {
      try {
        const vaultOwner = await PublicKey.createProgramAddress(
          [market.publicKey.toBuffer(), nonce.toArrayLike(Buffer, "le", 8)],
          dexProgramId
        );
        return [vaultOwner, nonce];
      } catch (e) {
        nonce.iaddn(1);
      }
    }
  }