febo 11 miesięcy temu
rodzic
commit
80883acabf

+ 6 - 5
interface/Cargo.toml

@@ -1,12 +1,13 @@
 [package]
 name = "token-interface"
 version = "0.0.0"
-edition = "2021"
+edition = { workspace = true }
 readme = "./README.md"
-license-file = "../LICENSE"
+license = { workspace = true }
+repository = { workspace = true }
 publish = false
 
 [dependencies]
-bytemuck = { version="1.18.0", features=["derive"] }
-pinocchio = "0.6"
-pinocchio-pubkey = "0.2"
+bytemuck = { workspace = true }
+pinocchio = { workspace = true }
+pinocchio-pubkey = { workspace = true }

+ 8 - 4
interface/src/state/account.rs

@@ -1,7 +1,7 @@
 use bytemuck::{Pod, Zeroable};
 use pinocchio::pubkey::Pubkey;
 
-use super::PodCOption;
+use super::{PodCOption, PodU64};
 
 /// Account data.
 #[repr(C)]
@@ -14,7 +14,7 @@ pub struct Account {
     pub owner: Pubkey,
 
     /// The amount of tokens this account holds.
-    pub amount: [u8; 8],
+    pub amount: PodU64,
 
     /// If `delegate` is `Some` then `delegated_amount` represents
     /// the amount authorized by the delegate
@@ -27,10 +27,10 @@ pub struct Account {
     /// rent-exempt reserve. An Account is required to be rent-exempt, so
     /// the value is used by the Processor to ensure that wrapped SOL
     /// accounts do not drop below this threshold.
-    pub is_native: PodCOption<[u8; 8]>,
+    pub is_native: PodCOption<PodU64>,
 
     /// The amount delegated
-    pub delegated_amount: [u8; 8],
+    pub delegated_amount: PodU64,
 
     /// Optional authority to close the account.
     pub close_authority: PodCOption<Pubkey>,
@@ -46,6 +46,10 @@ impl Account {
     pub fn is_frozen(&self) -> bool {
         self.state == AccountState::Frozen as u8
     }
+
+    pub fn amount(&self) -> u64 {
+        self.amount.into()
+    }
 }
 
 /// Account state.

+ 2 - 2
interface/src/state/mint.rs

@@ -1,7 +1,7 @@
 use bytemuck::{Pod, Zeroable};
 use pinocchio::pubkey::Pubkey;
 
-use super::{PodBool, PodCOption};
+use super::{PodBool, PodCOption, PodU64};
 
 /// Mint data.
 #[repr(C)]
@@ -14,7 +14,7 @@ pub struct Mint {
     pub mint_authority: PodCOption<Pubkey>,
 
     /// Total supply of tokens.
-    pub supply: [u8; 8],
+    pub supply: PodU64,
 
     /// Number of base 10 digits to the right of the decimal place.
     pub decimals: u8,

+ 20 - 0
interface/src/state/mod.rs

@@ -133,3 +133,23 @@ impl From<PodBool> for bool {
         b.0 != 0
     }
 }
+
+#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
+#[repr(C)]
+pub struct PodU64(pub [u8; 8]);
+
+impl PodU64 {
+    pub const fn from_primitive(n: u64) -> Self {
+        Self(n.to_le_bytes())
+    }
+}
+impl From<u64> for PodU64 {
+    fn from(n: u64) -> Self {
+        Self::from_primitive(n)
+    }
+}
+impl From<PodU64> for u64 {
+    fn from(pod: PodU64) -> Self {
+        Self::from_le_bytes(pod.0)
+    }
+}

+ 1 - 1
p-token/src/lib.rs

@@ -3,4 +3,4 @@
 mod entrypoint;
 mod processor;
 
-pinocchio_pubkey::declare_id!("TokenLight111111111111111111111111111111111");
+pinocchio_pubkey::declare_id!("Tokenocchio11111111111111111111111111111111");

+ 4 - 4
p-token/src/processor/initialize_account.rs

@@ -70,24 +70,24 @@ pub fn process_initialize_account(
     account.owner = *owner;
     account.close_authority.clear();
     account.delegate.clear();
-    account.delegated_amount = 0u64.to_le_bytes();
+    account.delegated_amount = 0u64.into();
     account.state = AccountState::Initialized as u8;
 
     if is_native_mint {
         let rent = Rent::get()?;
         let rent_exempt_reserve = rent.minimum_balance(size_of::<Account>());
 
-        account.is_native = PodCOption::from(Some(rent_exempt_reserve.to_le_bytes()));
+        account.is_native = PodCOption::from(Some(rent_exempt_reserve.into()));
         unsafe {
             account.amount = new_account_info
                 .borrow_lamports_unchecked()
                 .checked_sub(rent_exempt_reserve)
                 .ok_or(TokenError::Overflow)?
-                .to_le_bytes()
+                .into()
         }
     } else {
         account.is_native.clear();
-        account.amount = 0u64.to_le_bytes();
+        account.amount = 0u64.into();
     };
 
     Ok(())

+ 4 - 4
p-token/src/processor/mint_to.rs

@@ -57,15 +57,15 @@ pub fn process_mint_to(
         check_account_owner(program_id, destination_account_info)?;
     }
 
-    let destination_amount = u64::from_le_bytes(destination_account.amount)
+    let destination_amount = u64::from(destination_account.amount)
         .checked_add(amount)
         .ok_or(ProgramError::InvalidAccountData)?;
-    destination_account.amount = destination_amount.to_le_bytes();
+    destination_account.amount = destination_amount.into();
 
-    let mint_supply = u64::from_le_bytes(mint.supply)
+    let mint_supply = u64::from(mint.supply)
         .checked_add(amount)
         .ok_or(ProgramError::InvalidAccountData)?;
-    mint.supply = mint_supply.to_le_bytes();
+    mint.supply = mint_supply.into();
 
     Ok(())
 }

+ 6 - 6
p-token/src/processor/transfer.rs

@@ -69,7 +69,7 @@ pub fn process_transfer(
     // FEBO: Implicitly validates that the account has enough tokens by calculating the
     // remaining amount. The amount is only updated on the account if the transfer
     // is successful.
-    let remaining_amount = u64::from_le_bytes(source_account.amount)
+    let remaining_amount = u64::from(source_account.amount)
         .checked_sub(amount)
         .ok_or(TokenError::InsufficientFunds)?;
 
@@ -99,12 +99,12 @@ pub fn process_transfer(
     if source_account.delegate.as_ref() == Some(authority_info.key()) {
         validate_owner(program_id, authority_info.key(), authority_info, remaning)?;
 
-        let delegated_amount = u64::from_le_bytes(source_account.delegated_amount)
+        let delegated_amount = u64::from(source_account.delegated_amount)
             .checked_sub(amount)
             .ok_or(TokenError::InsufficientFunds)?;
 
         if !self_transfer {
-            source_account.delegated_amount = delegated_amount.to_le_bytes();
+            source_account.delegated_amount = delegated_amount.into();
 
             if delegated_amount == 0 {
                 source_account.delegate = PodCOption::from(None);
@@ -135,12 +135,12 @@ pub fn process_transfer(
 
     // Moves the tokens.
 
-    source_account.amount = remaining_amount.to_le_bytes();
+    source_account.amount = remaining_amount.into();
 
-    let destination_amount = u64::from_le_bytes(destination_account.amount)
+    let destination_amount = u64::from(destination_account.amount)
         .checked_add(amount)
         .ok_or(TokenError::Overflow)?;
-    destination_account.amount = destination_amount.to_le_bytes();
+    destination_account.amount = destination_amount.into();
 
     if is_native_mint(&source_account.mint) {
         let mut source_lamports = source_account_info.try_borrow_mut_lamports()?;