processor.rs 191 KB

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