findProgramAddress static method

(SolAddress, int) findProgramAddress({
  1. required List<List<int>> seeds,
  2. required SolAddress programId,
})

Converts a Solana value to lamports.

Implementation

static (SolAddress, int) findProgramAddress({
  required List<List<int>> seeds,
  required SolAddress programId,
}) {
  int nonce = 255;
  List<int> seedBytes = [];
  for (final i in seeds) {
    if (i.length > maxSeedLength) {
      throw const SolanaPluginException('Max seed length exceeded');
    }
    seedBytes = [...seedBytes, ...i];
  }
  while (nonce != 0) {
    try {
      final List<int> seedsWithNonce = [...seedBytes, nonce];
      final addr = createProgramAddress(
        seedBytes: seedsWithNonce,
        programId: programId,
      );
      return (addr, nonce);
    } catch (e) {
      nonce--;
      continue;
    }
  }
  throw const SolanaPluginException(
    'Unable to find a viable program address nonce',
  );
}