123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349 |
- //! Program state processor
- #![cfg(feature = "program")]
- use crate::{
- error::TokenError,
- instruction::{is_valid_signer_index, TokenInstruction},
- option::COption,
- state::{self, Account, Mint, Multisig},
- };
- use num_traits::FromPrimitive;
- use solana_sdk::{
- account_info::{next_account_info, AccountInfo},
- decode_error::DecodeError,
- entrypoint::ProgramResult,
- info,
- program_error::{PrintProgramError, ProgramError},
- pubkey::Pubkey,
- };
- use std::mem::size_of;
- /// Program state handler.
- pub struct Processor {}
- impl Processor {
- /// Processes an [InitializeMint](enum.TokenInstruction.html) instruction.
- pub fn process_initialize_mint(
- accounts: &[AccountInfo],
- amount: u64,
- decimals: u8,
- ) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let mint_info = next_account_info(account_info_iter)?;
- let mut mint_info_data = mint_info.data.borrow_mut();
- let mut mint: &mut Mint = state::unpack_unchecked(&mut mint_info_data)?;
- if mint.is_initialized {
- return Err(TokenError::AlreadyInUse.into());
- }
- let owner = if amount != 0 {
- let dest_account_info = next_account_info(account_info_iter)?;
- let mut dest_account_data = dest_account_info.data.borrow_mut();
- let mut dest_account: &mut Account = state::unpack(&mut dest_account_data)?;
- if mint_info.key != &dest_account.mint {
- return Err(TokenError::MintMismatch.into());
- }
- dest_account.amount = amount;
- if let Ok(owner_info) = next_account_info(account_info_iter) {
- COption::Some(*owner_info.key)
- } else {
- COption::None
- }
- } else if let Ok(owner_info) = next_account_info(account_info_iter) {
- COption::Some(*owner_info.key)
- } else {
- return Err(TokenError::OwnerRequiredIfNoInitialSupply.into());
- };
- mint.owner = owner;
- mint.decimals = decimals;
- mint.is_initialized = true;
- Ok(())
- }
- /// Processes an [InitializeAccount](enum.TokenInstruction.html) instruction.
- pub fn process_initialize_account(accounts: &[AccountInfo]) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let new_account_info = next_account_info(account_info_iter)?;
- let mint_info = next_account_info(account_info_iter)?;
- let owner_info = next_account_info(account_info_iter)?;
- let mut new_account_data = new_account_info.data.borrow_mut();
- let mut account: &mut Account = state::unpack_unchecked(&mut new_account_data)?;
- if account.is_initialized {
- return Err(TokenError::AlreadyInUse.into());
- }
- account.mint = *mint_info.key;
- account.owner = *owner_info.key;
- account.delegate = COption::None;
- account.delegated_amount = 0;
- account.is_initialized = true;
- if *mint_info.key == crate::native_mint::id() {
- account.is_native = true;
- account.amount = new_account_info.lamports();
- } else {
- account.is_native = false;
- account.amount = 0;
- };
- Ok(())
- }
- /// Processes a [InitializeMultisig](enum.TokenInstruction.html) instruction.
- pub fn process_initialize_multisig(accounts: &[AccountInfo], m: u8) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let multisig_info = next_account_info(account_info_iter)?;
- let mut multisig_account_data = multisig_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack_unchecked(&mut multisig_account_data)?;
- if multisig.is_initialized {
- return Err(TokenError::AlreadyInUse.into());
- }
- let signer_infos = account_info_iter.as_slice();
- multisig.m = m;
- multisig.n = signer_infos.len() as u8;
- if !is_valid_signer_index(multisig.n as usize) {
- return Err(TokenError::InvalidNumberOfProvidedSigners.into());
- }
- if !is_valid_signer_index(multisig.m as usize) {
- return Err(TokenError::InvalidNumberOfRequiredSigners.into());
- }
- for (i, signer_info) in signer_infos.iter().enumerate() {
- multisig.signers[i] = *signer_info.key;
- }
- multisig.is_initialized = true;
- Ok(())
- }
- /// Processes a [Transfer](enum.TokenInstruction.html) instruction.
- pub fn process_transfer(
- program_id: &Pubkey,
- accounts: &[AccountInfo],
- amount: u64,
- ) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let source_account_info = next_account_info(account_info_iter)?;
- let dest_account_info = next_account_info(account_info_iter)?;
- let authority_info = next_account_info(account_info_iter)?;
- let mut source_data = source_account_info.data.borrow_mut();
- let mut source_account: &mut Account = state::unpack(&mut source_data)?;
- let mut dest_data = dest_account_info.data.borrow_mut();
- let mut dest_account: &mut Account = state::unpack(&mut dest_data)?;
- if source_account.amount < amount {
- return Err(TokenError::InsufficientFunds.into());
- }
- if source_account.mint != dest_account.mint {
- return Err(TokenError::MintMismatch.into());
- }
- match source_account.delegate {
- COption::Some(ref delegate) if authority_info.key == delegate => {
- Self::validate_owner(
- program_id,
- delegate,
- authority_info,
- account_info_iter.as_slice(),
- )?;
- if source_account.delegated_amount < amount {
- return Err(TokenError::InsufficientFunds.into());
- }
- source_account.delegated_amount -= amount;
- if source_account.delegated_amount == 0 {
- source_account.delegate = COption::None;
- }
- }
- _ => Self::validate_owner(
- program_id,
- &source_account.owner,
- authority_info,
- account_info_iter.as_slice(),
- )?,
- };
- source_account.amount -= amount;
- dest_account.amount = dest_account
- .amount
- .checked_add(amount)
- .ok_or(TokenError::Overflow)?;
- if source_account.is_native {
- **source_account_info.lamports.borrow_mut() -= amount;
- **dest_account_info.lamports.borrow_mut() += amount;
- }
- Ok(())
- }
- /// Processes an [Approve](enum.TokenInstruction.html) instruction.
- pub fn process_approve(
- program_id: &Pubkey,
- accounts: &[AccountInfo],
- amount: u64,
- ) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let source_account_info = next_account_info(account_info_iter)?;
- let mut source_data = source_account_info.data.borrow_mut();
- let mut source_account: &mut Account = state::unpack(&mut source_data)?;
- let delegate_info = next_account_info(account_info_iter)?;
- let owner_info = next_account_info(account_info_iter)?;
- Self::validate_owner(
- program_id,
- &source_account.owner,
- owner_info,
- account_info_iter.as_slice(),
- )?;
- source_account.delegate = COption::Some(*delegate_info.key);
- source_account.delegated_amount = amount;
- Ok(())
- }
- /// Processes an [Revoke](enum.TokenInstruction.html) instruction.
- pub fn process_revoke(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let source_account_info = next_account_info(account_info_iter)?;
- let mut source_data = source_account_info.data.borrow_mut();
- let mut source_account: &mut Account = state::unpack(&mut source_data)?;
- let owner_info = next_account_info(account_info_iter)?;
- Self::validate_owner(
- program_id,
- &source_account.owner,
- owner_info,
- account_info_iter.as_slice(),
- )?;
- source_account.delegate = COption::None;
- source_account.delegated_amount = 0;
- Ok(())
- }
- /// Processes a [SetOwner](enum.TokenInstruction.html) instruction.
- pub fn process_set_owner(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let account_info = next_account_info(account_info_iter)?;
- let new_owner_info = next_account_info(account_info_iter)?;
- let authority_info = next_account_info(account_info_iter)?;
- if account_info.data_len() == size_of::<Account>() {
- let mut account_data = account_info.data.borrow_mut();
- let mut account: &mut Account = state::unpack(&mut account_data)?;
- Self::validate_owner(
- program_id,
- &account.owner,
- authority_info,
- account_info_iter.as_slice(),
- )?;
- account.owner = *new_owner_info.key;
- } else if account_info.data_len() == size_of::<Mint>() {
- let mut account_data = account_info.data.borrow_mut();
- let mut mint: &mut Mint = state::unpack(&mut account_data)?;
- match mint.owner {
- COption::Some(ref owner) => {
- Self::validate_owner(
- program_id,
- owner,
- authority_info,
- account_info_iter.as_slice(),
- )?;
- }
- COption::None => return Err(TokenError::FixedSupply.into()),
- }
- mint.owner = COption::Some(*new_owner_info.key);
- } else {
- return Err(ProgramError::InvalidArgument);
- }
- Ok(())
- }
- /// Processes a [MintTo](enum.TokenInstruction.html) instruction.
- pub fn process_mint_to(
- program_id: &Pubkey,
- accounts: &[AccountInfo],
- amount: u64,
- ) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let mint_info = next_account_info(account_info_iter)?;
- let dest_account_info = next_account_info(account_info_iter)?;
- let owner_info = next_account_info(account_info_iter)?;
- let mut dest_account_data = dest_account_info.data.borrow_mut();
- let mut dest_account: &mut Account = state::unpack(&mut dest_account_data)?;
- if dest_account.is_native {
- return Err(TokenError::NativeNotSupported.into());
- }
- if mint_info.key != &dest_account.mint {
- return Err(TokenError::MintMismatch.into());
- }
- let mut mint_info_data = mint_info.data.borrow_mut();
- let mint: &mut Mint = state::unpack(&mut mint_info_data)?;
- match mint.owner {
- COption::Some(owner) => {
- Self::validate_owner(program_id, &owner, owner_info, account_info_iter.as_slice())?;
- }
- COption::None => {
- return Err(TokenError::FixedSupply.into());
- }
- }
- dest_account.amount = dest_account
- .amount
- .checked_add(amount)
- .ok_or(TokenError::Overflow)?;
- Ok(())
- }
- /// Processes a [Burn](enum.TokenInstruction.html) instruction.
- pub fn process_burn(
- program_id: &Pubkey,
- accounts: &[AccountInfo],
- amount: u64,
- ) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let source_account_info = next_account_info(account_info_iter)?;
- let authority_info = next_account_info(account_info_iter)?;
- let mut source_data = source_account_info.data.borrow_mut();
- let source_account: &mut Account = state::unpack(&mut source_data)?;
- if source_account.is_native {
- return Err(TokenError::NativeNotSupported.into());
- }
- if source_account.amount < amount {
- return Err(TokenError::InsufficientFunds.into());
- }
- match source_account.delegate {
- COption::Some(ref delegate) if authority_info.key == delegate => {
- Self::validate_owner(
- program_id,
- delegate,
- authority_info,
- account_info_iter.as_slice(),
- )?;
- if source_account.delegated_amount < amount {
- return Err(TokenError::InsufficientFunds.into());
- }
- source_account.delegated_amount -= amount;
- if source_account.delegated_amount == 0 {
- source_account.delegate = COption::None;
- }
- }
- _ => Self::validate_owner(
- program_id,
- &source_account.owner,
- authority_info,
- account_info_iter.as_slice(),
- )?,
- }
- source_account.amount -= amount;
- Ok(())
- }
- /// Processes a [CloseAccount](enum.TokenInstruction.html) instruction.
- pub fn process_close_account(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
- let account_info_iter = &mut accounts.iter();
- let source_account_info = next_account_info(account_info_iter)?;
- let dest_account_info = next_account_info(account_info_iter)?;
- let authority_info = next_account_info(account_info_iter)?;
- let mut source_data = source_account_info.data.borrow_mut();
- let source_account: &mut Account = state::unpack(&mut source_data)?;
- if !source_account.is_native {
- return Err(TokenError::NonNativeNotSupported.into());
- }
- Self::validate_owner(
- program_id,
- &source_account.owner,
- authority_info,
- account_info_iter.as_slice(),
- )?;
- **dest_account_info.lamports.borrow_mut() += source_account_info.lamports();
- **source_account_info.lamports.borrow_mut() = 0;
- source_account.amount = 0;
- Ok(())
- }
- /// Processes an [Instruction](enum.Instruction.html).
- pub fn process(program_id: &Pubkey, accounts: &[AccountInfo], input: &[u8]) -> ProgramResult {
- let instruction = TokenInstruction::unpack(input)?;
- match instruction {
- TokenInstruction::InitializeMint { amount, decimals } => {
- info!("Instruction: InitializeMint");
- Self::process_initialize_mint(accounts, amount, decimals)
- }
- TokenInstruction::InitializeAccount => {
- info!("Instruction: InitializeAccount");
- Self::process_initialize_account(accounts)
- }
- TokenInstruction::InitializeMultisig { m } => {
- info!("Instruction: InitializeMultisig");
- Self::process_initialize_multisig(accounts, m)
- }
- TokenInstruction::Transfer { amount } => {
- info!("Instruction: Transfer");
- Self::process_transfer(program_id, accounts, amount)
- }
- TokenInstruction::Approve { amount } => {
- info!("Instruction: Approve");
- Self::process_approve(program_id, accounts, amount)
- }
- TokenInstruction::Revoke => {
- info!("Instruction: Revoke");
- Self::process_revoke(program_id, accounts)
- }
- TokenInstruction::SetOwner => {
- info!("Instruction: SetOwner");
- Self::process_set_owner(program_id, accounts)
- }
- TokenInstruction::MintTo { amount } => {
- info!("Instruction: MintTo");
- Self::process_mint_to(program_id, accounts, amount)
- }
- TokenInstruction::Burn { amount } => {
- info!("Instruction: Burn");
- Self::process_burn(program_id, accounts, amount)
- }
- TokenInstruction::CloseAccount => {
- info!("Instruction: CloseAccount");
- Self::process_close_account(program_id, accounts)
- }
- }
- }
- /// Validates owner(s) are present
- pub fn validate_owner(
- program_id: &Pubkey,
- expected_owner: &Pubkey,
- owner_account_info: &AccountInfo,
- signers: &[AccountInfo],
- ) -> ProgramResult {
- if expected_owner != owner_account_info.key {
- return Err(TokenError::OwnerMismatch.into());
- }
- if program_id == owner_account_info.owner
- && owner_account_info.data_len() == std::mem::size_of::<Multisig>()
- {
- let mut owner_data = owner_account_info.data.borrow_mut();
- let multisig: &mut Multisig = state::unpack(&mut owner_data)?;
- let mut num_signers = 0;
- for signer in signers.iter() {
- if multisig.signers[0..multisig.n as usize].contains(signer.key) {
- if !signer.is_signer {
- return Err(ProgramError::MissingRequiredSignature);
- }
- num_signers += 1;
- }
- }
- if num_signers < multisig.m {
- return Err(ProgramError::MissingRequiredSignature);
- }
- } else if !owner_account_info.is_signer {
- return Err(ProgramError::MissingRequiredSignature);
- }
- Ok(())
- }
- }
- impl PrintProgramError for TokenError {
- fn print<E>(&self)
- where
- E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
- {
- match self {
- TokenError::InsufficientFunds => info!("Error: insufficient funds"),
- TokenError::MintMismatch => info!("Error: Account not associated with this Mint"),
- TokenError::OwnerMismatch => info!("Error: owner does not match"),
- TokenError::FixedSupply => info!("Error: the total supply of this token is fixed"),
- TokenError::AlreadyInUse => info!("Error: account or token already in use"),
- TokenError::OwnerRequiredIfNoInitialSupply => {
- info!("Error: An owner is required if supply is zero")
- }
- TokenError::InvalidNumberOfProvidedSigners => {
- info!("Error: Invalid number of provided signers")
- }
- TokenError::InvalidNumberOfRequiredSigners => {
- info!("Error: Invalid number of required signers")
- }
- TokenError::UninitializedState => info!("Error: State is uninitialized"),
- TokenError::NativeNotSupported => {
- info!("Error: Instruction does not support native tokens")
- }
- TokenError::NonNativeNotSupported => {
- info!("Error: Instruction does not support non-native tokens")
- }
- TokenError::InvalidInstruction => info!("Error: Invalid instruction"),
- TokenError::Overflow => info!("Error: Operation overflowed"),
- }
- }
- }
- // Pull in syscall stubs when building for non-BPF targets
- #[cfg(not(target_arch = "bpf"))]
- solana_sdk::program_stubs!();
- #[cfg(test)]
- mod tests {
- use super::*;
- use crate::instruction::{
- approve, burn, close_account, initialize_account, initialize_mint, initialize_multisig,
- mint_to, revoke, set_owner, transfer, MAX_SIGNERS,
- };
- use solana_sdk::{
- account::Account as SolanaAccount, account_info::create_is_signer_account_infos,
- clock::Epoch, instruction::Instruction,
- };
- fn pubkey_rand() -> Pubkey {
- Pubkey::new(&rand::random::<[u8; 32]>())
- }
- fn do_process_instruction(
- instruction: Instruction,
- accounts: Vec<&mut SolanaAccount>,
- ) -> ProgramResult {
- let mut meta = instruction
- .accounts
- .iter()
- .zip(accounts)
- .map(|(account_meta, account)| (&account_meta.pubkey, account_meta.is_signer, account))
- .collect::<Vec<_>>();
- let account_infos = create_is_signer_account_infos(&mut meta);
- Processor::process(&instruction.program_id, &account_infos, &instruction.data)
- }
- fn return_token_error_as_program_error() -> ProgramError {
- TokenError::MintMismatch.into()
- }
- #[test]
- fn test_print_error() {
- let error = return_token_error_as_program_error();
- error.print::<TokenError>();
- }
- #[test]
- #[should_panic(expected = "Custom(1)")]
- fn test_error_unwrap() {
- Err::<(), ProgramError>(return_token_error_as_program_error()).unwrap();
- }
- #[test]
- fn test_unique_account_sizes() {
- assert_ne!(size_of::<Mint>(), 0);
- assert_ne!(size_of::<Mint>(), size_of::<Account>());
- assert_ne!(size_of::<Mint>(), size_of::<Multisig>());
- assert_ne!(size_of::<Account>(), 0);
- assert_ne!(size_of::<Account>(), size_of::<Multisig>());
- assert_ne!(size_of::<Multisig>(), 0);
- }
- #[test]
- fn test_initialize_mint() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let mint2_key = pubkey_rand();
- let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // account not created
- assert_eq!(
- Err(TokenError::UninitializedState.into()),
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
- vec![&mut mint_account, &mut account_account]
- )
- );
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut owner_account, &mut mint_account],
- )
- .unwrap();
- // create new mint
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
- vec![&mut mint_account, &mut account_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account2_account, &mut owner_account, &mut mint_account],
- )
- .unwrap();
- // mismatch account
- assert_eq!(
- Err(TokenError::MintMismatch.into()),
- do_process_instruction(
- initialize_mint(&program_id, &mint2_key, Some(&account2_key), None, 1000, 2)
- .unwrap(),
- vec![&mut mint2_account, &mut account2_account]
- )
- );
- // create twice
- assert_eq!(
- Err(TokenError::AlreadyInUse.into()),
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
- vec![&mut mint_account, &mut account_account]
- )
- );
- }
- #[test]
- fn test_initialize_mint_account() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create twice
- assert_eq!(
- Err(TokenError::AlreadyInUse.into()),
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- );
- }
- #[test]
- fn test_transfer() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account3_key = pubkey_rand();
- let mut account3_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let delegate_key = pubkey_rand();
- let mut delegate_account = SolanaAccount::default();
- let mismatch_key = pubkey_rand();
- let mut mismatch_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let mint2_key = pubkey_rand();
- let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account2_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account3_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create mismatch account
- do_process_instruction(
- initialize_account(&program_id, &mismatch_key, &mint2_key, &owner_key).unwrap(),
- vec![
- &mut mismatch_account,
- &mut mint2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // create new mint
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
- vec![&mut mint_account, &mut account_account],
- )
- .unwrap();
- // missing signer
- let mut instruction = transfer(
- &program_id,
- &account_key,
- &account2_key,
- &owner_key,
- &[],
- 1000,
- )
- .unwrap();
- instruction.accounts[2].is_signer = false;
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- do_process_instruction(
- instruction,
- vec![
- &mut account_account,
- &mut account2_account,
- &mut owner_account,
- ],
- )
- );
- // mismatch mint
- assert_eq!(
- Err(TokenError::MintMismatch.into()),
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &mismatch_key,
- &owner_key,
- &[],
- 1000
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut mismatch_account,
- &mut owner_account,
- ],
- )
- );
- // missing owner
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &owner2_key,
- &[],
- 1000
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut owner2_account,
- ],
- )
- );
- // transfer
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &owner_key,
- &[],
- 1000,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // insufficient funds
- assert_eq!(
- Err(TokenError::InsufficientFunds.into()),
- do_process_instruction(
- transfer(&program_id, &account_key, &account2_key, &owner_key, &[], 1).unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut owner_account,
- ],
- )
- );
- // transfer half back
- do_process_instruction(
- transfer(
- &program_id,
- &account2_key,
- &account_key,
- &owner_key,
- &[],
- 500,
- )
- .unwrap(),
- vec![
- &mut account2_account,
- &mut account_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // transfer rest
- do_process_instruction(
- transfer(
- &program_id,
- &account2_key,
- &account_key,
- &owner_key,
- &[],
- 500,
- )
- .unwrap(),
- vec![
- &mut account2_account,
- &mut account_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // insufficient funds
- assert_eq!(
- Err(TokenError::InsufficientFunds.into()),
- do_process_instruction(
- transfer(&program_id, &account2_key, &account_key, &owner_key, &[], 1).unwrap(),
- vec![
- &mut account2_account,
- &mut account_account,
- &mut owner_account,
- ],
- )
- );
- // approve delegate
- do_process_instruction(
- approve(
- &program_id,
- &account_key,
- &delegate_key,
- &owner_key,
- &[],
- 100,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut delegate_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // transfer via delegate
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &delegate_key,
- &[],
- 100,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut delegate_account,
- ],
- )
- .unwrap();
- // insufficient funds approved via delegate
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &delegate_key,
- &[],
- 100
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut delegate_account,
- ],
- )
- );
- // transfer rest
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &owner_key,
- &[],
- 900,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // approve delegate
- do_process_instruction(
- approve(
- &program_id,
- &account_key,
- &delegate_key,
- &owner_key,
- &[],
- 100,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut delegate_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // insufficient funds in source account via delegate
- assert_eq!(
- Err(TokenError::InsufficientFunds.into()),
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &delegate_key,
- &[],
- 100
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut delegate_account,
- ],
- )
- );
- }
- #[test]
- fn test_mintable_token_with_zero_supply() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut owner_account, &mut mint_account],
- )
- .unwrap();
- // create mint-able token without owner
- let mut instruction =
- initialize_mint(&program_id, &mint_key, None, Some(&owner_key), 0, 2).unwrap();
- instruction.accounts.pop();
- assert_eq!(
- Err(TokenError::OwnerRequiredIfNoInitialSupply.into()),
- do_process_instruction(instruction, vec![&mut mint_account])
- );
- // create mint-able token with zero supply
- let amount = 0;
- let decimals = 2;
- do_process_instruction(
- initialize_mint(
- &program_id,
- &mint_key,
- None,
- Some(&owner_key),
- amount,
- decimals,
- )
- .unwrap(),
- vec![&mut mint_account, &mut account_account],
- )
- .unwrap();
- let mint: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
- assert_eq!(
- *mint,
- Mint {
- owner: COption::Some(owner_key),
- decimals,
- is_initialized: true,
- }
- );
- // mint to
- do_process_instruction(
- mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 42).unwrap(),
- vec![&mut mint_account, &mut account_account, &mut owner_account],
- )
- .unwrap();
- let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
- let dest_account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert_eq!(dest_account.amount, 42);
- }
- #[test]
- fn test_approve() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let delegate_key = pubkey_rand();
- let mut delegate_account = SolanaAccount::default();
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut owner_account, &mut mint_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account2_account, &mut owner_account, &mut mint_account],
- )
- .unwrap();
- // create new mint
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
- vec![&mut mint_account, &mut account_account],
- )
- .unwrap();
- // missing signer
- let mut instruction = approve(
- &program_id,
- &account_key,
- &delegate_key,
- &owner_key,
- &[],
- 100,
- )
- .unwrap();
- instruction.accounts[2].is_signer = false;
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- do_process_instruction(
- instruction,
- vec![
- &mut account_account,
- &mut delegate_account,
- &mut owner_account,
- ],
- )
- );
- // no owner
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- approve(
- &program_id,
- &account_key,
- &delegate_key,
- &owner2_key,
- &[],
- 100
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut delegate_account,
- &mut owner2_account,
- ],
- )
- );
- // approve delegate
- do_process_instruction(
- approve(
- &program_id,
- &account_key,
- &delegate_key,
- &owner_key,
- &[],
- 100,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut delegate_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // revoke delegate
- do_process_instruction(
- revoke(&program_id, &account_key, &owner_key, &[]).unwrap(),
- vec![&mut account_account, &mut owner_account],
- )
- .unwrap();
- }
- #[test]
- fn test_set_owner() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- let owner3_key = pubkey_rand();
- let mut owner3_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let mint2_key = pubkey_rand();
- let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // invalid account
- assert_eq!(
- Err(TokenError::UninitializedState.into()),
- do_process_instruction(
- set_owner(&program_id, &account_key, &owner2_key, &owner_key, &[]).unwrap(),
- vec![
- &mut account_account,
- &mut owner2_account,
- &mut owner_account,
- ],
- )
- );
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint2_key, &owner_key).unwrap(),
- vec![
- &mut account2_account,
- &mut mint2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // missing owner
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- set_owner(&program_id, &account_key, &owner_key, &owner2_key, &[]).unwrap(),
- vec![
- &mut account_account,
- &mut owner_account,
- &mut owner2_account,
- ],
- )
- );
- // owner did not sign
- let mut instruction =
- set_owner(&program_id, &account_key, &owner2_key, &owner_key, &[]).unwrap();
- instruction.accounts[2].is_signer = false;
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- do_process_instruction(
- instruction,
- vec![
- &mut account_account,
- &mut owner2_account,
- &mut owner_account,
- ],
- )
- );
- // set owner
- do_process_instruction(
- set_owner(&program_id, &account_key, &owner2_key, &owner_key, &[]).unwrap(),
- vec![
- &mut account_account,
- &mut owner2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // create new mint with owner
- do_process_instruction(
- initialize_mint(
- &program_id,
- &mint_key,
- Some(&account_key),
- Some(&owner_key),
- 1000,
- 2,
- )
- .unwrap(),
- vec![&mut mint_account, &mut account_account, &mut owner_account],
- )
- .unwrap();
- // wrong account
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- set_owner(&program_id, &mint_key, &owner3_key, &owner2_key, &[]).unwrap(),
- vec![&mut mint_account, &mut owner3_account, &mut owner2_account],
- )
- );
- // owner did not sign
- let mut instruction =
- set_owner(&program_id, &mint_key, &owner2_key, &owner_key, &[]).unwrap();
- instruction.accounts[2].is_signer = false;
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- do_process_instruction(
- instruction,
- vec![&mut mint_account, &mut owner2_account, &mut owner_account],
- )
- );
- // set owner
- do_process_instruction(
- set_owner(&program_id, &mint_key, &owner2_key, &owner_key, &[]).unwrap(),
- vec![&mut mint_account, &mut owner2_account, &mut owner_account],
- )
- .unwrap();
- // create new mint without owner
- do_process_instruction(
- initialize_mint(&program_id, &mint2_key, Some(&account2_key), None, 1000, 2).unwrap(),
- vec![&mut mint2_account, &mut account2_account],
- )
- .unwrap();
- // set owner for non-mint-able token
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- set_owner(&program_id, &mint2_key, &owner2_key, &owner_key, &[]).unwrap(),
- vec![&mut mint_account, &mut owner2_account, &mut owner_account],
- )
- );
- }
- #[test]
- fn test_mint_to() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account3_key = pubkey_rand();
- let mut account3_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let mismatch_key = pubkey_rand();
- let mut mismatch_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let mint2_key = pubkey_rand();
- let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let uninitialized_key = pubkey_rand();
- let mut uninitialized_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account2_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account3_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create mismatch account
- do_process_instruction(
- initialize_account(&program_id, &mismatch_key, &mint2_key, &owner_key).unwrap(),
- vec![
- &mut mismatch_account,
- &mut mint2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // create new mint with owner
- do_process_instruction(
- initialize_mint(
- &program_id,
- &mint_key,
- Some(&account_key),
- Some(&owner_key),
- 1000,
- 2,
- )
- .unwrap(),
- vec![&mut mint_account, &mut account_account, &mut owner_account],
- )
- .unwrap();
- // mint to
- do_process_instruction(
- mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap(),
- vec![&mut mint_account, &mut account2_account, &mut owner_account],
- )
- .unwrap();
- let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
- let dest_account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
- assert_eq!(dest_account.amount, 42);
- // missing signer
- let mut instruction =
- mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap();
- instruction.accounts[2].is_signer = false;
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- do_process_instruction(
- instruction,
- vec![&mut mint_account, &mut account2_account, &mut owner_account],
- )
- );
- // mismatch account
- assert_eq!(
- Err(TokenError::MintMismatch.into()),
- do_process_instruction(
- mint_to(&program_id, &mint_key, &mismatch_key, &owner_key, &[], 42).unwrap(),
- vec![&mut mint_account, &mut mismatch_account, &mut owner_account],
- )
- );
- // missing owner
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- mint_to(&program_id, &mint_key, &account2_key, &owner2_key, &[], 42).unwrap(),
- vec![
- &mut mint_account,
- &mut account2_account,
- &mut owner2_account,
- ],
- )
- );
- // uninitialized destination account
- assert_eq!(
- Err(TokenError::UninitializedState.into()),
- do_process_instruction(
- mint_to(
- &program_id,
- &mint_key,
- &uninitialized_key,
- &owner_key,
- &[],
- 42
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut uninitialized_account,
- &mut owner_account,
- ],
- )
- );
- }
- #[test]
- fn test_burn() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account3_key = pubkey_rand();
- let mut account3_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let delegate_key = pubkey_rand();
- let mut delegate_account = SolanaAccount::default();
- let mismatch_key = pubkey_rand();
- let mut mismatch_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let mint2_key = pubkey_rand();
- let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // create account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account2_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account3_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create mismatch account
- do_process_instruction(
- initialize_account(&program_id, &mismatch_key, &mint2_key, &owner_key).unwrap(),
- vec![
- &mut mismatch_account,
- &mut mint2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // create new mint
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
- vec![&mut mint_account, &mut account_account],
- )
- .unwrap();
- // missing signer
- let mut instruction = burn(&program_id, &account_key, &delegate_key, &[], 42).unwrap();
- instruction.accounts[1].is_signer = false;
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- instruction,
- vec![&mut account_account, &mut delegate_account],
- )
- );
- // missing owner
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- burn(&program_id, &account_key, &owner2_key, &[], 42).unwrap(),
- vec![&mut account_account, &mut owner2_account],
- )
- );
- // burn
- do_process_instruction(
- burn(&program_id, &account_key, &owner_key, &[], 42).unwrap(),
- vec![&mut account_account, &mut owner_account],
- )
- .unwrap();
- let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert_eq!(account.amount, 1000 - 42);
- // insufficient funds
- assert_eq!(
- Err(TokenError::InsufficientFunds.into()),
- do_process_instruction(
- burn(&program_id, &account_key, &owner_key, &[], 100_000_000).unwrap(),
- vec![&mut account_account, &mut owner_account],
- )
- );
- // approve delegate
- do_process_instruction(
- approve(
- &program_id,
- &account_key,
- &delegate_key,
- &owner_key,
- &[],
- 84,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut delegate_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- // not a delegate of source account
- assert_eq!(
- Err(TokenError::InsufficientFunds.into()),
- do_process_instruction(
- burn(&program_id, &account_key, &owner_key, &[], 100_000_000).unwrap(),
- vec![&mut account_account, &mut owner_account],
- )
- );
- // burn via delegate
- do_process_instruction(
- burn(&program_id, &account_key, &delegate_key, &[], 84).unwrap(),
- vec![&mut account_account, &mut delegate_account],
- )
- .unwrap();
- // match
- let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert_eq!(account.amount, 1000 - 42 - 84);
- // insufficient funds approved via delegate
- assert_eq!(
- Err(TokenError::OwnerMismatch.into()),
- do_process_instruction(
- burn(&program_id, &account_key, &delegate_key, &[], 100).unwrap(),
- vec![&mut account_account, &mut delegate_account],
- )
- );
- }
- #[test]
- fn test_multisig() {
- let program_id = pubkey_rand();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let account_key = pubkey_rand();
- let mut account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let multisig_key = pubkey_rand();
- let mut multisig_account = SolanaAccount::new(0, size_of::<Multisig>(), &program_id);
- let multisig_delegate_key = pubkey_rand();
- let mut multisig_delegate_account =
- SolanaAccount::new(0, size_of::<Multisig>(), &program_id);
- let signer_keys = vec![pubkey_rand(); MAX_SIGNERS];
- let signer_key_refs: Vec<&Pubkey> = signer_keys.iter().map(|key| key).collect();
- let mut signer_accounts = vec![SolanaAccount::new(0, 0, &program_id); MAX_SIGNERS];
- // single signer
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- initialize_multisig(&program_id, &multisig_key, &[&signer_keys[0]], 1).unwrap(),
- vec![
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // multiple signer
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- initialize_multisig(
- &program_id,
- &multisig_delegate_key,
- &signer_key_refs,
- MAX_SIGNERS as u8,
- )
- .unwrap(),
- vec![
- &mut multisig_delegate_account,
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // create account with multisig owner
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &multisig_key).unwrap(),
- vec![&mut account, &mut mint_account, &mut multisig_account],
- )
- .unwrap();
- // create another account with multisig owner
- do_process_instruction(
- initialize_account(
- &program_id,
- &account2_key,
- &mint_key,
- &multisig_delegate_key,
- )
- .unwrap(),
- vec![
- &mut account2_account,
- &mut mint_account,
- &mut multisig_account,
- ],
- )
- .unwrap();
- // create new m int with multisig owner
- do_process_instruction(
- initialize_mint(
- &program_id,
- &mint_key,
- Some(&account_key),
- Some(&multisig_key),
- 1000,
- 2,
- )
- .unwrap(),
- vec![&mut mint_account, &mut account, &mut multisig_account],
- )
- .unwrap();
- // approve
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- approve(
- &program_id,
- &account_key,
- &multisig_delegate_key,
- &multisig_key,
- &[&signer_keys[0]],
- 100,
- )
- .unwrap(),
- vec![
- &mut account,
- &mut multisig_delegate_account,
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // transfer
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &multisig_key,
- &[&signer_keys[0]],
- 42,
- )
- .unwrap(),
- vec![
- &mut account,
- &mut account2_account,
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // transfer via delegate
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &multisig_delegate_key,
- &signer_key_refs,
- 42,
- )
- .unwrap(),
- vec![
- &mut account,
- &mut account2_account,
- &mut multisig_delegate_account,
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // mint to
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- mint_to(
- &program_id,
- &mint_key,
- &account2_key,
- &multisig_key,
- &[&signer_keys[0]],
- 42,
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut account2_account,
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // burn
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- burn(
- &program_id,
- &account_key,
- &multisig_key,
- &[&signer_keys[0]],
- 42,
- )
- .unwrap(),
- vec![
- &mut account,
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // burn via delegate
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- burn(
- &program_id,
- &account_key,
- &multisig_delegate_key,
- &signer_key_refs,
- 42,
- )
- .unwrap(),
- vec![
- &mut account,
- &mut multisig_delegate_account,
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // do SetOwner on mint
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- set_owner(
- &program_id,
- &mint_key,
- &owner_key,
- &multisig_key,
- &[&signer_keys[0]],
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut owner_account,
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- // do SetOwner on account
- let account_info_iter = &mut signer_accounts.iter_mut();
- do_process_instruction(
- set_owner(
- &program_id,
- &account_key,
- &owner_key,
- &multisig_key,
- &[&signer_keys[0]],
- )
- .unwrap(),
- vec![
- &mut account,
- &mut owner_account,
- &mut multisig_account,
- &mut account_info_iter.next().unwrap(),
- ],
- )
- .unwrap();
- }
- #[test]
- fn test_validate_owner() {
- let program_id = pubkey_rand();
- let owner_key = pubkey_rand();
- let mut signer_keys = [Pubkey::default(); MAX_SIGNERS];
- for signer_key in signer_keys.iter_mut().take(MAX_SIGNERS) {
- *signer_key = pubkey_rand();
- }
- let mut signer_lamports = 0;
- let mut signer_data = vec![];
- let mut signers = vec![
- AccountInfo::new(
- &owner_key,
- true,
- false,
- &mut signer_lamports,
- &mut signer_data,
- &program_id,
- false,
- Epoch::default(),
- );
- MAX_SIGNERS + 1
- ];
- for (signer, key) in signers.iter_mut().zip(&signer_keys) {
- signer.key = key;
- }
- let mut lamports = 0;
- let mut data = vec![0; size_of::<Multisig>()];
- let mut multisig: &mut Multisig = state::unpack_unchecked(&mut data).unwrap();
- multisig.m = MAX_SIGNERS as u8;
- multisig.n = MAX_SIGNERS as u8;
- multisig.signers = signer_keys;
- multisig.is_initialized = true;
- let owner_account_info = AccountInfo::new(
- &owner_key,
- false,
- false,
- &mut lamports,
- &mut data,
- &program_id,
- false,
- Epoch::default(),
- );
- // full 11 of 11
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers).unwrap();
- // 1 of 11
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 1;
- }
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers).unwrap();
- // 2:1
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 2;
- multisig.n = 1;
- }
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers)
- );
- // 0:11
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 0;
- multisig.n = 11;
- }
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers).unwrap();
- // 2:11 but 0 provided
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 2;
- multisig.n = 11;
- }
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &[])
- );
- // 2:11 but 1 provided
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 2;
- multisig.n = 11;
- }
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers[0..1])
- );
- // 2:11, 2 from middle provided
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 2;
- multisig.n = 11;
- }
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers[5..7])
- .unwrap();
- // 11:11, one is not a signer
- {
- let mut data_ref_mut = owner_account_info.data.borrow_mut();
- let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
- multisig.m = 2;
- multisig.n = 11;
- }
- signers[5].is_signer = false;
- assert_eq!(
- Err(ProgramError::MissingRequiredSignature),
- Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers)
- );
- signers[5].is_signer = true;
- }
- #[test]
- fn test_close_account() {
- let program_id = pubkey_rand();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(42, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(2, size_of::<Account>(), &program_id);
- let account3_key = pubkey_rand();
- let mut account3_account = SolanaAccount::new(2, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- // uninitialized
- assert_eq!(
- Err(TokenError::UninitializedState.into()),
- do_process_instruction(
- close_account(&program_id, &account_key, &account3_key, &owner2_key, &[]).unwrap(),
- vec![
- &mut account_account,
- &mut account3_account,
- &mut owner2_account,
- ],
- )
- );
- // initialize non-native account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // initialize native account
- do_process_instruction(
- initialize_account(
- &program_id,
- &account2_key,
- &crate::native_mint::id(),
- &owner_key,
- )
- .unwrap(),
- vec![&mut account2_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account.amount, 2);
- // close non-native account
- assert_eq!(
- Err(TokenError::NonNativeNotSupported.into()),
- do_process_instruction(
- close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
- vec![
- &mut account_account,
- &mut account3_account,
- &mut owner_account,
- ],
- )
- );
- assert_eq!(account_account.lamports, 42);
- // close native account
- do_process_instruction(
- close_account(&program_id, &account2_key, &account3_key, &owner_key, &[]).unwrap(),
- vec![
- &mut account2_account,
- &mut account3_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- let account: &mut Account = state::unpack_unchecked(&mut account2_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account.amount, 0);
- assert_eq!(account3_account.lamports, 4);
- }
- #[test]
- fn test_native_token() {
- let program_id = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(42, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(2, size_of::<Account>(), &program_id);
- let account3_key = pubkey_rand();
- let mut account3_account = SolanaAccount::new(2, 0, &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- // initialize native account
- do_process_instruction(
- initialize_account(
- &program_id,
- &account_key,
- &crate::native_mint::id(),
- &owner_key,
- )
- .unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account.amount, 42);
- // initialize native account
- do_process_instruction(
- initialize_account(
- &program_id,
- &account2_key,
- &crate::native_mint::id(),
- &owner_key,
- )
- .unwrap(),
- vec![&mut account2_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account.amount, 2);
- // mint_to unsupported
- assert_eq!(
- Err(TokenError::NativeNotSupported.into()),
- do_process_instruction(
- mint_to(
- &program_id,
- &crate::native_mint::id(),
- &account_key,
- &owner_key,
- &[],
- 42
- )
- .unwrap(),
- vec![&mut mint_account, &mut account_account, &mut owner_account],
- )
- );
- // burn unsupported
- assert_eq!(
- Err(TokenError::NativeNotSupported.into()),
- do_process_instruction(
- burn(&program_id, &account_key, &owner_key, &[], 42).unwrap(),
- vec![&mut account_account, &mut owner_account],
- )
- );
- // initialize native account
- do_process_instruction(
- transfer(
- &program_id,
- &account_key,
- &account2_key,
- &owner_key,
- &[],
- 40,
- )
- .unwrap(),
- vec![
- &mut account_account,
- &mut account2_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account_account.lamports, 2);
- assert_eq!(account.amount, 2);
- let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account2_account.lamports, 42);
- assert_eq!(account.amount, 42);
- // close native account
- do_process_instruction(
- close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
- vec![
- &mut account_account,
- &mut account3_account,
- &mut owner_account,
- ],
- )
- .unwrap();
- let account: &mut Account = state::unpack_unchecked(&mut account_account.data).unwrap();
- assert!(account.is_native);
- assert_eq!(account_account.lamports, 0);
- assert_eq!(account.amount, 0);
- assert_eq!(account3_account.lamports, 4);
- }
- #[test]
- fn test_overflow() {
- let program_id = pubkey_rand();
- let account_key = pubkey_rand();
- let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let account2_key = pubkey_rand();
- let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
- let owner_key = pubkey_rand();
- let mut owner_account = SolanaAccount::default();
- let owner2_key = pubkey_rand();
- let mut owner2_account = SolanaAccount::default();
- let mint_owner_key = pubkey_rand();
- let mut mint_owner_account = SolanaAccount::default();
- let mint_key = pubkey_rand();
- let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
- // create victim account
- do_process_instruction(
- initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
- vec![&mut account_account, &mut mint_account, &mut owner_account],
- )
- .unwrap();
- // create another account
- do_process_instruction(
- initialize_account(&program_id, &account2_key, &mint_key, &owner2_key).unwrap(),
- vec![
- &mut account2_account,
- &mut mint_account,
- &mut owner2_account,
- ],
- )
- .unwrap();
- // create new mint with owner
- do_process_instruction(
- initialize_mint(&program_id, &mint_key, None, Some(&mint_owner_key), 0, 2).unwrap(),
- vec![&mut mint_account, &mut mint_owner_account],
- )
- .unwrap();
- // mint the max to attacker
- do_process_instruction(
- mint_to(
- &program_id,
- &mint_key,
- &account2_key,
- &mint_owner_key,
- &[],
- 42,
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut account2_account,
- &mut mint_owner_account,
- ],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
- assert_eq!(account.amount, 42);
- // mint the max to victum
- do_process_instruction(
- mint_to(
- &program_id,
- &mint_key,
- &account_key,
- &mint_owner_key,
- &[],
- u64::MAX,
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut account_account,
- &mut mint_owner_account,
- ],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert_eq!(account.amount, u64::MAX);
- // mint one more
- assert_eq!(
- Err(TokenError::Overflow.into()),
- do_process_instruction(
- mint_to(
- &program_id,
- &mint_key,
- &account_key,
- &mint_owner_key,
- &[],
- 1,
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut account_account,
- &mut mint_owner_account,
- ],
- )
- );
- // mint back to large amount
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- account.amount = 0;
- do_process_instruction(
- mint_to(
- &program_id,
- &mint_key,
- &account_key,
- &mint_owner_key,
- &[],
- u64::MAX,
- )
- .unwrap(),
- vec![
- &mut mint_account,
- &mut account_account,
- &mut mint_owner_account,
- ],
- )
- .unwrap();
- let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
- assert_eq!(account.amount, u64::MAX);
- // transfer to burn victim
- assert_eq!(
- Err(TokenError::Overflow.into()),
- do_process_instruction(
- transfer(
- &program_id,
- &account2_key,
- &account_key,
- &owner2_key,
- &[],
- 1,
- )
- .unwrap(),
- vec![
- &mut account2_account,
- &mut account_account,
- &mut owner2_account,
- ],
- )
- );
- }
- }
|