91 lines
2.4 KiB
Rust
Raw Normal View History

2025-09-30 16:45:25 -03:00
use crate::{address::Address, program::ProgramId};
2025-11-19 09:00:32 +02:00
use borsh::{BorshDeserialize, BorshSerialize};
2025-08-27 16:24:20 -03:00
use serde::{Deserialize, Serialize};
2025-08-06 20:05:04 -03:00
pub type Nonce = u128;
2025-09-12 15:18:25 -03:00
pub type Data = Vec<u8>;
2025-08-06 20:05:04 -03:00
/// Account to be used both in public and private contexts
#[derive(
Serialize, Deserialize, Clone, Default, PartialEq, Eq, BorshSerialize, BorshDeserialize,
)]
#[cfg_attr(any(feature = "host", test), derive(Debug))]
2025-08-06 20:05:04 -03:00
pub struct Account {
pub program_owner: ProgramId,
pub balance: u128,
pub data: Data,
pub nonce: Nonce,
}
2025-09-30 16:45:25 -03:00
pub type AccountId = Address;
2025-09-08 19:29:56 -03:00
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
2025-08-06 20:05:04 -03:00
pub struct AccountWithMetadata {
pub account: Account,
2025-09-09 17:03:58 -03:00
pub is_authorized: bool,
2025-09-12 09:18:40 -03:00
pub account_id: AccountId,
2025-08-06 20:05:04 -03:00
}
2025-09-11 16:37:28 -03:00
#[cfg(feature = "host")]
impl AccountWithMetadata {
2025-09-12 09:18:40 -03:00
pub fn new(account: Account, is_authorized: bool, account_id: impl Into<AccountId>) -> Self {
2025-09-11 16:37:28 -03:00
Self {
account,
is_authorized,
2025-09-12 09:18:40 -03:00
account_id: account_id.into(),
2025-09-11 16:37:28 -03:00
}
}
}
2025-08-08 13:32:50 -03:00
#[cfg(test)]
mod tests {
use crate::program::DEFAULT_PROGRAM_ID;
2025-08-08 13:32:50 -03:00
use super::*;
#[test]
fn test_zero_balance_account_data_creation() {
let new_acc = Account::default();
assert_eq!(new_acc.balance, 0);
}
#[test]
fn test_zero_nonce_account_data_creation() {
let new_acc = Account::default();
assert_eq!(new_acc.nonce, 0);
}
#[test]
fn test_empty_data_account_data_creation() {
let new_acc = Account::default();
2025-08-10 18:59:29 -03:00
assert!(new_acc.data.is_empty());
2025-08-08 13:32:50 -03:00
}
#[test]
fn test_default_program_owner_account_data_creation() {
let new_acc = Account::default();
assert_eq!(new_acc.program_owner, DEFAULT_PROGRAM_ID);
2025-08-06 20:05:04 -03:00
}
2025-09-11 16:37:28 -03:00
#[cfg(feature = "host")]
2025-09-11 16:37:28 -03:00
#[test]
fn test_account_with_metadata_constructor() {
let account = Account {
program_owner: [1, 2, 3, 4, 5, 6, 7, 8],
balance: 1337,
data: b"testing_account_with_metadata_constructor".to_vec(),
nonce: 0xdeadbeef,
};
2025-09-12 09:18:40 -03:00
let fingerprint = AccountId::new([8; 32]);
2025-10-03 08:08:54 -03:00
let new_acc_with_metadata = AccountWithMetadata::new(account.clone(), true, fingerprint);
2025-09-11 16:37:28 -03:00
assert_eq!(new_acc_with_metadata.account, account);
assert!(new_acc_with_metadata.is_authorized);
2025-09-12 09:18:40 -03:00
assert_eq!(new_acc_with_metadata.account_id, fingerprint);
2025-09-11 16:37:28 -03:00
}
2025-08-06 20:05:04 -03:00
}