fix: fmt and lints

This commit is contained in:
Daniil Polyakov 2025-12-25 17:05:06 +03:00
parent e81d7f099d
commit de751952af

View File

@ -6,54 +6,52 @@ use nssa_core::{
}; };
// The token program has three functions: // The token program has three functions:
// 1. New token definition. // 1. New token definition. Arguments to this function are:
// Arguments to this function are: // * Two **default** accounts: [definition_account, holding_account]. The first default account
// * Two **default** accounts: [definition_account, holding_account]. // will be initialized with the token definition account values. The second account will be
// The first default account will be initialized with the token definition account values. The second account will // initialized to a token holding account for the new token, holding the entire total supply.
// be initialized to a token holding account for the new token, holding the entire total supply. // * An instruction data of 23-bytes, indicating the total supply and the token name, with the
// * An instruction data of 23-bytes, indicating the total supply and the token name, with // following layout: [0x00 || total_supply (little-endian 16 bytes) || name (6 bytes)] The
// the following layout: // name cannot be equal to [0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
// [0x00 || total_supply (little-endian 16 bytes) || name (6 bytes)] // 2. Token transfer Arguments to this function are:
// The name cannot be equal to [0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
// 2. Token transfer
// Arguments to this function are:
// * Two accounts: [sender_account, recipient_account]. // * Two accounts: [sender_account, recipient_account].
// * An instruction data byte string of length 23, indicating the total supply with the following layout // * An instruction data byte string of length 23, indicating the total supply with the
// [0x01 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00 || 0x00 || 0x00]. // following layout [0x01 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00
// 3. Initialize account with zero balance // || 0x00 || 0x00].
// Arguments to this function are: // 3. Initialize account with zero balance Arguments to this function are:
// * Two accounts: [definition_account, account_to_initialize]. // * Two accounts: [definition_account, account_to_initialize].
// * An dummy byte string of length 23, with the following layout // * An dummy byte string of length 23, with the following layout [0x02 || 0x00 || 0x00 || 0x00
// [0x02 || 0x00 || 0x00 || 0x00 || ... || 0x00 || 0x00]. // || ... || 0x00 || 0x00].
// 4. Burn tokens from a Token Holding account (thus lowering total supply) // 4. Burn tokens from a Token Holding account (thus lowering total supply) Arguments to this
// Arguments to this function are: // function are:
// * Two accounts: [definition_account, holding_account]. // * Two accounts: [definition_account, holding_account].
// * Authorization required: holding_account // * Authorization required: holding_account
// * An instruction data byte string of length 23, indicating the balance to burn with the folloiwng layout // * An instruction data byte string of length 23, indicating the balance to burn with the
// folloiwng layout
// [0x03 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00 || 0x00 || 0x00]. // [0x03 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00 || 0x00 || 0x00].
// 5. Mint additional supply of tokens tokens to a Token Holding account (thus increasing total supply) // 5. Mint additional supply of tokens tokens to a Token Holding account (thus increasing total
// Arguments to this function are: // supply) Arguments to this function are:
// * Two accounts: [definition_account, holding_account]. // * Two accounts: [definition_account, holding_account].
// * Authorization required: definition_account // * Authorization required: definition_account
// * An instruction data byte string of length 23, indicating the balance to mint with the folloiwng layout // * An instruction data byte string of length 23, indicating the balance to mint with the
// folloiwng layout
// [0x04 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00 || 0x00 || 0x00]. // [0x04 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00 || 0x00 || 0x00].
// 6. New token definition with metadata. // 6. New token definition with metadata. Arguments to this function are:
// Arguments to this function are: // * Three **default** accounts: [definition_account, metadata_account. holding_account]. The
// * Three **default** accounts: [definition_account, metadata_account. holding_account]. // first default account will be initialized with the token definition account values. The
// The first default account will be initialized with the token definition account values. The second account // second account will be initialized to a token metadata account for the new token
// will be initialized to a token metadata account for the new token definition. The third account will be // definition. The third account will be initialized to a token holding account for the new
// initialized to a token holding account for the new token, holding the entire total supply. // token, holding the entire total supply.
// * An instruction data of 474-bytes, indicating the token name, total supply, token standard, metadata standard // * An instruction data of 474-bytes, indicating the token name, total supply, token standard,
// and metadata_values (uri and creators). // metadata standard and metadata_values (uri and creators). the following layout: [0x05 ||
// the following layout: // total_supply (little-endian 16 bytes) || name (6 bytes) || token_standard ||
// [0x05 || total_supply (little-endian 16 bytes) || name (6 bytes) || token_standard || metadata_standard || metadata_values] // metadata_standard || metadata_values] The name cannot be equal to [0x00, 0x00, 0x00, 0x00,
// The name cannot be equal to [0x00, 0x00, 0x00, 0x00, 0x00, 0x00] // 0x00, 0x00]
// 7. Print NFT copy from Master NFT // 7. Print NFT copy from Master NFT Arguments to this function are:
// Arguments to this function are:
// * Two accounts: [master_nft, printed_account (default)]. // * Two accounts: [master_nft, printed_account (default)].
// * Authorization required: master_nft // * Authorization required: master_nft
// * An dummy byte string of length 23, with the following layout // * An dummy byte string of length 23, with the following layout [0x06 || 0x00 || 0x00 || 0x00
// [0x06 || 0x00 || 0x00 || 0x00 || ... || 0x00 || 0x00]. // || ... || 0x00 || 0x00].
const TOKEN_STANDARD_FUNGIBLE_TOKEN: u8 = 0; const TOKEN_STANDARD_FUNGIBLE_TOKEN: u8 = 0;
const TOKEN_STANDARD_FUNGIBLE_ASSET: u8 = 1; const TOKEN_STANDARD_FUNGIBLE_ASSET: u8 = 1;
const TOKEN_STANDARD_NONFUNGIBLE: u8 = 2; const TOKEN_STANDARD_NONFUNGIBLE: u8 = 2;
@ -243,41 +241,6 @@ impl TokenMetadata {
Data::try_from(bytes).expect("Invalid data") Data::try_from(bytes).expect("Invalid data")
} }
fn parse(data: &Data) -> Option<Self> {
let data = Vec::<u8>::from(data.clone());
if data.len() != TOKEN_METADATA_DATA_SIZE || !is_metadata_type_valid(data[0]) {
None
} else {
let account_type = data[0];
let version = data[1];
let definition_id = AccountId::new(
data[2..34]
.try_into()
.expect("Token Program expects valid Account Id for Metadata"),
);
let uri: [u8; 200] = data[34..234]
.try_into()
.expect("Token Program expects valid uri for Metadata");
let creators: [u8; 250] = data[234..484]
.try_into()
.expect("Token Program expects valid creators for Metadata");
let primary_sale_date = u64::from_le_bytes(
data[484..TOKEN_METADATA_DATA_SIZE]
.try_into()
.expect("Token Program expects valid blockid for Metadata"),
);
Some(Self {
account_type,
version,
definition_id,
uri,
creators,
primary_sale_date,
})
}
}
} }
fn transfer(pre_states: &[AccountWithMetadata], balance_to_move: u128) -> Vec<AccountPostState> { fn transfer(pre_states: &[AccountWithMetadata], balance_to_move: u128) -> Vec<AccountPostState> {
@ -495,7 +458,7 @@ fn new_definition_with_metadata(
definition_id: definition_target_account.account_id, definition_id: definition_target_account.account_id,
uri, uri,
creators, creators,
primary_sale_date: 0u64, //TODO: future works to implement this primary_sale_date: 0u64, // TODO: future works to implement this
}; };
let mut definition_target_account_post = definition_target_account.account.clone(); let mut definition_target_account_post = definition_target_account.account.clone();
@ -862,10 +825,9 @@ mod tests {
use crate::{ use crate::{
TOKEN_DEFINITION_DATA_SIZE, TOKEN_HOLDING_DATA_SIZE, TOKEN_HOLDING_NFT_MASTER, TOKEN_DEFINITION_DATA_SIZE, TOKEN_HOLDING_DATA_SIZE, TOKEN_HOLDING_NFT_MASTER,
TOKEN_HOLDING_NFT_PRINTED_COPY, TOKEN_HOLDING_STANDARD, TOKEN_HOLDING_NFT_PRINTED_COPY, TOKEN_HOLDING_STANDARD, TOKEN_STANDARD_FUNGIBLE_TOKEN,
TOKEN_STANDARD_FUNGIBLE_TOKEN, TOKEN_STANDARD_NONFUNGIBLE, TokenDefinition, TokenHolding, TOKEN_STANDARD_NONFUNGIBLE, TokenDefinition, TokenHolding, burn, mint_additional_supply,
burn, mint_additional_supply, new_definition, new_definition, new_definition_with_metadata, print_nft, transfer,
new_definition_with_metadata, print_nft, transfer,
}; };
struct BalanceForTests; struct BalanceForTests;
@ -1054,6 +1016,7 @@ mod tests {
account_id: IdForTests::pool_definition_id(), account_id: IdForTests::pool_definition_id(),
} }
} }
fn definition_account_mint() -> AccountWithMetadata { fn definition_account_mint() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account { account: Account {
@ -1071,6 +1034,7 @@ mod tests {
account_id: IdForTests::pool_definition_id(), account_id: IdForTests::pool_definition_id(),
} }
} }
fn holding_same_definition_with_authorization_and_large_balance() -> AccountWithMetadata { fn holding_same_definition_with_authorization_and_large_balance() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account { account: Account {
@ -1087,6 +1051,7 @@ mod tests {
account_id: IdForTests::pool_definition_id(), account_id: IdForTests::pool_definition_id(),
} }
} }
fn definition_account_with_authorization_nonfungible() -> AccountWithMetadata { fn definition_account_with_authorization_nonfungible() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account { account: Account {
@ -1104,6 +1069,7 @@ mod tests {
account_id: IdForTests::pool_definition_id(), account_id: IdForTests::pool_definition_id(),
} }
} }
fn definition_account_uninit() -> AccountWithMetadata { fn definition_account_uninit() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account::default(), account: Account::default(),
@ -1128,6 +1094,7 @@ mod tests {
account_id: IdForTests::holding_id(), account_id: IdForTests::holding_id(),
} }
} }
fn definition_account_unclaimed() -> AccountWithMetadata { fn definition_account_unclaimed() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account { account: Account {
@ -1145,6 +1112,7 @@ mod tests {
account_id: IdForTests::pool_definition_id(), account_id: IdForTests::pool_definition_id(),
} }
} }
fn holding_account_unclaimed() -> AccountWithMetadata { fn holding_account_unclaimed() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account { account: Account {
@ -1213,23 +1181,6 @@ mod tests {
} }
} }
fn holding_account2_uninit_post_transfer() -> AccountWithMetadata {
AccountWithMetadata {
account: Account {
program_owner: [0u32; 8],
balance: 0u128,
data: TokenHolding::into_data(TokenHolding {
account_type: TOKEN_HOLDING_STANDARD,
definition_id: IdForTests::pool_definition_id(),
balance: BalanceForTests::recipient_uninit_post_transfer(),
}),
nonce: 0,
},
is_authorized: true,
account_id: IdForTests::holding_id_2(),
}
}
fn holding_account_master_nft() -> AccountWithMetadata { fn holding_account_master_nft() -> AccountWithMetadata {
AccountWithMetadata { AccountWithMetadata {
account: Account { account: Account {
@ -1382,10 +1333,6 @@ mod tests {
105_000 105_000
} }
fn recipient_uninit_post_transfer() -> u128 {
5_000
}
fn transfer_amount() -> u128 { fn transfer_amount() -> u128 {
5_000 5_000
} }
@ -1411,10 +1358,6 @@ mod tests {
fn holding_id_2() -> AccountId { fn holding_id_2() -> AccountId {
AccountId::new([42; 32]) AccountId::new([42; 32])
} }
fn metadata_id() -> AccountId {
AccountId::new([31; 32])
}
} }
#[should_panic(expected = "Invalid number of input accounts")] #[should_panic(expected = "Invalid number of input accounts")]
@ -1688,7 +1631,7 @@ mod tests {
AccountForTests::holding_account_master_nft(), AccountForTests::holding_account_master_nft(),
AccountForTests::holding_account_uninit(), AccountForTests::holding_account_uninit(),
]; ];
let post_states = transfer(&pre_states, BalanceForTests::transfer_amount()); let _post_states = transfer(&pre_states, BalanceForTests::transfer_amount());
} }
#[should_panic(expected = "Invalid balance in recipient account for NFT transfer")] #[should_panic(expected = "Invalid balance in recipient account for NFT transfer")]