Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getAccountsWithOptionalFeePayer #184

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/solana/lib/src/encoder/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,22 @@ extension InstructionListExt on List<Instruction> {
List<AccountMeta> getAccountsWithOptionalFeePayer({
String? feePayer,
}) {
final programIds = <String>[];
final accounts = expand<AccountMeta>(
(Instruction instruction) => [
...instruction.accounts,

/// Append the instruction program id
AccountMeta.readonly(pubKey: instruction.programId, isSigner: false),
],
(Instruction instruction) {
/// Store instruction programId
if (!programIds.contains(instruction.programId)) {
programIds.add(instruction.programId);
}
return instruction.accounts;
},
).toList();

/// Append programIds to accounts (at the end)
for (final programId in programIds) {
accounts.add(AccountMeta.readonly(pubKey: programId, isSigner: false));
}

if (feePayer != null) {
final index = accounts.indexWhere(
(AccountMeta account) => account.pubKey == feePayer,
Expand Down
43 changes: 43 additions & 0 deletions packages/solana/test/instruction_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:solana/encoder.dart';
import 'package:test/test.dart';

void main() {
test('Account metas are correctly sorted', () {
final accounts = _instructionList.getAccountsWithOptionalFeePayer(
feePayer: '9KaA7vEBUdRCcBWxfuMjxYwKfvu8Us3Cg5gkhVFt2LNk');
for (var i = 0; i < accounts.length; i++) {
expect(accounts[i].pubKey, equals(_expectedResult[i]));
}
});
}

final _instructionList = [
Instruction(
programId: '6KR4qkJN91LGko2gdizheri8LMtCwsJrhtsQt6QPwCi5',
accounts: [
AccountMeta(
pubKey: 'JBm16mKFNXotwiYjdGuVJcLBeuJdQkT84aNBNhnakToW',
isWriteable: false,
isSigner: false),
],
data: [],
),
Instruction(
programId: 'DAz7SnxRL2HRZ2GPigdVRGkoF39bD4Qdv8td5wD3i35u',
accounts: [
AccountMeta(
pubKey: '2qXtx3xmmmJyA64vrszWAqSXEvYaQNAo1yL4tCxBYPZv',
isWriteable: false,
isSigner: false),
],
data: [],
),
];

final _expectedResult = [
'9KaA7vEBUdRCcBWxfuMjxYwKfvu8Us3Cg5gkhVFt2LNk',
'JBm16mKFNXotwiYjdGuVJcLBeuJdQkT84aNBNhnakToW',
'2qXtx3xmmmJyA64vrszWAqSXEvYaQNAo1yL4tCxBYPZv',
'6KR4qkJN91LGko2gdizheri8LMtCwsJrhtsQt6QPwCi5',
'DAz7SnxRL2HRZ2GPigdVRGkoF39bD4Qdv8td5wD3i35u',
];