|
1 |
| -export {}; // TODO: implement |
| 1 | +import { TokenInstruction } from './types'; |
| 2 | +import { struct, u8 } from '@solana/buffer-layout'; |
| 3 | +import { publicKey } from '@solana/buffer-layout-utils'; |
| 4 | +import { AccountMeta, PublicKey, SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js'; |
| 5 | +import { TOKEN_PROGRAM_ID } from '../constants'; |
| 6 | +import { |
| 7 | + TokenInvalidInstructionDataError, |
| 8 | + TokenInvalidInstructionKeysError, |
| 9 | + TokenInvalidInstructionProgramError, |
| 10 | + TokenInvalidInstructionTypeError, |
| 11 | +} from '../errors'; |
| 12 | + |
| 13 | +export interface InitializeAccount3InstructionData { |
| 14 | + instruction: TokenInstruction.InitializeAccount3; |
| 15 | + owner: PublicKey; |
| 16 | +} |
| 17 | + |
| 18 | +export const initializeAccount3InstructionData = struct<InitializeAccount3InstructionData>([ |
| 19 | + u8('instruction'), |
| 20 | + publicKey('owner'), |
| 21 | +]); |
| 22 | + |
| 23 | +/** |
| 24 | + * Construct an InitializeAccount3 instruction |
| 25 | + * |
| 26 | + * @param account New token account |
| 27 | + * @param mint Mint account |
| 28 | + * @param owner New account's owner/multisignature |
| 29 | + * @param programId SPL Token program account |
| 30 | + * |
| 31 | + * @return Instruction to add to a transaction |
| 32 | + */ |
| 33 | +export function createInitializeAccount3Instruction( |
| 34 | + account: PublicKey, |
| 35 | + mint: PublicKey, |
| 36 | + owner: PublicKey, |
| 37 | + programId = TOKEN_PROGRAM_ID |
| 38 | +): TransactionInstruction { |
| 39 | + const keys = [ |
| 40 | + { pubkey: account, isSigner: false, isWritable: true }, |
| 41 | + { pubkey: mint, isSigner: false, isWritable: false }, |
| 42 | + ]; |
| 43 | + const data = Buffer.alloc(initializeAccount3InstructionData.span); |
| 44 | + initializeAccount3InstructionData.encode({ instruction: TokenInstruction.InitializeAccount3, owner }, data); |
| 45 | + return new TransactionInstruction({ keys, programId, data }); |
| 46 | +} |
| 47 | + |
| 48 | +/** A decoded, valid InitializeAccount3 instruction */ |
| 49 | +export interface DecodedInitializeAccount3Instruction { |
| 50 | + programId: PublicKey; |
| 51 | + keys: { |
| 52 | + account: AccountMeta; |
| 53 | + mint: AccountMeta; |
| 54 | + }; |
| 55 | + data: { |
| 56 | + instruction: TokenInstruction.InitializeAccount3; |
| 57 | + owner: PublicKey; |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Decode an InitializeAccount3 instruction and validate it |
| 63 | + * |
| 64 | + * @param instruction Transaction instruction to decode |
| 65 | + * @param programId SPL Token program account |
| 66 | + * |
| 67 | + * @return Decoded, valid instruction |
| 68 | + */ |
| 69 | +export function decodeInitializeAccount3Instruction( |
| 70 | + instruction: TransactionInstruction, |
| 71 | + programId = TOKEN_PROGRAM_ID |
| 72 | +): DecodedInitializeAccount3Instruction { |
| 73 | + if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError(); |
| 74 | + if (instruction.data.length !== initializeAccount3InstructionData.span) |
| 75 | + throw new TokenInvalidInstructionDataError(); |
| 76 | + |
| 77 | + const { |
| 78 | + keys: { account, mint }, |
| 79 | + data, |
| 80 | + } = decodeInitializeAccount3InstructionUnchecked(instruction); |
| 81 | + if (data.instruction !== TokenInstruction.InitializeAccount3) throw new TokenInvalidInstructionTypeError(); |
| 82 | + if (!account || !mint) throw new TokenInvalidInstructionKeysError(); |
| 83 | + |
| 84 | + // TODO: key checks? |
| 85 | + |
| 86 | + return { |
| 87 | + programId, |
| 88 | + keys: { |
| 89 | + account, |
| 90 | + mint, |
| 91 | + }, |
| 92 | + data, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +/** A decoded, non-validated InitializeAccount3 instruction */ |
| 97 | +export interface DecodedInitializeAccount3InstructionUnchecked { |
| 98 | + programId: PublicKey; |
| 99 | + keys: { |
| 100 | + account: AccountMeta | undefined; |
| 101 | + mint: AccountMeta | undefined; |
| 102 | + }; |
| 103 | + data: { |
| 104 | + instruction: number; |
| 105 | + owner: PublicKey; |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Decode an InitializeAccount3 instruction without validating it |
| 111 | + * |
| 112 | + * @param instruction Transaction instruction to decode |
| 113 | + * |
| 114 | + * @return Decoded, non-validated instruction |
| 115 | + */ |
| 116 | +export function decodeInitializeAccount3InstructionUnchecked({ |
| 117 | + programId, |
| 118 | + keys: [account, mint], |
| 119 | + data, |
| 120 | +}: TransactionInstruction): DecodedInitializeAccount3InstructionUnchecked { |
| 121 | + return { |
| 122 | + programId, |
| 123 | + keys: { |
| 124 | + account, |
| 125 | + mint, |
| 126 | + }, |
| 127 | + data: initializeAccount3InstructionData.decode(data), |
| 128 | + }; |
| 129 | +} |
0 commit comments