From e78e26597e39348ad5273218e31817b3632329b8 Mon Sep 17 00:00:00 2001 From: Jack May Date: Tue, 22 Sep 2020 00:55:59 -0700 Subject: [PATCH] Rename x2 instructions to xChecked --- token/cli/src/main.rs | 6 +-- token/program/src/instruction.rs | 66 ++++++++++++------------ token/program/src/processor.rs | 86 ++++++++++++++++---------------- 3 files changed, 79 insertions(+), 79 deletions(-) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index d156aed8..6058c229 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -214,7 +214,7 @@ fn command_transfer( let amount = spl_token::ui_amount_to_amount(ui_amount, sender_token_balance.decimals); let mut transaction = Transaction::new_with_payer( - &[transfer2( + &[transfer_checked( &spl_token::id(), &sender, &mint_pubkey, @@ -251,7 +251,7 @@ fn command_burn(config: &Config, source: Pubkey, ui_amount: f64) -> CommandResul let mint_pubkey = Account::unpack_from_slice(&data)?.mint; let amount = spl_token::ui_amount_to_amount(ui_amount, source_token_balance.decimals); let mut transaction = Transaction::new_with_payer( - &[burn2( + &[burn_checked( &spl_token::id(), &source, &mint_pubkey, @@ -289,7 +289,7 @@ fn command_mint( let amount = spl_token::ui_amount_to_amount(ui_amount, recipient_token_balance.decimals); let mut transaction = Transaction::new_with_payer( - &[mint_to2( + &[mint_to_checked( &spl_token::id(), &token, &recipient, diff --git a/token/program/src/instruction.rs b/token/program/src/instruction.rs index ae01523e..de75b736 100644 --- a/token/program/src/instruction.rs +++ b/token/program/src/instruction.rs @@ -233,7 +233,7 @@ pub enum TokenInstruction { /// transferred to the destination account. /// /// This instruction differs from Transfer in that the token mint and decimals value is - /// asserted by the caller. This may be useful when creating transactions offline or within a + /// checked by the caller. This may be useful when creating transactions offline or within a /// hardware wallet. /// /// Accounts expected by this instruction: @@ -250,7 +250,7 @@ pub enum TokenInstruction { /// 2. `[writable]` The destination account. /// 3. `[]` The source account's multisignature owner/delegate. /// 4. ..4+M `[signer]` M signer accounts. - Transfer2 { + TransferChecked { /// The amount of tokens to transfer. amount: u64, /// Expected number of base 10 digits to the right of the decimal place. @@ -259,7 +259,7 @@ pub enum TokenInstruction { /// Approves a delegate. A delegate is given the authority over /// tokens on behalf of the source account's owner. /// - /// This instruction differs from Approve in that the token mint and decimals value is asserted + /// This instruction differs from Approve in that the token mint and decimals value is checked /// by the caller. This may be useful when creating transactions offline or within a hardware /// wallet. /// @@ -277,7 +277,7 @@ pub enum TokenInstruction { /// 2. `[]` The delegate. /// 3. `[]` The source account's multisignature owner. /// 4. ..4+M `[signer]` M signer accounts - Approve2 { + ApproveChecked { /// The amount of tokens the delegate is approved for. amount: u64, /// Expected number of base 10 digits to the right of the decimal place. @@ -285,7 +285,7 @@ pub enum TokenInstruction { }, /// Mints new tokens to an account. The native mint does not support minting. /// - /// This instruction differs from MintTo in that the decimals value is asserted by the + /// This instruction differs from MintTo in that the decimals value is checked by the /// caller. This may be useful when creating transactions offline or within a hardware wallet. /// /// Accounts expected by this instruction: @@ -300,16 +300,16 @@ pub enum TokenInstruction { /// 1. `[writable]` The account to mint tokens to. /// 2. `[]` The mint's multisignature mint-tokens authority. /// 3. ..3+M `[signer]` M signer accounts. - MintTo2 { + MintToChecked { /// The amount of new tokens to mint. amount: u64, /// Expected number of base 10 digits to the right of the decimal place. decimals: u8, }, - /// Burns tokens by removing them from an account. `Burn2` does not support accounts + /// Burns tokens by removing them from an account. `BurnChecked` does not support accounts /// associated with the native mint, use `CloseAccount` instead. /// - /// This instruction differs from Burn in that the decimals value is asserted by the caller. + /// This instruction differs from Burn in that the decimals value is checked by the caller. /// This may be useful when creating transactions offline or within a hardware wallet. /// /// Accounts expected by this instruction: @@ -324,7 +324,7 @@ pub enum TokenInstruction { /// 1. `[writable]` The token mint. /// 2. `[]` The account's multisignature owner/delegate. /// 3. ..3+M `[signer]` M signer accounts. - Burn2 { + BurnChecked { /// The amount of tokens to burn. amount: u64, /// Expected number of base 10 digits to the right of the decimal place. @@ -392,7 +392,7 @@ impl TokenInstruction { .ok_or(InvalidInstruction)?; let (&decimals, _rest) = rest.split_first().ok_or(InvalidInstruction)?; - Self::Transfer2 { amount, decimals } + Self::TransferChecked { amount, decimals } } 13 => { let (amount, rest) = rest.split_at(8); @@ -403,7 +403,7 @@ impl TokenInstruction { .ok_or(InvalidInstruction)?; let (&decimals, _rest) = rest.split_first().ok_or(InvalidInstruction)?; - Self::Approve2 { amount, decimals } + Self::ApproveChecked { amount, decimals } } 14 => { let (amount, rest) = rest.split_at(8); @@ -414,7 +414,7 @@ impl TokenInstruction { .ok_or(InvalidInstruction)?; let (&decimals, _rest) = rest.split_first().ok_or(InvalidInstruction)?; - Self::MintTo2 { amount, decimals } + Self::MintToChecked { amount, decimals } } 15 => { let (amount, rest) = rest.split_at(8); @@ -425,7 +425,7 @@ impl TokenInstruction { .ok_or(InvalidInstruction)?; let (&decimals, _rest) = rest.split_first().ok_or(InvalidInstruction)?; - Self::Burn2 { amount, decimals } + Self::BurnChecked { amount, decimals } } _ => return Err(TokenError::InvalidInstruction.into()), @@ -479,22 +479,22 @@ impl TokenInstruction { Self::CloseAccount => buf.push(9), Self::FreezeAccount => buf.push(10), Self::ThawAccount => buf.push(11), - &Self::Transfer2 { amount, decimals } => { + &Self::TransferChecked { amount, decimals } => { buf.push(12); buf.extend_from_slice(&amount.to_le_bytes()); buf.push(decimals); } - &Self::Approve2 { amount, decimals } => { + &Self::ApproveChecked { amount, decimals } => { buf.push(13); buf.extend_from_slice(&amount.to_le_bytes()); buf.push(decimals); } - &Self::MintTo2 { amount, decimals } => { + &Self::MintToChecked { amount, decimals } => { buf.push(14); buf.extend_from_slice(&amount.to_le_bytes()); buf.push(decimals); } - &Self::Burn2 { amount, decimals } => { + &Self::BurnChecked { amount, decimals } => { buf.push(15); buf.extend_from_slice(&amount.to_le_bytes()); buf.push(decimals); @@ -910,9 +910,9 @@ pub fn thaw_account( }) } -/// Creates a `Transfer2` instruction. +/// Creates a `TransferChecked` instruction. #[allow(clippy::too_many_arguments)] -pub fn transfer2( +pub fn transfer_checked( token_program_id: &Pubkey, source_pubkey: &Pubkey, mint_pubkey: &Pubkey, @@ -922,7 +922,7 @@ pub fn transfer2( amount: u64, decimals: u8, ) -> Result { - let data = TokenInstruction::Transfer2 { amount, decimals }.pack(); + let data = TokenInstruction::TransferChecked { amount, decimals }.pack(); let mut accounts = Vec::with_capacity(4 + signer_pubkeys.len()); accounts.push(AccountMeta::new(*source_pubkey, false)); @@ -943,9 +943,9 @@ pub fn transfer2( }) } -/// Creates an `Approve2` instruction. +/// Creates an `ApproveChecked` instruction. #[allow(clippy::too_many_arguments)] -pub fn approve2( +pub fn approve_checked( token_program_id: &Pubkey, source_pubkey: &Pubkey, mint_pubkey: &Pubkey, @@ -955,7 +955,7 @@ pub fn approve2( amount: u64, decimals: u8, ) -> Result { - let data = TokenInstruction::Approve2 { amount, decimals }.pack(); + let data = TokenInstruction::ApproveChecked { amount, decimals }.pack(); let mut accounts = Vec::with_capacity(4 + signer_pubkeys.len()); accounts.push(AccountMeta::new(*source_pubkey, false)); @@ -976,8 +976,8 @@ pub fn approve2( }) } -/// Creates a `MintTo2` instruction. -pub fn mint_to2( +/// Creates a `MintToChecked` instruction. +pub fn mint_to_checked( token_program_id: &Pubkey, mint_pubkey: &Pubkey, account_pubkey: &Pubkey, @@ -986,7 +986,7 @@ pub fn mint_to2( amount: u64, decimals: u8, ) -> Result { - let data = TokenInstruction::MintTo2 { amount, decimals }.pack(); + let data = TokenInstruction::MintToChecked { amount, decimals }.pack(); let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len()); accounts.push(AccountMeta::new(*mint_pubkey, false)); @@ -1006,8 +1006,8 @@ pub fn mint_to2( }) } -/// Creates a `Burn2` instruction. -pub fn burn2( +/// Creates a `BurnChecked` instruction. +pub fn burn_checked( token_program_id: &Pubkey, account_pubkey: &Pubkey, mint_pubkey: &Pubkey, @@ -1016,7 +1016,7 @@ pub fn burn2( amount: u64, decimals: u8, ) -> Result { - let data = TokenInstruction::Burn2 { amount, decimals }.pack(); + let data = TokenInstruction::BurnChecked { amount, decimals }.pack(); let mut accounts = Vec::with_capacity(3 + signer_pubkeys.len()); accounts.push(AccountMeta::new(*account_pubkey, false)); @@ -1156,7 +1156,7 @@ mod test { let unpacked = TokenInstruction::unpack(&expect).unwrap(); assert_eq!(unpacked, check); - let check = TokenInstruction::Transfer2 { + let check = TokenInstruction::TransferChecked { amount: 1, decimals: 2, }; @@ -1166,7 +1166,7 @@ mod test { let unpacked = TokenInstruction::unpack(&expect).unwrap(); assert_eq!(unpacked, check); - let check = TokenInstruction::Approve2 { + let check = TokenInstruction::ApproveChecked { amount: 1, decimals: 2, }; @@ -1176,7 +1176,7 @@ mod test { let unpacked = TokenInstruction::unpack(&expect).unwrap(); assert_eq!(unpacked, check); - let check = TokenInstruction::MintTo2 { + let check = TokenInstruction::MintToChecked { amount: 1, decimals: 2, }; @@ -1186,7 +1186,7 @@ mod test { let unpacked = TokenInstruction::unpack(&expect).unwrap(); assert_eq!(unpacked, check); - let check = TokenInstruction::Burn2 { + let check = TokenInstruction::BurnChecked { amount: 1, decimals: 2, }; diff --git a/token/program/src/processor.rs b/token/program/src/processor.rs index 7c32213f..87bdb41b 100644 --- a/token/program/src/processor.rs +++ b/token/program/src/processor.rs @@ -680,20 +680,20 @@ impl Processor { info!("Instruction: FreezeAccount"); Self::process_toggle_freeze_account(program_id, accounts, false) } - TokenInstruction::Transfer2 { amount, decimals } => { - info!("Instruction: Transfer"); + TokenInstruction::TransferChecked { amount, decimals } => { + info!("Instruction: TransferChecked"); Self::process_transfer(program_id, accounts, amount, Some(decimals)) } - TokenInstruction::Approve2 { amount, decimals } => { - info!("Instruction: Approve"); + TokenInstruction::ApproveChecked { amount, decimals } => { + info!("Instruction: ApproveChecked"); Self::process_approve(program_id, accounts, amount, Some(decimals)) } - TokenInstruction::MintTo2 { amount, decimals } => { - info!("Instruction: MintTo"); + TokenInstruction::MintToChecked { amount, decimals } => { + info!("Instruction: MintToChecked"); Self::process_mint_to(program_id, accounts, amount, Some(decimals)) } - TokenInstruction::Burn2 { amount, decimals } => { - info!("Instruction: Burn"); + TokenInstruction::BurnChecked { amount, decimals } => { + info!("Instruction: BurnChecked"); Self::process_burn(program_id, accounts, amount, Some(decimals)) } } @@ -1196,9 +1196,9 @@ mod tests { ) .unwrap(); - // source-owner transfer2 + // source-owner TransferChecked do_process_instruction_dups( - transfer2( + transfer_checked( &program_id, &account1_key, &mint_key, @@ -1244,9 +1244,9 @@ mod tests { ) .unwrap(); - // source-delegate transfer2 + // source-delegate TransferChecked do_process_instruction_dups( - transfer2( + transfer_checked( &program_id, &account1_key, &mint_key, @@ -1303,9 +1303,9 @@ mod tests { ) .unwrap(); - // destination-owner transfer2 + // destination-owner TransferChecked do_process_instruction_dups( - transfer2( + transfer_checked( &program_id, &account3_key, &mint_key, @@ -1373,9 +1373,9 @@ mod tests { ) .unwrap(); - // source-multisig-signer transfer2 + // source-multisig-signer TransferChecked do_process_instruction_dups( - transfer2( + transfer_checked( &program_id, &account4_key, &mint_key, @@ -1621,7 +1621,7 @@ mod tests { assert_eq!( Err(TokenError::MintDecimalsMismatch.into()), do_process_instruction( - transfer2( + transfer_checked( &program_id, &account2_key, &mint_key, @@ -1645,7 +1645,7 @@ mod tests { assert_eq!( Err(TokenError::MintMismatch.into()), do_process_instruction( - transfer2( + transfer_checked( &program_id, &account2_key, &account3_key, // <-- incorrect mint @@ -1666,7 +1666,7 @@ mod tests { ); // transfer rest with explicit decimals do_process_instruction( - transfer2( + transfer_checked( &program_id, &account2_key, &mint_key, @@ -1914,7 +1914,7 @@ mod tests { assert_eq!( Err(TokenError::MintDecimalsMismatch.into()), do_process_instruction( - mint_to2( + mint_to_checked( &program_id, &mint_key, &account_key, @@ -1934,7 +1934,7 @@ mod tests { // mint to 2 do_process_instruction( - mint_to2( + mint_to_checked( &program_id, &mint_key, &account_key, @@ -2051,9 +2051,9 @@ mod tests { ) .unwrap(); - // source-owner approve2 + // source-owner approve_checked do_process_instruction_dups( - approve2( + approve_checked( &program_id, &account1_key, &mint_key, @@ -2128,9 +2128,9 @@ mod tests { ) .unwrap(); - // source-multisig-signer approve2 + // source-multisig-signer approve_checked do_process_instruction_dups( - approve2( + approve_checked( &program_id, &account3_key, &mint_key, @@ -2294,7 +2294,7 @@ mod tests { assert_eq!( Err(TokenError::MintDecimalsMismatch.into()), do_process_instruction( - approve2( + approve_checked( &program_id, &account_key, &mint_key, @@ -2318,7 +2318,7 @@ mod tests { assert_eq!( Err(TokenError::MintMismatch.into()), do_process_instruction( - approve2( + approve_checked( &program_id, &account_key, &account2_key, // <-- bad mint @@ -2340,7 +2340,7 @@ mod tests { // approve delegate 2 do_process_instruction( - approve2( + approve_checked( &program_id, &account_key, &mint_key, @@ -2857,9 +2857,9 @@ mod tests { ) .unwrap(); - // mint_to2 when mint_authority is self + // mint_to_checked when mint_authority is self do_process_instruction_dups( - mint_to2(&program_id, &mint_key, &account1_key, &mint_key, &[], 42, 2).unwrap(), + mint_to_checked(&program_id, &mint_key, &account1_key, &mint_key, &[], 42, 2).unwrap(), vec![mint_info.clone(), account1_info.clone(), mint_info.clone()], ) .unwrap(); @@ -2886,7 +2886,7 @@ mod tests { ) .unwrap(); - // mint_to2 when mint_authority is account owner + // mint_to_checked when mint_authority is account owner do_process_instruction_dups( mint_to( &program_id, @@ -3175,9 +3175,9 @@ mod tests { ) .unwrap(); - // source-owner burn2 + // source-owner burn_checked do_process_instruction_dups( - burn2( + burn_checked( &program_id, &account1_key, &mint_key, @@ -3210,9 +3210,9 @@ mod tests { ) .unwrap(); - // mint-owner burn2 + // mint-owner burn_checked do_process_instruction_dups( - burn2( + burn_checked( &program_id, &account1_key, &mint_key, @@ -3255,9 +3255,9 @@ mod tests { ) .unwrap(); - // source-delegate burn2 + // source-delegate burn_checked do_process_instruction_dups( - burn2( + burn_checked( &program_id, &account1_key, &mint_key, @@ -3292,9 +3292,9 @@ mod tests { ) .unwrap(); - // mint-delegate burn2 + // mint-delegate burn_checked do_process_instruction_dups( - burn2( + burn_checked( &program_id, &account1_key, &mint_key, @@ -3461,18 +3461,18 @@ mod tests { ) .unwrap(); - // burn2, with incorrect decimals + // burn_checked, with incorrect decimals assert_eq!( Err(TokenError::MintDecimalsMismatch.into()), do_process_instruction( - burn2(&program_id, &account_key, &mint_key, &owner_key, &[], 21, 3).unwrap(), + burn_checked(&program_id, &account_key, &mint_key, &owner_key, &[], 21, 3).unwrap(), vec![&mut account_account, &mut mint_account, &mut owner_account], ) ); - // burn2 + // burn_checked do_process_instruction( - burn2(&program_id, &account_key, &mint_key, &owner_key, &[], 21, 2).unwrap(), + burn_checked(&program_id, &account_key, &mint_key, &owner_key, &[], 21, 2).unwrap(), vec![&mut account_account, &mut mint_account, &mut owner_account], ) .unwrap();