1
0

processor.rs 183 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278
  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. }
  5071. #[test]
  5072. fn test_frozen() {
  5073. let program_id = spl_token::id();
  5074. let account_key = Pubkey::new_unique();
  5075. let mut account_account = SolanaAccount::new(
  5076. account_minimum_balance(),
  5077. Account::get_packed_len(),
  5078. &program_id,
  5079. );
  5080. let account2_key = Pubkey::new_unique();
  5081. let mut account2_account = SolanaAccount::new(
  5082. account_minimum_balance(),
  5083. Account::get_packed_len(),
  5084. &program_id,
  5085. );
  5086. let owner_key = Pubkey::new_unique();
  5087. let mut owner_account = SolanaAccount::default();
  5088. let mint_key = Pubkey::new_unique();
  5089. let mut mint_account =
  5090. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5091. let mut rent_sysvar = rent_sysvar();
  5092. // create new mint and fund first account
  5093. do_process_instruction(
  5094. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5095. vec![&mut mint_account, &mut rent_sysvar],
  5096. &[Check::success()],
  5097. )
  5098. .unwrap();
  5099. // create account
  5100. do_process_instruction(
  5101. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  5102. vec![
  5103. &mut account_account,
  5104. &mut mint_account,
  5105. &mut owner_account,
  5106. &mut rent_sysvar,
  5107. ],
  5108. &[Check::success()],
  5109. )
  5110. .unwrap();
  5111. // create another account
  5112. do_process_instruction(
  5113. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  5114. vec![
  5115. &mut account2_account,
  5116. &mut mint_account,
  5117. &mut owner_account,
  5118. &mut rent_sysvar,
  5119. ],
  5120. &[Check::success()],
  5121. )
  5122. .unwrap();
  5123. // fund first account
  5124. do_process_instruction(
  5125. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  5126. vec![&mut mint_account, &mut account_account, &mut owner_account],
  5127. &[Check::success()],
  5128. )
  5129. .unwrap();
  5130. // no transfer if either account is frozen
  5131. let mut account = Account::unpack_unchecked(&account2_account.data).unwrap();
  5132. account.state = AccountState::Frozen;
  5133. Account::pack(account, &mut account2_account.data).unwrap();
  5134. assert_eq!(
  5135. Err(TokenError::AccountFrozen.into()),
  5136. do_process_instruction(
  5137. transfer(
  5138. &program_id,
  5139. &account_key,
  5140. &account2_key,
  5141. &owner_key,
  5142. &[],
  5143. 500,
  5144. )
  5145. .unwrap(),
  5146. vec![
  5147. &mut account_account,
  5148. &mut account2_account,
  5149. &mut owner_account,
  5150. ],
  5151. &[Check::err(TokenError::AccountFrozen.into())],
  5152. )
  5153. );
  5154. let mut account = Account::unpack_unchecked(&account_account.data).unwrap();
  5155. account.state = AccountState::Initialized;
  5156. Account::pack(account, &mut account_account.data).unwrap();
  5157. let mut account = Account::unpack_unchecked(&account2_account.data).unwrap();
  5158. account.state = AccountState::Frozen;
  5159. Account::pack(account, &mut account2_account.data).unwrap();
  5160. assert_eq!(
  5161. Err(TokenError::AccountFrozen.into()),
  5162. do_process_instruction(
  5163. transfer(
  5164. &program_id,
  5165. &account_key,
  5166. &account2_key,
  5167. &owner_key,
  5168. &[],
  5169. 500,
  5170. )
  5171. .unwrap(),
  5172. vec![
  5173. &mut account_account,
  5174. &mut account2_account,
  5175. &mut owner_account,
  5176. ],
  5177. &[Check::err(TokenError::AccountFrozen.into())],
  5178. )
  5179. );
  5180. // no approve if account is frozen
  5181. let mut account = Account::unpack_unchecked(&account_account.data).unwrap();
  5182. account.state = AccountState::Frozen;
  5183. Account::pack(account, &mut account_account.data).unwrap();
  5184. let delegate_key = Pubkey::new_unique();
  5185. let mut delegate_account = SolanaAccount::default();
  5186. assert_eq!(
  5187. Err(TokenError::AccountFrozen.into()),
  5188. do_process_instruction(
  5189. approve(
  5190. &program_id,
  5191. &account_key,
  5192. &delegate_key,
  5193. &owner_key,
  5194. &[],
  5195. 100
  5196. )
  5197. .unwrap(),
  5198. vec![
  5199. &mut account_account,
  5200. &mut delegate_account,
  5201. &mut owner_account,
  5202. ],
  5203. &[Check::err(TokenError::AccountFrozen.into())],
  5204. )
  5205. );
  5206. // no revoke if account is frozen
  5207. let mut account = Account::unpack_unchecked(&account_account.data).unwrap();
  5208. account.delegate = COption::Some(delegate_key);
  5209. account.delegated_amount = 100;
  5210. Account::pack(account, &mut account_account.data).unwrap();
  5211. assert_eq!(
  5212. Err(TokenError::AccountFrozen.into()),
  5213. do_process_instruction(
  5214. revoke(&program_id, &account_key, &owner_key, &[]).unwrap(),
  5215. vec![&mut account_account, &mut owner_account],
  5216. &[Check::err(TokenError::AccountFrozen.into())],
  5217. )
  5218. );
  5219. // no set authority if account is frozen
  5220. let new_owner_key = Pubkey::new_unique();
  5221. assert_eq!(
  5222. Err(TokenError::AccountFrozen.into()),
  5223. do_process_instruction(
  5224. set_authority(
  5225. &program_id,
  5226. &account_key,
  5227. Some(&new_owner_key),
  5228. AuthorityType::AccountOwner,
  5229. &owner_key,
  5230. &[]
  5231. )
  5232. .unwrap(),
  5233. vec![&mut account_account, &mut owner_account,],
  5234. &[Check::err(TokenError::AccountFrozen.into())],
  5235. )
  5236. );
  5237. // no mint_to if destination account is frozen
  5238. assert_eq!(
  5239. Err(TokenError::AccountFrozen.into()),
  5240. do_process_instruction(
  5241. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 100).unwrap(),
  5242. vec![&mut mint_account, &mut account_account, &mut owner_account,],
  5243. &[Check::err(TokenError::AccountFrozen.into())],
  5244. )
  5245. );
  5246. // no burn if account is frozen
  5247. assert_eq!(
  5248. Err(TokenError::AccountFrozen.into()),
  5249. do_process_instruction(
  5250. burn(&program_id, &account_key, &mint_key, &owner_key, &[], 100).unwrap(),
  5251. vec![&mut account_account, &mut mint_account, &mut owner_account],
  5252. &[Check::err(TokenError::AccountFrozen.into())],
  5253. )
  5254. );
  5255. }
  5256. #[test]
  5257. fn test_freeze_thaw_dups() {
  5258. let program_id = spl_token::id();
  5259. let account1_key = Pubkey::new_unique();
  5260. let mut account1_account = SolanaAccount::new(
  5261. account_minimum_balance(),
  5262. Account::get_packed_len(),
  5263. &program_id,
  5264. );
  5265. let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
  5266. let owner_key = Pubkey::new_unique();
  5267. let mint_key = Pubkey::new_unique();
  5268. let mut mint_account =
  5269. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5270. let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
  5271. let rent_key = rent::id();
  5272. let mut rent_sysvar = rent_sysvar();
  5273. let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
  5274. // create mint
  5275. do_process_instruction_dups(
  5276. initialize_mint(&program_id, &mint_key, &owner_key, Some(&account1_key), 2).unwrap(),
  5277. vec![mint_info.clone(), rent_info.clone()],
  5278. &[Check::success()],
  5279. )
  5280. .unwrap();
  5281. // create account
  5282. do_process_instruction_dups(
  5283. initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
  5284. vec![
  5285. account1_info.clone(),
  5286. mint_info.clone(),
  5287. account1_info.clone(),
  5288. rent_info.clone(),
  5289. ],
  5290. &[Check::success()],
  5291. )
  5292. .unwrap();
  5293. // freeze where mint freeze_authority is account
  5294. do_process_instruction_dups(
  5295. freeze_account(&program_id, &account1_key, &mint_key, &account1_key, &[]).unwrap(),
  5296. vec![
  5297. account1_info.clone(),
  5298. mint_info.clone(),
  5299. account1_info.clone(),
  5300. ],
  5301. &[Check::success()],
  5302. )
  5303. .unwrap();
  5304. // thaw where mint freeze_authority is account
  5305. let mut account = Account::unpack_unchecked(&account1_info.data.borrow()).unwrap();
  5306. account.state = AccountState::Frozen;
  5307. Account::pack(account, &mut account1_info.data.borrow_mut()).unwrap();
  5308. do_process_instruction_dups(
  5309. thaw_account(&program_id, &account1_key, &mint_key, &account1_key, &[]).unwrap(),
  5310. vec![
  5311. account1_info.clone(),
  5312. mint_info.clone(),
  5313. account1_info.clone(),
  5314. ],
  5315. &[Check::success()],
  5316. )
  5317. .unwrap();
  5318. }
  5319. #[test]
  5320. fn test_freeze_account() {
  5321. let program_id = spl_token::id();
  5322. let account_key = Pubkey::new_unique();
  5323. let mut account_account = SolanaAccount::new(
  5324. account_minimum_balance(),
  5325. Account::get_packed_len(),
  5326. &program_id,
  5327. );
  5328. let account_owner_key = Pubkey::new_unique();
  5329. let mut account_owner_account = SolanaAccount::default();
  5330. let owner_key = Pubkey::new_unique();
  5331. let mut owner_account = SolanaAccount::default();
  5332. let owner2_key = Pubkey::new_unique();
  5333. let mut owner2_account = SolanaAccount::default();
  5334. let mint_key = Pubkey::new_unique();
  5335. let mut mint_account =
  5336. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5337. let mut rent_sysvar = rent_sysvar();
  5338. // create new mint with owner different from account owner
  5339. do_process_instruction(
  5340. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5341. vec![&mut mint_account, &mut rent_sysvar],
  5342. &[Check::success()],
  5343. )
  5344. .unwrap();
  5345. // create account
  5346. do_process_instruction(
  5347. initialize_account(&program_id, &account_key, &mint_key, &account_owner_key).unwrap(),
  5348. vec![
  5349. &mut account_account,
  5350. &mut mint_account,
  5351. &mut account_owner_account,
  5352. &mut rent_sysvar,
  5353. ],
  5354. &[Check::success()],
  5355. )
  5356. .unwrap();
  5357. // mint to account
  5358. do_process_instruction(
  5359. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 1000).unwrap(),
  5360. vec![&mut mint_account, &mut account_account, &mut owner_account],
  5361. &[Check::success()],
  5362. )
  5363. .unwrap();
  5364. // mint cannot freeze
  5365. assert_eq!(
  5366. Err(TokenError::MintCannotFreeze.into()),
  5367. do_process_instruction(
  5368. freeze_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  5369. vec![&mut account_account, &mut mint_account, &mut owner_account],
  5370. &[Check::err(TokenError::MintCannotFreeze.into())],
  5371. )
  5372. );
  5373. // missing freeze_authority
  5374. let mut mint = Mint::unpack_unchecked(&mint_account.data).unwrap();
  5375. mint.freeze_authority = COption::Some(owner_key);
  5376. Mint::pack(mint, &mut mint_account.data).unwrap();
  5377. assert_eq!(
  5378. Err(TokenError::OwnerMismatch.into()),
  5379. do_process_instruction(
  5380. freeze_account(&program_id, &account_key, &mint_key, &owner2_key, &[]).unwrap(),
  5381. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  5382. &[Check::err(TokenError::OwnerMismatch.into())],
  5383. )
  5384. );
  5385. // check explicit thaw
  5386. assert_eq!(
  5387. Err(TokenError::InvalidState.into()),
  5388. do_process_instruction(
  5389. thaw_account(&program_id, &account_key, &mint_key, &owner2_key, &[]).unwrap(),
  5390. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  5391. &[Check::err(TokenError::InvalidState.into())],
  5392. )
  5393. );
  5394. // freeze
  5395. do_process_instruction(
  5396. freeze_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  5397. vec![&mut account_account, &mut mint_account, &mut owner_account],
  5398. &[
  5399. Check::success(),
  5400. Check::account(&account_key)
  5401. .data_slice(108, &[AccountState::Frozen as u8])
  5402. .build(),
  5403. ],
  5404. )
  5405. .unwrap();
  5406. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  5407. assert_eq!(account.state, AccountState::Frozen);
  5408. // check explicit freeze
  5409. assert_eq!(
  5410. Err(TokenError::InvalidState.into()),
  5411. do_process_instruction(
  5412. freeze_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  5413. vec![&mut account_account, &mut mint_account, &mut owner_account],
  5414. &[Check::err(TokenError::InvalidState.into())],
  5415. )
  5416. );
  5417. // check thaw authority
  5418. assert_eq!(
  5419. Err(TokenError::OwnerMismatch.into()),
  5420. do_process_instruction(
  5421. thaw_account(&program_id, &account_key, &mint_key, &owner2_key, &[]).unwrap(),
  5422. vec![&mut account_account, &mut mint_account, &mut owner2_account],
  5423. &[Check::err(TokenError::OwnerMismatch.into())],
  5424. )
  5425. );
  5426. // thaw
  5427. do_process_instruction(
  5428. thaw_account(&program_id, &account_key, &mint_key, &owner_key, &[]).unwrap(),
  5429. vec![&mut account_account, &mut mint_account, &mut owner_account],
  5430. &[
  5431. Check::success(),
  5432. Check::account(&account_key)
  5433. .data_slice(108, &[AccountState::Initialized as u8])
  5434. .build(),
  5435. ],
  5436. )
  5437. .unwrap();
  5438. let account = Account::unpack_unchecked(&account_account.data).unwrap();
  5439. assert_eq!(account.state, AccountState::Initialized);
  5440. }
  5441. #[test]
  5442. fn test_initialize_account2_and_3() {
  5443. let program_id = spl_token::id();
  5444. let account_key = Pubkey::new_unique();
  5445. let mut account_account = SolanaAccount::new(
  5446. account_minimum_balance(),
  5447. Account::get_packed_len(),
  5448. &program_id,
  5449. );
  5450. let mut account2_account = SolanaAccount::new(
  5451. account_minimum_balance(),
  5452. Account::get_packed_len(),
  5453. &program_id,
  5454. );
  5455. let mut account3_account = SolanaAccount::new(
  5456. account_minimum_balance(),
  5457. Account::get_packed_len(),
  5458. &program_id,
  5459. );
  5460. let owner_key = Pubkey::new_unique();
  5461. let mut owner_account = SolanaAccount::default();
  5462. let mint_key = Pubkey::new_unique();
  5463. let mut mint_account =
  5464. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5465. let mut rent_sysvar = rent_sysvar();
  5466. // create mint
  5467. do_process_instruction(
  5468. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5469. vec![&mut mint_account, &mut rent_sysvar],
  5470. &[Check::success()],
  5471. )
  5472. .unwrap();
  5473. do_process_instruction(
  5474. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  5475. vec![
  5476. &mut account_account,
  5477. &mut mint_account,
  5478. &mut owner_account,
  5479. &mut rent_sysvar,
  5480. ],
  5481. &[Check::success()],
  5482. )
  5483. .unwrap();
  5484. do_process_instruction(
  5485. initialize_account2(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  5486. vec![&mut account2_account, &mut mint_account, &mut rent_sysvar],
  5487. &[Check::success()],
  5488. )
  5489. .unwrap();
  5490. assert_eq!(account_account, account2_account);
  5491. do_process_instruction(
  5492. initialize_account3(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  5493. vec![&mut account3_account, &mut mint_account],
  5494. &[Check::success()],
  5495. )
  5496. .unwrap();
  5497. assert_eq!(account_account, account3_account);
  5498. }
  5499. #[test]
  5500. fn test_sync_native() {
  5501. let program_id = spl_token::id();
  5502. let mint_key = Pubkey::new_unique();
  5503. let mut mint_account =
  5504. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5505. let native_account_key = Pubkey::new_unique();
  5506. let lamports = 40;
  5507. let mut native_account = SolanaAccount::new(
  5508. account_minimum_balance() + lamports,
  5509. Account::get_packed_len(),
  5510. &program_id,
  5511. );
  5512. let non_native_account_key = Pubkey::new_unique();
  5513. let mut non_native_account = SolanaAccount::new(
  5514. account_minimum_balance() + 50,
  5515. Account::get_packed_len(),
  5516. &program_id,
  5517. );
  5518. let owner_key = Pubkey::new_unique();
  5519. let mut owner_account = SolanaAccount::default();
  5520. let mut rent_sysvar = rent_sysvar();
  5521. // initialize non-native mint
  5522. do_process_instruction(
  5523. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5524. vec![&mut mint_account, &mut rent_sysvar],
  5525. &[Check::success()],
  5526. )
  5527. .unwrap();
  5528. // initialize non-native account
  5529. do_process_instruction(
  5530. initialize_account(&program_id, &non_native_account_key, &mint_key, &owner_key).unwrap(),
  5531. vec![
  5532. &mut non_native_account,
  5533. &mut mint_account,
  5534. &mut owner_account,
  5535. &mut rent_sysvar,
  5536. ],
  5537. &[
  5538. Check::success(),
  5539. Check::account(&non_native_account_key)
  5540. .data_slice(109, &[0, 0, 0, 0])
  5541. .build(),
  5542. Check::account(&non_native_account_key)
  5543. .data_slice(64, &0u64.to_le_bytes())
  5544. .build(),
  5545. ],
  5546. )
  5547. .unwrap();
  5548. let account = Account::unpack_unchecked(&non_native_account.data).unwrap();
  5549. assert!(!account.is_native());
  5550. assert_eq!(account.amount, 0);
  5551. // fail sync non-native
  5552. assert_eq!(
  5553. Err(TokenError::NonNativeNotSupported.into()),
  5554. do_process_instruction(
  5555. sync_native(&program_id, &non_native_account_key,).unwrap(),
  5556. vec![&mut non_native_account],
  5557. &[Check::err(TokenError::NonNativeNotSupported.into())],
  5558. )
  5559. );
  5560. // fail sync uninitialized
  5561. assert_eq!(
  5562. Err(ProgramError::UninitializedAccount),
  5563. do_process_instruction(
  5564. sync_native(&program_id, &native_account_key,).unwrap(),
  5565. vec![&mut native_account],
  5566. &[Check::err(ProgramError::UninitializedAccount)],
  5567. )
  5568. );
  5569. // wrap native account
  5570. do_process_instruction(
  5571. initialize_account(
  5572. &program_id,
  5573. &native_account_key,
  5574. &spl_token::native_mint::id(),
  5575. &owner_key,
  5576. )
  5577. .unwrap(),
  5578. vec![
  5579. &mut native_account,
  5580. &mut mint_account,
  5581. &mut owner_account,
  5582. &mut rent_sysvar,
  5583. ],
  5584. &[Check::success()],
  5585. )
  5586. .unwrap();
  5587. // fail sync, not owned by program
  5588. let not_program_id = Pubkey::new_unique();
  5589. native_account.owner = not_program_id;
  5590. assert_eq!(
  5591. Err(ProgramError::IncorrectProgramId),
  5592. do_process_instruction(
  5593. sync_native(&program_id, &native_account_key,).unwrap(),
  5594. vec![&mut native_account],
  5595. &[
  5596. Check::err(ProgramError::IncorrectProgramId),
  5597. Check::account(&native_account_key)
  5598. .data_slice(109, &[1, 0, 0, 0])
  5599. .build(),
  5600. Check::account(&native_account_key)
  5601. .data_slice(64, &lamports.to_le_bytes())
  5602. .build()
  5603. ],
  5604. )
  5605. );
  5606. native_account.owner = program_id;
  5607. let account = Account::unpack_unchecked(&native_account.data).unwrap();
  5608. assert!(account.is_native());
  5609. assert_eq!(account.amount, lamports);
  5610. // sync, no change
  5611. do_process_instruction(
  5612. sync_native(&program_id, &native_account_key).unwrap(),
  5613. vec![&mut native_account],
  5614. &[
  5615. Check::success(),
  5616. Check::account(&native_account_key)
  5617. .data_slice(64, &lamports.to_le_bytes())
  5618. .build(),
  5619. ],
  5620. )
  5621. .unwrap();
  5622. let account = Account::unpack_unchecked(&native_account.data).unwrap();
  5623. assert_eq!(account.amount, lamports);
  5624. // transfer sol
  5625. let new_lamports = lamports + 50;
  5626. native_account.lamports = account_minimum_balance() + new_lamports;
  5627. // success sync
  5628. do_process_instruction(
  5629. sync_native(&program_id, &native_account_key).unwrap(),
  5630. vec![&mut native_account],
  5631. &[
  5632. Check::success(),
  5633. Check::account(&native_account_key)
  5634. .data_slice(64, &new_lamports.to_le_bytes())
  5635. .build(),
  5636. ],
  5637. )
  5638. .unwrap();
  5639. let account = Account::unpack_unchecked(&native_account.data).unwrap();
  5640. assert_eq!(account.amount, new_lamports);
  5641. // reduce sol
  5642. native_account.lamports -= 1;
  5643. // fail sync
  5644. assert_eq!(
  5645. Err(TokenError::InvalidState.into()),
  5646. do_process_instruction(
  5647. sync_native(&program_id, &native_account_key,).unwrap(),
  5648. vec![&mut native_account],
  5649. &[Check::err(TokenError::InvalidState.into())],
  5650. )
  5651. );
  5652. }
  5653. #[test]
  5654. #[serial]
  5655. fn test_get_account_data_size() {
  5656. // see integration tests for return-data validity
  5657. let program_id = spl_token::id();
  5658. let owner_key = Pubkey::new_unique();
  5659. let mut rent_sysvar = rent_sysvar();
  5660. let mut mint_account =
  5661. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5662. let mint_key = Pubkey::new_unique();
  5663. // fail if an invalid mint is passed in
  5664. assert_eq!(
  5665. Err(TokenError::InvalidMint.into()),
  5666. do_process_instruction(
  5667. get_account_data_size(&program_id, &mint_key).unwrap(),
  5668. vec![&mut mint_account],
  5669. &[Check::err(TokenError::InvalidMint.into())],
  5670. )
  5671. );
  5672. do_process_instruction(
  5673. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5674. vec![&mut mint_account, &mut rent_sysvar],
  5675. &[Check::success()],
  5676. )
  5677. .unwrap();
  5678. do_process_instruction(
  5679. get_account_data_size(&program_id, &mint_key).unwrap(),
  5680. vec![&mut mint_account],
  5681. &[
  5682. Check::success(),
  5683. Check::return_data(&Account::LEN.to_le_bytes()),
  5684. ],
  5685. )
  5686. .unwrap();
  5687. }
  5688. #[test]
  5689. fn test_initialize_immutable_owner() {
  5690. let program_id = spl_token::id();
  5691. let account_key = Pubkey::new_unique();
  5692. let mut account_account = SolanaAccount::new(
  5693. account_minimum_balance(),
  5694. Account::get_packed_len(),
  5695. &program_id,
  5696. );
  5697. let owner_key = Pubkey::new_unique();
  5698. let mut owner_account = SolanaAccount::default();
  5699. let mint_key = Pubkey::new_unique();
  5700. let mut mint_account =
  5701. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5702. let mut rent_sysvar = rent_sysvar();
  5703. // create mint
  5704. do_process_instruction(
  5705. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5706. vec![&mut mint_account, &mut rent_sysvar],
  5707. &[Check::success()],
  5708. )
  5709. .unwrap();
  5710. // success initialize immutable
  5711. do_process_instruction(
  5712. initialize_immutable_owner(&program_id, &account_key).unwrap(),
  5713. vec![&mut account_account],
  5714. &[Check::success()],
  5715. )
  5716. .unwrap();
  5717. // create account
  5718. do_process_instruction(
  5719. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  5720. vec![
  5721. &mut account_account,
  5722. &mut mint_account,
  5723. &mut owner_account,
  5724. &mut rent_sysvar,
  5725. ],
  5726. &[Check::success()],
  5727. )
  5728. .unwrap();
  5729. // fail post-init
  5730. assert_eq!(
  5731. Err(TokenError::AlreadyInUse.into()),
  5732. do_process_instruction(
  5733. initialize_immutable_owner(&program_id, &account_key).unwrap(),
  5734. vec![&mut account_account],
  5735. &[Check::err(TokenError::AlreadyInUse.into())],
  5736. )
  5737. );
  5738. }
  5739. #[test]
  5740. #[serial]
  5741. fn test_amount_to_ui_amount() {
  5742. let program_id = spl_token::id();
  5743. let owner_key = Pubkey::new_unique();
  5744. let mint_key = Pubkey::new_unique();
  5745. let mut mint_account =
  5746. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5747. let mut rent_sysvar = rent_sysvar();
  5748. // fail if an invalid mint is passed in
  5749. assert_eq!(
  5750. Err(TokenError::InvalidMint.into()),
  5751. do_process_instruction(
  5752. amount_to_ui_amount(&program_id, &mint_key, 110).unwrap(),
  5753. vec![&mut mint_account],
  5754. &[Check::err(TokenError::InvalidMint.into())],
  5755. )
  5756. );
  5757. // create mint
  5758. do_process_instruction(
  5759. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5760. vec![&mut mint_account, &mut rent_sysvar],
  5761. &[Check::success()],
  5762. )
  5763. .unwrap();
  5764. do_process_instruction(
  5765. amount_to_ui_amount(&program_id, &mint_key, 23).unwrap(),
  5766. vec![&mut mint_account],
  5767. &[Check::success(), Check::return_data("0.23".as_bytes())],
  5768. )
  5769. .unwrap();
  5770. do_process_instruction(
  5771. amount_to_ui_amount(&program_id, &mint_key, 110).unwrap(),
  5772. vec![&mut mint_account],
  5773. &[Check::success(), Check::return_data("1.1".as_bytes())],
  5774. )
  5775. .unwrap();
  5776. do_process_instruction(
  5777. amount_to_ui_amount(&program_id, &mint_key, 4200).unwrap(),
  5778. vec![&mut mint_account],
  5779. &[Check::success(), Check::return_data("42".as_bytes())],
  5780. )
  5781. .unwrap();
  5782. do_process_instruction(
  5783. amount_to_ui_amount(&program_id, &mint_key, 0).unwrap(),
  5784. vec![&mut mint_account],
  5785. &[Check::success(), Check::return_data("0".as_bytes())],
  5786. )
  5787. .unwrap();
  5788. }
  5789. #[test]
  5790. #[serial]
  5791. fn test_ui_amount_to_amount() {
  5792. let program_id = spl_token::id();
  5793. let owner_key = Pubkey::new_unique();
  5794. let mint_key = Pubkey::new_unique();
  5795. let mut mint_account =
  5796. SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
  5797. let mut rent_sysvar = rent_sysvar();
  5798. // fail if an invalid mint is passed in
  5799. assert_eq!(
  5800. Err(TokenError::InvalidMint.into()),
  5801. do_process_instruction(
  5802. ui_amount_to_amount(&program_id, &mint_key, "1.1").unwrap(),
  5803. vec![&mut mint_account],
  5804. &[Check::err(TokenError::InvalidMint.into())],
  5805. )
  5806. );
  5807. // create mint
  5808. do_process_instruction(
  5809. initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),
  5810. vec![&mut mint_account, &mut rent_sysvar],
  5811. &[Check::success()],
  5812. )
  5813. .unwrap();
  5814. do_process_instruction(
  5815. ui_amount_to_amount(&program_id, &mint_key, "0.23").unwrap(),
  5816. vec![&mut mint_account],
  5817. &[Check::success(), Check::return_data(&23u64.to_le_bytes())],
  5818. )
  5819. .unwrap();
  5820. do_process_instruction(
  5821. ui_amount_to_amount(&program_id, &mint_key, "0.20").unwrap(),
  5822. vec![&mut mint_account],
  5823. &[Check::success(), Check::return_data(&20u64.to_le_bytes())],
  5824. )
  5825. .unwrap();
  5826. do_process_instruction(
  5827. ui_amount_to_amount(&program_id, &mint_key, "0.2000").unwrap(),
  5828. vec![&mut mint_account],
  5829. &[Check::success(), Check::return_data(&20u64.to_le_bytes())],
  5830. )
  5831. .unwrap();
  5832. do_process_instruction(
  5833. ui_amount_to_amount(&program_id, &mint_key, ".20").unwrap(),
  5834. vec![&mut mint_account],
  5835. &[Check::success(), Check::return_data(&20u64.to_le_bytes())],
  5836. )
  5837. .unwrap();
  5838. do_process_instruction(
  5839. ui_amount_to_amount(&program_id, &mint_key, "1.1").unwrap(),
  5840. vec![&mut mint_account],
  5841. &[Check::success(), Check::return_data(&110u64.to_le_bytes())],
  5842. )
  5843. .unwrap();
  5844. do_process_instruction(
  5845. ui_amount_to_amount(&program_id, &mint_key, "1.10").unwrap(),
  5846. vec![&mut mint_account],
  5847. &[Check::success(), Check::return_data(&110u64.to_le_bytes())],
  5848. )
  5849. .unwrap();
  5850. do_process_instruction(
  5851. ui_amount_to_amount(&program_id, &mint_key, "42").unwrap(),
  5852. vec![&mut mint_account],
  5853. &[Check::success(), Check::return_data(&4200u64.to_le_bytes())],
  5854. )
  5855. .unwrap();
  5856. do_process_instruction(
  5857. ui_amount_to_amount(&program_id, &mint_key, "42.").unwrap(),
  5858. vec![&mut mint_account],
  5859. &[Check::success(), Check::return_data(&4200u64.to_le_bytes())],
  5860. )
  5861. .unwrap();
  5862. do_process_instruction(
  5863. ui_amount_to_amount(&program_id, &mint_key, "0").unwrap(),
  5864. vec![&mut mint_account],
  5865. &[Check::success(), Check::return_data(&0u64.to_le_bytes())],
  5866. )
  5867. .unwrap();
  5868. // fail if invalid ui_amount passed in
  5869. assert_eq!(
  5870. Err(ProgramError::InvalidArgument),
  5871. do_process_instruction(
  5872. ui_amount_to_amount(&program_id, &mint_key, "").unwrap(),
  5873. vec![&mut mint_account],
  5874. &[Check::err(ProgramError::InvalidArgument)],
  5875. )
  5876. );
  5877. assert_eq!(
  5878. Err(ProgramError::InvalidArgument),
  5879. do_process_instruction(
  5880. ui_amount_to_amount(&program_id, &mint_key, ".").unwrap(),
  5881. vec![&mut mint_account],
  5882. &[Check::err(ProgramError::InvalidArgument)],
  5883. )
  5884. );
  5885. assert_eq!(
  5886. Err(ProgramError::InvalidArgument),
  5887. do_process_instruction(
  5888. ui_amount_to_amount(&program_id, &mint_key, "0.111").unwrap(),
  5889. vec![&mut mint_account],
  5890. &[Check::err(ProgramError::InvalidArgument)],
  5891. )
  5892. );
  5893. assert_eq!(
  5894. Err(ProgramError::InvalidArgument),
  5895. do_process_instruction(
  5896. ui_amount_to_amount(&program_id, &mint_key, "0.t").unwrap(),
  5897. vec![&mut mint_account],
  5898. &[Check::err(ProgramError::InvalidArgument)],
  5899. )
  5900. );
  5901. }