processor.rs 190 KB

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