processor.rs 232 KB

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