|
@@ -1,3 +1,4 @@
|
|
|
|
+use crate::error::ErrorCode;
|
|
use crate::{
|
|
use crate::{
|
|
Accounts, AccountsExit, AccountsInit, ToAccountInfo, ToAccountInfos, ToAccountMetas, ZeroCopy,
|
|
Accounts, AccountsExit, AccountsInit, ToAccountInfo, ToAccountInfos, ToAccountMetas, ZeroCopy,
|
|
};
|
|
};
|
|
@@ -44,7 +45,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
let mut disc_bytes = [0u8; 8];
|
|
let mut disc_bytes = [0u8; 8];
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
if disc_bytes != T::discriminator() {
|
|
if disc_bytes != T::discriminator() {
|
|
- return Err(ProgramError::InvalidAccountData);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
}
|
|
}
|
|
|
|
|
|
Ok(Loader::new(acc_info.clone()))
|
|
Ok(Loader::new(acc_info.clone()))
|
|
@@ -60,7 +61,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
let discriminator = u64::from_le_bytes(disc_bytes);
|
|
let discriminator = u64::from_le_bytes(disc_bytes);
|
|
if discriminator != 0 {
|
|
if discriminator != 0 {
|
|
- return Err(ProgramError::InvalidAccountData);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountDiscriminatorAlreadySet.into());
|
|
}
|
|
}
|
|
|
|
|
|
Ok(Loader::new(acc_info.clone()))
|
|
Ok(Loader::new(acc_info.clone()))
|
|
@@ -73,7 +74,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
let mut disc_bytes = [0u8; 8];
|
|
let mut disc_bytes = [0u8; 8];
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
if disc_bytes != T::discriminator() {
|
|
if disc_bytes != T::discriminator() {
|
|
- return Err(ProgramError::InvalidAccountData);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
}
|
|
}
|
|
|
|
|
|
Ok(Ref::map(data, |data| bytemuck::from_bytes(&data[8..])))
|
|
Ok(Ref::map(data, |data| bytemuck::from_bytes(&data[8..])))
|
|
@@ -84,7 +85,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
// AccountInfo api allows you to borrow mut even if the account isn't
|
|
// AccountInfo api allows you to borrow mut even if the account isn't
|
|
// writable, so add this check for a better dev experience.
|
|
// writable, so add this check for a better dev experience.
|
|
if !self.acc_info.is_writable {
|
|
if !self.acc_info.is_writable {
|
|
- return Err(ProgramError::Custom(87)); // todo: proper error
|
|
|
|
|
|
+ return Err(ErrorCode::AccountNotMutable.into());
|
|
}
|
|
}
|
|
|
|
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
@@ -92,7 +93,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
let mut disc_bytes = [0u8; 8];
|
|
let mut disc_bytes = [0u8; 8];
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
if disc_bytes != T::discriminator() {
|
|
if disc_bytes != T::discriminator() {
|
|
- return Err(ProgramError::InvalidAccountData);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
}
|
|
}
|
|
|
|
|
|
Ok(RefMut::map(data, |data| {
|
|
Ok(RefMut::map(data, |data| {
|
|
@@ -106,7 +107,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
// AccountInfo api allows you to borrow mut even if the account isn't
|
|
// AccountInfo api allows you to borrow mut even if the account isn't
|
|
// writable, so add this check for a better dev experience.
|
|
// writable, so add this check for a better dev experience.
|
|
if !self.acc_info.is_writable {
|
|
if !self.acc_info.is_writable {
|
|
- return Err(ProgramError::Custom(87)); // todo: proper error
|
|
|
|
|
|
+ return Err(ErrorCode::AccountNotMutable.into());
|
|
}
|
|
}
|
|
|
|
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
@@ -116,7 +117,7 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
let discriminator = u64::from_le_bytes(disc_bytes);
|
|
let discriminator = u64::from_le_bytes(disc_bytes);
|
|
if discriminator != 0 {
|
|
if discriminator != 0 {
|
|
- return Err(ProgramError::InvalidAccountData);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountDiscriminatorAlreadySet.into());
|
|
}
|
|
}
|
|
|
|
|
|
Ok(RefMut::map(data, |data| {
|
|
Ok(RefMut::map(data, |data| {
|
|
@@ -132,13 +133,13 @@ impl<'info, T: ZeroCopy> Accounts<'info> for Loader<'info, T> {
|
|
accounts: &mut &[AccountInfo<'info>],
|
|
accounts: &mut &[AccountInfo<'info>],
|
|
) -> Result<Self, ProgramError> {
|
|
) -> Result<Self, ProgramError> {
|
|
if accounts.is_empty() {
|
|
if accounts.is_empty() {
|
|
- return Err(ProgramError::NotEnoughAccountKeys);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountNotEnoughKeys.into());
|
|
}
|
|
}
|
|
let account = &accounts[0];
|
|
let account = &accounts[0];
|
|
*accounts = &accounts[1..];
|
|
*accounts = &accounts[1..];
|
|
let l = Loader::try_from(account)?;
|
|
let l = Loader::try_from(account)?;
|
|
if l.acc_info.owner != program_id {
|
|
if l.acc_info.owner != program_id {
|
|
- return Err(ProgramError::Custom(1)); // todo: proper error
|
|
|
|
|
|
+ return Err(ErrorCode::AccountNotProgramOwned.into());
|
|
}
|
|
}
|
|
Ok(l)
|
|
Ok(l)
|
|
}
|
|
}
|
|
@@ -151,13 +152,13 @@ impl<'info, T: ZeroCopy> AccountsInit<'info> for Loader<'info, T> {
|
|
accounts: &mut &[AccountInfo<'info>],
|
|
accounts: &mut &[AccountInfo<'info>],
|
|
) -> Result<Self, ProgramError> {
|
|
) -> Result<Self, ProgramError> {
|
|
if accounts.is_empty() {
|
|
if accounts.is_empty() {
|
|
- return Err(ProgramError::NotEnoughAccountKeys);
|
|
|
|
|
|
+ return Err(ErrorCode::AccountNotEnoughKeys.into());
|
|
}
|
|
}
|
|
let account = &accounts[0];
|
|
let account = &accounts[0];
|
|
*accounts = &accounts[1..];
|
|
*accounts = &accounts[1..];
|
|
let l = Loader::try_from_init(account)?;
|
|
let l = Loader::try_from_init(account)?;
|
|
if l.acc_info.owner != program_id {
|
|
if l.acc_info.owner != program_id {
|
|
- return Err(ProgramError::Custom(1)); // todo: proper error
|
|
|
|
|
|
+ return Err(ErrorCode::AccountNotProgramOwned.into());
|
|
}
|
|
}
|
|
Ok(l)
|
|
Ok(l)
|
|
}
|
|
}
|