processor.rs 162 KB

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