-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathaddress-table-lookups.ts
37 lines (34 loc) · 1.44 KB
/
address-table-lookups.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Address, getAddressComparator } from '@solana/addresses';
import { AccountRole } from '@solana/instructions';
import { OrderedAccounts } from '../compile/accounts';
type AddressTableLookup = Readonly<{
/** The address of the address lookup table account. */
lookupTableAddress: Address;
/** Indices of accounts in a lookup table to load as read-only. */
readableIndices: readonly number[];
/** Indices of accounts in a lookup table to load as writable. */
writableIndices: readonly number[];
}>;
export function getCompiledAddressTableLookups(orderedAccounts: OrderedAccounts): AddressTableLookup[] {
const index: Record<Address, { readonly readableIndices: number[]; readonly writableIndices: number[] }> = {};
for (const account of orderedAccounts) {
if (!('lookupTableAddress' in account)) {
continue;
}
const entry = (index[account.lookupTableAddress] ||= {
readableIndices: [],
writableIndices: [],
});
if (account.role === AccountRole.WRITABLE) {
entry.writableIndices.push(account.addressIndex);
} else {
entry.readableIndices.push(account.addressIndex);
}
}
return Object.keys(index)
.sort(getAddressComparator())
.map(lookupTableAddress => ({
lookupTableAddress: lookupTableAddress as Address,
...index[lookupTableAddress as unknown as Address],
}));
}