@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
55 lines • 2 kB
JavaScript
/**
* Address Lookup Table (ALT) derivation utilities
* @module pda/lookupTable
*/
import { address, getProgramDerivedAddress, getAddressEncoder } from '@solana/kit';
import { getSignerAddress } from '../utils/signer';
/**
* Address Lookup Table program address
*/
const ALT_PROGRAM = 'AddressLookupTab1e1111111111111111111111111';
const addressEncoder = getAddressEncoder();
/**
* Convert a BigInt or number slot to little-endian bytes (u64)
*/
function slotToBytes(slot) {
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setBigUint64(0, BigInt(slot), true); // little-endian
return new Uint8Array(buffer);
}
/**
* Derive an Address Lookup Table (ALT) address
*
* ALT addresses are deterministically derived from an authority and a recent slot.
* The slot is typically obtained from getSlot() RPC call.
*
* @param authority - The authority that will own the lookup table
* @param recentSlot - A recent slot number (from getSlot RPC)
* @param altProgramAddress - Optional ALT program address (defaults to mainnet)
* @returns The derived lookup table address
*
* @example
* ```typescript
* import { deriveLookupTable } from './lookupTable';
*
* // Get recent slot from RPC
* const slot = await rpc.getSlot().send();
*
* // Derive the lookup table address
* const lookupTable = await deriveLookupTable(authorityAddress, slot);
* ```
*/
export async function deriveLookupTable(authority, recentSlot, altProgramAddress) {
const authorityAddress = getSignerAddress(authority);
const altProgram = altProgramAddress ? getSignerAddress(altProgramAddress) : address(ALT_PROGRAM);
// Encode authority and slot to bytes
const authorityBytes = addressEncoder.encode(authorityAddress);
const slotBytes = slotToBytes(recentSlot);
const [pda] = await getProgramDerivedAddress({
programAddress: altProgram,
seeds: [authorityBytes, slotBytes],
});
return pda;
}
//# sourceMappingURL=lookupTable.js.map