Back
+10 XP
12/12
Challenge: Build a Custom Instruction
Construct a custom Solana instruction that could be used to interact with a program. You'll define the program ID, the accounts involved, and the instruction data.
Requirements
- Create a function that accepts
programId(PublicKey),payer(PublicKey),dataAccount(PublicKey), andamount(number) - Return an instruction object with:
programId: the program to callkeys: an array of AccountMeta objects (payer as signer + writable, dataAccount as writable)data: a Uint8Array containing the amount encoded as 8 bytes (little-endian u64)
- Use
DataViewto encode the amount as a little-endian 64-bit unsigned integer
Test Cases
Returns programId, keys array and Uint8Array data
Input:
programId, sender, recipient, 1000Expected: result.programId && Array.isArray(result.keys) && result.data instanceof Uint8ArrayHas at least 2 keys, first is a signer
Input:
programId, sender, recipient, 1000Expected: result.keys.length >= 2 && result.keys[0].isSigner === trueEncodes the amount as an 8-byte little-endian u64
Input:
programId, sender, recipient, 1000Expected: result.data.length === 8 && result.data[0] === 232 && result.data[1] === 3