@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
72 lines • 2.58 kB
JavaScript
/**
* @purpose InstructionResult - Array-like wrapper for @solana/kit Instructions with legacy conversion
*
* Provides native @solana/kit Instruction[] compatibility with optional .toLegacy() conversion
* for web3.js users.
*/
import { getSdkConfig } from './config';
/**
* Convert a single @solana/kit Instruction to legacy web3.js format
*/
function convertToLegacy(instruction, PublicKey) {
const accounts = instruction.accounts || [];
const data = instruction.data || new Uint8Array();
return {
keys: accounts.map(account => ({
pubkey: new PublicKey(String(account.address)),
isWritable: (account.role & 1) === 1, // Check writable bit
isSigner: (account.role & 2) === 2, // Check signer bit
})),
programId: new PublicKey(String(instruction.programAddress)),
data,
};
}
/**
* InstructionResult - Wrapper for @solana/kit Instructions with legacy conversion
*
* Contains a plain Instruction[] array accessible via `.instructions`.
* Supports iteration (`for...of`, spread) and `.toLegacy()` conversion for web3.js.
*
* @example
* ```typescript
* // Kit users - access instructions directly
* const result = await createContract(params);
* for (const ix of result) { ... }
* // Or unwrap: result.instructions
*
* // Web3.js users - convert to legacy format
* import { PublicKey } from '@solana/web3.js';
* const legacyIxs = result.toLegacy(PublicKey);
* // Or if PublicKey is configured in SDK:
* setSdkConfig({ PublicKey });
* const legacyIxs = result.toLegacy();
* ```
*/
export class InstructionResult {
instructions;
constructor(instructions) {
this.instructions = instructions;
}
/**
* Convert to legacy web3.js instruction format
*
* @param PublicKey - Optional PublicKey constructor. If not provided,
* uses the PublicKey from SDK config (set via setSdkConfig)
* @returns Array of instructions in web3.js format
* @throws Error if no PublicKey is available
*/
toLegacy(PublicKey) {
const pk = PublicKey || getSdkConfig().PublicKey;
if (!pk) {
throw new Error('PublicKey constructor required - pass it to toLegacy() or set in SDK config via setSdkConfig({ PublicKey })');
}
return this.instructions.map(ix => convertToLegacy(ix, pk));
}
get length() {
return this.instructions.length;
}
[Symbol.iterator]() {
return this.instructions[Symbol.iterator]();
}
}
//# sourceMappingURL=instructionResult.js.map