I have the following Rust code that structured a Solana account and test cases from another file using typescript:
const DISCRIMINATOR_LENGTH: usize = 8;
const PUBLIC_KEY_LENGTH: usize = 32;
const TIMESTAMP_LENGTH: usize = 8;
const STRING_LENGTH_PREFIX: usize = 4; // Stores the size of the string.
const MAX_FIRST_NAME_LENGTH: usize = 30 * 4; // 30 chars max.
const MAX_LAST_NAME_LENGTH: usize = 30 * 4; // 30 chars max.
const MAX_EMAIL_LENGTH: usize = 20 * 4; // 20 chars max.
const MAX_PHONE_LENGTH: usize = 10 * 4;//10 char max
#[account]
pub struct Contact {
pub author: Pubkey,
pub timestamp: i64,
pub first_name: String,
pub last_name: String,
pub email: String,
pub phone: String,
}
impl Contact {
const LEN: usize = DISCRIMINATOR_LENGTH
+ PUBLIC_KEY_LENGTH // Author.
+ TIMESTAMP_LENGTH // Timestamp.
+ STRING_LENGTH_PREFIX + MAX_FIRST_NAME_LENGTH // first Name.
+ STRING_LENGTH_PREFIX + MAX_LAST_NAME_LENGTH // last Name.
+ STRING_LENGTH_PREFIX + MAX_EMAIL_LENGTH // Email.
+ STRING_LENGTH_PREFIX + MAX_PHONE_LENGTH; //phone
}
it('can filter tweets by first name', async () => {
//const author = program.provider.wallet.publicKey;
const name = bs58.encode(Buffer.from('Peter'));
const contactAccounts = await program.account.contact.all([
{
memcmp: {
offset: 52,
bytes: name,
}
}
]);
assert.equal(contactAccounts.length, 1);
assert.ok(contactAccounts.every(contactAccount => {
return contactAccount.account.firstName==='Peter'
}))
});
Finding the first name at the 52'th location is working but when I try to find the last name location @ 176 bytes (52+120+4) the test case can't find any account.
What would be the exact location of the last name, or email? I have a test case that created Peter and Jordan as first and last names respectively.