Skip to content

Commit 5291729

Browse files
committed
A few more fixes
1 parent 640f93e commit 5291729

File tree

8 files changed

+141
-84
lines changed

8 files changed

+141
-84
lines changed

Cargo.lock

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ crate-type = ["rlib"]
1414
[dependencies]
1515
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
1616
pinocchio-pubkey = { version = "0.2", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
17+
18+
[dev-dependencies]
19+
strum = "0.27"
20+
strum_macros = "0.27"

interface/src/instruction.rs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pinocchio::program_error::ProgramError;
55
/// Instructions supported by the token program.
66
#[repr(u8)]
77
#[derive(Clone, Debug, PartialEq)]
8+
#[cfg_attr(test, derive(strum_macros::FromRepr, strum_macros::EnumIter))]
89
pub enum TokenInstruction {
910
/// Initializes a new mint and optionally deposits all the newly minted
1011
/// tokens in an account.
@@ -20,7 +21,7 @@ pub enum TokenInstruction {
2021
/// 0. `[writable]` The mint to initialize.
2122
/// 1. `[]` Rent sysvar.
2223
///
23-
/// Instructions data expected by this instruction:
24+
/// Data expected by this instruction:
2425
///
2526
/// - `u8` The number of base 10 digits to the right of the decimal place.
2627
/// - `Pubkey` The authority/multisignature to mint tokens.
@@ -66,7 +67,7 @@ pub enum TokenInstruction {
6667
/// 1. `[]` Rent sysvar.
6768
/// 2. `..+N` `[signer]` The signer accounts, must equal to N where `1 <= N <= 11`.
6869
///
69-
/// Instructions data expected by this instruction:
70+
/// Data expected by this instruction:
7071
///
7172
/// - `u8` The number of signers (M) required to validate this multisignature account.
7273
InitializeMultisig,
@@ -89,7 +90,7 @@ pub enum TokenInstruction {
8990
/// 2. `[]` The source account's multisignature owner/delegate.
9091
/// 3. `..+M` `[signer]` M signer accounts.
9192
///
92-
/// Instructions data expected by this instruction:
93+
/// Data expected by this instruction:
9394
///
9495
/// - `u64` The amount of tokens to transfer.
9596
Transfer,
@@ -110,7 +111,7 @@ pub enum TokenInstruction {
110111
/// 2. `[]` The source account's multisignature owner.
111112
/// 3. `..+M` `[signer]` M signer accounts.
112113
///
113-
/// Instructions data expected by this instruction:
114+
/// Data expected by this instruction:
114115
///
115116
/// - `u64` The amount of tokens the delegate is approved for.
116117
Approve,
@@ -142,7 +143,7 @@ pub enum TokenInstruction {
142143
/// 1. `[]` The mint's or account's current multisignature authority.
143144
/// 2. `..+M` `[signer]` M signer accounts.
144145
///
145-
/// Instructions data expected by this instruction:
146+
/// Data expected by this instruction:
146147
///
147148
/// - `AuthorityType` The type of authority to update.
148149
/// - `Option<Pubkey>` The new authority.
@@ -164,7 +165,7 @@ pub enum TokenInstruction {
164165
/// 2. `[]` The mint's multisignature mint-tokens authority.
165166
/// 3. `..+M` `[signer]` M signer accounts.
166167
///
167-
/// Instructions data expected by this instruction:
168+
/// Data expected by this instruction:
168169
///
169170
/// - `u64` The amount of new tokens to mint.
170171
MintTo,
@@ -185,7 +186,7 @@ pub enum TokenInstruction {
185186
/// 2. `[]` The account's multisignature owner/delegate.
186187
/// 3. `..+M` `[signer]` M signer accounts.
187188
///
188-
/// Instructions data expected by this instruction:
189+
/// Data expected by this instruction:
189190
///
190191
/// - `u64` The amount of tokens to burn.
191192
Burn,
@@ -264,7 +265,7 @@ pub enum TokenInstruction {
264265
/// 3. `[]` The source account's multisignature owner/delegate.
265266
/// 4. `..+M` `[signer]` M signer accounts.
266267
///
267-
/// Instructions data expected by this instruction:
268+
/// Data expected by this instruction:
268269
///
269270
/// - `u64` The amount of tokens to transfer.
270271
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
@@ -292,7 +293,7 @@ pub enum TokenInstruction {
292293
/// 3. `[]` The source account's multisignature owner.
293294
/// 4. `..+M` `[signer]` M signer accounts.
294295
///
295-
/// Instructions data expected by this instruction:
296+
/// Data expected by this instruction:
296297
///
297298
/// - `u64` The amount of tokens the delegate is approved for.
298299
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
@@ -318,7 +319,7 @@ pub enum TokenInstruction {
318319
/// 2. `[]` The mint's multisignature mint-tokens authority.
319320
/// 3. `..+M` `[signer]` M signer accounts.
320321
///
321-
/// Instructions data expected by this instruction:
322+
/// Data expected by this instruction:
322323
///
323324
/// - `u64` The amount of new tokens to mint.
324325
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
@@ -345,7 +346,7 @@ pub enum TokenInstruction {
345346
/// 2. `[]` The account's multisignature owner/delegate.
346347
/// 3. `..+M` `[signer]` M signer accounts.
347348
///
348-
/// Instructions data expected by this instruction:
349+
/// Data expected by this instruction:
349350
///
350351
/// - `u64` The amount of tokens to burn.
351352
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
@@ -362,7 +363,7 @@ pub enum TokenInstruction {
362363
/// 1. `[]` The mint this account will be associated with.
363364
/// 2. `[]` Rent sysvar.
364365
///
365-
/// Instructions data expected by this instruction:
366+
/// Data expected by this instruction:
366367
///
367368
/// - `Pubkey` The new account's owner/multisignature.
368369
InitializeAccount2,
@@ -387,7 +388,7 @@ pub enum TokenInstruction {
387388
/// 0. `[writable]` The account to initialize.
388389
/// 1. `[]` The mint this account will be associated with.
389390
///
390-
/// Instructions data expected by this instruction:
391+
/// Data expected by this instruction:
391392
///
392393
/// - `Pubkey` The new account's owner/multisignature.
393394
InitializeAccount3,
@@ -400,7 +401,7 @@ pub enum TokenInstruction {
400401
/// 0. `[writable]` The multisignature account to initialize.
401402
/// 1. `..+N` `[signer]` The signer accounts, must equal to N where `1 <= N <= 11`.
402403
///
403-
/// Instructions data expected by this instruction:
404+
/// Data expected by this instruction:
404405
///
405406
/// - `u8` The number of signers (M) required to validate this multisignature account.
406407
InitializeMultisig2,
@@ -412,7 +413,7 @@ pub enum TokenInstruction {
412413
///
413414
/// 0. `[writable]` The mint to initialize.
414415
///
415-
/// Instructions data expected by this instruction:
416+
/// Data expected by this instruction:
416417
///
417418
/// - `u8` The number of base 10 digits to the right of the decimal place.
418419
/// - `Pubkey` The authority/multisignature to mint tokens.
@@ -456,7 +457,7 @@ pub enum TokenInstruction {
456457
///
457458
/// 0. `[]` The mint to calculate for
458459
///
459-
/// Instructions data expected by this instruction:
460+
/// Data expected by this instruction:
460461
///
461462
/// - `u64` The amount of tokens to reformat.
462463
AmountToUiAmount,
@@ -472,7 +473,7 @@ pub enum TokenInstruction {
472473
///
473474
/// 0. `[]` The mint to calculate for.
474475
///
475-
/// Instructions data expected by this instruction:
476+
/// Data expected by this instruction:
476477
///
477478
/// - `&str` The `ui_amount` of tokens to reformat.
478479
UiAmountToAmount,
@@ -497,6 +498,7 @@ impl TryFrom<u8> for TokenInstruction {
497498
/// Specifies the authority type for `SetAuthority` instructions
498499
#[repr(u8)]
499500
#[derive(Clone, Debug, PartialEq)]
501+
#[cfg_attr(test, derive(strum_macros::FromRepr, strum_macros::EnumIter))]
500502
pub enum AuthorityType {
501503
/// Authority to mint new tokens
502504
MintTokens,
@@ -520,3 +522,33 @@ impl TryFrom<u8> for AuthorityType {
520522
}
521523
}
522524
}
525+
526+
#[cfg(test)]
527+
mod tests {
528+
use super::{AuthorityType, TokenInstruction};
529+
use strum::IntoEnumIterator;
530+
531+
#[test]
532+
fn test_token_instruction_from_u8_exhaustive() {
533+
for variant in TokenInstruction::iter() {
534+
let variant_u8 = variant.clone() as u8;
535+
assert_eq!(
536+
TokenInstruction::from_repr(variant_u8),
537+
Some(TokenInstruction::try_from(variant_u8).unwrap())
538+
);
539+
assert_eq!(TokenInstruction::try_from(variant_u8).unwrap(), variant);
540+
}
541+
}
542+
543+
#[test]
544+
fn test_authority_type_from_u8_exhaustive() {
545+
for variant in AuthorityType::iter() {
546+
let variant_u8 = variant.clone() as u8;
547+
assert_eq!(
548+
AuthorityType::from_repr(variant_u8),
549+
Some(AuthorityType::try_from(variant_u8).unwrap())
550+
);
551+
assert_eq!(AuthorityType::try_from(variant_u8).unwrap(), variant);
552+
}
553+
}
554+
}

p-token/src/processor/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ fn try_ui_amount_into_amount(ui_amount: &str, decimals: u8) -> Result<u64, Progr
171171
);
172172
}
173173

174-
let remaining = decimals.saturating_sub(after_decimal.len());
175-
let length = amount_str.len() + after_decimal.len() + remaining;
174+
let length = amount_str.len() + decimals;
176175

177176
// SAFETY: `digits` only contains valid UTF-8 bytes.
178177
unsafe {

0 commit comments

Comments
 (0)