Assertions
Assert Account Data
AssertAccountData Instruction
The AssertAccountData instruction gives you tools to make assertions about arbitrary account data by deserializing account data slices into a specified type and comparing it to an expected value.
The assertion
argument for the instruction is the DataValueAssertion
enum which allows you to describe the type you would like to deserialize the account data into and the expected value and operator used in the comparison.
Supported types: bool
, u8
, i8
, u16
, i16
, u32
, i32
, u64
, i64
, u128
, i128
, bytes
, Pubkey
.
Example: Asserting on integer values in account data.
Imagine a struct with the following layout:
pub struct TestAccount {
pub account_discriminator: [u8; 8], // bytes 0 to 7
pub balance: u64, // bytes 8 to 16
}
Say we wanted to assert on the balance
field in our TestAccount
struct. If the struct uses a serialization schema that uses little-endian for integers (borsh, bytemuck, ...) we can deserialize the field and assert on that value at runtime!
Data value assertion instruction builder example
const ix = getAssertAccountDataInstruction({
targetAccount,
assertion: dataValueAssertion('U64', {
value: 420,
operator: IntegerOperator.GreaterThanOrEqual,
}),
offset: 8,
})
Example: Equality assertion on pubkey in account data.
Imagine we expand the struct from before and add a pubkey field.
pub struct TestAccount {
pub account_discriminator: [u8; 8], // bytes 0 to 7
pub balance: u64, // bytes 8 to 16
pub owner: Pubkey, // bytes 16 to 48
}
Say we wanted to assert on the owner
field in our TestAccount
struct. We can deserialize the Pubkey and assert using the example code below.
Data value assertion instruction builder example
const ix = getAssertAccountDataInstruction({
targetAccount,
assertion: dataValueAssertion('Pubkey', {
value: ownerPubkey,
operator: 'Equal',
}),
offset: 16,
})