Finish renaming x2 instructions to xChecked (#981)

This commit is contained in:
Justin Starry 2020-12-29 17:09:26 +08:00 committed by GitHub
parent d14e403599
commit b9aa9d0be8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 117 additions and 110 deletions

View File

@ -9,15 +9,15 @@ import {
createMint,
createAccount,
transfer,
transfer2,
transferChecked,
approveRevoke,
failOnApproveOverspend,
setAuthority,
mintTo,
mintTo2,
mintToChecked,
multisig,
burn,
burn2,
burnChecked,
freezeThawAccount,
closeAccount,
nativeToken,
@ -32,12 +32,12 @@ async function main() {
await createAccount();
console.log('Run test: mintTo');
await mintTo();
console.log('Run test: mintTo2');
await mintTo2();
console.log('Run test: mintToChecked');
await mintToChecked();
console.log('Run test: transfer');
await transfer();
console.log('Run test: transfer2');
await transfer2();
console.log('Run test: transferChecked');
await transferChecked();
console.log('Run test: approveRevoke');
await approveRevoke();
console.log('Run test: failOnApproveOverspend');
@ -46,8 +46,8 @@ async function main() {
await setAuthority();
console.log('Run test: burn');
await burn();
console.log('Run test: burn2');
await burn2();
console.log('Run test: burnChecked');
await burnChecked();
console.log('Run test: freezeThawAccount');
await freezeThawAccount();
console.log('Run test: closeAccount');

View File

@ -172,9 +172,9 @@ export async function mintTo(): Promise<void> {
assert(accountInfo.amount.toNumber() === 1000);
}
export async function mintTo2(): Promise<void> {
export async function mintToChecked(): Promise<void> {
assert(
await didThrow(testToken, testToken.mintTo2, [
await didThrow(testToken, testToken.mintToChecked, [
testAccount,
testMintAuthority,
[],
@ -183,7 +183,7 @@ export async function mintTo2(): Promise<void> {
]),
);
await testToken.mintTo2(testAccount, testMintAuthority, [], 1000, 2);
await testToken.mintToChecked(testAccount, testMintAuthority, [], 1000, 2);
const mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() === 2000);
@ -208,12 +208,12 @@ export async function transfer(): Promise<void> {
assert(testAccountInfo.amount.toNumber() === 1900);
}
export async function transfer2(): Promise<void> {
export async function transferChecked(): Promise<void> {
const destOwner = new Account();
const dest = await testToken.createAccount(destOwner.publicKey);
assert(
await didThrow(testToken, testToken.transfer2, [
await didThrow(testToken, testToken.transferChecked, [
testAccount,
dest,
testAccountOwner,
@ -223,7 +223,14 @@ export async function transfer2(): Promise<void> {
]),
);
await testToken.transfer2(testAccount, dest, testAccountOwner, [], 100, 2);
await testToken.transferChecked(
testAccount,
dest,
testAccountOwner,
[],
100,
2,
);
const mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() === 2000);
@ -337,12 +344,12 @@ export async function burn(): Promise<void> {
assert(accountInfo.amount.toNumber() == amount - 1);
}
export async function burn2(): Promise<void> {
export async function burnChecked(): Promise<void> {
let accountInfo = await testToken.getAccountInfo(testAccount);
const amount = accountInfo.amount.toNumber();
assert(
await didThrow(testToken, testToken.burn2, [
await didThrow(testToken, testToken.burnChecked, [
testAccount,
testAccountOwner,
[],
@ -351,7 +358,7 @@ export async function burn2(): Promise<void> {
]),
);
await testToken.burn2(testAccount, testAccountOwner, [], 1, 2);
await testToken.burnChecked(testAccount, testAccountOwner, [], 1, 2);
accountInfo = await testToken.getAccountInfo(testAccount);
assert(accountInfo.amount.toNumber() == amount - 1);

View File

@ -1093,7 +1093,7 @@ export class Token {
* @param amount Number of tokens to transfer
* @param decimals Number of decimals in transfer amount
*/
async transfer2(
async transferChecked(
source: PublicKey,
destination: PublicKey,
owner: any,
@ -1111,10 +1111,10 @@ export class Token {
signers = multiSigners;
}
return await sendAndConfirmTransaction(
'Transfer2',
'TransferChecked',
this.connection,
new Transaction().add(
Token.createTransfer2Instruction(
Token.createTransferCheckedInstruction(
this.programId,
source,
this.publicKey,
@ -1141,7 +1141,7 @@ export class Token {
* @param amount Maximum number of tokens the delegate may transfer
* @param decimals Number of decimals in approve amount
*/
async approve2(
async approveChecked(
account: PublicKey,
delegate: PublicKey,
owner: any,
@ -1159,10 +1159,10 @@ export class Token {
signers = multiSigners;
}
await sendAndConfirmTransaction(
'Approve2',
'ApproveChecked',
this.connection,
new Transaction().add(
Token.createApprove2Instruction(
Token.createApproveCheckedInstruction(
this.programId,
account,
this.publicKey,
@ -1187,7 +1187,7 @@ export class Token {
* @param amount Amount to mint
* @param decimals Number of decimals in amount to mint
*/
async mintTo2(
async mintToChecked(
dest: PublicKey,
authority: any,
multiSigners: Array<Account>,
@ -1204,10 +1204,10 @@ export class Token {
signers = multiSigners;
}
await sendAndConfirmTransaction(
'MintTo2',
'MintToChecked',
this.connection,
new Transaction().add(
Token.createMintTo2Instruction(
Token.createMintToCheckedInstruction(
this.programId,
this.publicKey,
dest,
@ -1231,7 +1231,7 @@ export class Token {
* @param amount Amount to burn
* @param decimals Number of decimals in amount to burn
*/
async burn2(
async burnChecked(
account: PublicKey,
owner: any,
multiSigners: Array<Account>,
@ -1248,10 +1248,10 @@ export class Token {
signers = multiSigners;
}
await sendAndConfirmTransaction(
'Burn2',
'BurnChecked',
this.connection,
new Transaction().add(
Token.createBurn2Instruction(
Token.createBurnCheckedInstruction(
this.programId,
this.publicKey,
account,
@ -1839,7 +1839,7 @@ export class Token {
}
/**
* Construct a Transfer2 instruction
* Construct a TransferChecked instruction
*
* @param programId SPL Token program account
* @param source Source account
@ -1850,7 +1850,7 @@ export class Token {
* @param amount Number of tokens to transfer
* @param decimals Number of decimals in transfer amount
*/
static createTransfer2Instruction(
static createTransferCheckedInstruction(
programId: PublicKey,
source: PublicKey,
mint: PublicKey,
@ -1869,7 +1869,7 @@ export class Token {
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 12, // Transfer2 instruction
instruction: 12, // TransferChecked instruction
amount: new u64(amount).toBuffer(),
decimals,
},
@ -1905,7 +1905,7 @@ export class Token {
}
/**
* Construct an Approve2 instruction
* Construct an ApproveChecked instruction
*
* @param programId SPL Token program account
* @param account Public key of the account
@ -1916,7 +1916,7 @@ export class Token {
* @param amount Maximum number of tokens the delegate may transfer
* @param decimals Number of decimals in approve amount
*/
static createApprove2Instruction(
static createApproveCheckedInstruction(
programId: PublicKey,
account: PublicKey,
mint: PublicKey,
@ -1935,7 +1935,7 @@ export class Token {
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 13, // Approve2 instruction
instruction: 13, // ApproveChecked instruction
amount: new u64(amount).toBuffer(),
decimals,
},
@ -1968,7 +1968,7 @@ export class Token {
}
/**
* Construct a MintTo2 instruction
* Construct a MintToChecked instruction
*
* @param programId SPL Token program account
* @param mint Public key of the mint
@ -1978,7 +1978,7 @@ export class Token {
* @param amount Amount to mint
* @param decimals Number of decimals in amount to mint
*/
static createMintTo2Instruction(
static createMintToCheckedInstruction(
programId: PublicKey,
mint: PublicKey,
dest: PublicKey,
@ -1996,7 +1996,7 @@ export class Token {
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 14, // MintTo2 instruction
instruction: 14, // MintToChecked instruction
amount: new u64(amount).toBuffer(),
decimals,
},
@ -2032,7 +2032,7 @@ export class Token {
}
/**
* Construct a Burn2 instruction
* Construct a BurnChecked instruction
*
* @param programId SPL Token program account
* @param mint Mint for the account
@ -2041,7 +2041,7 @@ export class Token {
* @param multiSigners Signing accounts if `authority` is a multiSig
* @param amount amount to burn
*/
static createBurn2Instruction(
static createBurnCheckedInstruction(
programId: PublicKey,
mint: PublicKey,
account: PublicKey,
@ -2059,7 +2059,7 @@ export class Token {
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 15, // Burn2 instruction
instruction: 15, // BurnChecked instruction
amount: new u64(amount).toBuffer(),
decimals,
},

View File

@ -265,7 +265,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.
@ -292,7 +292,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.
@ -317,13 +317,13 @@ 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
/// 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
@ -342,7 +342,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.
@ -410,7 +410,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);
@ -421,7 +421,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);
@ -432,7 +432,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);
@ -443,7 +443,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()),
@ -497,22 +497,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);
@ -928,9 +928,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,
@ -940,7 +940,7 @@ pub fn transfer2(
amount: u64,
decimals: u8,
) -> Result<Instruction, ProgramError> {
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));
@ -961,9 +961,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,
@ -973,7 +973,7 @@ pub fn approve2(
amount: u64,
decimals: u8,
) -> Result<Instruction, ProgramError> {
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));
@ -994,8 +994,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,
@ -1004,7 +1004,7 @@ pub fn mint_to2(
amount: u64,
decimals: u8,
) -> Result<Instruction, ProgramError> {
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));
@ -1024,8 +1024,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,
@ -1034,7 +1034,7 @@ pub fn burn2(
amount: u64,
decimals: u8,
) -> Result<Instruction, ProgramError> {
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));
@ -1174,7 +1174,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,
};
@ -1184,7 +1184,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,
};
@ -1194,7 +1194,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,
};
@ -1204,7 +1204,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,
};

View File

@ -678,19 +678,19 @@ impl Processor {
msg!("Instruction: FreezeAccount");
Self::process_toggle_freeze_account(program_id, accounts, false)
}
TokenInstruction::Transfer2 { amount, decimals } => {
TokenInstruction::TransferChecked { amount, decimals } => {
msg!("Instruction: Transfer");
Self::process_transfer(program_id, accounts, amount, Some(decimals))
}
TokenInstruction::Approve2 { amount, decimals } => {
TokenInstruction::ApproveChecked { amount, decimals } => {
msg!("Instruction: Approve");
Self::process_approve(program_id, accounts, amount, Some(decimals))
}
TokenInstruction::MintTo2 { amount, decimals } => {
TokenInstruction::MintToChecked { amount, decimals } => {
msg!("Instruction: MintTo");
Self::process_mint_to(program_id, accounts, amount, Some(decimals))
}
TokenInstruction::Burn2 { amount, decimals } => {
TokenInstruction::BurnChecked { amount, decimals } => {
msg!("Instruction: Burn");
Self::process_burn(program_id, accounts, amount, Some(decimals))
}
@ -1183,9 +1183,9 @@ mod tests {
)
.unwrap();
// source-owner transfer2
// source-owner transfer_checked
do_process_instruction_dups(
transfer2(
transfer_checked(
&program_id,
&account1_key,
&mint_key,
@ -1231,9 +1231,9 @@ mod tests {
)
.unwrap();
// source-delegate transfer2
// source-delegate transfer_checked
do_process_instruction_dups(
transfer2(
transfer_checked(
&program_id,
&account1_key,
&mint_key,
@ -1290,9 +1290,9 @@ mod tests {
)
.unwrap();
// destination-owner transfer2
// destination-owner transfer_checked
do_process_instruction_dups(
transfer2(
transfer_checked(
&program_id,
&account3_key,
&mint_key,
@ -1360,9 +1360,9 @@ mod tests {
)
.unwrap();
// source-multisig-signer transfer2
// source-multisig-signer transfer_checked
do_process_instruction_dups(
transfer2(
transfer_checked(
&program_id,
&account4_key,
&mint_key,
@ -1608,7 +1608,7 @@ mod tests {
assert_eq!(
Err(TokenError::MintDecimalsMismatch.into()),
do_process_instruction(
transfer2(
transfer_checked(
&program_id,
&account2_key,
&mint_key,
@ -1632,7 +1632,7 @@ mod tests {
assert_eq!(
Err(TokenError::MintMismatch.into()),
do_process_instruction(
transfer2(
transfer_checked(
&program_id,
&account2_key,
&account3_key, // <-- incorrect mint
@ -1653,7 +1653,7 @@ mod tests {
);
// transfer rest with explicit decimals
do_process_instruction(
transfer2(
transfer_checked(
&program_id,
&account2_key,
&mint_key,
@ -1944,7 +1944,7 @@ mod tests {
assert_eq!(
Err(TokenError::MintDecimalsMismatch.into()),
do_process_instruction(
mint_to2(
mint_to_checked(
&program_id,
&mint_key,
&account_key,
@ -1964,7 +1964,7 @@ mod tests {
// mint to 2
do_process_instruction(
mint_to2(
mint_to_checked(
&program_id,
&mint_key,
&account_key,
@ -2081,9 +2081,9 @@ mod tests {
)
.unwrap();
// source-owner approve2
// source-owner approve_checked
do_process_instruction_dups(
approve2(
approve_checked(
&program_id,
&account1_key,
&mint_key,
@ -2158,9 +2158,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,
@ -2324,7 +2324,7 @@ mod tests {
assert_eq!(
Err(TokenError::MintDecimalsMismatch.into()),
do_process_instruction(
approve2(
approve_checked(
&program_id,
&account_key,
&mint_key,
@ -2348,7 +2348,7 @@ mod tests {
assert_eq!(
Err(TokenError::MintMismatch.into()),
do_process_instruction(
approve2(
approve_checked(
&program_id,
&account_key,
&account2_key, // <-- bad mint
@ -2370,7 +2370,7 @@ mod tests {
// approve delegate 2
do_process_instruction(
approve2(
approve_checked(
&program_id,
&account_key,
&mint_key,
@ -2887,9 +2887,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();
@ -2916,7 +2916,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,
@ -3205,9 +3205,9 @@ mod tests {
)
.unwrap();
// source-owner burn2
// source-owner burn_checked
do_process_instruction_dups(
burn2(
burn_checked(
&program_id,
&account1_key,
&mint_key,
@ -3240,9 +3240,9 @@ mod tests {
)
.unwrap();
// mint-owner burn2
// mint-owner burn_checked
do_process_instruction_dups(
burn2(
burn_checked(
&program_id,
&account1_key,
&mint_key,
@ -3285,9 +3285,9 @@ mod tests {
)
.unwrap();
// source-delegate burn2
// source-delegate burn_checked
do_process_instruction_dups(
burn2(
burn_checked(
&program_id,
&account1_key,
&mint_key,
@ -3322,9 +3322,9 @@ mod tests {
)
.unwrap();
// mint-delegate burn2
// mint-delegate burn_checked
do_process_instruction_dups(
burn2(
burn_checked(
&program_id,
&account1_key,
&mint_key,
@ -3484,18 +3484,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();