getAccounts method
Prepares all the accounts from instructions:
- extracts accounts from every instruction;
- appends program id accounts (accounts with the sample public keys);
- removes duplicates (by picking signers and writeable accounts over non-writeable and non-signers);
- sorts accounts according to Account Addresses Format.
Implementation
List<AccountMeta> getAccounts({
required Ed25519HDPublicKey feePayer,
}) {
final accounts = expand<AccountMeta>(
(Instruction instruction) => [
...instruction.accounts,
/// Append the instruction program id
AccountMeta.readonly(
pubKey: instruction.programId,
isSigner: false,
),
],
).toList();
final index = accounts.indexWhere((account) => account.pubKey == feePayer);
if (index != -1) {
// If the account is already here, remove it as we are going
// to put it as the first element of the accounts array anyway
accounts.removeAt(index);
}
// The fee payer must be the first account in they "keys" provided with
// the message object
accounts.insert(
0,
AccountMeta.writeable(pubKey: feePayer, isSigner: true),
);
return accounts.unique()..sort();
}