processor.rs 184 KB

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