processor.rs 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706
  1. #![cfg(feature = "test-sbf")]
  2. //! Program state processor tests
  3. use {
  4. mollusk_svm::Mollusk,
  5. serial_test::serial,
  6. solana_sdk::{
  7. account::{
  8. create_account_for_test, Account as SolanaAccount, AccountSharedData, ReadableAccount,
  9. },
  10. account_info::{AccountInfo, IntoAccountInfo},
  11. entrypoint::ProgramResult,
  12. instruction::Instruction,
  13. program_error::ProgramError,
  14. program_option::COption,
  15. program_pack::Pack,
  16. pubkey::Pubkey,
  17. rent::Rent,
  18. sysvar::rent,
  19. },
  20. spl_token::{
  21. error::TokenError,
  22. instruction::{
  23. amount_to_ui_amount, approve, approve_checked, burn, burn_checked, close_account,
  24. freeze_account, get_account_data_size, initialize_account, initialize_account2,
  25. initialize_account3, initialize_immutable_owner, initialize_mint, initialize_mint2,
  26. initialize_multisig, initialize_multisig2, mint_to, mint_to_checked, revoke,
  27. set_authority, sync_native, thaw_account, transfer, transfer_checked,
  28. ui_amount_to_amount, AuthorityType, MAX_SIGNERS,
  29. },
  30. state::{Account, AccountState, Mint, Multisig},
  31. },
  32. std::{
  33. collections::HashMap,
  34. sync::{Arc, RwLock},
  35. },
  36. };
  37. lazy_static::lazy_static! {
  38. static ref EXPECTED_DATA: Arc<RwLock<Vec<u8>>> = Arc::new(RwLock::new(Vec::new()));
  39. }
  40. fn do_process_instruction(
  41. instruction: Instruction,
  42. mut accounts: Vec<&mut SolanaAccount>,
  43. ) -> ProgramResult {
  44. // Prepare accounts for mollusk.
  45. let instruction_accounts: Vec<(Pubkey, AccountSharedData)> = instruction
  46. .accounts
  47. .iter()
  48. .zip(&accounts)
  49. .map(|(account_meta, account)| {
  50. (
  51. account_meta.pubkey,
  52. AccountSharedData::from((*account).clone()),
  53. )
  54. })
  55. .collect();
  56. let mollusk = Mollusk::new(&spl_token::ID, "spl_token");
  57. let result = mollusk.process_instruction(&instruction, &instruction_accounts);
  58. // Update accounts after the instruction is processed.
  59. for (original, (_, updated)) in accounts
  60. .iter_mut()
  61. .zip(result.resulting_accounts.into_iter())
  62. {
  63. original.data = updated.data().to_vec();
  64. original.lamports = updated.lamports();
  65. original.owner = *updated.owner();
  66. }
  67. result
  68. .raw_result
  69. .map_err(|e| ProgramError::try_from(e).unwrap())
  70. }
  71. fn do_process_instruction_dups(
  72. instruction: Instruction,
  73. account_infos: Vec<AccountInfo>,
  74. ) -> ProgramResult {
  75. let mut cached_accounts = HashMap::new();
  76. let mut dedup_accounts = Vec::new();
  77. // Dedup accounts for mollusk.
  78. account_infos.iter().for_each(|account_info| {
  79. if !cached_accounts.contains_key(account_info.key) {
  80. let account = SolanaAccount {
  81. lamports: account_info.lamports(),
  82. data: account_info.try_borrow_data().unwrap().to_vec(),
  83. owner: *account_info.owner,
  84. executable: account_info.executable,
  85. rent_epoch: account_info.rent_epoch,
  86. };
  87. dedup_accounts.push((*account_info.key, AccountSharedData::from(account)));
  88. cached_accounts.insert(account_info.key, account_info);
  89. }
  90. });
  91. let mollusk = Mollusk::new(&spl_token::ID, "spl_token");
  92. let result = mollusk.process_instruction(&instruction, &dedup_accounts);
  93. // Update accounts after the instruction is processed.
  94. result
  95. .resulting_accounts
  96. .into_iter()
  97. .for_each(|(pubkey, account)| {
  98. let account_info = cached_accounts.get(&pubkey).unwrap();
  99. if account.data().is_empty() {
  100. // When the account is closed, the tests expect the data to
  101. // be zeroed.
  102. account_info.try_borrow_mut_data().unwrap().fill(0);
  103. } else {
  104. account_info
  105. .try_borrow_mut_data()
  106. .unwrap()
  107. .copy_from_slice(account.data());
  108. }
  109. **account_info.try_borrow_mut_lamports().unwrap() = account.lamports();
  110. account_info.assign(account.owner());
  111. });
  112. result
  113. .raw_result
  114. .map_err(|e| ProgramError::try_from(e).unwrap())
  115. }
  116. fn set_expected_data(expected_data: Vec<u8>) {
  117. *EXPECTED_DATA.write().unwrap() = expected_data;
  118. }
  119. fn rent_sysvar() -> SolanaAccount {
  120. create_account_for_test(&Rent::default())
  121. }
  122. fn mint_minimum_balance() -> u64 {
  123. Rent::default().minimum_balance(Mint::get_packed_len())
  124. }
  125. fn account_minimum_balance() -> u64 {
  126. Rent::default().minimum_balance(Account::get_packed_len())
  127. }
  128. fn multisig_minimum_balance() -> u64 {
  129. Rent::default().minimum_balance(Multisig::get_packed_len())
  130. }
  131. #[test]
  132. fn test_initialize_mint() {
  133. let program_id = spl_token::id();
  134. let owner_key = Pubkey::new_unique();
  135. let mint_key = Pubkey::new_unique();
  136. let mut mint_account = SolanaAccount::new(42, Mint::get_packed_len(), &program_id);
  137. let mint2_key = Pubkey::new_unique();
  138. let mut mint2_account =
  139. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  140. let mut rent_sysvar = rent_sysvar();
  141. // mint is not rent exempt
  142. assert_eq!(
  143. Err(TokenError::NotRentExempt.into()),
  144. do_process_instruction(
  145. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  146. vec![&mut mint_account, &mut rent_sysvar]
  147. )
  148. );
  149. mint_account.lamports = mint_minimum_balance();
  150. // create new mint
  151. do_process_instruction(
  152. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  153. vec![&mut mint_account, &mut rent_sysvar],
  154. )
  155. .unwrap();
  156. // create twice
  157. assert_eq!(
  158. Err(TokenError::AlreadyInUse.into()),
  159. do_process_instruction(
  160. initialize_mint(&program_id, &mint_key, &owner_key, None, 2,).unwrap(),
  161. vec![&mut mint_account, &mut rent_sysvar]
  162. )
  163. );
  164. // create another mint that can freeze
  165. do_process_instruction(
  166. initialize_mint(&program_id, &mint2_key, &owner_key, Some(&owner_key), 2).unwrap(),
  167. vec![&mut mint2_account, &mut rent_sysvar],
  168. )
  169. .unwrap();
  170. let mint = Mint::unpack_unchecked(&mint2_account.data).unwrap();
  171. assert_eq!(mint.freeze_authority, COption::Some(owner_key));
  172. }
  173. #[test]
  174. fn test_initialize_mint2() {
  175. let program_id = spl_token::id();
  176. let owner_key = Pubkey::new_unique();
  177. let mint_key = Pubkey::new_unique();
  178. let mut mint_account = SolanaAccount::new(42, Mint::get_packed_len(), &program_id);
  179. let mint2_key = Pubkey::new_unique();
  180. let mut mint2_account =
  181. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  182. // mint is not rent exempt
  183. assert_eq!(
  184. Err(TokenError::NotRentExempt.into()),
  185. do_process_instruction(
  186. initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  187. vec![&mut mint_account]
  188. )
  189. );
  190. mint_account.lamports = mint_minimum_balance();
  191. // create new mint
  192. do_process_instruction(
  193. initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  194. vec![&mut mint_account],
  195. )
  196. .unwrap();
  197. // create twice
  198. assert_eq!(
  199. Err(TokenError::AlreadyInUse.into()),
  200. do_process_instruction(
  201. initialize_mint2(&program_id, &mint_key, &owner_key, None, 2,).unwrap(),
  202. vec![&mut mint_account]
  203. )
  204. );
  205. // create another mint that can freeze
  206. do_process_instruction(
  207. initialize_mint2(&program_id, &mint2_key, &owner_key, Some(&owner_key), 2).unwrap(),
  208. vec![&mut mint2_account],
  209. )
  210. .unwrap();
  211. let mint = Mint::unpack_unchecked(&mint2_account.data).unwrap();
  212. assert_eq!(mint.freeze_authority, COption::Some(owner_key));
  213. }
  214. #[test]
  215. fn test_initialize_mint_account() {
  216. let program_id = spl_token::id();
  217. let account_key = Pubkey::new_unique();
  218. let mut account_account = SolanaAccount::new(42, Account::get_packed_len(), &program_id);
  219. let owner_key = Pubkey::new_unique();
  220. let mut owner_account = SolanaAccount::default();
  221. let mint_key = Pubkey::new_unique();
  222. let mut mint_account =
  223. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  224. let mut rent_sysvar = rent_sysvar();
  225. // account is not rent exempt
  226. assert_eq!(
  227. Err(TokenError::NotRentExempt.into()),
  228. do_process_instruction(
  229. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  230. vec![
  231. &mut account_account,
  232. &mut mint_account,
  233. &mut owner_account,
  234. &mut rent_sysvar
  235. ],
  236. )
  237. );
  238. account_account.lamports = account_minimum_balance();
  239. // mint is not valid (not initialized)
  240. assert_eq!(
  241. Err(TokenError::InvalidMint.into()),
  242. do_process_instruction(
  243. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  244. vec![
  245. &mut account_account,
  246. &mut mint_account,
  247. &mut owner_account,
  248. &mut rent_sysvar
  249. ],
  250. )
  251. );
  252. // create mint
  253. do_process_instruction(
  254. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  255. vec![&mut mint_account, &mut rent_sysvar],
  256. )
  257. .unwrap();
  258. // mint not owned by program
  259. let not_program_id = Pubkey::new_unique();
  260. mint_account.owner = not_program_id;
  261. assert_eq!(
  262. Err(ProgramError::IncorrectProgramId),
  263. do_process_instruction(
  264. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  265. vec![
  266. &mut account_account,
  267. &mut mint_account,
  268. &mut owner_account,
  269. &mut rent_sysvar
  270. ],
  271. )
  272. );
  273. mint_account.owner = program_id;
  274. // create account
  275. do_process_instruction(
  276. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  277. vec![
  278. &mut account_account,
  279. &mut mint_account,
  280. &mut owner_account,
  281. &mut rent_sysvar,
  282. ],
  283. )
  284. .unwrap();
  285. // create twice
  286. assert_eq!(
  287. Err(TokenError::AlreadyInUse.into()),
  288. do_process_instruction(
  289. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  290. vec![
  291. &mut account_account,
  292. &mut mint_account,
  293. &mut owner_account,
  294. &mut rent_sysvar
  295. ],
  296. )
  297. );
  298. }
  299. #[test]
  300. fn test_transfer_dups() {
  301. let program_id = spl_token::id();
  302. let account1_key = Pubkey::new_unique();
  303. let mut account1_account = SolanaAccount::new(
  304. account_minimum_balance(),
  305. Account::get_packed_len(),
  306. &program_id,
  307. );
  308. let mut account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  309. let account2_key = Pubkey::new_unique();
  310. let mut account2_account = SolanaAccount::new(
  311. account_minimum_balance(),
  312. Account::get_packed_len(),
  313. &program_id,
  314. );
  315. let mut account2_info: AccountInfo = (&account2_key, false, &mut account2_account).into();
  316. let account3_key = Pubkey::new_unique();
  317. let mut account3_account = SolanaAccount::new(
  318. account_minimum_balance(),
  319. Account::get_packed_len(),
  320. &program_id,
  321. );
  322. let account3_info: AccountInfo = (&account3_key, false, &mut account3_account).into();
  323. let account4_key = Pubkey::new_unique();
  324. let mut account4_account = SolanaAccount::new(
  325. account_minimum_balance(),
  326. Account::get_packed_len(),
  327. &program_id,
  328. );
  329. let account4_info: AccountInfo = (&account4_key, true, &mut account4_account).into();
  330. let multisig_key = Pubkey::new_unique();
  331. let mut multisig_account = SolanaAccount::new(
  332. multisig_minimum_balance(),
  333. Multisig::get_packed_len(),
  334. &program_id,
  335. );
  336. let multisig_info: AccountInfo = (&multisig_key, true, &mut multisig_account).into();
  337. let owner_key = Pubkey::new_unique();
  338. let mut owner_account = SolanaAccount::default();
  339. let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
  340. let mint_key = Pubkey::new_unique();
  341. let mut mint_account =
  342. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  343. let mint_info: AccountInfo = (&mint_key, false, &mut mint_account).into();
  344. let rent_key = rent::id();
  345. let mut rent_sysvar = rent_sysvar();
  346. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  347. // create mint
  348. do_process_instruction_dups(
  349. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  350. vec![mint_info.clone(), rent_info.clone()],
  351. )
  352. .unwrap();
  353. // create account
  354. do_process_instruction_dups(
  355. initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
  356. vec![
  357. account1_info.clone(),
  358. mint_info.clone(),
  359. account1_info.clone(),
  360. rent_info.clone(),
  361. ],
  362. )
  363. .unwrap();
  364. // create another account
  365. do_process_instruction_dups(
  366. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  367. vec![
  368. account2_info.clone(),
  369. mint_info.clone(),
  370. owner_info.clone(),
  371. rent_info.clone(),
  372. ],
  373. )
  374. .unwrap();
  375. // mint to account
  376. do_process_instruction_dups(
  377. mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
  378. vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
  379. )
  380. .unwrap();
  381. // source-owner transfer
  382. do_process_instruction_dups(
  383. transfer(
  384. &program_id,
  385. &account1_key,
  386. &account2_key,
  387. &account1_key,
  388. &[],
  389. 500,
  390. )
  391. .unwrap(),
  392. vec![
  393. account1_info.clone(),
  394. account2_info.clone(),
  395. account1_info.clone(),
  396. ],
  397. )
  398. .unwrap();
  399. // source-owner TransferChecked
  400. do_process_instruction_dups(
  401. transfer_checked(
  402. &program_id,
  403. &account1_key,
  404. &mint_key,
  405. &account2_key,
  406. &account1_key,
  407. &[],
  408. 500,
  409. 2,
  410. )
  411. .unwrap(),
  412. vec![
  413. account1_info.clone(),
  414. mint_info.clone(),
  415. account2_info.clone(),
  416. account1_info.clone(),
  417. ],
  418. )
  419. .unwrap();
  420. // source-delegate transfer
  421. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  422. account.amount = 1000;
  423. account.delegated_amount = 1000;
  424. account.delegate = COption::Some(account1_key);
  425. account.owner = owner_key;
  426. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  427. do_process_instruction_dups(
  428. transfer(
  429. &program_id,
  430. &account1_key,
  431. &account2_key,
  432. &account1_key,
  433. &[],
  434. 500,
  435. )
  436. .unwrap(),
  437. vec![
  438. account1_info.clone(),
  439. account2_info.clone(),
  440. account1_info.clone(),
  441. ],
  442. )
  443. .unwrap();
  444. // source-delegate TransferChecked
  445. do_process_instruction_dups(
  446. transfer_checked(
  447. &program_id,
  448. &account1_key,
  449. &mint_key,
  450. &account2_key,
  451. &account1_key,
  452. &[],
  453. 500,
  454. 2,
  455. )
  456. .unwrap(),
  457. vec![
  458. account1_info.clone(),
  459. mint_info.clone(),
  460. account2_info.clone(),
  461. account1_info.clone(),
  462. ],
  463. )
  464. .unwrap();
  465. // test destination-owner transfer
  466. do_process_instruction_dups(
  467. initialize_account(&program_id, &account3_key, &mint_key, &account2_key).unwrap(),
  468. vec![
  469. account3_info.clone(),
  470. mint_info.clone(),
  471. account2_info.clone(),
  472. rent_info.clone(),
  473. ],
  474. )
  475. .unwrap();
  476. do_process_instruction_dups(
  477. mint_to(&program_id, &mint_key, &account3_key, &owner_key, &[], 1000).unwrap(),
  478. vec![mint_info.clone(), account3_info.clone(), owner_info.clone()],
  479. )
  480. .unwrap();
  481. account1_info.is_signer = false;
  482. account2_info.is_signer = true;
  483. do_process_instruction_dups(
  484. transfer(
  485. &program_id,
  486. &account3_key,
  487. &account2_key,
  488. &account2_key,
  489. &[],
  490. 500,
  491. )
  492. .unwrap(),
  493. vec![
  494. account3_info.clone(),
  495. account2_info.clone(),
  496. account2_info.clone(),
  497. ],
  498. )
  499. .unwrap();
  500. // destination-owner TransferChecked
  501. do_process_instruction_dups(
  502. transfer_checked(
  503. &program_id,
  504. &account3_key,
  505. &mint_key,
  506. &account2_key,
  507. &account2_key,
  508. &[],
  509. 500,
  510. 2,
  511. )
  512. .unwrap(),
  513. vec![
  514. account3_info.clone(),
  515. mint_info.clone(),
  516. account2_info.clone(),
  517. account2_info.clone(),
  518. ],
  519. )
  520. .unwrap();
  521. // test source-multisig signer
  522. do_process_instruction_dups(
  523. initialize_multisig(&program_id, &multisig_key, &[&account4_key], 1).unwrap(),
  524. vec![
  525. multisig_info.clone(),
  526. rent_info.clone(),
  527. account4_info.clone(),
  528. ],
  529. )
  530. .unwrap();
  531. do_process_instruction_dups(
  532. initialize_account(&program_id, &account4_key, &mint_key, &multisig_key).unwrap(),
  533. vec![
  534. account4_info.clone(),
  535. mint_info.clone(),
  536. multisig_info.clone(),
  537. rent_info.clone(),
  538. ],
  539. )
  540. .unwrap();
  541. do_process_instruction_dups(
  542. mint_to(&program_id, &mint_key, &account4_key, &owner_key, &[], 1000).unwrap(),
  543. vec![mint_info.clone(), account4_info.clone(), owner_info.clone()],
  544. )
  545. .unwrap();
  546. // source-multisig-signer transfer
  547. do_process_instruction_dups(
  548. transfer(
  549. &program_id,
  550. &account4_key,
  551. &account2_key,
  552. &multisig_key,
  553. &[&account4_key],
  554. 500,
  555. )
  556. .unwrap(),
  557. vec![
  558. account4_info.clone(),
  559. account2_info.clone(),
  560. multisig_info.clone(),
  561. account4_info.clone(),
  562. ],
  563. )
  564. .unwrap();
  565. // source-multisig-signer TransferChecked
  566. do_process_instruction_dups(
  567. transfer_checked(
  568. &program_id,
  569. &account4_key,
  570. &mint_key,
  571. &account2_key,
  572. &multisig_key,
  573. &[&account4_key],
  574. 500,
  575. 2,
  576. )
  577. .unwrap(),
  578. vec![
  579. account4_info.clone(),
  580. mint_info.clone(),
  581. account2_info.clone(),
  582. multisig_info.clone(),
  583. account4_info.clone(),
  584. ],
  585. )
  586. .unwrap();
  587. }
  588. #[test]
  589. fn test_transfer() {
  590. let program_id = spl_token::id();
  591. let account_key = Pubkey::new_unique();
  592. let mut account_account = SolanaAccount::new(
  593. account_minimum_balance(),
  594. Account::get_packed_len(),
  595. &program_id,
  596. );
  597. let account2_key = Pubkey::new_unique();
  598. let mut account2_account = SolanaAccount::new(
  599. account_minimum_balance(),
  600. Account::get_packed_len(),
  601. &program_id,
  602. );
  603. let account3_key = Pubkey::new_unique();
  604. let mut account3_account = SolanaAccount::new(
  605. account_minimum_balance(),
  606. Account::get_packed_len(),
  607. &program_id,
  608. );
  609. let delegate_key = Pubkey::new_unique();
  610. let mut delegate_account = SolanaAccount::default();
  611. let mismatch_key = Pubkey::new_unique();
  612. let mut mismatch_account = SolanaAccount::new(
  613. account_minimum_balance(),
  614. Account::get_packed_len(),
  615. &program_id,
  616. );
  617. let owner_key = Pubkey::new_unique();
  618. let mut owner_account = SolanaAccount::default();
  619. let owner2_key = Pubkey::new_unique();
  620. let mut owner2_account = SolanaAccount::default();
  621. let mint_key = Pubkey::new_unique();
  622. let mut mint_account =
  623. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  624. let mint2_key = Pubkey::new_unique();
  625. let mut rent_sysvar = rent_sysvar();
  626. // create mint
  627. do_process_instruction(
  628. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  629. vec![&mut mint_account, &mut rent_sysvar],
  630. )
  631. .unwrap();
  632. // create account
  633. do_process_instruction(
  634. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  635. vec![
  636. &mut account_account,
  637. &mut mint_account,
  638. &mut owner_account,
  639. &mut rent_sysvar,
  640. ],
  641. )
  642. .unwrap();
  643. // create another account
  644. do_process_instruction(
  645. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  646. vec![
  647. &mut account2_account,
  648. &mut mint_account,
  649. &mut owner_account,
  650. &mut rent_sysvar,
  651. ],
  652. )
  653. .unwrap();
  654. // create another account
  655. do_process_instruction(
  656. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  657. vec![
  658. &mut account3_account,
  659. &mut mint_account,
  660. &mut owner_account,
  661. &mut rent_sysvar,
  662. ],
  663. )
  664. .unwrap();
  665. // create mismatch account
  666. do_process_instruction(
  667. initialize_account(&program_id, &mismatch_key, &mint_key, &owner_key).unwrap(),
  668. vec![
  669. &mut mismatch_account,
  670. &mut mint_account,
  671. &mut owner_account,
  672. &mut rent_sysvar,
  673. ],
  674. )
  675. .unwrap();
  676. let mut account = Account::unpack_unchecked(&mismatch_account.data).unwrap();
  677. account.mint = mint2_key;
  678. Account::pack(account, &mut mismatch_account.data).unwrap();
  679. // mint to account
  680. do_process_instruction(
  681. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  682. vec![&mut mint_account, &mut account_account, &mut owner_account],
  683. )
  684. .unwrap();
  685. // missing signer
  686. let mut instruction = transfer(
  687. &program_id,
  688. &account_key,
  689. &account2_key,
  690. &owner_key,
  691. &[],
  692. 1000,
  693. )
  694. .unwrap();
  695. instruction.accounts[2].is_signer = false;
  696. assert_eq!(
  697. Err(ProgramError::MissingRequiredSignature),
  698. do_process_instruction(
  699. instruction,
  700. vec![
  701. &mut account_account,
  702. &mut account2_account,
  703. &mut owner_account,
  704. ],
  705. )
  706. );
  707. // mismatch mint
  708. assert_eq!(
  709. Err(TokenError::MintMismatch.into()),
  710. do_process_instruction(
  711. transfer(
  712. &program_id,
  713. &account_key,
  714. &mismatch_key,
  715. &owner_key,
  716. &[],
  717. 1000
  718. )
  719. .unwrap(),
  720. vec![
  721. &mut account_account,
  722. &mut mismatch_account,
  723. &mut owner_account,
  724. ],
  725. )
  726. );
  727. // missing owner
  728. assert_eq!(
  729. Err(TokenError::OwnerMismatch.into()),
  730. do_process_instruction(
  731. transfer(
  732. &program_id,
  733. &account_key,
  734. &account2_key,
  735. &owner2_key,
  736. &[],
  737. 1000
  738. )
  739. .unwrap(),
  740. vec![
  741. &mut account_account,
  742. &mut account2_account,
  743. &mut owner2_account,
  744. ],
  745. )
  746. );
  747. // account not owned by program
  748. let not_program_id = Pubkey::new_unique();
  749. account_account.owner = not_program_id;
  750. assert_eq!(
  751. Err(ProgramError::IncorrectProgramId),
  752. do_process_instruction(
  753. transfer(&program_id, &account_key, &account2_key, &owner_key, &[], 0,).unwrap(),
  754. vec![
  755. &mut account_account,
  756. &mut account2_account,
  757. &mut owner2_account,
  758. ],
  759. )
  760. );
  761. account_account.owner = program_id;
  762. // account 2 not owned by program
  763. let not_program_id = Pubkey::new_unique();
  764. account2_account.owner = not_program_id;
  765. assert_eq!(
  766. Err(ProgramError::IncorrectProgramId),
  767. do_process_instruction(
  768. transfer(&program_id, &account_key, &account2_key, &owner_key, &[], 0,).unwrap(),
  769. vec![
  770. &mut account_account,
  771. &mut account2_account,
  772. &mut owner2_account,
  773. ],
  774. )
  775. );
  776. account2_account.owner = program_id;
  777. // transfer
  778. do_process_instruction(
  779. transfer(
  780. &program_id,
  781. &account_key,
  782. &account2_key,
  783. &owner_key,
  784. &[],
  785. 1000,
  786. )
  787. .unwrap(),
  788. vec![
  789. &mut account_account,
  790. &mut account2_account,
  791. &mut owner_account,
  792. ],
  793. )
  794. .unwrap();
  795. // insufficient funds
  796. assert_eq!(
  797. Err(TokenError::InsufficientFunds.into()),
  798. do_process_instruction(
  799. transfer(&program_id, &account_key, &account2_key, &owner_key, &[], 1).unwrap(),
  800. vec![
  801. &mut account_account,
  802. &mut account2_account,
  803. &mut owner_account,
  804. ],
  805. )
  806. );
  807. // transfer half back
  808. do_process_instruction(
  809. transfer(
  810. &program_id,
  811. &account2_key,
  812. &account_key,
  813. &owner_key,
  814. &[],
  815. 500,
  816. )
  817. .unwrap(),
  818. vec![
  819. &mut account2_account,
  820. &mut account_account,
  821. &mut owner_account,
  822. ],
  823. )
  824. .unwrap();
  825. // incorrect decimals
  826. assert_eq!(
  827. Err(TokenError::MintDecimalsMismatch.into()),
  828. do_process_instruction(
  829. transfer_checked(
  830. &program_id,
  831. &account2_key,
  832. &mint_key,
  833. &account_key,
  834. &owner_key,
  835. &[],
  836. 1,
  837. 10 // <-- incorrect decimals
  838. )
  839. .unwrap(),
  840. vec![
  841. &mut account2_account,
  842. &mut mint_account,
  843. &mut account_account,
  844. &mut owner_account,
  845. ],
  846. )
  847. );
  848. // incorrect mint
  849. assert_eq!(
  850. Err(TokenError::MintMismatch.into()),
  851. do_process_instruction(
  852. transfer_checked(
  853. &program_id,
  854. &account2_key,
  855. &account3_key, // <-- incorrect mint
  856. &account_key,
  857. &owner_key,
  858. &[],
  859. 1,
  860. 2
  861. )
  862. .unwrap(),
  863. vec![
  864. &mut account2_account,
  865. &mut account3_account, // <-- incorrect mint
  866. &mut account_account,
  867. &mut owner_account,
  868. ],
  869. )
  870. );
  871. // transfer rest with explicit decimals
  872. do_process_instruction(
  873. transfer_checked(
  874. &program_id,
  875. &account2_key,
  876. &mint_key,
  877. &account_key,
  878. &owner_key,
  879. &[],
  880. 500,
  881. 2,
  882. )
  883. .unwrap(),
  884. vec![
  885. &mut account2_account,
  886. &mut mint_account,
  887. &mut account_account,
  888. &mut owner_account,
  889. ],
  890. )
  891. .unwrap();
  892. // insufficient funds
  893. assert_eq!(
  894. Err(TokenError::InsufficientFunds.into()),
  895. do_process_instruction(
  896. transfer(&program_id, &account2_key, &account_key, &owner_key, &[], 1).unwrap(),
  897. vec![
  898. &mut account2_account,
  899. &mut account_account,
  900. &mut owner_account,
  901. ],
  902. )
  903. );
  904. // approve delegate
  905. do_process_instruction(
  906. approve(
  907. &program_id,
  908. &account_key,
  909. &delegate_key,
  910. &owner_key,
  911. &[],
  912. 100,
  913. )
  914. .unwrap(),
  915. vec![
  916. &mut account_account,
  917. &mut delegate_account,
  918. &mut owner_account,
  919. ],
  920. )
  921. .unwrap();
  922. // not a delegate of source account
  923. assert_eq!(
  924. Err(TokenError::OwnerMismatch.into()),
  925. do_process_instruction(
  926. transfer(
  927. &program_id,
  928. &account_key,
  929. &account2_key,
  930. &owner2_key, // <-- incorrect owner or delegate
  931. &[],
  932. 1,
  933. )
  934. .unwrap(),
  935. vec![
  936. &mut account_account,
  937. &mut account2_account,
  938. &mut owner2_account,
  939. ],
  940. )
  941. );
  942. // insufficient funds approved via delegate
  943. assert_eq!(
  944. Err(TokenError::InsufficientFunds.into()),
  945. do_process_instruction(
  946. transfer(
  947. &program_id,
  948. &account_key,
  949. &account2_key,
  950. &delegate_key,
  951. &[],
  952. 101
  953. )
  954. .unwrap(),
  955. vec![
  956. &mut account_account,
  957. &mut account2_account,
  958. &mut delegate_account,
  959. ],
  960. )
  961. );
  962. // transfer via delegate
  963. do_process_instruction(
  964. transfer(
  965. &program_id,
  966. &account_key,
  967. &account2_key,
  968. &delegate_key,
  969. &[],
  970. 100,
  971. )
  972. .unwrap(),
  973. vec![
  974. &mut account_account,
  975. &mut account2_account,
  976. &mut delegate_account,
  977. ],
  978. )
  979. .unwrap();
  980. // insufficient funds approved via delegate
  981. assert_eq!(
  982. Err(TokenError::OwnerMismatch.into()),
  983. do_process_instruction(
  984. transfer(
  985. &program_id,
  986. &account_key,
  987. &account2_key,
  988. &delegate_key,
  989. &[],
  990. 1
  991. )
  992. .unwrap(),
  993. vec![
  994. &mut account_account,
  995. &mut account2_account,
  996. &mut delegate_account,
  997. ],
  998. )
  999. );
  1000. // transfer rest
  1001. do_process_instruction(
  1002. transfer(
  1003. &program_id,
  1004. &account_key,
  1005. &account2_key,
  1006. &owner_key,
  1007. &[],
  1008. 900,
  1009. )
  1010. .unwrap(),
  1011. vec![
  1012. &mut account_account,
  1013. &mut account2_account,
  1014. &mut owner_account,
  1015. ],
  1016. )
  1017. .unwrap();
  1018. // approve delegate
  1019. do_process_instruction(
  1020. approve(
  1021. &program_id,
  1022. &account_key,
  1023. &delegate_key,
  1024. &owner_key,
  1025. &[],
  1026. 100,
  1027. )
  1028. .unwrap(),
  1029. vec![
  1030. &mut account_account,
  1031. &mut delegate_account,
  1032. &mut owner_account,
  1033. ],
  1034. )
  1035. .unwrap();
  1036. // insufficient funds in source account via delegate
  1037. assert_eq!(
  1038. Err(TokenError::InsufficientFunds.into()),
  1039. do_process_instruction(
  1040. transfer(
  1041. &program_id,
  1042. &account_key,
  1043. &account2_key,
  1044. &delegate_key,
  1045. &[],
  1046. 100
  1047. )
  1048. .unwrap(),
  1049. vec![
  1050. &mut account_account,
  1051. &mut account2_account,
  1052. &mut delegate_account,
  1053. ],
  1054. )
  1055. );
  1056. }
  1057. #[test]
  1058. fn test_self_transfer() {
  1059. let program_id = spl_token::id();
  1060. let account_key = Pubkey::new_unique();
  1061. let mut account_account = SolanaAccount::new(
  1062. account_minimum_balance(),
  1063. Account::get_packed_len(),
  1064. &program_id,
  1065. );
  1066. let account2_key = Pubkey::new_unique();
  1067. let mut account2_account = SolanaAccount::new(
  1068. account_minimum_balance(),
  1069. Account::get_packed_len(),
  1070. &program_id,
  1071. );
  1072. let account3_key = Pubkey::new_unique();
  1073. let mut account3_account = SolanaAccount::new(
  1074. account_minimum_balance(),
  1075. Account::get_packed_len(),
  1076. &program_id,
  1077. );
  1078. let delegate_key = Pubkey::new_unique();
  1079. let mut delegate_account = SolanaAccount::default();
  1080. let owner_key = Pubkey::new_unique();
  1081. let mut owner_account = SolanaAccount::default();
  1082. let owner2_key = Pubkey::new_unique();
  1083. let mut owner2_account = SolanaAccount::default();
  1084. let mint_key = Pubkey::new_unique();
  1085. let mut mint_account =
  1086. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  1087. let mut rent_sysvar = rent_sysvar();
  1088. // create mint
  1089. do_process_instruction(
  1090. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  1091. vec![&mut mint_account, &mut rent_sysvar],
  1092. )
  1093. .unwrap();
  1094. // create account
  1095. do_process_instruction(
  1096. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1097. vec![
  1098. &mut account_account,
  1099. &mut mint_account,
  1100. &mut owner_account,
  1101. &mut rent_sysvar,
  1102. ],
  1103. )
  1104. .unwrap();
  1105. // create another account
  1106. do_process_instruction(
  1107. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  1108. vec![
  1109. &mut account2_account,
  1110. &mut mint_account,
  1111. &mut owner_account,
  1112. &mut rent_sysvar,
  1113. ],
  1114. )
  1115. .unwrap();
  1116. // create another account
  1117. do_process_instruction(
  1118. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  1119. vec![
  1120. &mut account3_account,
  1121. &mut mint_account,
  1122. &mut owner_account,
  1123. &mut rent_sysvar,
  1124. ],
  1125. )
  1126. .unwrap();
  1127. // mint to account
  1128. do_process_instruction(
  1129. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  1130. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1131. )
  1132. .unwrap();
  1133. let account_info = (&account_key, false, &mut account_account).into_account_info();
  1134. let account3_info = (&account3_key, false, &mut account3_account).into_account_info();
  1135. let delegate_info = (&delegate_key, true, &mut delegate_account).into_account_info();
  1136. let owner_info = (&owner_key, true, &mut owner_account).into_account_info();
  1137. let owner2_info = (&owner2_key, true, &mut owner2_account).into_account_info();
  1138. let mint_info = (&mint_key, false, &mut mint_account).into_account_info();
  1139. // transfer
  1140. let instruction = transfer(
  1141. &program_id,
  1142. account_info.key,
  1143. account_info.key,
  1144. owner_info.key,
  1145. &[],
  1146. 1000,
  1147. )
  1148. .unwrap();
  1149. assert_eq!(
  1150. Ok(()),
  1151. do_process_instruction_dups(
  1152. instruction,
  1153. vec![
  1154. account_info.clone(),
  1155. account_info.clone(),
  1156. owner_info.clone(),
  1157. ],
  1158. )
  1159. );
  1160. // no balance change...
  1161. let account = Account::unpack_unchecked(&account_info.try_borrow_data().unwrap()).unwrap();
  1162. assert_eq!(account.amount, 1000);
  1163. // transfer checked
  1164. let instruction = transfer_checked(
  1165. &program_id,
  1166. account_info.key,
  1167. mint_info.key,
  1168. account_info.key,
  1169. owner_info.key,
  1170. &[],
  1171. 1000,
  1172. 2,
  1173. )
  1174. .unwrap();
  1175. assert_eq!(
  1176. Ok(()),
  1177. do_process_instruction_dups(
  1178. instruction,
  1179. vec![
  1180. account_info.clone(),
  1181. mint_info.clone(),
  1182. account_info.clone(),
  1183. owner_info.clone(),
  1184. ],
  1185. )
  1186. );
  1187. // no balance change...
  1188. let account = Account::unpack_unchecked(&account_info.try_borrow_data().unwrap()).unwrap();
  1189. assert_eq!(account.amount, 1000);
  1190. // missing signer
  1191. let mut owner_no_sign_info = owner_info.clone();
  1192. let mut instruction = transfer(
  1193. &program_id,
  1194. account_info.key,
  1195. account_info.key,
  1196. owner_no_sign_info.key,
  1197. &[],
  1198. 1000,
  1199. )
  1200. .unwrap();
  1201. instruction.accounts[2].is_signer = false;
  1202. owner_no_sign_info.is_signer = false;
  1203. assert_eq!(
  1204. Err(ProgramError::MissingRequiredSignature),
  1205. do_process_instruction_dups(
  1206. instruction,
  1207. vec![
  1208. account_info.clone(),
  1209. account_info.clone(),
  1210. owner_no_sign_info.clone(),
  1211. ],
  1212. )
  1213. );
  1214. // missing signer checked
  1215. let mut instruction = transfer_checked(
  1216. &program_id,
  1217. account_info.key,
  1218. mint_info.key,
  1219. account_info.key,
  1220. owner_no_sign_info.key,
  1221. &[],
  1222. 1000,
  1223. 2,
  1224. )
  1225. .unwrap();
  1226. instruction.accounts[3].is_signer = false;
  1227. assert_eq!(
  1228. Err(ProgramError::MissingRequiredSignature),
  1229. do_process_instruction_dups(
  1230. instruction,
  1231. vec![
  1232. account_info.clone(),
  1233. mint_info.clone(),
  1234. account_info.clone(),
  1235. owner_no_sign_info,
  1236. ],
  1237. )
  1238. );
  1239. // missing owner
  1240. let instruction = transfer(
  1241. &program_id,
  1242. account_info.key,
  1243. account_info.key,
  1244. owner2_info.key,
  1245. &[],
  1246. 1000,
  1247. )
  1248. .unwrap();
  1249. assert_eq!(
  1250. Err(TokenError::OwnerMismatch.into()),
  1251. do_process_instruction_dups(
  1252. instruction,
  1253. vec![
  1254. account_info.clone(),
  1255. account_info.clone(),
  1256. owner2_info.clone(),
  1257. ],
  1258. )
  1259. );
  1260. // missing owner checked
  1261. let instruction = transfer_checked(
  1262. &program_id,
  1263. account_info.key,
  1264. mint_info.key,
  1265. account_info.key,
  1266. owner2_info.key,
  1267. &[],
  1268. 1000,
  1269. 2,
  1270. )
  1271. .unwrap();
  1272. assert_eq!(
  1273. Err(TokenError::OwnerMismatch.into()),
  1274. do_process_instruction_dups(
  1275. instruction,
  1276. vec![
  1277. account_info.clone(),
  1278. mint_info.clone(),
  1279. account_info.clone(),
  1280. owner2_info.clone(),
  1281. ],
  1282. )
  1283. );
  1284. // insufficient funds
  1285. let instruction = transfer(
  1286. &program_id,
  1287. account_info.key,
  1288. account_info.key,
  1289. owner_info.key,
  1290. &[],
  1291. 1001,
  1292. )
  1293. .unwrap();
  1294. assert_eq!(
  1295. Err(TokenError::InsufficientFunds.into()),
  1296. do_process_instruction_dups(
  1297. instruction,
  1298. vec![
  1299. account_info.clone(),
  1300. account_info.clone(),
  1301. owner_info.clone(),
  1302. ],
  1303. )
  1304. );
  1305. // insufficient funds checked
  1306. let instruction = transfer_checked(
  1307. &program_id,
  1308. account_info.key,
  1309. mint_info.key,
  1310. account_info.key,
  1311. owner_info.key,
  1312. &[],
  1313. 1001,
  1314. 2,
  1315. )
  1316. .unwrap();
  1317. assert_eq!(
  1318. Err(TokenError::InsufficientFunds.into()),
  1319. do_process_instruction_dups(
  1320. instruction,
  1321. vec![
  1322. account_info.clone(),
  1323. mint_info.clone(),
  1324. account_info.clone(),
  1325. owner_info.clone(),
  1326. ],
  1327. )
  1328. );
  1329. // incorrect decimals
  1330. let instruction = transfer_checked(
  1331. &program_id,
  1332. account_info.key,
  1333. mint_info.key,
  1334. account_info.key,
  1335. owner_info.key,
  1336. &[],
  1337. 1,
  1338. 10, // <-- incorrect decimals
  1339. )
  1340. .unwrap();
  1341. assert_eq!(
  1342. Err(TokenError::MintDecimalsMismatch.into()),
  1343. do_process_instruction_dups(
  1344. instruction,
  1345. vec![
  1346. account_info.clone(),
  1347. mint_info.clone(),
  1348. account_info.clone(),
  1349. owner_info.clone(),
  1350. ],
  1351. )
  1352. );
  1353. // incorrect mint
  1354. let instruction = transfer_checked(
  1355. &program_id,
  1356. account_info.key,
  1357. account3_info.key, // <-- incorrect mint
  1358. account_info.key,
  1359. owner_info.key,
  1360. &[],
  1361. 1,
  1362. 2,
  1363. )
  1364. .unwrap();
  1365. assert_eq!(
  1366. Err(TokenError::MintMismatch.into()),
  1367. do_process_instruction_dups(
  1368. instruction,
  1369. vec![
  1370. account_info.clone(),
  1371. account3_info.clone(), // <-- incorrect mint
  1372. account_info.clone(),
  1373. owner_info.clone(),
  1374. ],
  1375. )
  1376. );
  1377. // approve delegate
  1378. let instruction = approve(
  1379. &program_id,
  1380. account_info.key,
  1381. delegate_info.key,
  1382. owner_info.key,
  1383. &[],
  1384. 100,
  1385. )
  1386. .unwrap();
  1387. do_process_instruction_dups(
  1388. instruction,
  1389. vec![
  1390. account_info.clone(),
  1391. delegate_info.clone(),
  1392. owner_info.clone(),
  1393. ],
  1394. )
  1395. .unwrap();
  1396. // delegate transfer
  1397. let instruction = transfer(
  1398. &program_id,
  1399. account_info.key,
  1400. account_info.key,
  1401. delegate_info.key,
  1402. &[],
  1403. 100,
  1404. )
  1405. .unwrap();
  1406. assert_eq!(
  1407. Ok(()),
  1408. do_process_instruction_dups(
  1409. instruction,
  1410. vec![
  1411. account_info.clone(),
  1412. account_info.clone(),
  1413. delegate_info.clone(),
  1414. ],
  1415. )
  1416. );
  1417. // no balance change...
  1418. let account = Account::unpack_unchecked(&account_info.try_borrow_data().unwrap()).unwrap();
  1419. assert_eq!(account.amount, 1000);
  1420. assert_eq!(account.delegated_amount, 100);
  1421. // delegate transfer checked
  1422. let instruction = transfer_checked(
  1423. &program_id,
  1424. account_info.key,
  1425. mint_info.key,
  1426. account_info.key,
  1427. delegate_info.key,
  1428. &[],
  1429. 100,
  1430. 2,
  1431. )
  1432. .unwrap();
  1433. assert_eq!(
  1434. Ok(()),
  1435. do_process_instruction_dups(
  1436. instruction,
  1437. vec![
  1438. account_info.clone(),
  1439. mint_info.clone(),
  1440. account_info.clone(),
  1441. delegate_info.clone(),
  1442. ],
  1443. )
  1444. );
  1445. // no balance change...
  1446. let account = Account::unpack_unchecked(&account_info.try_borrow_data().unwrap()).unwrap();
  1447. assert_eq!(account.amount, 1000);
  1448. assert_eq!(account.delegated_amount, 100);
  1449. // delegate insufficient funds
  1450. let instruction = transfer(
  1451. &program_id,
  1452. account_info.key,
  1453. account_info.key,
  1454. delegate_info.key,
  1455. &[],
  1456. 101,
  1457. )
  1458. .unwrap();
  1459. assert_eq!(
  1460. Err(TokenError::InsufficientFunds.into()),
  1461. do_process_instruction_dups(
  1462. instruction,
  1463. vec![
  1464. account_info.clone(),
  1465. account_info.clone(),
  1466. delegate_info.clone(),
  1467. ],
  1468. )
  1469. );
  1470. // delegate insufficient funds checked
  1471. let instruction = transfer_checked(
  1472. &program_id,
  1473. account_info.key,
  1474. mint_info.key,
  1475. account_info.key,
  1476. delegate_info.key,
  1477. &[],
  1478. 101,
  1479. 2,
  1480. )
  1481. .unwrap();
  1482. assert_eq!(
  1483. Err(TokenError::InsufficientFunds.into()),
  1484. do_process_instruction_dups(
  1485. instruction,
  1486. vec![
  1487. account_info.clone(),
  1488. mint_info.clone(),
  1489. account_info.clone(),
  1490. delegate_info.clone(),
  1491. ],
  1492. )
  1493. );
  1494. // owner transfer with delegate assigned
  1495. let instruction = transfer(
  1496. &program_id,
  1497. account_info.key,
  1498. account_info.key,
  1499. owner_info.key,
  1500. &[],
  1501. 1000,
  1502. )
  1503. .unwrap();
  1504. assert_eq!(
  1505. Ok(()),
  1506. do_process_instruction_dups(
  1507. instruction,
  1508. vec![
  1509. account_info.clone(),
  1510. account_info.clone(),
  1511. owner_info.clone(),
  1512. ],
  1513. )
  1514. );
  1515. // no balance change...
  1516. let account = Account::unpack_unchecked(&account_info.try_borrow_data().unwrap()).unwrap();
  1517. assert_eq!(account.amount, 1000);
  1518. // owner transfer with delegate assigned checked
  1519. let instruction = transfer_checked(
  1520. &program_id,
  1521. account_info.key,
  1522. mint_info.key,
  1523. account_info.key,
  1524. owner_info.key,
  1525. &[],
  1526. 1000,
  1527. 2,
  1528. )
  1529. .unwrap();
  1530. assert_eq!(
  1531. Ok(()),
  1532. do_process_instruction_dups(
  1533. instruction,
  1534. vec![
  1535. account_info.clone(),
  1536. mint_info.clone(),
  1537. account_info.clone(),
  1538. owner_info.clone(),
  1539. ],
  1540. )
  1541. );
  1542. // no balance change...
  1543. let account = Account::unpack_unchecked(&account_info.try_borrow_data().unwrap()).unwrap();
  1544. assert_eq!(account.amount, 1000);
  1545. }
  1546. #[test]
  1547. fn test_mintable_token_with_zero_supply() {
  1548. let program_id = spl_token::id();
  1549. let account_key = Pubkey::new_unique();
  1550. let mut account_account = SolanaAccount::new(
  1551. account_minimum_balance(),
  1552. Account::get_packed_len(),
  1553. &program_id,
  1554. );
  1555. let owner_key = Pubkey::new_unique();
  1556. let mut owner_account = SolanaAccount::default();
  1557. let mint_key = Pubkey::new_unique();
  1558. let mut mint_account =
  1559. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  1560. let mut rent_sysvar = rent_sysvar();
  1561. // create mint-able token with zero supply
  1562. let decimals = 2;
  1563. do_process_instruction(
  1564. initialize_mint(&program_id, &mint_key, &owner_key, None, decimals).unwrap(),
  1565. vec![&mut mint_account, &mut rent_sysvar],
  1566. )
  1567. .unwrap();
  1568. let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  1569. assert_eq!(
  1570. mint,
  1571. Mint {
  1572. mint_authority: COption::Some(owner_key),
  1573. supply: 0,
  1574. decimals,
  1575. is_initialized: true,
  1576. freeze_authority: COption::None,
  1577. }
  1578. );
  1579. // create account
  1580. do_process_instruction(
  1581. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1582. vec![
  1583. &mut account_account,
  1584. &mut mint_account,
  1585. &mut owner_account,
  1586. &mut rent_sysvar,
  1587. ],
  1588. )
  1589. .unwrap();
  1590. // mint to
  1591. do_process_instruction(
  1592. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 42).unwrap(),
  1593. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1594. )
  1595. .unwrap();
  1596. let _ = Mint::unpack(&mint_account.data).unwrap();
  1597. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  1598. assert_eq!(account.amount, 42);
  1599. // mint to 2, with incorrect decimals
  1600. assert_eq!(
  1601. Err(TokenError::MintDecimalsMismatch.into()),
  1602. do_process_instruction(
  1603. mint_to_checked(
  1604. &program_id,
  1605. &mint_key,
  1606. &account_key,
  1607. &owner_key,
  1608. &[],
  1609. 42,
  1610. decimals + 1
  1611. )
  1612. .unwrap(),
  1613. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1614. )
  1615. );
  1616. let _ = Mint::unpack(&mint_account.data).unwrap();
  1617. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  1618. assert_eq!(account.amount, 42);
  1619. // mint to 2
  1620. do_process_instruction(
  1621. mint_to_checked(
  1622. &program_id,
  1623. &mint_key,
  1624. &account_key,
  1625. &owner_key,
  1626. &[],
  1627. 42,
  1628. decimals,
  1629. )
  1630. .unwrap(),
  1631. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1632. )
  1633. .unwrap();
  1634. let _ = Mint::unpack(&mint_account.data).unwrap();
  1635. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  1636. assert_eq!(account.amount, 84);
  1637. }
  1638. #[test]
  1639. fn test_approve_dups() {
  1640. let program_id = spl_token::id();
  1641. let account1_key = Pubkey::new_unique();
  1642. let mut account1_account = SolanaAccount::new(
  1643. account_minimum_balance(),
  1644. Account::get_packed_len(),
  1645. &program_id,
  1646. );
  1647. let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  1648. let account2_key = Pubkey::new_unique();
  1649. let mut account2_account = SolanaAccount::new(
  1650. account_minimum_balance(),
  1651. Account::get_packed_len(),
  1652. &program_id,
  1653. );
  1654. let account2_info: AccountInfo = (&account2_key, false, &mut account2_account).into();
  1655. let account3_key = Pubkey::new_unique();
  1656. let mut account3_account = SolanaAccount::new(
  1657. account_minimum_balance(),
  1658. Account::get_packed_len(),
  1659. &program_id,
  1660. );
  1661. let account3_info: AccountInfo = (&account3_key, true, &mut account3_account).into();
  1662. let multisig_key = Pubkey::new_unique();
  1663. let mut multisig_account = SolanaAccount::new(
  1664. multisig_minimum_balance(),
  1665. Multisig::get_packed_len(),
  1666. &program_id,
  1667. );
  1668. let multisig_info: AccountInfo = (&multisig_key, true, &mut multisig_account).into();
  1669. let owner_key = Pubkey::new_unique();
  1670. let mut owner_account = SolanaAccount::default();
  1671. let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
  1672. let mint_key = Pubkey::new_unique();
  1673. let mut mint_account =
  1674. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  1675. let mint_info: AccountInfo = (&mint_key, false, &mut mint_account).into();
  1676. let rent_key = rent::id();
  1677. let mut rent_sysvar = rent_sysvar();
  1678. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  1679. // create mint
  1680. do_process_instruction_dups(
  1681. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  1682. vec![mint_info.clone(), rent_info.clone()],
  1683. )
  1684. .unwrap();
  1685. // create account
  1686. do_process_instruction_dups(
  1687. initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
  1688. vec![
  1689. account1_info.clone(),
  1690. mint_info.clone(),
  1691. account1_info.clone(),
  1692. rent_info.clone(),
  1693. ],
  1694. )
  1695. .unwrap();
  1696. // create another account
  1697. do_process_instruction_dups(
  1698. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  1699. vec![
  1700. account2_info.clone(),
  1701. mint_info.clone(),
  1702. owner_info.clone(),
  1703. rent_info.clone(),
  1704. ],
  1705. )
  1706. .unwrap();
  1707. // mint to account
  1708. do_process_instruction_dups(
  1709. mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
  1710. vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
  1711. )
  1712. .unwrap();
  1713. // source-owner approve
  1714. do_process_instruction_dups(
  1715. approve(
  1716. &program_id,
  1717. &account1_key,
  1718. &account2_key,
  1719. &account1_key,
  1720. &[],
  1721. 500,
  1722. )
  1723. .unwrap(),
  1724. vec![
  1725. account1_info.clone(),
  1726. account2_info.clone(),
  1727. account1_info.clone(),
  1728. ],
  1729. )
  1730. .unwrap();
  1731. // source-owner approve_checked
  1732. do_process_instruction_dups(
  1733. approve_checked(
  1734. &program_id,
  1735. &account1_key,
  1736. &mint_key,
  1737. &account2_key,
  1738. &account1_key,
  1739. &[],
  1740. 500,
  1741. 2,
  1742. )
  1743. .unwrap(),
  1744. vec![
  1745. account1_info.clone(),
  1746. mint_info.clone(),
  1747. account2_info.clone(),
  1748. account1_info.clone(),
  1749. ],
  1750. )
  1751. .unwrap();
  1752. // source-owner revoke
  1753. do_process_instruction_dups(
  1754. revoke(&program_id, &account1_key, &account1_key, &[]).unwrap(),
  1755. vec![account1_info.clone(), account1_info.clone()],
  1756. )
  1757. .unwrap();
  1758. // test source-multisig signer
  1759. do_process_instruction_dups(
  1760. initialize_multisig(&program_id, &multisig_key, &[&account3_key], 1).unwrap(),
  1761. vec![
  1762. multisig_info.clone(),
  1763. rent_info.clone(),
  1764. account3_info.clone(),
  1765. ],
  1766. )
  1767. .unwrap();
  1768. do_process_instruction_dups(
  1769. initialize_account(&program_id, &account3_key, &mint_key, &multisig_key).unwrap(),
  1770. vec![
  1771. account3_info.clone(),
  1772. mint_info.clone(),
  1773. multisig_info.clone(),
  1774. rent_info.clone(),
  1775. ],
  1776. )
  1777. .unwrap();
  1778. do_process_instruction_dups(
  1779. mint_to(&program_id, &mint_key, &account3_key, &owner_key, &[], 1000).unwrap(),
  1780. vec![mint_info.clone(), account3_info.clone(), owner_info.clone()],
  1781. )
  1782. .unwrap();
  1783. // source-multisig-signer approve
  1784. do_process_instruction_dups(
  1785. approve(
  1786. &program_id,
  1787. &account3_key,
  1788. &account2_key,
  1789. &multisig_key,
  1790. &[&account3_key],
  1791. 500,
  1792. )
  1793. .unwrap(),
  1794. vec![
  1795. account3_info.clone(),
  1796. account2_info.clone(),
  1797. multisig_info.clone(),
  1798. account3_info.clone(),
  1799. ],
  1800. )
  1801. .unwrap();
  1802. // source-multisig-signer approve_checked
  1803. do_process_instruction_dups(
  1804. approve_checked(
  1805. &program_id,
  1806. &account3_key,
  1807. &mint_key,
  1808. &account2_key,
  1809. &multisig_key,
  1810. &[&account3_key],
  1811. 500,
  1812. 2,
  1813. )
  1814. .unwrap(),
  1815. vec![
  1816. account3_info.clone(),
  1817. mint_info.clone(),
  1818. account2_info.clone(),
  1819. multisig_info.clone(),
  1820. account3_info.clone(),
  1821. ],
  1822. )
  1823. .unwrap();
  1824. // source-owner multisig-signer
  1825. do_process_instruction_dups(
  1826. revoke(&program_id, &account3_key, &multisig_key, &[&account3_key]).unwrap(),
  1827. vec![
  1828. account3_info.clone(),
  1829. multisig_info.clone(),
  1830. account3_info.clone(),
  1831. ],
  1832. )
  1833. .unwrap();
  1834. }
  1835. #[test]
  1836. fn test_approve() {
  1837. let program_id = spl_token::id();
  1838. let account_key = Pubkey::new_unique();
  1839. let mut account_account = SolanaAccount::new(
  1840. account_minimum_balance(),
  1841. Account::get_packed_len(),
  1842. &program_id,
  1843. );
  1844. let account2_key = Pubkey::new_unique();
  1845. let mut account2_account = SolanaAccount::new(
  1846. account_minimum_balance(),
  1847. Account::get_packed_len(),
  1848. &program_id,
  1849. );
  1850. let delegate_key = Pubkey::new_unique();
  1851. let mut delegate_account = SolanaAccount::default();
  1852. let owner_key = Pubkey::new_unique();
  1853. let mut owner_account = SolanaAccount::default();
  1854. let owner2_key = Pubkey::new_unique();
  1855. let mut owner2_account = SolanaAccount::default();
  1856. let mint_key = Pubkey::new_unique();
  1857. let mut mint_account =
  1858. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  1859. let mut rent_sysvar = rent_sysvar();
  1860. // create mint
  1861. do_process_instruction(
  1862. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  1863. vec![&mut mint_account, &mut rent_sysvar],
  1864. )
  1865. .unwrap();
  1866. // create account
  1867. do_process_instruction(
  1868. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1869. vec![
  1870. &mut account_account,
  1871. &mut mint_account,
  1872. &mut owner_account,
  1873. &mut rent_sysvar,
  1874. ],
  1875. )
  1876. .unwrap();
  1877. // create another account
  1878. do_process_instruction(
  1879. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  1880. vec![
  1881. &mut account2_account,
  1882. &mut mint_account,
  1883. &mut owner_account,
  1884. &mut rent_sysvar,
  1885. ],
  1886. )
  1887. .unwrap();
  1888. // mint to account
  1889. do_process_instruction(
  1890. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  1891. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1892. )
  1893. .unwrap();
  1894. // missing signer
  1895. let mut instruction = approve(
  1896. &program_id,
  1897. &account_key,
  1898. &delegate_key,
  1899. &owner_key,
  1900. &[],
  1901. 100,
  1902. )
  1903. .unwrap();
  1904. instruction.accounts[2].is_signer = false;
  1905. assert_eq!(
  1906. Err(ProgramError::MissingRequiredSignature),
  1907. do_process_instruction(
  1908. instruction,
  1909. vec![
  1910. &mut account_account,
  1911. &mut delegate_account,
  1912. &mut owner_account,
  1913. ],
  1914. )
  1915. );
  1916. // no owner
  1917. assert_eq!(
  1918. Err(TokenError::OwnerMismatch.into()),
  1919. do_process_instruction(
  1920. approve(
  1921. &program_id,
  1922. &account_key,
  1923. &delegate_key,
  1924. &owner2_key,
  1925. &[],
  1926. 100
  1927. )
  1928. .unwrap(),
  1929. vec![
  1930. &mut account_account,
  1931. &mut delegate_account,
  1932. &mut owner2_account,
  1933. ],
  1934. )
  1935. );
  1936. // approve delegate
  1937. do_process_instruction(
  1938. approve(
  1939. &program_id,
  1940. &account_key,
  1941. &delegate_key,
  1942. &owner_key,
  1943. &[],
  1944. 100,
  1945. )
  1946. .unwrap(),
  1947. vec![
  1948. &mut account_account,
  1949. &mut delegate_account,
  1950. &mut owner_account,
  1951. ],
  1952. )
  1953. .unwrap();
  1954. // approve delegate 2, with incorrect decimals
  1955. assert_eq!(
  1956. Err(TokenError::MintDecimalsMismatch.into()),
  1957. do_process_instruction(
  1958. approve_checked(
  1959. &program_id,
  1960. &account_key,
  1961. &mint_key,
  1962. &delegate_key,
  1963. &owner_key,
  1964. &[],
  1965. 100,
  1966. 0 // <-- incorrect decimals
  1967. )
  1968. .unwrap(),
  1969. vec![
  1970. &mut account_account,
  1971. &mut mint_account,
  1972. &mut delegate_account,
  1973. &mut owner_account,
  1974. ],
  1975. )
  1976. );
  1977. // approve delegate 2, with incorrect mint
  1978. assert_eq!(
  1979. Err(TokenError::MintMismatch.into()),
  1980. do_process_instruction(
  1981. approve_checked(
  1982. &program_id,
  1983. &account_key,
  1984. &account2_key, // <-- bad mint
  1985. &delegate_key,
  1986. &owner_key,
  1987. &[],
  1988. 100,
  1989. 0
  1990. )
  1991. .unwrap(),
  1992. vec![
  1993. &mut account_account,
  1994. &mut account2_account, // <-- bad mint
  1995. &mut delegate_account,
  1996. &mut owner_account,
  1997. ],
  1998. )
  1999. );
  2000. // approve delegate 2
  2001. do_process_instruction(
  2002. approve_checked(
  2003. &program_id,
  2004. &account_key,
  2005. &mint_key,
  2006. &delegate_key,
  2007. &owner_key,
  2008. &[],
  2009. 100,
  2010. 2,
  2011. )
  2012. .unwrap(),
  2013. vec![
  2014. &mut account_account,
  2015. &mut mint_account,
  2016. &mut delegate_account,
  2017. &mut owner_account,
  2018. ],
  2019. )
  2020. .unwrap();
  2021. // revoke delegate
  2022. do_process_instruction(
  2023. revoke(&program_id, &account_key, &owner_key, &[]).unwrap(),
  2024. vec![&mut account_account, &mut owner_account],
  2025. )
  2026. .unwrap();
  2027. }
  2028. #[test]
  2029. fn test_set_authority_dups() {
  2030. let program_id = spl_token::id();
  2031. let account1_key = Pubkey::new_unique();
  2032. let mut account1_account = SolanaAccount::new(
  2033. account_minimum_balance(),
  2034. Account::get_packed_len(),
  2035. &program_id,
  2036. );
  2037. let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  2038. let owner_key = Pubkey::new_unique();
  2039. let mint_key = Pubkey::new_unique();
  2040. let mut mint_account =
  2041. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  2042. let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
  2043. let rent_key = rent::id();
  2044. let mut rent_sysvar = rent_sysvar();
  2045. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  2046. // create mint
  2047. do_process_instruction_dups(
  2048. initialize_mint(&program_id, &mint_key, &mint_key, Some(&mint_key), 2).unwrap(),
  2049. vec![mint_info.clone(), rent_info.clone()],
  2050. )
  2051. .unwrap();
  2052. // create account
  2053. do_process_instruction_dups(
  2054. initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
  2055. vec![
  2056. account1_info.clone(),
  2057. mint_info.clone(),
  2058. account1_info.clone(),
  2059. rent_info.clone(),
  2060. ],
  2061. )
  2062. .unwrap();
  2063. // set mint_authority when currently self
  2064. do_process_instruction_dups(
  2065. set_authority(
  2066. &program_id,
  2067. &mint_key,
  2068. Some(&owner_key),
  2069. AuthorityType::MintTokens,
  2070. &mint_key,
  2071. &[],
  2072. )
  2073. .unwrap(),
  2074. vec![mint_info.clone(), mint_info.clone()],
  2075. )
  2076. .unwrap();
  2077. // set freeze_authority when currently self
  2078. do_process_instruction_dups(
  2079. set_authority(
  2080. &program_id,
  2081. &mint_key,
  2082. Some(&owner_key),
  2083. AuthorityType::FreezeAccount,
  2084. &mint_key,
  2085. &[],
  2086. )
  2087. .unwrap(),
  2088. vec![mint_info.clone(), mint_info.clone()],
  2089. )
  2090. .unwrap();
  2091. // set account owner when currently self
  2092. do_process_instruction_dups(
  2093. set_authority(
  2094. &program_id,
  2095. &account1_key,
  2096. Some(&owner_key),
  2097. AuthorityType::AccountOwner,
  2098. &account1_key,
  2099. &[],
  2100. )
  2101. .unwrap(),
  2102. vec![account1_info.clone(), account1_info.clone()],
  2103. )
  2104. .unwrap();
  2105. // set close_authority when currently self
  2106. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  2107. account.close_authority = COption::Some(account1_key);
  2108. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  2109. do_process_instruction_dups(
  2110. set_authority(
  2111. &program_id,
  2112. &account1_key,
  2113. Some(&owner_key),
  2114. AuthorityType::CloseAccount,
  2115. &account1_key,
  2116. &[],
  2117. )
  2118. .unwrap(),
  2119. vec![account1_info.clone(), account1_info.clone()],
  2120. )
  2121. .unwrap();
  2122. }
  2123. #[test]
  2124. fn test_set_authority() {
  2125. let program_id = spl_token::id();
  2126. let account_key = Pubkey::new_unique();
  2127. let mut account_account = SolanaAccount::new(
  2128. account_minimum_balance(),
  2129. Account::get_packed_len(),
  2130. &program_id,
  2131. );
  2132. let account2_key = Pubkey::new_unique();
  2133. let mut account2_account = SolanaAccount::new(
  2134. account_minimum_balance(),
  2135. Account::get_packed_len(),
  2136. &program_id,
  2137. );
  2138. let owner_key = Pubkey::new_unique();
  2139. let mut owner_account = SolanaAccount::default();
  2140. let owner2_key = Pubkey::new_unique();
  2141. let mut owner2_account = SolanaAccount::default();
  2142. let owner3_key = Pubkey::new_unique();
  2143. let mut owner3_account = SolanaAccount::default();
  2144. let mint_key = Pubkey::new_unique();
  2145. let mut mint_account =
  2146. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  2147. let mint2_key = Pubkey::new_unique();
  2148. let mut mint2_account =
  2149. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  2150. let mut rent_sysvar = rent_sysvar();
  2151. // create new mint with owner
  2152. do_process_instruction(
  2153. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  2154. vec![&mut mint_account, &mut rent_sysvar],
  2155. )
  2156. .unwrap();
  2157. // create mint with owner and freeze_authority
  2158. do_process_instruction(
  2159. initialize_mint(&program_id, &mint2_key, &owner_key, Some(&owner_key), 2).unwrap(),
  2160. vec![&mut mint2_account, &mut rent_sysvar],
  2161. )
  2162. .unwrap();
  2163. // invalid account
  2164. assert_eq!(
  2165. Err(ProgramError::UninitializedAccount),
  2166. do_process_instruction(
  2167. set_authority(
  2168. &program_id,
  2169. &account_key,
  2170. Some(&owner2_key),
  2171. AuthorityType::AccountOwner,
  2172. &owner_key,
  2173. &[]
  2174. )
  2175. .unwrap(),
  2176. vec![&mut account_account, &mut owner_account],
  2177. )
  2178. );
  2179. // create account
  2180. do_process_instruction(
  2181. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  2182. vec![
  2183. &mut account_account,
  2184. &mut mint_account,
  2185. &mut owner_account,
  2186. &mut rent_sysvar,
  2187. ],
  2188. )
  2189. .unwrap();
  2190. // create another account
  2191. do_process_instruction(
  2192. initialize_account(&program_id, &account2_key, &mint2_key, &owner_key).unwrap(),
  2193. vec![
  2194. &mut account2_account,
  2195. &mut mint2_account,
  2196. &mut owner_account,
  2197. &mut rent_sysvar,
  2198. ],
  2199. )
  2200. .unwrap();
  2201. // missing owner
  2202. assert_eq!(
  2203. Err(TokenError::OwnerMismatch.into()),
  2204. do_process_instruction(
  2205. set_authority(
  2206. &program_id,
  2207. &account_key,
  2208. Some(&owner_key),
  2209. AuthorityType::AccountOwner,
  2210. &owner2_key,
  2211. &[]
  2212. )
  2213. .unwrap(),
  2214. vec![&mut account_account, &mut owner2_account],
  2215. )
  2216. );
  2217. // owner did not sign
  2218. let mut instruction = set_authority(
  2219. &program_id,
  2220. &account_key,
  2221. Some(&owner2_key),
  2222. AuthorityType::AccountOwner,
  2223. &owner_key,
  2224. &[],
  2225. )
  2226. .unwrap();
  2227. instruction.accounts[1].is_signer = false;
  2228. assert_eq!(
  2229. Err(ProgramError::MissingRequiredSignature),
  2230. do_process_instruction(instruction, vec![&mut account_account, &mut owner_account,],)
  2231. );
  2232. // wrong authority type
  2233. assert_eq!(
  2234. Err(TokenError::AuthorityTypeNotSupported.into()),
  2235. do_process_instruction(
  2236. set_authority(
  2237. &program_id,
  2238. &account_key,
  2239. Some(&owner2_key),
  2240. AuthorityType::FreezeAccount,
  2241. &owner_key,
  2242. &[],
  2243. )
  2244. .unwrap(),
  2245. vec![&mut account_account, &mut owner_account],
  2246. )
  2247. );
  2248. // account owner may not be set to None
  2249. assert_eq!(
  2250. Err(TokenError::InvalidInstruction.into()),
  2251. do_process_instruction(
  2252. set_authority(
  2253. &program_id,
  2254. &account_key,
  2255. None,
  2256. AuthorityType::AccountOwner,
  2257. &owner_key,
  2258. &[],
  2259. )
  2260. .unwrap(),
  2261. vec![&mut account_account, &mut owner_account],
  2262. )
  2263. );
  2264. // set delegate
  2265. do_process_instruction(
  2266. approve(
  2267. &program_id,
  2268. &account_key,
  2269. &owner2_key,
  2270. &owner_key,
  2271. &[],
  2272. u64::MAX,
  2273. )
  2274. .unwrap(),
  2275. vec![
  2276. &mut account_account,
  2277. &mut owner2_account,
  2278. &mut owner_account,
  2279. ],
  2280. )
  2281. .unwrap();
  2282. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  2283. assert_eq!(account.delegate, COption::Some(owner2_key));
  2284. assert_eq!(account.delegated_amount, u64::MAX);
  2285. // set owner
  2286. do_process_instruction(
  2287. set_authority(
  2288. &program_id,
  2289. &account_key,
  2290. Some(&owner3_key),
  2291. AuthorityType::AccountOwner,
  2292. &owner_key,
  2293. &[],
  2294. )
  2295. .unwrap(),
  2296. vec![&mut account_account, &mut owner_account],
  2297. )
  2298. .unwrap();
  2299. // check delegate cleared
  2300. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  2301. assert_eq!(account.delegate, COption::None);
  2302. assert_eq!(account.delegated_amount, 0);
  2303. // set owner without existing delegate
  2304. do_process_instruction(
  2305. set_authority(
  2306. &program_id,
  2307. &account_key,
  2308. Some(&owner2_key),
  2309. AuthorityType::AccountOwner,
  2310. &owner3_key,
  2311. &[],
  2312. )
  2313. .unwrap(),
  2314. vec![&mut account_account, &mut owner3_account],
  2315. )
  2316. .unwrap();
  2317. // set close_authority
  2318. do_process_instruction(
  2319. set_authority(
  2320. &program_id,
  2321. &account_key,
  2322. Some(&owner2_key),
  2323. AuthorityType::CloseAccount,
  2324. &owner2_key,
  2325. &[],
  2326. )
  2327. .unwrap(),
  2328. vec![&mut account_account, &mut owner2_account],
  2329. )
  2330. .unwrap();
  2331. // close_authority may be set to None
  2332. do_process_instruction(
  2333. set_authority(
  2334. &program_id,
  2335. &account_key,
  2336. None,
  2337. AuthorityType::CloseAccount,
  2338. &owner2_key,
  2339. &[],
  2340. )
  2341. .unwrap(),
  2342. vec![&mut account_account, &mut owner2_account],
  2343. )
  2344. .unwrap();
  2345. // wrong owner
  2346. assert_eq!(
  2347. Err(TokenError::OwnerMismatch.into()),
  2348. do_process_instruction(
  2349. set_authority(
  2350. &program_id,
  2351. &mint_key,
  2352. Some(&owner3_key),
  2353. AuthorityType::MintTokens,
  2354. &owner2_key,
  2355. &[]
  2356. )
  2357. .unwrap(),
  2358. vec![&mut mint_account, &mut owner2_account],
  2359. )
  2360. );
  2361. // owner did not sign
  2362. let mut instruction = set_authority(
  2363. &program_id,
  2364. &mint_key,
  2365. Some(&owner2_key),
  2366. AuthorityType::MintTokens,
  2367. &owner_key,
  2368. &[],
  2369. )
  2370. .unwrap();
  2371. instruction.accounts[1].is_signer = false;
  2372. assert_eq!(
  2373. Err(ProgramError::MissingRequiredSignature),
  2374. do_process_instruction(instruction, vec![&mut mint_account, &mut owner_account],)
  2375. );
  2376. // cannot freeze
  2377. assert_eq!(
  2378. Err(TokenError::MintCannotFreeze.into()),
  2379. do_process_instruction(
  2380. set_authority(
  2381. &program_id,
  2382. &mint_key,
  2383. Some(&owner2_key),
  2384. AuthorityType::FreezeAccount,
  2385. &owner_key,
  2386. &[],
  2387. )
  2388. .unwrap(),
  2389. vec![&mut mint_account, &mut owner_account],
  2390. )
  2391. );
  2392. // set owner
  2393. do_process_instruction(
  2394. set_authority(
  2395. &program_id,
  2396. &mint_key,
  2397. Some(&owner2_key),
  2398. AuthorityType::MintTokens,
  2399. &owner_key,
  2400. &[],
  2401. )
  2402. .unwrap(),
  2403. vec![&mut mint_account, &mut owner_account],
  2404. )
  2405. .unwrap();
  2406. // set owner to None
  2407. do_process_instruction(
  2408. set_authority(
  2409. &program_id,
  2410. &mint_key,
  2411. None,
  2412. AuthorityType::MintTokens,
  2413. &owner2_key,
  2414. &[],
  2415. )
  2416. .unwrap(),
  2417. vec![&mut mint_account, &mut owner2_account],
  2418. )
  2419. .unwrap();
  2420. // test unsetting mint_authority is one-way operation
  2421. assert_eq!(
  2422. Err(TokenError::FixedSupply.into()),
  2423. do_process_instruction(
  2424. set_authority(
  2425. &program_id,
  2426. &mint2_key,
  2427. Some(&owner2_key),
  2428. AuthorityType::MintTokens,
  2429. &owner_key,
  2430. &[]
  2431. )
  2432. .unwrap(),
  2433. vec![&mut mint_account, &mut owner_account],
  2434. )
  2435. );
  2436. // set freeze_authority
  2437. do_process_instruction(
  2438. set_authority(
  2439. &program_id,
  2440. &mint2_key,
  2441. Some(&owner2_key),
  2442. AuthorityType::FreezeAccount,
  2443. &owner_key,
  2444. &[],
  2445. )
  2446. .unwrap(),
  2447. vec![&mut mint2_account, &mut owner_account],
  2448. )
  2449. .unwrap();
  2450. // test unsetting freeze_authority is one-way operation
  2451. do_process_instruction(
  2452. set_authority(
  2453. &program_id,
  2454. &mint2_key,
  2455. None,
  2456. AuthorityType::FreezeAccount,
  2457. &owner2_key,
  2458. &[],
  2459. )
  2460. .unwrap(),
  2461. vec![&mut mint2_account, &mut owner2_account],
  2462. )
  2463. .unwrap();
  2464. assert_eq!(
  2465. Err(TokenError::MintCannotFreeze.into()),
  2466. do_process_instruction(
  2467. set_authority(
  2468. &program_id,
  2469. &mint2_key,
  2470. Some(&owner2_key),
  2471. AuthorityType::FreezeAccount,
  2472. &owner_key,
  2473. &[],
  2474. )
  2475. .unwrap(),
  2476. vec![&mut mint2_account, &mut owner2_account],
  2477. )
  2478. );
  2479. }
  2480. #[test]
  2481. fn test_mint_to_dups() {
  2482. let program_id = spl_token::id();
  2483. let account1_key = Pubkey::new_unique();
  2484. let mut account1_account = SolanaAccount::new(
  2485. account_minimum_balance(),
  2486. Account::get_packed_len(),
  2487. &program_id,
  2488. );
  2489. let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  2490. let owner_key = Pubkey::new_unique();
  2491. let mut owner_account = SolanaAccount::default();
  2492. let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
  2493. let mint_key = Pubkey::new_unique();
  2494. let mut mint_account =
  2495. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  2496. let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
  2497. let rent_key = rent::id();
  2498. let mut rent_sysvar = rent_sysvar();
  2499. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  2500. // create mint
  2501. do_process_instruction_dups(
  2502. initialize_mint(&program_id, &mint_key, &mint_key, None, 2).unwrap(),
  2503. vec![mint_info.clone(), rent_info.clone()],
  2504. )
  2505. .unwrap();
  2506. // create account
  2507. do_process_instruction_dups(
  2508. initialize_account(&program_id, &account1_key, &mint_key, &owner_key).unwrap(),
  2509. vec![
  2510. account1_info.clone(),
  2511. mint_info.clone(),
  2512. owner_info.clone(),
  2513. rent_info.clone(),
  2514. ],
  2515. )
  2516. .unwrap();
  2517. // mint_to when mint_authority is self
  2518. do_process_instruction_dups(
  2519. mint_to(&program_id, &mint_key, &account1_key, &mint_key, &[], 42).unwrap(),
  2520. vec![mint_info.clone(), account1_info.clone(), mint_info.clone()],
  2521. )
  2522. .unwrap();
  2523. // mint_to_checked when mint_authority is self
  2524. do_process_instruction_dups(
  2525. mint_to_checked(&program_id, &mint_key, &account1_key, &mint_key, &[], 42, 2).unwrap(),
  2526. vec![mint_info.clone(), account1_info.clone(), mint_info.clone()],
  2527. )
  2528. .unwrap();
  2529. // mint_to when mint_authority is account owner
  2530. let mut mint = Mint::unpack_unchecked(&mint_info.data.borrow()).unwrap();
  2531. mint.mint_authority = COption::Some(account1_key);
  2532. Mint::pack(mint, &mut mint_info.data.borrow_mut()).unwrap();
  2533. do_process_instruction_dups(
  2534. mint_to(
  2535. &program_id,
  2536. &mint_key,
  2537. &account1_key,
  2538. &account1_key,
  2539. &[],
  2540. 42,
  2541. )
  2542. .unwrap(),
  2543. vec![
  2544. mint_info.clone(),
  2545. account1_info.clone(),
  2546. account1_info.clone(),
  2547. ],
  2548. )
  2549. .unwrap();
  2550. // mint_to_checked when mint_authority is account owner
  2551. do_process_instruction_dups(
  2552. mint_to(
  2553. &program_id,
  2554. &mint_key,
  2555. &account1_key,
  2556. &account1_key,
  2557. &[],
  2558. 42,
  2559. )
  2560. .unwrap(),
  2561. vec![
  2562. mint_info.clone(),
  2563. account1_info.clone(),
  2564. account1_info.clone(),
  2565. ],
  2566. )
  2567. .unwrap();
  2568. }
  2569. #[test]
  2570. fn test_mint_to() {
  2571. let program_id = spl_token::id();
  2572. let account_key = Pubkey::new_unique();
  2573. let mut account_account = SolanaAccount::new(
  2574. account_minimum_balance(),
  2575. Account::get_packed_len(),
  2576. &program_id,
  2577. );
  2578. let account2_key = Pubkey::new_unique();
  2579. let mut account2_account = SolanaAccount::new(
  2580. account_minimum_balance(),
  2581. Account::get_packed_len(),
  2582. &program_id,
  2583. );
  2584. let account3_key = Pubkey::new_unique();
  2585. let mut account3_account = SolanaAccount::new(
  2586. account_minimum_balance(),
  2587. Account::get_packed_len(),
  2588. &program_id,
  2589. );
  2590. let mismatch_key = Pubkey::new_unique();
  2591. let mut mismatch_account = SolanaAccount::new(
  2592. account_minimum_balance(),
  2593. Account::get_packed_len(),
  2594. &program_id,
  2595. );
  2596. let owner_key = Pubkey::new_unique();
  2597. let mut owner_account = SolanaAccount::default();
  2598. let owner2_key = Pubkey::new_unique();
  2599. let mut owner2_account = SolanaAccount::default();
  2600. let mint_key = Pubkey::new_unique();
  2601. let mut mint_account =
  2602. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  2603. let mint2_key = Pubkey::new_unique();
  2604. let uninitialized_key = Pubkey::new_unique();
  2605. let mut uninitialized_account = SolanaAccount::new(
  2606. account_minimum_balance(),
  2607. Account::get_packed_len(),
  2608. &program_id,
  2609. );
  2610. let mut rent_sysvar = rent_sysvar();
  2611. // create new mint with owner
  2612. do_process_instruction(
  2613. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  2614. vec![&mut mint_account, &mut rent_sysvar],
  2615. )
  2616. .unwrap();
  2617. // create account
  2618. do_process_instruction(
  2619. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  2620. vec![
  2621. &mut account_account,
  2622. &mut mint_account,
  2623. &mut owner_account,
  2624. &mut rent_sysvar,
  2625. ],
  2626. )
  2627. .unwrap();
  2628. // create another account
  2629. do_process_instruction(
  2630. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  2631. vec![
  2632. &mut account2_account,
  2633. &mut mint_account,
  2634. &mut owner_account,
  2635. &mut rent_sysvar,
  2636. ],
  2637. )
  2638. .unwrap();
  2639. // create another account
  2640. do_process_instruction(
  2641. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  2642. vec![
  2643. &mut account3_account,
  2644. &mut mint_account,
  2645. &mut owner_account,
  2646. &mut rent_sysvar,
  2647. ],
  2648. )
  2649. .unwrap();
  2650. // create mismatch account
  2651. do_process_instruction(
  2652. initialize_account(&program_id, &mismatch_key, &mint_key, &owner_key).unwrap(),
  2653. vec![
  2654. &mut mismatch_account,
  2655. &mut mint_account,
  2656. &mut owner_account,
  2657. &mut rent_sysvar,
  2658. ],
  2659. )
  2660. .unwrap();
  2661. let mut account = Account::unpack_unchecked(&mismatch_account.data).unwrap();
  2662. account.mint = mint2_key;
  2663. Account::pack(account, &mut mismatch_account.data).unwrap();
  2664. // mint to
  2665. do_process_instruction(
  2666. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 42).unwrap(),
  2667. vec![&mut mint_account, &mut account_account, &mut owner_account],
  2668. )
  2669. .unwrap();
  2670. let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  2671. assert_eq!(mint.supply, 42);
  2672. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  2673. assert_eq!(account.amount, 42);
  2674. // mint to another account to test supply accumulation
  2675. do_process_instruction(
  2676. mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap(),
  2677. vec![&mut mint_account, &mut account2_account, &mut owner_account],
  2678. )
  2679. .unwrap();
  2680. let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  2681. assert_eq!(mint.supply, 84);
  2682. let account = Account::unpack_unchecked(&account2_account.data).unwrap();
  2683. assert_eq!(account.amount, 42);
  2684. // missing signer
  2685. let mut instruction =
  2686. mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap();
  2687. instruction.accounts[2].is_signer = false;
  2688. assert_eq!(
  2689. Err(ProgramError::MissingRequiredSignature),
  2690. do_process_instruction(
  2691. instruction,
  2692. vec![&mut mint_account, &mut account2_account, &mut owner_account],
  2693. )
  2694. );
  2695. // mismatch account
  2696. assert_eq!(
  2697. Err(TokenError::MintMismatch.into()),
  2698. do_process_instruction(
  2699. mint_to(&program_id, &mint_key, &mismatch_key, &owner_key, &[], 42).unwrap(),
  2700. vec![&mut mint_account, &mut mismatch_account, &mut owner_account],
  2701. )
  2702. );
  2703. // missing owner
  2704. assert_eq!(
  2705. Err(TokenError::OwnerMismatch.into()),
  2706. do_process_instruction(
  2707. mint_to(&program_id, &mint_key, &account2_key, &owner2_key, &[], 42).unwrap(),
  2708. vec![
  2709. &mut mint_account,
  2710. &mut account2_account,
  2711. &mut owner2_account,
  2712. ],
  2713. )
  2714. );
  2715. // mint not owned by program
  2716. let not_program_id = Pubkey::new_unique();
  2717. mint_account.owner = not_program_id;
  2718. assert_eq!(
  2719. Err(ProgramError::IncorrectProgramId),
  2720. do_process_instruction(
  2721. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 0).unwrap(),
  2722. vec![&mut mint_account, &mut account_account, &mut owner_account],
  2723. )
  2724. );
  2725. mint_account.owner = program_id;
  2726. // account not owned by program
  2727. let not_program_id = Pubkey::new_unique();
  2728. account_account.owner = not_program_id;
  2729. assert_eq!(
  2730. Err(ProgramError::IncorrectProgramId),
  2731. do_process_instruction(
  2732. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 0).unwrap(),
  2733. vec![&mut mint_account, &mut account_account, &mut owner_account],
  2734. )
  2735. );
  2736. account_account.owner = program_id;
  2737. // uninitialized destination account
  2738. assert_eq!(
  2739. Err(ProgramError::UninitializedAccount),
  2740. do_process_instruction(
  2741. mint_to(
  2742. &program_id,
  2743. &mint_key,
  2744. &uninitialized_key,
  2745. &owner_key,
  2746. &[],
  2747. 42
  2748. )
  2749. .unwrap(),
  2750. vec![
  2751. &mut mint_account,
  2752. &mut uninitialized_account,
  2753. &mut owner_account,
  2754. ],
  2755. )
  2756. );
  2757. // unset mint_authority and test minting fails
  2758. do_process_instruction(
  2759. set_authority(
  2760. &program_id,
  2761. &mint_key,
  2762. None,
  2763. AuthorityType::MintTokens,
  2764. &owner_key,
  2765. &[],
  2766. )
  2767. .unwrap(),
  2768. vec![&mut mint_account, &mut owner_account],
  2769. )
  2770. .unwrap();
  2771. assert_eq!(
  2772. Err(TokenError::FixedSupply.into()),
  2773. do_process_instruction(
  2774. mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap(),
  2775. vec![&mut mint_account, &mut account2_account, &mut owner_account],
  2776. )
  2777. );
  2778. }
  2779. #[test]
  2780. fn test_burn_dups() {
  2781. let program_id = spl_token::id();
  2782. let account1_key = Pubkey::new_unique();
  2783. let mut account1_account = SolanaAccount::new(
  2784. account_minimum_balance(),
  2785. Account::get_packed_len(),
  2786. &program_id,
  2787. );
  2788. let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  2789. let owner_key = Pubkey::new_unique();
  2790. let mut owner_account = SolanaAccount::default();
  2791. let owner_info: AccountInfo = (&owner_key, true, &mut owner_account).into();
  2792. let mint_key = Pubkey::new_unique();
  2793. let mut mint_account =
  2794. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  2795. let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
  2796. let rent_key = rent::id();
  2797. let mut rent_sysvar = rent_sysvar();
  2798. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  2799. // create mint
  2800. do_process_instruction_dups(
  2801. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  2802. vec![mint_info.clone(), rent_info.clone()],
  2803. )
  2804. .unwrap();
  2805. // create account
  2806. do_process_instruction_dups(
  2807. initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
  2808. vec![
  2809. account1_info.clone(),
  2810. mint_info.clone(),
  2811. account1_info.clone(),
  2812. rent_info.clone(),
  2813. ],
  2814. )
  2815. .unwrap();
  2816. // mint to account
  2817. do_process_instruction_dups(
  2818. mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
  2819. vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
  2820. )
  2821. .unwrap();
  2822. // source-owner burn
  2823. do_process_instruction_dups(
  2824. burn(
  2825. &program_id,
  2826. &account1_key,
  2827. &mint_key,
  2828. &account1_key,
  2829. &[],
  2830. 500,
  2831. )
  2832. .unwrap(),
  2833. vec![
  2834. account1_info.clone(),
  2835. mint_info.clone(),
  2836. account1_info.clone(),
  2837. ],
  2838. )
  2839. .unwrap();
  2840. // source-owner burn_checked
  2841. do_process_instruction_dups(
  2842. burn_checked(
  2843. &program_id,
  2844. &account1_key,
  2845. &mint_key,
  2846. &account1_key,
  2847. &[],
  2848. 500,
  2849. 2,
  2850. )
  2851. .unwrap(),
  2852. vec![
  2853. account1_info.clone(),
  2854. mint_info.clone(),
  2855. account1_info.clone(),
  2856. ],
  2857. )
  2858. .unwrap();
  2859. // mint-owner burn
  2860. do_process_instruction_dups(
  2861. mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
  2862. vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
  2863. )
  2864. .unwrap();
  2865. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  2866. account.owner = mint_key;
  2867. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  2868. do_process_instruction_dups(
  2869. burn(&program_id, &account1_key, &mint_key, &mint_key, &[], 500).unwrap(),
  2870. vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
  2871. )
  2872. .unwrap();
  2873. // mint-owner burn_checked
  2874. do_process_instruction_dups(
  2875. burn_checked(
  2876. &program_id,
  2877. &account1_key,
  2878. &mint_key,
  2879. &mint_key,
  2880. &[],
  2881. 500,
  2882. 2,
  2883. )
  2884. .unwrap(),
  2885. vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
  2886. )
  2887. .unwrap();
  2888. // source-delegate burn
  2889. do_process_instruction_dups(
  2890. mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
  2891. vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
  2892. )
  2893. .unwrap();
  2894. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  2895. account.delegated_amount = 1000;
  2896. account.delegate = COption::Some(account1_key);
  2897. account.owner = owner_key;
  2898. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  2899. do_process_instruction_dups(
  2900. burn(
  2901. &program_id,
  2902. &account1_key,
  2903. &mint_key,
  2904. &account1_key,
  2905. &[],
  2906. 500,
  2907. )
  2908. .unwrap(),
  2909. vec![
  2910. account1_info.clone(),
  2911. mint_info.clone(),
  2912. account1_info.clone(),
  2913. ],
  2914. )
  2915. .unwrap();
  2916. // source-delegate burn_checked
  2917. do_process_instruction_dups(
  2918. burn_checked(
  2919. &program_id,
  2920. &account1_key,
  2921. &mint_key,
  2922. &account1_key,
  2923. &[],
  2924. 500,
  2925. 2,
  2926. )
  2927. .unwrap(),
  2928. vec![
  2929. account1_info.clone(),
  2930. mint_info.clone(),
  2931. account1_info.clone(),
  2932. ],
  2933. )
  2934. .unwrap();
  2935. // mint-delegate burn
  2936. do_process_instruction_dups(
  2937. mint_to(&program_id, &mint_key, &account1_key, &owner_key, &[], 1000).unwrap(),
  2938. vec![mint_info.clone(), account1_info.clone(), owner_info.clone()],
  2939. )
  2940. .unwrap();
  2941. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  2942. account.delegated_amount = 1000;
  2943. account.delegate = COption::Some(mint_key);
  2944. account.owner = owner_key;
  2945. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  2946. do_process_instruction_dups(
  2947. burn(&program_id, &account1_key, &mint_key, &mint_key, &[], 500).unwrap(),
  2948. vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
  2949. )
  2950. .unwrap();
  2951. // mint-delegate burn_checked
  2952. do_process_instruction_dups(
  2953. burn_checked(
  2954. &program_id,
  2955. &account1_key,
  2956. &mint_key,
  2957. &mint_key,
  2958. &[],
  2959. 500,
  2960. 2,
  2961. )
  2962. .unwrap(),
  2963. vec![account1_info.clone(), mint_info.clone(), mint_info.clone()],
  2964. )
  2965. .unwrap();
  2966. }
  2967. #[test]
  2968. fn test_burn() {
  2969. let program_id = spl_token::id();
  2970. let account_key = Pubkey::new_unique();
  2971. let mut account_account = SolanaAccount::new(
  2972. account_minimum_balance(),
  2973. Account::get_packed_len(),
  2974. &program_id,
  2975. );
  2976. let account2_key = Pubkey::new_unique();
  2977. let mut account2_account = SolanaAccount::new(
  2978. account_minimum_balance(),
  2979. Account::get_packed_len(),
  2980. &program_id,
  2981. );
  2982. let account3_key = Pubkey::new_unique();
  2983. let mut account3_account = SolanaAccount::new(
  2984. account_minimum_balance(),
  2985. Account::get_packed_len(),
  2986. &program_id,
  2987. );
  2988. let delegate_key = Pubkey::new_unique();
  2989. let mut delegate_account = SolanaAccount::default();
  2990. let mismatch_key = Pubkey::new_unique();
  2991. let mut mismatch_account = SolanaAccount::new(
  2992. account_minimum_balance(),
  2993. Account::get_packed_len(),
  2994. &program_id,
  2995. );
  2996. let owner_key = Pubkey::new_unique();
  2997. let mut owner_account = SolanaAccount::default();
  2998. let owner2_key = Pubkey::new_unique();
  2999. let mut owner2_account = SolanaAccount::default();
  3000. let mint_key = Pubkey::new_unique();
  3001. let mut mint_account =
  3002. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  3003. let mint2_key = Pubkey::new_unique();
  3004. let mut rent_sysvar = rent_sysvar();
  3005. // create new mint
  3006. do_process_instruction(
  3007. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  3008. vec![&mut mint_account, &mut rent_sysvar],
  3009. )
  3010. .unwrap();
  3011. // create account
  3012. do_process_instruction(
  3013. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  3014. vec![
  3015. &mut account_account,
  3016. &mut mint_account,
  3017. &mut owner_account,
  3018. &mut rent_sysvar,
  3019. ],
  3020. )
  3021. .unwrap();
  3022. // create another account
  3023. do_process_instruction(
  3024. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  3025. vec![
  3026. &mut account2_account,
  3027. &mut mint_account,
  3028. &mut owner_account,
  3029. &mut rent_sysvar,
  3030. ],
  3031. )
  3032. .unwrap();
  3033. // create another account
  3034. do_process_instruction(
  3035. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  3036. vec![
  3037. &mut account3_account,
  3038. &mut mint_account,
  3039. &mut owner_account,
  3040. &mut rent_sysvar,
  3041. ],
  3042. )
  3043. .unwrap();
  3044. // create mismatch account
  3045. do_process_instruction(
  3046. initialize_account(&program_id, &mismatch_key, &mint_key, &owner_key).unwrap(),
  3047. vec![
  3048. &mut mismatch_account,
  3049. &mut mint_account,
  3050. &mut owner_account,
  3051. &mut rent_sysvar,
  3052. ],
  3053. )
  3054. .unwrap();
  3055. // mint to account
  3056. do_process_instruction(
  3057. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  3058. vec![&mut mint_account, &mut account_account, &mut owner_account],
  3059. )
  3060. .unwrap();
  3061. // mint to mismatch account and change mint key
  3062. do_process_instruction(
  3063. mint_to(&program_id, &mint_key, &mismatch_key, &owner_key, &[], 1000).unwrap(),
  3064. vec![&mut mint_account, &mut mismatch_account, &mut owner_account],
  3065. )
  3066. .unwrap();
  3067. let mut account = Account::unpack_unchecked(&mismatch_account.data).unwrap();
  3068. account.mint = mint2_key;
  3069. Account::pack(account, &mut mismatch_account.data).unwrap();
  3070. // missing signer
  3071. let mut instruction =
  3072. burn(&program_id, &account_key, &mint_key, &delegate_key, &[], 42).unwrap();
  3073. instruction.accounts[1].is_signer = false;
  3074. assert_eq!(
  3075. Err(TokenError::OwnerMismatch.into()),
  3076. do_process_instruction(
  3077. instruction,
  3078. vec![
  3079. &mut account_account,
  3080. &mut mint_account,
  3081. &mut delegate_account
  3082. ],
  3083. )
  3084. );
  3085. // missing owner
  3086. assert_eq!(
  3087. Err(TokenError::OwnerMismatch.into()),
  3088. do_process_instruction(
  3089. burn(&program_id, &account_key, &mint_key, &owner2_key, &[], 42).unwrap(),
  3090. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  3091. )
  3092. );
  3093. // account not owned by program
  3094. let not_program_id = Pubkey::new_unique();
  3095. account_account.owner = not_program_id;
  3096. assert_eq!(
  3097. Err(ProgramError::IncorrectProgramId),
  3098. do_process_instruction(
  3099. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 0).unwrap(),
  3100. vec![&mut account_account, &mut mint_account, &mut owner_account],
  3101. )
  3102. );
  3103. account_account.owner = program_id;
  3104. // mint not owned by program
  3105. let not_program_id = Pubkey::new_unique();
  3106. mint_account.owner = not_program_id;
  3107. assert_eq!(
  3108. Err(ProgramError::IncorrectProgramId),
  3109. do_process_instruction(
  3110. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 0).unwrap(),
  3111. vec![&mut account_account, &mut mint_account, &mut owner_account],
  3112. )
  3113. );
  3114. mint_account.owner = program_id;
  3115. // mint mismatch
  3116. assert_eq!(
  3117. Err(TokenError::MintMismatch.into()),
  3118. do_process_instruction(
  3119. burn(&program_id, &mismatch_key, &mint_key, &owner_key, &[], 42).unwrap(),
  3120. vec![&mut mismatch_account, &mut mint_account, &mut owner_account],
  3121. )
  3122. );
  3123. // burn
  3124. do_process_instruction(
  3125. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 21).unwrap(),
  3126. vec![&mut account_account, &mut mint_account, &mut owner_account],
  3127. )
  3128. .unwrap();
  3129. // burn_checked, with incorrect decimals
  3130. assert_eq!(
  3131. Err(TokenError::MintDecimalsMismatch.into()),
  3132. do_process_instruction(
  3133. burn_checked(&program_id, &account_key, &mint_key, &owner_key, &[], 21, 3).unwrap(),
  3134. vec![&mut account_account, &mut mint_account, &mut owner_account],
  3135. )
  3136. );
  3137. // burn_checked
  3138. do_process_instruction(
  3139. burn_checked(&program_id, &account_key, &mint_key, &owner_key, &[], 21, 2).unwrap(),
  3140. vec![&mut account_account, &mut mint_account, &mut owner_account],
  3141. )
  3142. .unwrap();
  3143. let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  3144. assert_eq!(mint.supply, 2000 - 42);
  3145. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  3146. assert_eq!(account.amount, 1000 - 42);
  3147. // insufficient funds
  3148. assert_eq!(
  3149. Err(TokenError::InsufficientFunds.into()),
  3150. do_process_instruction(
  3151. burn(
  3152. &program_id,
  3153. &account_key,
  3154. &mint_key,
  3155. &owner_key,
  3156. &[],
  3157. 100_000_000
  3158. )
  3159. .unwrap(),
  3160. vec![&mut account_account, &mut mint_account, &mut owner_account],
  3161. )
  3162. );
  3163. // approve delegate
  3164. do_process_instruction(
  3165. approve(
  3166. &program_id,
  3167. &account_key,
  3168. &delegate_key,
  3169. &owner_key,
  3170. &[],
  3171. 84,
  3172. )
  3173. .unwrap(),
  3174. vec![
  3175. &mut account_account,
  3176. &mut delegate_account,
  3177. &mut owner_account,
  3178. ],
  3179. )
  3180. .unwrap();
  3181. // not a delegate of source account
  3182. assert_eq!(
  3183. Err(TokenError::OwnerMismatch.into()),
  3184. do_process_instruction(
  3185. burn(
  3186. &program_id,
  3187. &account_key,
  3188. &mint_key,
  3189. &owner2_key, // <-- incorrect owner or delegate
  3190. &[],
  3191. 1,
  3192. )
  3193. .unwrap(),
  3194. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  3195. )
  3196. );
  3197. // insufficient funds approved via delegate
  3198. assert_eq!(
  3199. Err(TokenError::InsufficientFunds.into()),
  3200. do_process_instruction(
  3201. burn(&program_id, &account_key, &mint_key, &delegate_key, &[], 85).unwrap(),
  3202. vec![
  3203. &mut account_account,
  3204. &mut mint_account,
  3205. &mut delegate_account
  3206. ],
  3207. )
  3208. );
  3209. // burn via delegate
  3210. do_process_instruction(
  3211. burn(&program_id, &account_key, &mint_key, &delegate_key, &[], 84).unwrap(),
  3212. vec![
  3213. &mut account_account,
  3214. &mut mint_account,
  3215. &mut delegate_account,
  3216. ],
  3217. )
  3218. .unwrap();
  3219. // match
  3220. let mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  3221. assert_eq!(mint.supply, 2000 - 42 - 84);
  3222. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  3223. assert_eq!(account.amount, 1000 - 42 - 84);
  3224. // insufficient funds approved via delegate
  3225. assert_eq!(
  3226. Err(TokenError::OwnerMismatch.into()),
  3227. do_process_instruction(
  3228. burn(&program_id, &account_key, &mint_key, &delegate_key, &[], 1).unwrap(),
  3229. vec![
  3230. &mut account_account,
  3231. &mut mint_account,
  3232. &mut delegate_account
  3233. ],
  3234. )
  3235. );
  3236. }
  3237. #[test]
  3238. fn test_burn_and_close_system_and_incinerator_tokens() {
  3239. let program_id = spl_token::id();
  3240. let account_key = Pubkey::new_unique();
  3241. let mut account_account = SolanaAccount::new(
  3242. account_minimum_balance(),
  3243. Account::get_packed_len(),
  3244. &program_id,
  3245. );
  3246. let incinerator_account_key = Pubkey::new_unique();
  3247. let mut incinerator_account = SolanaAccount::new(
  3248. account_minimum_balance(),
  3249. Account::get_packed_len(),
  3250. &program_id,
  3251. );
  3252. let system_account_key = Pubkey::new_unique();
  3253. let mut system_account = SolanaAccount::new(
  3254. account_minimum_balance(),
  3255. Account::get_packed_len(),
  3256. &program_id,
  3257. );
  3258. let owner_key = Pubkey::new_unique();
  3259. let mut owner_account = SolanaAccount::default();
  3260. let recipient_key = Pubkey::new_unique();
  3261. let mut recipient_account = SolanaAccount::default();
  3262. let mut mock_incinerator_account = SolanaAccount::default();
  3263. let mint_key = Pubkey::new_unique();
  3264. let mut mint_account =
  3265. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  3266. // create new mint
  3267. do_process_instruction(
  3268. initialize_mint2(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  3269. vec![&mut mint_account],
  3270. )
  3271. .unwrap();
  3272. // create account
  3273. do_process_instruction(
  3274. initialize_account3(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  3275. vec![&mut account_account, &mut mint_account],
  3276. )
  3277. .unwrap();
  3278. // create incinerator- and system-owned accounts
  3279. do_process_instruction(
  3280. initialize_account3(
  3281. &program_id,
  3282. &incinerator_account_key,
  3283. &mint_key,
  3284. &solana_program::incinerator::id(),
  3285. )
  3286. .unwrap(),
  3287. vec![&mut incinerator_account, &mut mint_account],
  3288. )
  3289. .unwrap();
  3290. do_process_instruction(
  3291. initialize_account3(
  3292. &program_id,
  3293. &system_account_key,
  3294. &mint_key,
  3295. &solana_program::system_program::id(),
  3296. )
  3297. .unwrap(),
  3298. vec![&mut system_account, &mut mint_account],
  3299. )
  3300. .unwrap();
  3301. // mint to account
  3302. do_process_instruction(
  3303. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  3304. vec![&mut mint_account, &mut account_account, &mut owner_account],
  3305. )
  3306. .unwrap();
  3307. // transfer half to incinerator, half to system program
  3308. do_process_instruction(
  3309. transfer(
  3310. &program_id,
  3311. &account_key,
  3312. &incinerator_account_key,
  3313. &owner_key,
  3314. &[],
  3315. 500,
  3316. )
  3317. .unwrap(),
  3318. vec![
  3319. &mut account_account,
  3320. &mut incinerator_account,
  3321. &mut owner_account,
  3322. ],
  3323. )
  3324. .unwrap();
  3325. do_process_instruction(
  3326. transfer(
  3327. &program_id,
  3328. &account_key,
  3329. &system_account_key,
  3330. &owner_key,
  3331. &[],
  3332. 500,
  3333. )
  3334. .unwrap(),
  3335. vec![
  3336. &mut account_account,
  3337. &mut system_account,
  3338. &mut owner_account,
  3339. ],
  3340. )
  3341. .unwrap();
  3342. // close with balance fails
  3343. assert_eq!(
  3344. Err(TokenError::NonNativeHasBalance.into()),
  3345. do_process_instruction(
  3346. close_account(
  3347. &program_id,
  3348. &incinerator_account_key,
  3349. &solana_program::incinerator::id(),
  3350. &owner_key,
  3351. &[]
  3352. )
  3353. .unwrap(),
  3354. vec![
  3355. &mut incinerator_account,
  3356. &mut mock_incinerator_account,
  3357. &mut owner_account,
  3358. ],
  3359. )
  3360. );
  3361. assert_eq!(
  3362. Err(TokenError::NonNativeHasBalance.into()),
  3363. do_process_instruction(
  3364. close_account(
  3365. &program_id,
  3366. &system_account_key,
  3367. &solana_program::incinerator::id(),
  3368. &owner_key,
  3369. &[]
  3370. )
  3371. .unwrap(),
  3372. vec![
  3373. &mut system_account,
  3374. &mut mock_incinerator_account,
  3375. &mut owner_account,
  3376. ],
  3377. )
  3378. );
  3379. // anyone can burn
  3380. do_process_instruction(
  3381. burn(
  3382. &program_id,
  3383. &incinerator_account_key,
  3384. &mint_key,
  3385. &recipient_key,
  3386. &[],
  3387. 500,
  3388. )
  3389. .unwrap(),
  3390. vec![
  3391. &mut incinerator_account,
  3392. &mut mint_account,
  3393. &mut recipient_account,
  3394. ],
  3395. )
  3396. .unwrap();
  3397. do_process_instruction(
  3398. burn(
  3399. &program_id,
  3400. &system_account_key,
  3401. &mint_key,
  3402. &recipient_key,
  3403. &[],
  3404. 500,
  3405. )
  3406. .unwrap(),
  3407. vec![
  3408. &mut system_account,
  3409. &mut mint_account,
  3410. &mut recipient_account,
  3411. ],
  3412. )
  3413. .unwrap();
  3414. // closing fails if destination is not the incinerator
  3415. assert_eq!(
  3416. Err(ProgramError::InvalidAccountData),
  3417. do_process_instruction(
  3418. close_account(
  3419. &program_id,
  3420. &incinerator_account_key,
  3421. &recipient_key,
  3422. &owner_key,
  3423. &[]
  3424. )
  3425. .unwrap(),
  3426. vec![
  3427. &mut incinerator_account,
  3428. &mut recipient_account,
  3429. &mut owner_account,
  3430. ],
  3431. )
  3432. );
  3433. assert_eq!(
  3434. Err(ProgramError::InvalidAccountData),
  3435. do_process_instruction(
  3436. close_account(
  3437. &program_id,
  3438. &system_account_key,
  3439. &recipient_key,
  3440. &owner_key,
  3441. &[]
  3442. )
  3443. .unwrap(),
  3444. vec![
  3445. &mut system_account,
  3446. &mut recipient_account,
  3447. &mut owner_account,
  3448. ],
  3449. )
  3450. );
  3451. // closing succeeds with incinerator recipient
  3452. do_process_instruction(
  3453. close_account(
  3454. &program_id,
  3455. &incinerator_account_key,
  3456. &solana_program::incinerator::id(),
  3457. &owner_key,
  3458. &[],
  3459. )
  3460. .unwrap(),
  3461. vec![
  3462. &mut incinerator_account,
  3463. &mut mock_incinerator_account,
  3464. &mut owner_account,
  3465. ],
  3466. )
  3467. .unwrap();
  3468. do_process_instruction(
  3469. close_account(
  3470. &program_id,
  3471. &system_account_key,
  3472. &solana_program::incinerator::id(),
  3473. &owner_key,
  3474. &[],
  3475. )
  3476. .unwrap(),
  3477. vec![
  3478. &mut system_account,
  3479. &mut mock_incinerator_account,
  3480. &mut owner_account,
  3481. ],
  3482. )
  3483. .unwrap();
  3484. }
  3485. #[test]
  3486. fn test_multisig() {
  3487. let program_id = spl_token::id();
  3488. let mint_key = Pubkey::new_unique();
  3489. let mut mint_account =
  3490. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  3491. let account_key = Pubkey::new_unique();
  3492. let mut account = SolanaAccount::new(
  3493. account_minimum_balance(),
  3494. Account::get_packed_len(),
  3495. &program_id,
  3496. );
  3497. let account2_key = Pubkey::new_unique();
  3498. let mut account2_account = SolanaAccount::new(
  3499. account_minimum_balance(),
  3500. Account::get_packed_len(),
  3501. &program_id,
  3502. );
  3503. let owner_key = Pubkey::new_unique();
  3504. let mut owner_account = SolanaAccount::default();
  3505. let multisig_key = Pubkey::new_unique();
  3506. let mut multisig_account = SolanaAccount::new(42, Multisig::get_packed_len(), &program_id);
  3507. let multisig_delegate_key = Pubkey::new_unique();
  3508. let mut multisig_delegate_account = SolanaAccount::new(
  3509. multisig_minimum_balance(),
  3510. Multisig::get_packed_len(),
  3511. &program_id,
  3512. );
  3513. let signer_keys = vec![Pubkey::new_unique(); MAX_SIGNERS];
  3514. let signer_key_refs: Vec<&Pubkey> = signer_keys.iter().collect();
  3515. let mut signer_accounts = vec![SolanaAccount::new(0, 0, &program_id); MAX_SIGNERS];
  3516. let mut rent_sysvar = rent_sysvar();
  3517. // multisig is not rent exempt
  3518. let account_info_iter = &mut signer_accounts.iter_mut();
  3519. assert_eq!(
  3520. Err(TokenError::NotRentExempt.into()),
  3521. do_process_instruction(
  3522. initialize_multisig(&program_id, &multisig_key, &[&signer_keys[0]], 1).unwrap(),
  3523. vec![
  3524. &mut multisig_account,
  3525. &mut rent_sysvar,
  3526. account_info_iter.next().unwrap(),
  3527. ],
  3528. )
  3529. );
  3530. multisig_account.lamports = multisig_minimum_balance();
  3531. let mut multisig_account2 = multisig_account.clone();
  3532. // single signer
  3533. let account_info_iter = &mut signer_accounts.iter_mut();
  3534. do_process_instruction(
  3535. initialize_multisig(&program_id, &multisig_key, &[&signer_keys[0]], 1).unwrap(),
  3536. vec![
  3537. &mut multisig_account,
  3538. &mut rent_sysvar,
  3539. account_info_iter.next().unwrap(),
  3540. ],
  3541. )
  3542. .unwrap();
  3543. // single signer using `initialize_multisig2`
  3544. let account_info_iter = &mut signer_accounts.iter_mut();
  3545. do_process_instruction(
  3546. initialize_multisig2(&program_id, &multisig_key, &[&signer_keys[0]], 1).unwrap(),
  3547. vec![&mut multisig_account2, account_info_iter.next().unwrap()],
  3548. )
  3549. .unwrap();
  3550. // multiple signer
  3551. let account_info_iter = &mut signer_accounts.iter_mut();
  3552. do_process_instruction(
  3553. initialize_multisig(
  3554. &program_id,
  3555. &multisig_delegate_key,
  3556. &signer_key_refs,
  3557. MAX_SIGNERS as u8,
  3558. )
  3559. .unwrap(),
  3560. vec![
  3561. &mut multisig_delegate_account,
  3562. &mut rent_sysvar,
  3563. account_info_iter.next().unwrap(),
  3564. account_info_iter.next().unwrap(),
  3565. account_info_iter.next().unwrap(),
  3566. account_info_iter.next().unwrap(),
  3567. account_info_iter.next().unwrap(),
  3568. account_info_iter.next().unwrap(),
  3569. account_info_iter.next().unwrap(),
  3570. account_info_iter.next().unwrap(),
  3571. account_info_iter.next().unwrap(),
  3572. account_info_iter.next().unwrap(),
  3573. account_info_iter.next().unwrap(),
  3574. ],
  3575. )
  3576. .unwrap();
  3577. // create new mint with multisig owner
  3578. do_process_instruction(
  3579. initialize_mint(&program_id, &mint_key, &multisig_key, None, 2).unwrap(),
  3580. vec![&mut mint_account, &mut rent_sysvar],
  3581. )
  3582. .unwrap();
  3583. // create account with multisig owner
  3584. do_process_instruction(
  3585. initialize_account(&program_id, &account_key, &mint_key, &multisig_key).unwrap(),
  3586. vec![
  3587. &mut account,
  3588. &mut mint_account,
  3589. &mut multisig_account,
  3590. &mut rent_sysvar,
  3591. ],
  3592. )
  3593. .unwrap();
  3594. // create another account with multisig owner
  3595. do_process_instruction(
  3596. initialize_account(
  3597. &program_id,
  3598. &account2_key,
  3599. &mint_key,
  3600. &multisig_delegate_key,
  3601. )
  3602. .unwrap(),
  3603. vec![
  3604. &mut account2_account,
  3605. &mut mint_account,
  3606. &mut multisig_account,
  3607. &mut rent_sysvar,
  3608. ],
  3609. )
  3610. .unwrap();
  3611. // mint to account
  3612. let account_info_iter = &mut signer_accounts.iter_mut();
  3613. do_process_instruction(
  3614. mint_to(
  3615. &program_id,
  3616. &mint_key,
  3617. &account_key,
  3618. &multisig_key,
  3619. &[&signer_keys[0]],
  3620. 1000,
  3621. )
  3622. .unwrap(),
  3623. vec![
  3624. &mut mint_account,
  3625. &mut account,
  3626. &mut multisig_account,
  3627. account_info_iter.next().unwrap(),
  3628. ],
  3629. )
  3630. .unwrap();
  3631. // approve
  3632. let account_info_iter = &mut signer_accounts.iter_mut();
  3633. do_process_instruction(
  3634. approve(
  3635. &program_id,
  3636. &account_key,
  3637. &multisig_delegate_key,
  3638. &multisig_key,
  3639. &[&signer_keys[0]],
  3640. 100,
  3641. )
  3642. .unwrap(),
  3643. vec![
  3644. &mut account,
  3645. &mut multisig_delegate_account,
  3646. &mut multisig_account,
  3647. account_info_iter.next().unwrap(),
  3648. ],
  3649. )
  3650. .unwrap();
  3651. // transfer
  3652. let account_info_iter = &mut signer_accounts.iter_mut();
  3653. do_process_instruction(
  3654. transfer(
  3655. &program_id,
  3656. &account_key,
  3657. &account2_key,
  3658. &multisig_key,
  3659. &[&signer_keys[0]],
  3660. 42,
  3661. )
  3662. .unwrap(),
  3663. vec![
  3664. &mut account,
  3665. &mut account2_account,
  3666. &mut multisig_account,
  3667. account_info_iter.next().unwrap(),
  3668. ],
  3669. )
  3670. .unwrap();
  3671. // transfer via delegate
  3672. let account_info_iter = &mut signer_accounts.iter_mut();
  3673. do_process_instruction(
  3674. transfer(
  3675. &program_id,
  3676. &account_key,
  3677. &account2_key,
  3678. &multisig_delegate_key,
  3679. &signer_key_refs,
  3680. 42,
  3681. )
  3682. .unwrap(),
  3683. vec![
  3684. &mut account,
  3685. &mut account2_account,
  3686. &mut multisig_delegate_account,
  3687. account_info_iter.next().unwrap(),
  3688. account_info_iter.next().unwrap(),
  3689. account_info_iter.next().unwrap(),
  3690. account_info_iter.next().unwrap(),
  3691. account_info_iter.next().unwrap(),
  3692. account_info_iter.next().unwrap(),
  3693. account_info_iter.next().unwrap(),
  3694. account_info_iter.next().unwrap(),
  3695. account_info_iter.next().unwrap(),
  3696. account_info_iter.next().unwrap(),
  3697. account_info_iter.next().unwrap(),
  3698. ],
  3699. )
  3700. .unwrap();
  3701. // mint to
  3702. let account_info_iter = &mut signer_accounts.iter_mut();
  3703. do_process_instruction(
  3704. mint_to(
  3705. &program_id,
  3706. &mint_key,
  3707. &account2_key,
  3708. &multisig_key,
  3709. &[&signer_keys[0]],
  3710. 42,
  3711. )
  3712. .unwrap(),
  3713. vec![
  3714. &mut mint_account,
  3715. &mut account2_account,
  3716. &mut multisig_account,
  3717. account_info_iter.next().unwrap(),
  3718. ],
  3719. )
  3720. .unwrap();
  3721. // burn
  3722. let account_info_iter = &mut signer_accounts.iter_mut();
  3723. do_process_instruction(
  3724. burn(
  3725. &program_id,
  3726. &account_key,
  3727. &mint_key,
  3728. &multisig_key,
  3729. &[&signer_keys[0]],
  3730. 42,
  3731. )
  3732. .unwrap(),
  3733. vec![
  3734. &mut account,
  3735. &mut mint_account,
  3736. &mut multisig_account,
  3737. account_info_iter.next().unwrap(),
  3738. ],
  3739. )
  3740. .unwrap();
  3741. // burn via delegate
  3742. let account_info_iter = &mut signer_accounts.iter_mut();
  3743. do_process_instruction(
  3744. burn(
  3745. &program_id,
  3746. &account_key,
  3747. &mint_key,
  3748. &multisig_delegate_key,
  3749. &signer_key_refs,
  3750. 42,
  3751. )
  3752. .unwrap(),
  3753. vec![
  3754. &mut account,
  3755. &mut mint_account,
  3756. &mut multisig_delegate_account,
  3757. account_info_iter.next().unwrap(),
  3758. account_info_iter.next().unwrap(),
  3759. account_info_iter.next().unwrap(),
  3760. account_info_iter.next().unwrap(),
  3761. account_info_iter.next().unwrap(),
  3762. account_info_iter.next().unwrap(),
  3763. account_info_iter.next().unwrap(),
  3764. account_info_iter.next().unwrap(),
  3765. account_info_iter.next().unwrap(),
  3766. account_info_iter.next().unwrap(),
  3767. account_info_iter.next().unwrap(),
  3768. ],
  3769. )
  3770. .unwrap();
  3771. // freeze account
  3772. let account3_key = Pubkey::new_unique();
  3773. let mut account3_account = SolanaAccount::new(
  3774. account_minimum_balance(),
  3775. Account::get_packed_len(),
  3776. &program_id,
  3777. );
  3778. let mint2_key = Pubkey::new_unique();
  3779. let mut mint2_account =
  3780. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  3781. do_process_instruction(
  3782. initialize_mint(
  3783. &program_id,
  3784. &mint2_key,
  3785. &multisig_key,
  3786. Some(&multisig_key),
  3787. 2,
  3788. )
  3789. .unwrap(),
  3790. vec![&mut mint2_account, &mut rent_sysvar],
  3791. )
  3792. .unwrap();
  3793. do_process_instruction(
  3794. initialize_account(&program_id, &account3_key, &mint2_key, &owner_key).unwrap(),
  3795. vec![
  3796. &mut account3_account,
  3797. &mut mint2_account,
  3798. &mut owner_account,
  3799. &mut rent_sysvar,
  3800. ],
  3801. )
  3802. .unwrap();
  3803. let account_info_iter = &mut signer_accounts.iter_mut();
  3804. do_process_instruction(
  3805. mint_to(
  3806. &program_id,
  3807. &mint2_key,
  3808. &account3_key,
  3809. &multisig_key,
  3810. &[&signer_keys[0]],
  3811. 1000,
  3812. )
  3813. .unwrap(),
  3814. vec![
  3815. &mut mint2_account,
  3816. &mut account3_account,
  3817. &mut multisig_account,
  3818. account_info_iter.next().unwrap(),
  3819. ],
  3820. )
  3821. .unwrap();
  3822. let account_info_iter = &mut signer_accounts.iter_mut();
  3823. do_process_instruction(
  3824. freeze_account(
  3825. &program_id,
  3826. &account3_key,
  3827. &mint2_key,
  3828. &multisig_key,
  3829. &[&signer_keys[0]],
  3830. )
  3831. .unwrap(),
  3832. vec![
  3833. &mut account3_account,
  3834. &mut mint2_account,
  3835. &mut multisig_account,
  3836. account_info_iter.next().unwrap(),
  3837. ],
  3838. )
  3839. .unwrap();
  3840. // do SetAuthority on mint
  3841. let account_info_iter = &mut signer_accounts.iter_mut();
  3842. do_process_instruction(
  3843. set_authority(
  3844. &program_id,
  3845. &mint_key,
  3846. Some(&owner_key),
  3847. AuthorityType::MintTokens,
  3848. &multisig_key,
  3849. &[&signer_keys[0]],
  3850. )
  3851. .unwrap(),
  3852. vec![
  3853. &mut mint_account,
  3854. &mut multisig_account,
  3855. account_info_iter.next().unwrap(),
  3856. ],
  3857. )
  3858. .unwrap();
  3859. // do SetAuthority on account
  3860. let account_info_iter = &mut signer_accounts.iter_mut();
  3861. do_process_instruction(
  3862. set_authority(
  3863. &program_id,
  3864. &account_key,
  3865. Some(&owner_key),
  3866. AuthorityType::AccountOwner,
  3867. &multisig_key,
  3868. &[&signer_keys[0]],
  3869. )
  3870. .unwrap(),
  3871. vec![
  3872. &mut account,
  3873. &mut multisig_account,
  3874. account_info_iter.next().unwrap(),
  3875. ],
  3876. )
  3877. .unwrap();
  3878. }
  3879. #[test]
  3880. fn test_owner_close_account_dups() {
  3881. let program_id = spl_token::id();
  3882. let owner_key = Pubkey::new_unique();
  3883. let mint_key = Pubkey::new_unique();
  3884. let mut mint_account =
  3885. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  3886. let mint_info: AccountInfo = (&mint_key, false, &mut mint_account).into();
  3887. let rent_key = rent::id();
  3888. let mut rent_sysvar = rent_sysvar();
  3889. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  3890. // create mint
  3891. do_process_instruction_dups(
  3892. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  3893. vec![mint_info.clone(), rent_info.clone()],
  3894. )
  3895. .unwrap();
  3896. let to_close_key = Pubkey::new_unique();
  3897. let mut to_close_account = SolanaAccount::new(
  3898. account_minimum_balance(),
  3899. Account::get_packed_len(),
  3900. &program_id,
  3901. );
  3902. let to_close_account_info: AccountInfo = (&to_close_key, true, &mut to_close_account).into();
  3903. let destination_account_key = Pubkey::new_unique();
  3904. let mut destination_account = SolanaAccount::new(
  3905. account_minimum_balance(),
  3906. Account::get_packed_len(),
  3907. &program_id,
  3908. );
  3909. let destination_account_info: AccountInfo =
  3910. (&destination_account_key, true, &mut destination_account).into();
  3911. // create account
  3912. do_process_instruction_dups(
  3913. initialize_account(&program_id, &to_close_key, &mint_key, &to_close_key).unwrap(),
  3914. vec![
  3915. to_close_account_info.clone(),
  3916. mint_info.clone(),
  3917. to_close_account_info.clone(),
  3918. rent_info.clone(),
  3919. ],
  3920. )
  3921. .unwrap();
  3922. // source-owner close
  3923. do_process_instruction_dups(
  3924. close_account(
  3925. &program_id,
  3926. &to_close_key,
  3927. &destination_account_key,
  3928. &to_close_key,
  3929. &[],
  3930. )
  3931. .unwrap(),
  3932. vec![
  3933. to_close_account_info.clone(),
  3934. destination_account_info.clone(),
  3935. to_close_account_info.clone(),
  3936. ],
  3937. )
  3938. .unwrap();
  3939. assert_eq!(*to_close_account_info.data.borrow(), &[0u8; Account::LEN]);
  3940. }
  3941. #[test]
  3942. fn test_close_authority_close_account_dups() {
  3943. let program_id = spl_token::id();
  3944. let owner_key = Pubkey::new_unique();
  3945. let mint_key = Pubkey::new_unique();
  3946. let mut mint_account =
  3947. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  3948. let mint_info: AccountInfo = (&mint_key, false, &mut mint_account).into();
  3949. let rent_key = rent::id();
  3950. let mut rent_sysvar = rent_sysvar();
  3951. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  3952. // create mint
  3953. do_process_instruction_dups(
  3954. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  3955. vec![mint_info.clone(), rent_info.clone()],
  3956. )
  3957. .unwrap();
  3958. let to_close_key = Pubkey::new_unique();
  3959. let mut to_close_account = SolanaAccount::new(
  3960. account_minimum_balance(),
  3961. Account::get_packed_len(),
  3962. &program_id,
  3963. );
  3964. let to_close_account_info: AccountInfo = (&to_close_key, true, &mut to_close_account).into();
  3965. let destination_account_key = Pubkey::new_unique();
  3966. let mut destination_account = SolanaAccount::new(
  3967. account_minimum_balance(),
  3968. Account::get_packed_len(),
  3969. &program_id,
  3970. );
  3971. let destination_account_info: AccountInfo =
  3972. (&destination_account_key, true, &mut destination_account).into();
  3973. // create account
  3974. do_process_instruction_dups(
  3975. initialize_account(&program_id, &to_close_key, &mint_key, &to_close_key).unwrap(),
  3976. vec![
  3977. to_close_account_info.clone(),
  3978. mint_info.clone(),
  3979. to_close_account_info.clone(),
  3980. rent_info.clone(),
  3981. ],
  3982. )
  3983. .unwrap();
  3984. let mut account = Account::unpack_unchecked(&to_close_account_info.data.borrow()).unwrap();
  3985. account.close_authority = COption::Some(to_close_key);
  3986. account.owner = owner_key;
  3987. Account::pack(account, &mut to_close_account_info.data.borrow_mut()).unwrap();
  3988. do_process_instruction_dups(
  3989. close_account(
  3990. &program_id,
  3991. &to_close_key,
  3992. &destination_account_key,
  3993. &to_close_key,
  3994. &[],
  3995. )
  3996. .unwrap(),
  3997. vec![
  3998. to_close_account_info.clone(),
  3999. destination_account_info.clone(),
  4000. to_close_account_info.clone(),
  4001. ],
  4002. )
  4003. .unwrap();
  4004. assert_eq!(*to_close_account_info.data.borrow(), &[0u8; Account::LEN]);
  4005. }
  4006. #[test]
  4007. fn test_close_account() {
  4008. let program_id = spl_token::id();
  4009. let mint_key = Pubkey::new_unique();
  4010. let mut mint_account =
  4011. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4012. let account_key = Pubkey::new_unique();
  4013. let mut account_account = SolanaAccount::new(
  4014. account_minimum_balance(),
  4015. Account::get_packed_len(),
  4016. &program_id,
  4017. );
  4018. let account2_key = Pubkey::new_unique();
  4019. let mut account2_account = SolanaAccount::new(
  4020. account_minimum_balance() + 42,
  4021. Account::get_packed_len(),
  4022. &program_id,
  4023. );
  4024. let account3_key = Pubkey::new_unique();
  4025. let mut account3_account = SolanaAccount::new(
  4026. account_minimum_balance(),
  4027. Account::get_packed_len(),
  4028. &program_id,
  4029. );
  4030. let owner_key = Pubkey::new_unique();
  4031. let mut owner_account = SolanaAccount::default();
  4032. let owner2_key = Pubkey::new_unique();
  4033. let mut owner2_account = SolanaAccount::default();
  4034. let mut rent_sysvar = rent_sysvar();
  4035. // uninitialized
  4036. assert_eq!(
  4037. Err(ProgramError::UninitializedAccount),
  4038. do_process_instruction(
  4039. close_account(&program_id, &account_key, &account3_key, &owner2_key, &[]).unwrap(),
  4040. vec![
  4041. &mut account_account,
  4042. &mut account3_account,
  4043. &mut owner2_account,
  4044. ],
  4045. )
  4046. );
  4047. // initialize and mint to non-native account
  4048. do_process_instruction(
  4049. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  4050. vec![&mut mint_account, &mut rent_sysvar],
  4051. )
  4052. .unwrap();
  4053. do_process_instruction(
  4054. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4055. vec![
  4056. &mut account_account,
  4057. &mut mint_account,
  4058. &mut owner_account,
  4059. &mut rent_sysvar,
  4060. ],
  4061. )
  4062. .unwrap();
  4063. do_process_instruction(
  4064. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 42).unwrap(),
  4065. vec![
  4066. &mut mint_account,
  4067. &mut account_account,
  4068. &mut owner_account,
  4069. &mut rent_sysvar,
  4070. ],
  4071. )
  4072. .unwrap();
  4073. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4074. assert_eq!(account.amount, 42);
  4075. // initialize native account
  4076. do_process_instruction(
  4077. initialize_account(
  4078. &program_id,
  4079. &account2_key,
  4080. &spl_token::native_mint::id(),
  4081. &owner_key,
  4082. )
  4083. .unwrap(),
  4084. vec![
  4085. &mut account2_account,
  4086. &mut mint_account,
  4087. &mut owner_account,
  4088. &mut rent_sysvar,
  4089. ],
  4090. )
  4091. .unwrap();
  4092. let account = Account::unpack_unchecked(&account2_account.data).unwrap();
  4093. assert!(account.is_native());
  4094. assert_eq!(account.amount, 42);
  4095. // close non-native account with balance
  4096. assert_eq!(
  4097. Err(TokenError::NonNativeHasBalance.into()),
  4098. do_process_instruction(
  4099. close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
  4100. vec![
  4101. &mut account_account,
  4102. &mut account3_account,
  4103. &mut owner_account,
  4104. ],
  4105. )
  4106. );
  4107. assert_eq!(account_account.lamports, account_minimum_balance());
  4108. // empty account
  4109. do_process_instruction(
  4110. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 42).unwrap(),
  4111. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4112. )
  4113. .unwrap();
  4114. // wrong owner
  4115. assert_eq!(
  4116. Err(TokenError::OwnerMismatch.into()),
  4117. do_process_instruction(
  4118. close_account(&program_id, &account_key, &account3_key, &owner2_key, &[]).unwrap(),
  4119. vec![
  4120. &mut account_account,
  4121. &mut account3_account,
  4122. &mut owner2_account,
  4123. ],
  4124. )
  4125. );
  4126. // close account
  4127. do_process_instruction(
  4128. close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
  4129. vec![
  4130. &mut account_account,
  4131. &mut account3_account,
  4132. &mut owner_account,
  4133. ],
  4134. )
  4135. .unwrap();
  4136. assert!(account_account.data.is_empty());
  4137. assert_eq!(account_account.lamports, 0);
  4138. assert_eq!(account3_account.lamports, 2 * account_minimum_balance());
  4139. // fund and initialize new non-native account to test close authority
  4140. let account_key = Pubkey::new_unique();
  4141. let mut account_account = SolanaAccount::new(
  4142. account_minimum_balance(),
  4143. Account::get_packed_len(),
  4144. &program_id,
  4145. );
  4146. let owner2_key = Pubkey::new_unique();
  4147. let mut owner2_account = SolanaAccount::new(
  4148. account_minimum_balance(),
  4149. Account::get_packed_len(),
  4150. &program_id,
  4151. );
  4152. do_process_instruction(
  4153. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4154. vec![
  4155. &mut account_account,
  4156. &mut mint_account,
  4157. &mut owner_account,
  4158. &mut rent_sysvar,
  4159. ],
  4160. )
  4161. .unwrap();
  4162. account_account.lamports = 2;
  4163. do_process_instruction(
  4164. set_authority(
  4165. &program_id,
  4166. &account_key,
  4167. Some(&owner2_key),
  4168. AuthorityType::CloseAccount,
  4169. &owner_key,
  4170. &[],
  4171. )
  4172. .unwrap(),
  4173. vec![&mut account_account, &mut owner_account],
  4174. )
  4175. .unwrap();
  4176. // account owner cannot authorize close if close_authority is set
  4177. assert_eq!(
  4178. Err(TokenError::OwnerMismatch.into()),
  4179. do_process_instruction(
  4180. close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
  4181. vec![
  4182. &mut account_account,
  4183. &mut account3_account,
  4184. &mut owner_account,
  4185. ],
  4186. )
  4187. );
  4188. // close non-native account with close_authority
  4189. do_process_instruction(
  4190. close_account(&program_id, &account_key, &account3_key, &owner2_key, &[]).unwrap(),
  4191. vec![
  4192. &mut account_account,
  4193. &mut account3_account,
  4194. &mut owner2_account,
  4195. ],
  4196. )
  4197. .unwrap();
  4198. assert!(account_account.data.is_empty());
  4199. assert_eq!(account_account.lamports, 0);
  4200. assert_eq!(account3_account.lamports, 2 * account_minimum_balance() + 2);
  4201. // close native account
  4202. do_process_instruction(
  4203. close_account(&program_id, &account2_key, &account3_key, &owner_key, &[]).unwrap(),
  4204. vec![
  4205. &mut account2_account,
  4206. &mut account3_account,
  4207. &mut owner_account,
  4208. ],
  4209. )
  4210. .unwrap();
  4211. assert!(account2_account.data.is_empty());
  4212. assert_eq!(
  4213. account3_account.lamports,
  4214. 3 * account_minimum_balance() + 2 + 42
  4215. );
  4216. }
  4217. #[test]
  4218. fn test_native_token() {
  4219. let program_id = spl_token::id();
  4220. let mut mint_account =
  4221. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4222. let account_key = Pubkey::new_unique();
  4223. let mut account_account = SolanaAccount::new(
  4224. account_minimum_balance() + 40,
  4225. Account::get_packed_len(),
  4226. &program_id,
  4227. );
  4228. let account2_key = Pubkey::new_unique();
  4229. let mut account2_account = SolanaAccount::new(
  4230. account_minimum_balance(),
  4231. Account::get_packed_len(),
  4232. &program_id,
  4233. );
  4234. let account3_key = Pubkey::new_unique();
  4235. let mut account3_account = SolanaAccount::new(account_minimum_balance(), 0, &program_id);
  4236. let owner_key = Pubkey::new_unique();
  4237. let mut owner_account = SolanaAccount::default();
  4238. let owner2_key = Pubkey::new_unique();
  4239. let mut owner2_account = SolanaAccount::default();
  4240. let owner3_key = Pubkey::new_unique();
  4241. let mut rent_sysvar = rent_sysvar();
  4242. // initialize native account
  4243. do_process_instruction(
  4244. initialize_account(
  4245. &program_id,
  4246. &account_key,
  4247. &spl_token::native_mint::id(),
  4248. &owner_key,
  4249. )
  4250. .unwrap(),
  4251. vec![
  4252. &mut account_account,
  4253. &mut mint_account,
  4254. &mut owner_account,
  4255. &mut rent_sysvar,
  4256. ],
  4257. )
  4258. .unwrap();
  4259. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4260. assert!(account.is_native());
  4261. assert_eq!(account.amount, 40);
  4262. // initialize native account
  4263. do_process_instruction(
  4264. initialize_account(
  4265. &program_id,
  4266. &account2_key,
  4267. &spl_token::native_mint::id(),
  4268. &owner_key,
  4269. )
  4270. .unwrap(),
  4271. vec![
  4272. &mut account2_account,
  4273. &mut mint_account,
  4274. &mut owner_account,
  4275. &mut rent_sysvar,
  4276. ],
  4277. )
  4278. .unwrap();
  4279. let account = Account::unpack_unchecked(&account2_account.data).unwrap();
  4280. assert!(account.is_native());
  4281. assert_eq!(account.amount, 0);
  4282. // mint_to unsupported
  4283. assert_eq!(
  4284. Err(TokenError::NativeNotSupported.into()),
  4285. do_process_instruction(
  4286. mint_to(
  4287. &program_id,
  4288. &spl_token::native_mint::id(),
  4289. &account_key,
  4290. &owner_key,
  4291. &[],
  4292. 42
  4293. )
  4294. .unwrap(),
  4295. vec![&mut mint_account, &mut account_account, &mut owner_account],
  4296. )
  4297. );
  4298. // burn unsupported
  4299. let bogus_mint_key = Pubkey::new_unique();
  4300. let mut bogus_mint_account =
  4301. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4302. do_process_instruction(
  4303. initialize_mint(&program_id, &bogus_mint_key, &owner_key, None, 2).unwrap(),
  4304. vec![&mut bogus_mint_account, &mut rent_sysvar],
  4305. )
  4306. .unwrap();
  4307. assert_eq!(
  4308. Err(TokenError::NativeNotSupported.into()),
  4309. do_process_instruction(
  4310. burn(
  4311. &program_id,
  4312. &account_key,
  4313. &bogus_mint_key,
  4314. &owner_key,
  4315. &[],
  4316. 42
  4317. )
  4318. .unwrap(),
  4319. vec![
  4320. &mut account_account,
  4321. &mut bogus_mint_account,
  4322. &mut owner_account
  4323. ],
  4324. )
  4325. );
  4326. // ensure can't transfer below rent-exempt reserve
  4327. assert_eq!(
  4328. Err(TokenError::InsufficientFunds.into()),
  4329. do_process_instruction(
  4330. transfer(
  4331. &program_id,
  4332. &account_key,
  4333. &account2_key,
  4334. &owner_key,
  4335. &[],
  4336. 50,
  4337. )
  4338. .unwrap(),
  4339. vec![
  4340. &mut account_account,
  4341. &mut account2_account,
  4342. &mut owner_account,
  4343. ],
  4344. )
  4345. );
  4346. // transfer between native accounts
  4347. do_process_instruction(
  4348. transfer(
  4349. &program_id,
  4350. &account_key,
  4351. &account2_key,
  4352. &owner_key,
  4353. &[],
  4354. 40,
  4355. )
  4356. .unwrap(),
  4357. vec![
  4358. &mut account_account,
  4359. &mut account2_account,
  4360. &mut owner_account,
  4361. ],
  4362. )
  4363. .unwrap();
  4364. assert_eq!(account_account.lamports, account_minimum_balance());
  4365. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4366. assert!(account.is_native());
  4367. assert_eq!(account.amount, 0);
  4368. assert_eq!(account2_account.lamports, account_minimum_balance() + 40);
  4369. let account = Account::unpack_unchecked(&account2_account.data).unwrap();
  4370. assert!(account.is_native());
  4371. assert_eq!(account.amount, 40);
  4372. // set close authority
  4373. do_process_instruction(
  4374. set_authority(
  4375. &program_id,
  4376. &account_key,
  4377. Some(&owner3_key),
  4378. AuthorityType::CloseAccount,
  4379. &owner_key,
  4380. &[],
  4381. )
  4382. .unwrap(),
  4383. vec![&mut account_account, &mut owner_account],
  4384. )
  4385. .unwrap();
  4386. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4387. assert_eq!(account.close_authority, COption::Some(owner3_key));
  4388. // set new account owner
  4389. do_process_instruction(
  4390. set_authority(
  4391. &program_id,
  4392. &account_key,
  4393. Some(&owner2_key),
  4394. AuthorityType::AccountOwner,
  4395. &owner_key,
  4396. &[],
  4397. )
  4398. .unwrap(),
  4399. vec![&mut account_account, &mut owner_account],
  4400. )
  4401. .unwrap();
  4402. // close authority cleared
  4403. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4404. assert_eq!(account.close_authority, COption::None);
  4405. // close native account
  4406. do_process_instruction(
  4407. close_account(&program_id, &account_key, &account3_key, &owner2_key, &[]).unwrap(),
  4408. vec![
  4409. &mut account_account,
  4410. &mut account3_account,
  4411. &mut owner2_account,
  4412. ],
  4413. )
  4414. .unwrap();
  4415. assert_eq!(account_account.lamports, 0);
  4416. assert_eq!(account3_account.lamports, 2 * account_minimum_balance());
  4417. assert!(account_account.data.is_empty());
  4418. }
  4419. #[test]
  4420. fn test_overflow() {
  4421. let program_id = spl_token::id();
  4422. let account_key = Pubkey::new_unique();
  4423. let mut account_account = SolanaAccount::new(
  4424. account_minimum_balance(),
  4425. Account::get_packed_len(),
  4426. &program_id,
  4427. );
  4428. let account2_key = Pubkey::new_unique();
  4429. let mut account2_account = SolanaAccount::new(
  4430. account_minimum_balance(),
  4431. Account::get_packed_len(),
  4432. &program_id,
  4433. );
  4434. let owner_key = Pubkey::new_unique();
  4435. let mut owner_account = SolanaAccount::default();
  4436. let owner2_key = Pubkey::new_unique();
  4437. let mut owner2_account = SolanaAccount::default();
  4438. let mint_owner_key = Pubkey::new_unique();
  4439. let mut mint_owner_account = SolanaAccount::default();
  4440. let mint_key = Pubkey::new_unique();
  4441. let mut mint_account =
  4442. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4443. let mut rent_sysvar = rent_sysvar();
  4444. // create new mint with owner
  4445. do_process_instruction(
  4446. initialize_mint(&program_id, &mint_key, &mint_owner_key, None, 2).unwrap(),
  4447. vec![&mut mint_account, &mut rent_sysvar],
  4448. )
  4449. .unwrap();
  4450. // create an account
  4451. do_process_instruction(
  4452. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4453. vec![
  4454. &mut account_account,
  4455. &mut mint_account,
  4456. &mut owner_account,
  4457. &mut rent_sysvar,
  4458. ],
  4459. )
  4460. .unwrap();
  4461. // create another account
  4462. do_process_instruction(
  4463. initialize_account(&program_id, &account2_key, &mint_key, &owner2_key).unwrap(),
  4464. vec![
  4465. &mut account2_account,
  4466. &mut mint_account,
  4467. &mut owner2_account,
  4468. &mut rent_sysvar,
  4469. ],
  4470. )
  4471. .unwrap();
  4472. // mint the max to an account
  4473. do_process_instruction(
  4474. mint_to(
  4475. &program_id,
  4476. &mint_key,
  4477. &account_key,
  4478. &mint_owner_key,
  4479. &[],
  4480. u64::MAX,
  4481. )
  4482. .unwrap(),
  4483. vec![
  4484. &mut mint_account,
  4485. &mut account_account,
  4486. &mut mint_owner_account,
  4487. ],
  4488. )
  4489. .unwrap();
  4490. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4491. assert_eq!(account.amount, u64::MAX);
  4492. // attempt to mint one more to account
  4493. assert_eq!(
  4494. Err(TokenError::Overflow.into()),
  4495. do_process_instruction(
  4496. mint_to(
  4497. &program_id,
  4498. &mint_key,
  4499. &account_key,
  4500. &mint_owner_key,
  4501. &[],
  4502. 1,
  4503. )
  4504. .unwrap(),
  4505. vec![
  4506. &mut mint_account,
  4507. &mut account_account,
  4508. &mut mint_owner_account,
  4509. ],
  4510. )
  4511. );
  4512. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4513. assert_eq!(account.amount, u64::MAX);
  4514. // attempt to mint one more to the other account
  4515. assert_eq!(
  4516. Err(TokenError::Overflow.into()),
  4517. do_process_instruction(
  4518. mint_to(
  4519. &program_id,
  4520. &mint_key,
  4521. &account2_key,
  4522. &mint_owner_key,
  4523. &[],
  4524. 1,
  4525. )
  4526. .unwrap(),
  4527. vec![
  4528. &mut mint_account,
  4529. &mut account2_account,
  4530. &mut mint_owner_account,
  4531. ],
  4532. )
  4533. );
  4534. // burn some of the supply
  4535. do_process_instruction(
  4536. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 100).unwrap(),
  4537. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4538. )
  4539. .unwrap();
  4540. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4541. assert_eq!(account.amount, u64::MAX - 100);
  4542. do_process_instruction(
  4543. mint_to(
  4544. &program_id,
  4545. &mint_key,
  4546. &account_key,
  4547. &mint_owner_key,
  4548. &[],
  4549. 100,
  4550. )
  4551. .unwrap(),
  4552. vec![
  4553. &mut mint_account,
  4554. &mut account_account,
  4555. &mut mint_owner_account,
  4556. ],
  4557. )
  4558. .unwrap();
  4559. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4560. assert_eq!(account.amount, u64::MAX);
  4561. // manipulate account balance to attempt overflow transfer
  4562. let mut account = Account::unpack_unchecked(&account2_account.data).unwrap();
  4563. account.amount = 1;
  4564. Account::pack(account, &mut account2_account.data).unwrap();
  4565. assert_eq!(
  4566. Err(TokenError::Overflow.into()),
  4567. do_process_instruction(
  4568. transfer(
  4569. &program_id,
  4570. &account2_key,
  4571. &account_key,
  4572. &owner2_key,
  4573. &[],
  4574. 1,
  4575. )
  4576. .unwrap(),
  4577. vec![
  4578. &mut account2_account,
  4579. &mut account_account,
  4580. &mut owner2_account,
  4581. ],
  4582. )
  4583. );
  4584. }
  4585. #[test]
  4586. fn test_frozen() {
  4587. let program_id = spl_token::id();
  4588. let account_key = Pubkey::new_unique();
  4589. let mut account_account = SolanaAccount::new(
  4590. account_minimum_balance(),
  4591. Account::get_packed_len(),
  4592. &program_id,
  4593. );
  4594. let account2_key = Pubkey::new_unique();
  4595. let mut account2_account = SolanaAccount::new(
  4596. account_minimum_balance(),
  4597. Account::get_packed_len(),
  4598. &program_id,
  4599. );
  4600. let owner_key = Pubkey::new_unique();
  4601. let mut owner_account = SolanaAccount::default();
  4602. let mint_key = Pubkey::new_unique();
  4603. let mut mint_account =
  4604. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4605. let mut rent_sysvar = rent_sysvar();
  4606. // create new mint and fund first account
  4607. do_process_instruction(
  4608. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  4609. vec![&mut mint_account, &mut rent_sysvar],
  4610. )
  4611. .unwrap();
  4612. // create account
  4613. do_process_instruction(
  4614. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4615. vec![
  4616. &mut account_account,
  4617. &mut mint_account,
  4618. &mut owner_account,
  4619. &mut rent_sysvar,
  4620. ],
  4621. )
  4622. .unwrap();
  4623. // create another account
  4624. do_process_instruction(
  4625. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  4626. vec![
  4627. &mut account2_account,
  4628. &mut mint_account,
  4629. &mut owner_account,
  4630. &mut rent_sysvar,
  4631. ],
  4632. )
  4633. .unwrap();
  4634. // fund first account
  4635. do_process_instruction(
  4636. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  4637. vec![&mut mint_account, &mut account_account, &mut owner_account],
  4638. )
  4639. .unwrap();
  4640. // no transfer if either account is frozen
  4641. let mut account = Account::unpack_unchecked(&account2_account.data).unwrap();
  4642. account.state = AccountState::Frozen;
  4643. Account::pack(account, &mut account2_account.data).unwrap();
  4644. assert_eq!(
  4645. Err(TokenError::AccountFrozen.into()),
  4646. do_process_instruction(
  4647. transfer(
  4648. &program_id,
  4649. &account_key,
  4650. &account2_key,
  4651. &owner_key,
  4652. &[],
  4653. 500,
  4654. )
  4655. .unwrap(),
  4656. vec![
  4657. &mut account_account,
  4658. &mut account2_account,
  4659. &mut owner_account,
  4660. ],
  4661. )
  4662. );
  4663. let mut account = Account::unpack_unchecked(&account_account.data).unwrap();
  4664. account.state = AccountState::Initialized;
  4665. Account::pack(account, &mut account_account.data).unwrap();
  4666. let mut account = Account::unpack_unchecked(&account2_account.data).unwrap();
  4667. account.state = AccountState::Frozen;
  4668. Account::pack(account, &mut account2_account.data).unwrap();
  4669. assert_eq!(
  4670. Err(TokenError::AccountFrozen.into()),
  4671. do_process_instruction(
  4672. transfer(
  4673. &program_id,
  4674. &account_key,
  4675. &account2_key,
  4676. &owner_key,
  4677. &[],
  4678. 500,
  4679. )
  4680. .unwrap(),
  4681. vec![
  4682. &mut account_account,
  4683. &mut account2_account,
  4684. &mut owner_account,
  4685. ],
  4686. )
  4687. );
  4688. // no approve if account is frozen
  4689. let mut account = Account::unpack_unchecked(&account_account.data).unwrap();
  4690. account.state = AccountState::Frozen;
  4691. Account::pack(account, &mut account_account.data).unwrap();
  4692. let delegate_key = Pubkey::new_unique();
  4693. let mut delegate_account = SolanaAccount::default();
  4694. assert_eq!(
  4695. Err(TokenError::AccountFrozen.into()),
  4696. do_process_instruction(
  4697. approve(
  4698. &program_id,
  4699. &account_key,
  4700. &delegate_key,
  4701. &owner_key,
  4702. &[],
  4703. 100
  4704. )
  4705. .unwrap(),
  4706. vec![
  4707. &mut account_account,
  4708. &mut delegate_account,
  4709. &mut owner_account,
  4710. ],
  4711. )
  4712. );
  4713. // no revoke if account is frozen
  4714. let mut account = Account::unpack_unchecked(&account_account.data).unwrap();
  4715. account.delegate = COption::Some(delegate_key);
  4716. account.delegated_amount = 100;
  4717. Account::pack(account, &mut account_account.data).unwrap();
  4718. assert_eq!(
  4719. Err(TokenError::AccountFrozen.into()),
  4720. do_process_instruction(
  4721. revoke(&program_id, &account_key, &owner_key, &[]).unwrap(),
  4722. vec![&mut account_account, &mut owner_account],
  4723. )
  4724. );
  4725. // no set authority if account is frozen
  4726. let new_owner_key = Pubkey::new_unique();
  4727. assert_eq!(
  4728. Err(TokenError::AccountFrozen.into()),
  4729. do_process_instruction(
  4730. set_authority(
  4731. &program_id,
  4732. &account_key,
  4733. Some(&new_owner_key),
  4734. AuthorityType::AccountOwner,
  4735. &owner_key,
  4736. &[]
  4737. )
  4738. .unwrap(),
  4739. vec![&mut account_account, &mut owner_account,],
  4740. )
  4741. );
  4742. // no mint_to if destination account is frozen
  4743. assert_eq!(
  4744. Err(TokenError::AccountFrozen.into()),
  4745. do_process_instruction(
  4746. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 100).unwrap(),
  4747. vec![&mut mint_account, &mut account_account, &mut owner_account,],
  4748. )
  4749. );
  4750. // no burn if account is frozen
  4751. assert_eq!(
  4752. Err(TokenError::AccountFrozen.into()),
  4753. do_process_instruction(
  4754. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 100).unwrap(),
  4755. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4756. )
  4757. );
  4758. }
  4759. #[test]
  4760. fn test_freeze_thaw_dups() {
  4761. let program_id = spl_token::id();
  4762. let account1_key = Pubkey::new_unique();
  4763. let mut account1_account = SolanaAccount::new(
  4764. account_minimum_balance(),
  4765. Account::get_packed_len(),
  4766. &program_id,
  4767. );
  4768. let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  4769. let owner_key = Pubkey::new_unique();
  4770. let mint_key = Pubkey::new_unique();
  4771. let mut mint_account =
  4772. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4773. let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
  4774. let rent_key = rent::id();
  4775. let mut rent_sysvar = rent_sysvar();
  4776. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  4777. // create mint
  4778. do_process_instruction_dups(
  4779. initialize_mint(&program_id, &mint_key, &owner_key, Some(&account1_key), 2).unwrap(),
  4780. vec![mint_info.clone(), rent_info.clone()],
  4781. )
  4782. .unwrap();
  4783. // create account
  4784. do_process_instruction_dups(
  4785. initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
  4786. vec![
  4787. account1_info.clone(),
  4788. mint_info.clone(),
  4789. account1_info.clone(),
  4790. rent_info.clone(),
  4791. ],
  4792. )
  4793. .unwrap();
  4794. // freeze where mint freeze_authority is account
  4795. do_process_instruction_dups(
  4796. freeze_account(&program_id, &account1_key, &mint_key, &account1_key, &[]).unwrap(),
  4797. vec![
  4798. account1_info.clone(),
  4799. mint_info.clone(),
  4800. account1_info.clone(),
  4801. ],
  4802. )
  4803. .unwrap();
  4804. // thaw where mint freeze_authority is account
  4805. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  4806. account.state = AccountState::Frozen;
  4807. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  4808. do_process_instruction_dups(
  4809. thaw_account(&program_id, &account1_key, &mint_key, &account1_key, &[]).unwrap(),
  4810. vec![
  4811. account1_info.clone(),
  4812. mint_info.clone(),
  4813. account1_info.clone(),
  4814. ],
  4815. )
  4816. .unwrap();
  4817. }
  4818. #[test]
  4819. fn test_freeze_account() {
  4820. let program_id = spl_token::id();
  4821. let account_key = Pubkey::new_unique();
  4822. let mut account_account = SolanaAccount::new(
  4823. account_minimum_balance(),
  4824. Account::get_packed_len(),
  4825. &program_id,
  4826. );
  4827. let account_owner_key = Pubkey::new_unique();
  4828. let mut account_owner_account = SolanaAccount::default();
  4829. let owner_key = Pubkey::new_unique();
  4830. let mut owner_account = SolanaAccount::default();
  4831. let owner2_key = Pubkey::new_unique();
  4832. let mut owner2_account = SolanaAccount::default();
  4833. let mint_key = Pubkey::new_unique();
  4834. let mut mint_account =
  4835. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4836. let mut rent_sysvar = rent_sysvar();
  4837. // create new mint with owner different from account owner
  4838. do_process_instruction(
  4839. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  4840. vec![&mut mint_account, &mut rent_sysvar],
  4841. )
  4842. .unwrap();
  4843. // create account
  4844. do_process_instruction(
  4845. initialize_account(&program_id, &account_key, &mint_key, &account_owner_key).unwrap(),
  4846. vec![
  4847. &mut account_account,
  4848. &mut mint_account,
  4849. &mut account_owner_account,
  4850. &mut rent_sysvar,
  4851. ],
  4852. )
  4853. .unwrap();
  4854. // mint to account
  4855. do_process_instruction(
  4856. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  4857. vec![&mut mint_account, &mut account_account, &mut owner_account],
  4858. )
  4859. .unwrap();
  4860. // mint cannot freeze
  4861. assert_eq!(
  4862. Err(TokenError::MintCannotFreeze.into()),
  4863. do_process_instruction(
  4864. freeze_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  4865. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4866. )
  4867. );
  4868. // missing freeze_authority
  4869. let mut mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  4870. mint.freeze_authority = COption::Some(owner_key);
  4871. Mint::pack(mint, &mut mint_account.data).unwrap();
  4872. assert_eq!(
  4873. Err(TokenError::OwnerMismatch.into()),
  4874. do_process_instruction(
  4875. freeze_account(&program_id, &account_key, &mint_key, &owner2_key, &[]).unwrap(),
  4876. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  4877. )
  4878. );
  4879. // check explicit thaw
  4880. assert_eq!(
  4881. Err(TokenError::InvalidState.into()),
  4882. do_process_instruction(
  4883. thaw_account(&program_id, &account_key, &mint_key, &owner2_key, &[]).unwrap(),
  4884. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  4885. )
  4886. );
  4887. // freeze
  4888. do_process_instruction(
  4889. freeze_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  4890. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4891. )
  4892. .unwrap();
  4893. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4894. assert_eq!(account.state, AccountState::Frozen);
  4895. // check explicit freeze
  4896. assert_eq!(
  4897. Err(TokenError::InvalidState.into()),
  4898. do_process_instruction(
  4899. freeze_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  4900. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4901. )
  4902. );
  4903. // check thaw authority
  4904. assert_eq!(
  4905. Err(TokenError::OwnerMismatch.into()),
  4906. do_process_instruction(
  4907. thaw_account(&program_id, &account_key, &mint_key, &owner2_key, &[]).unwrap(),
  4908. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  4909. )
  4910. );
  4911. // thaw
  4912. do_process_instruction(
  4913. thaw_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  4914. vec![&mut account_account, &mut mint_account, &mut owner_account],
  4915. )
  4916. .unwrap();
  4917. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  4918. assert_eq!(account.state, AccountState::Initialized);
  4919. }
  4920. #[test]
  4921. fn test_initialize_account2_and_3() {
  4922. let program_id = spl_token::id();
  4923. let account_key = Pubkey::new_unique();
  4924. let mut account_account = SolanaAccount::new(
  4925. account_minimum_balance(),
  4926. Account::get_packed_len(),
  4927. &program_id,
  4928. );
  4929. let mut account2_account = SolanaAccount::new(
  4930. account_minimum_balance(),
  4931. Account::get_packed_len(),
  4932. &program_id,
  4933. );
  4934. let mut account3_account = SolanaAccount::new(
  4935. account_minimum_balance(),
  4936. Account::get_packed_len(),
  4937. &program_id,
  4938. );
  4939. let owner_key = Pubkey::new_unique();
  4940. let mut owner_account = SolanaAccount::default();
  4941. let mint_key = Pubkey::new_unique();
  4942. let mut mint_account =
  4943. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4944. let mut rent_sysvar = rent_sysvar();
  4945. // create mint
  4946. do_process_instruction(
  4947. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  4948. vec![&mut mint_account, &mut rent_sysvar],
  4949. )
  4950. .unwrap();
  4951. do_process_instruction(
  4952. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4953. vec![
  4954. &mut account_account,
  4955. &mut mint_account,
  4956. &mut owner_account,
  4957. &mut rent_sysvar,
  4958. ],
  4959. )
  4960. .unwrap();
  4961. do_process_instruction(
  4962. initialize_account2(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4963. vec![&mut account2_account, &mut mint_account, &mut rent_sysvar],
  4964. )
  4965. .unwrap();
  4966. assert_eq!(account_account, account2_account);
  4967. do_process_instruction(
  4968. initialize_account3(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  4969. vec![&mut account3_account, &mut mint_account],
  4970. )
  4971. .unwrap();
  4972. assert_eq!(account_account, account3_account);
  4973. }
  4974. #[test]
  4975. fn test_sync_native() {
  4976. let program_id = spl_token::id();
  4977. let mint_key = Pubkey::new_unique();
  4978. let mut mint_account =
  4979. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  4980. let native_account_key = Pubkey::new_unique();
  4981. let lamports = 40;
  4982. let mut native_account = SolanaAccount::new(
  4983. account_minimum_balance() + lamports,
  4984. Account::get_packed_len(),
  4985. &program_id,
  4986. );
  4987. let non_native_account_key = Pubkey::new_unique();
  4988. let mut non_native_account = SolanaAccount::new(
  4989. account_minimum_balance() + 50,
  4990. Account::get_packed_len(),
  4991. &program_id,
  4992. );
  4993. let owner_key = Pubkey::new_unique();
  4994. let mut owner_account = SolanaAccount::default();
  4995. let mut rent_sysvar = rent_sysvar();
  4996. // initialize non-native mint
  4997. do_process_instruction(
  4998. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  4999. vec![&mut mint_account, &mut rent_sysvar],
  5000. )
  5001. .unwrap();
  5002. // initialize non-native account
  5003. do_process_instruction(
  5004. initialize_account(&program_id, &non_native_account_key, &mint_key, &owner_key).unwrap(),
  5005. vec![
  5006. &mut non_native_account,
  5007. &mut mint_account,
  5008. &mut owner_account,
  5009. &mut rent_sysvar,
  5010. ],
  5011. )
  5012. .unwrap();
  5013. let account = Account::unpack_unchecked(&non_native_account.data).unwrap();
  5014. assert!(!account.is_native());
  5015. assert_eq!(account.amount, 0);
  5016. // fail sync non-native
  5017. assert_eq!(
  5018. Err(TokenError::NonNativeNotSupported.into()),
  5019. do_process_instruction(
  5020. sync_native(&program_id, &non_native_account_key,).unwrap(),
  5021. vec![&mut non_native_account],
  5022. )
  5023. );
  5024. // fail sync uninitialized
  5025. assert_eq!(
  5026. Err(ProgramError::UninitializedAccount),
  5027. do_process_instruction(
  5028. sync_native(&program_id, &native_account_key,).unwrap(),
  5029. vec![&mut native_account],
  5030. )
  5031. );
  5032. // wrap native account
  5033. do_process_instruction(
  5034. initialize_account(
  5035. &program_id,
  5036. &native_account_key,
  5037. &spl_token::native_mint::id(),
  5038. &owner_key,
  5039. )
  5040. .unwrap(),
  5041. vec![
  5042. &mut native_account,
  5043. &mut mint_account,
  5044. &mut owner_account,
  5045. &mut rent_sysvar,
  5046. ],
  5047. )
  5048. .unwrap();
  5049. // fail sync, not owned by program
  5050. let not_program_id = Pubkey::new_unique();
  5051. native_account.owner = not_program_id;
  5052. assert_eq!(
  5053. Err(ProgramError::IncorrectProgramId),
  5054. do_process_instruction(
  5055. sync_native(&program_id, &native_account_key,).unwrap(),
  5056. vec![&mut native_account],
  5057. )
  5058. );
  5059. native_account.owner = program_id;
  5060. let account = Account::unpack_unchecked(&native_account.data).unwrap();
  5061. assert!(account.is_native());
  5062. assert_eq!(account.amount, lamports);
  5063. // sync, no change
  5064. do_process_instruction(
  5065. sync_native(&program_id, &native_account_key).unwrap(),
  5066. vec![&mut native_account],
  5067. )
  5068. .unwrap();
  5069. let account = Account::unpack_unchecked(&native_account.data).unwrap();
  5070. assert_eq!(account.amount, lamports);
  5071. // transfer sol
  5072. let new_lamports = lamports + 50;
  5073. native_account.lamports = account_minimum_balance() + new_lamports;
  5074. // success sync
  5075. do_process_instruction(
  5076. sync_native(&program_id, &native_account_key).unwrap(),
  5077. vec![&mut native_account],
  5078. )
  5079. .unwrap();
  5080. let account = Account::unpack_unchecked(&native_account.data).unwrap();
  5081. assert_eq!(account.amount, new_lamports);
  5082. // reduce sol
  5083. native_account.lamports -= 1;
  5084. // fail sync
  5085. assert_eq!(
  5086. Err(TokenError::InvalidState.into()),
  5087. do_process_instruction(
  5088. sync_native(&program_id, &native_account_key,).unwrap(),
  5089. vec![&mut native_account],
  5090. )
  5091. );
  5092. }
  5093. #[test]
  5094. #[serial]
  5095. fn test_get_account_data_size() {
  5096. // see integration tests for return-data validity
  5097. let program_id = spl_token::id();
  5098. let owner_key = Pubkey::new_unique();
  5099. let mut rent_sysvar = rent_sysvar();
  5100. let mut mint_account =
  5101. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5102. let mint_key = Pubkey::new_unique();
  5103. // fail if an invalid mint is passed in
  5104. assert_eq!(
  5105. Err(TokenError::InvalidMint.into()),
  5106. do_process_instruction(
  5107. get_account_data_size(&program_id, &mint_key).unwrap(),
  5108. vec![&mut mint_account],
  5109. )
  5110. );
  5111. do_process_instruction(
  5112. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5113. vec![&mut mint_account, &mut rent_sysvar],
  5114. )
  5115. .unwrap();
  5116. set_expected_data(Account::LEN.to_le_bytes().to_vec());
  5117. do_process_instruction(
  5118. get_account_data_size(&program_id, &mint_key).unwrap(),
  5119. vec![&mut mint_account],
  5120. )
  5121. .unwrap();
  5122. }
  5123. #[test]
  5124. fn test_initialize_immutable_owner() {
  5125. let program_id = spl_token::id();
  5126. let account_key = Pubkey::new_unique();
  5127. let mut account_account = SolanaAccount::new(
  5128. account_minimum_balance(),
  5129. Account::get_packed_len(),
  5130. &program_id,
  5131. );
  5132. let owner_key = Pubkey::new_unique();
  5133. let mut owner_account = SolanaAccount::default();
  5134. let mint_key = Pubkey::new_unique();
  5135. let mut mint_account =
  5136. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5137. let mut rent_sysvar = rent_sysvar();
  5138. // create mint
  5139. do_process_instruction(
  5140. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5141. vec![&mut mint_account, &mut rent_sysvar],
  5142. )
  5143. .unwrap();
  5144. // success initialize immutable
  5145. do_process_instruction(
  5146. initialize_immutable_owner(&program_id, &account_key).unwrap(),
  5147. vec![&mut account_account],
  5148. )
  5149. .unwrap();
  5150. // create account
  5151. do_process_instruction(
  5152. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  5153. vec![
  5154. &mut account_account,
  5155. &mut mint_account,
  5156. &mut owner_account,
  5157. &mut rent_sysvar,
  5158. ],
  5159. )
  5160. .unwrap();
  5161. // fail post-init
  5162. assert_eq!(
  5163. Err(TokenError::AlreadyInUse.into()),
  5164. do_process_instruction(
  5165. initialize_immutable_owner(&program_id, &account_key).unwrap(),
  5166. vec![&mut account_account],
  5167. )
  5168. );
  5169. }
  5170. #[test]
  5171. #[serial]
  5172. fn test_amount_to_ui_amount() {
  5173. let program_id = spl_token::id();
  5174. let owner_key = Pubkey::new_unique();
  5175. let mint_key = Pubkey::new_unique();
  5176. let mut mint_account =
  5177. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5178. let mut rent_sysvar = rent_sysvar();
  5179. // fail if an invalid mint is passed in
  5180. assert_eq!(
  5181. Err(TokenError::InvalidMint.into()),
  5182. do_process_instruction(
  5183. amount_to_ui_amount(&program_id, &mint_key, 110).unwrap(),
  5184. vec![&mut mint_account],
  5185. )
  5186. );
  5187. // create mint
  5188. do_process_instruction(
  5189. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5190. vec![&mut mint_account, &mut rent_sysvar],
  5191. )
  5192. .unwrap();
  5193. set_expected_data("0.23".as_bytes().to_vec());
  5194. do_process_instruction(
  5195. amount_to_ui_amount(&program_id, &mint_key, 23).unwrap(),
  5196. vec![&mut mint_account],
  5197. )
  5198. .unwrap();
  5199. set_expected_data("1.1".as_bytes().to_vec());
  5200. do_process_instruction(
  5201. amount_to_ui_amount(&program_id, &mint_key, 110).unwrap(),
  5202. vec![&mut mint_account],
  5203. )
  5204. .unwrap();
  5205. set_expected_data("42".as_bytes().to_vec());
  5206. do_process_instruction(
  5207. amount_to_ui_amount(&program_id, &mint_key, 4200).unwrap(),
  5208. vec![&mut mint_account],
  5209. )
  5210. .unwrap();
  5211. set_expected_data("0".as_bytes().to_vec());
  5212. do_process_instruction(
  5213. amount_to_ui_amount(&program_id, &mint_key, 0).unwrap(),
  5214. vec![&mut mint_account],
  5215. )
  5216. .unwrap();
  5217. }
  5218. #[test]
  5219. #[serial]
  5220. fn test_ui_amount_to_amount() {
  5221. let program_id = spl_token::id();
  5222. let owner_key = Pubkey::new_unique();
  5223. let mint_key = Pubkey::new_unique();
  5224. let mut mint_account =
  5225. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5226. let mut rent_sysvar = rent_sysvar();
  5227. // fail if an invalid mint is passed in
  5228. assert_eq!(
  5229. Err(TokenError::InvalidMint.into()),
  5230. do_process_instruction(
  5231. ui_amount_to_amount(&program_id, &mint_key, "1.1").unwrap(),
  5232. vec![&mut mint_account],
  5233. )
  5234. );
  5235. // create mint
  5236. do_process_instruction(
  5237. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5238. vec![&mut mint_account, &mut rent_sysvar],
  5239. )
  5240. .unwrap();
  5241. set_expected_data(23u64.to_le_bytes().to_vec());
  5242. do_process_instruction(
  5243. ui_amount_to_amount(&program_id, &mint_key, "0.23").unwrap(),
  5244. vec![&mut mint_account],
  5245. )
  5246. .unwrap();
  5247. set_expected_data(20u64.to_le_bytes().to_vec());
  5248. do_process_instruction(
  5249. ui_amount_to_amount(&program_id, &mint_key, "0.20").unwrap(),
  5250. vec![&mut mint_account],
  5251. )
  5252. .unwrap();
  5253. set_expected_data(20u64.to_le_bytes().to_vec());
  5254. do_process_instruction(
  5255. ui_amount_to_amount(&program_id, &mint_key, "0.2000").unwrap(),
  5256. vec![&mut mint_account],
  5257. )
  5258. .unwrap();
  5259. set_expected_data(20u64.to_le_bytes().to_vec());
  5260. do_process_instruction(
  5261. ui_amount_to_amount(&program_id, &mint_key, ".20").unwrap(),
  5262. vec![&mut mint_account],
  5263. )
  5264. .unwrap();
  5265. set_expected_data(110u64.to_le_bytes().to_vec());
  5266. do_process_instruction(
  5267. ui_amount_to_amount(&program_id, &mint_key, "1.1").unwrap(),
  5268. vec![&mut mint_account],
  5269. )
  5270. .unwrap();
  5271. set_expected_data(110u64.to_le_bytes().to_vec());
  5272. do_process_instruction(
  5273. ui_amount_to_amount(&program_id, &mint_key, "1.10").unwrap(),
  5274. vec![&mut mint_account],
  5275. )
  5276. .unwrap();
  5277. set_expected_data(4200u64.to_le_bytes().to_vec());
  5278. do_process_instruction(
  5279. ui_amount_to_amount(&program_id, &mint_key, "42").unwrap(),
  5280. vec![&mut mint_account],
  5281. )
  5282. .unwrap();
  5283. set_expected_data(4200u64.to_le_bytes().to_vec());
  5284. do_process_instruction(
  5285. ui_amount_to_amount(&program_id, &mint_key, "42.").unwrap(),
  5286. vec![&mut mint_account],
  5287. )
  5288. .unwrap();
  5289. set_expected_data(0u64.to_le_bytes().to_vec());
  5290. do_process_instruction(
  5291. ui_amount_to_amount(&program_id, &mint_key, "0").unwrap(),
  5292. vec![&mut mint_account],
  5293. )
  5294. .unwrap();
  5295. // fail if invalid ui_amount passed in
  5296. assert_eq!(
  5297. Err(ProgramError::InvalidArgument),
  5298. do_process_instruction(
  5299. ui_amount_to_amount(&program_id, &mint_key, "").unwrap(),
  5300. vec![&mut mint_account],
  5301. )
  5302. );
  5303. assert_eq!(
  5304. Err(ProgramError::InvalidArgument),
  5305. do_process_instruction(
  5306. ui_amount_to_amount(&program_id, &mint_key, ".").unwrap(),
  5307. vec![&mut mint_account],
  5308. )
  5309. );
  5310. assert_eq!(
  5311. Err(ProgramError::InvalidArgument),
  5312. do_process_instruction(
  5313. ui_amount_to_amount(&program_id, &mint_key, "0.111").unwrap(),
  5314. vec![&mut mint_account],
  5315. )
  5316. );
  5317. assert_eq!(
  5318. Err(ProgramError::InvalidArgument),
  5319. do_process_instruction(
  5320. ui_amount_to_amount(&program_id, &mint_key, "0.t").unwrap(),
  5321. vec![&mut mint_account],
  5322. )
  5323. );
  5324. }