processor.rs 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. //! Program state processor
  2. #![cfg(feature = "program")]
  3. use crate::{
  4. error::TokenError,
  5. instruction::{is_valid_signer_index, TokenInstruction},
  6. option::COption,
  7. state::{self, Account, Mint, Multisig},
  8. };
  9. use num_traits::FromPrimitive;
  10. use solana_sdk::{
  11. account_info::{next_account_info, AccountInfo},
  12. decode_error::DecodeError,
  13. entrypoint::ProgramResult,
  14. info,
  15. program_error::{PrintProgramError, ProgramError},
  16. pubkey::Pubkey,
  17. };
  18. use std::mem::size_of;
  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. amount: u64,
  26. decimals: u8,
  27. ) -> ProgramResult {
  28. let account_info_iter = &mut accounts.iter();
  29. let mint_info = next_account_info(account_info_iter)?;
  30. let mut mint_info_data = mint_info.data.borrow_mut();
  31. let mut mint: &mut Mint = state::unpack_unchecked(&mut mint_info_data)?;
  32. if mint.is_initialized {
  33. return Err(TokenError::AlreadyInUse.into());
  34. }
  35. let owner = if amount != 0 {
  36. let dest_account_info = next_account_info(account_info_iter)?;
  37. let mut dest_account_data = dest_account_info.data.borrow_mut();
  38. let mut dest_account: &mut Account = state::unpack(&mut dest_account_data)?;
  39. if mint_info.key != &dest_account.mint {
  40. return Err(TokenError::MintMismatch.into());
  41. }
  42. dest_account.amount = amount;
  43. if let Ok(owner_info) = next_account_info(account_info_iter) {
  44. COption::Some(*owner_info.key)
  45. } else {
  46. COption::None
  47. }
  48. } else if let Ok(owner_info) = next_account_info(account_info_iter) {
  49. COption::Some(*owner_info.key)
  50. } else {
  51. return Err(TokenError::OwnerRequiredIfNoInitialSupply.into());
  52. };
  53. mint.owner = owner;
  54. mint.decimals = decimals;
  55. mint.is_initialized = true;
  56. Ok(())
  57. }
  58. /// Processes an [InitializeAccount](enum.TokenInstruction.html) instruction.
  59. pub fn process_initialize_account(accounts: &[AccountInfo]) -> ProgramResult {
  60. let account_info_iter = &mut accounts.iter();
  61. let new_account_info = next_account_info(account_info_iter)?;
  62. let mint_info = next_account_info(account_info_iter)?;
  63. let owner_info = next_account_info(account_info_iter)?;
  64. let mut new_account_data = new_account_info.data.borrow_mut();
  65. let mut account: &mut Account = state::unpack_unchecked(&mut new_account_data)?;
  66. if account.is_initialized {
  67. return Err(TokenError::AlreadyInUse.into());
  68. }
  69. account.mint = *mint_info.key;
  70. account.owner = *owner_info.key;
  71. account.delegate = COption::None;
  72. account.delegated_amount = 0;
  73. account.is_initialized = true;
  74. if *mint_info.key == crate::native_mint::id() {
  75. account.is_native = true;
  76. account.amount = new_account_info.lamports();
  77. } else {
  78. account.is_native = false;
  79. account.amount = 0;
  80. };
  81. Ok(())
  82. }
  83. /// Processes a [InitializeMultisig](enum.TokenInstruction.html) instruction.
  84. pub fn process_initialize_multisig(accounts: &[AccountInfo], m: u8) -> ProgramResult {
  85. let account_info_iter = &mut accounts.iter();
  86. let multisig_info = next_account_info(account_info_iter)?;
  87. let mut multisig_account_data = multisig_info.data.borrow_mut();
  88. let mut multisig: &mut Multisig = state::unpack_unchecked(&mut multisig_account_data)?;
  89. if multisig.is_initialized {
  90. return Err(TokenError::AlreadyInUse.into());
  91. }
  92. let signer_infos = account_info_iter.as_slice();
  93. multisig.m = m;
  94. multisig.n = signer_infos.len() as u8;
  95. if !is_valid_signer_index(multisig.n as usize) {
  96. return Err(TokenError::InvalidNumberOfProvidedSigners.into());
  97. }
  98. if !is_valid_signer_index(multisig.m as usize) {
  99. return Err(TokenError::InvalidNumberOfRequiredSigners.into());
  100. }
  101. for (i, signer_info) in signer_infos.iter().enumerate() {
  102. multisig.signers[i] = *signer_info.key;
  103. }
  104. multisig.is_initialized = true;
  105. Ok(())
  106. }
  107. /// Processes a [Transfer](enum.TokenInstruction.html) instruction.
  108. pub fn process_transfer(
  109. program_id: &Pubkey,
  110. accounts: &[AccountInfo],
  111. amount: u64,
  112. ) -> ProgramResult {
  113. let account_info_iter = &mut accounts.iter();
  114. let source_account_info = next_account_info(account_info_iter)?;
  115. let dest_account_info = next_account_info(account_info_iter)?;
  116. let authority_info = next_account_info(account_info_iter)?;
  117. let mut source_data = source_account_info.data.borrow_mut();
  118. let mut source_account: &mut Account = state::unpack(&mut source_data)?;
  119. let mut dest_data = dest_account_info.data.borrow_mut();
  120. let mut dest_account: &mut Account = state::unpack(&mut dest_data)?;
  121. if source_account.amount < amount {
  122. return Err(TokenError::InsufficientFunds.into());
  123. }
  124. if source_account.mint != dest_account.mint {
  125. return Err(TokenError::MintMismatch.into());
  126. }
  127. match source_account.delegate {
  128. COption::Some(ref delegate) if authority_info.key == delegate => {
  129. Self::validate_owner(
  130. program_id,
  131. delegate,
  132. authority_info,
  133. account_info_iter.as_slice(),
  134. )?;
  135. if source_account.delegated_amount < amount {
  136. return Err(TokenError::InsufficientFunds.into());
  137. }
  138. source_account.delegated_amount -= amount;
  139. if source_account.delegated_amount == 0 {
  140. source_account.delegate = COption::None;
  141. }
  142. }
  143. _ => Self::validate_owner(
  144. program_id,
  145. &source_account.owner,
  146. authority_info,
  147. account_info_iter.as_slice(),
  148. )?,
  149. };
  150. source_account.amount -= amount;
  151. dest_account.amount = dest_account
  152. .amount
  153. .checked_add(amount)
  154. .ok_or(TokenError::Overflow)?;
  155. if source_account.is_native {
  156. **source_account_info.lamports.borrow_mut() -= amount;
  157. **dest_account_info.lamports.borrow_mut() += amount;
  158. }
  159. Ok(())
  160. }
  161. /// Processes an [Approve](enum.TokenInstruction.html) instruction.
  162. pub fn process_approve(
  163. program_id: &Pubkey,
  164. accounts: &[AccountInfo],
  165. amount: u64,
  166. ) -> ProgramResult {
  167. let account_info_iter = &mut accounts.iter();
  168. let source_account_info = next_account_info(account_info_iter)?;
  169. let mut source_data = source_account_info.data.borrow_mut();
  170. let mut source_account: &mut Account = state::unpack(&mut source_data)?;
  171. let delegate_info = next_account_info(account_info_iter)?;
  172. let owner_info = next_account_info(account_info_iter)?;
  173. Self::validate_owner(
  174. program_id,
  175. &source_account.owner,
  176. owner_info,
  177. account_info_iter.as_slice(),
  178. )?;
  179. source_account.delegate = COption::Some(*delegate_info.key);
  180. source_account.delegated_amount = amount;
  181. Ok(())
  182. }
  183. /// Processes an [Revoke](enum.TokenInstruction.html) instruction.
  184. pub fn process_revoke(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
  185. let account_info_iter = &mut accounts.iter();
  186. let source_account_info = next_account_info(account_info_iter)?;
  187. let mut source_data = source_account_info.data.borrow_mut();
  188. let mut source_account: &mut Account = state::unpack(&mut source_data)?;
  189. let owner_info = next_account_info(account_info_iter)?;
  190. Self::validate_owner(
  191. program_id,
  192. &source_account.owner,
  193. owner_info,
  194. account_info_iter.as_slice(),
  195. )?;
  196. source_account.delegate = COption::None;
  197. source_account.delegated_amount = 0;
  198. Ok(())
  199. }
  200. /// Processes a [SetOwner](enum.TokenInstruction.html) instruction.
  201. pub fn process_set_owner(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
  202. let account_info_iter = &mut accounts.iter();
  203. let account_info = next_account_info(account_info_iter)?;
  204. let new_owner_info = next_account_info(account_info_iter)?;
  205. let authority_info = next_account_info(account_info_iter)?;
  206. if account_info.data_len() == size_of::<Account>() {
  207. let mut account_data = account_info.data.borrow_mut();
  208. let mut account: &mut Account = state::unpack(&mut account_data)?;
  209. Self::validate_owner(
  210. program_id,
  211. &account.owner,
  212. authority_info,
  213. account_info_iter.as_slice(),
  214. )?;
  215. account.owner = *new_owner_info.key;
  216. } else if account_info.data_len() == size_of::<Mint>() {
  217. let mut account_data = account_info.data.borrow_mut();
  218. let mut mint: &mut Mint = state::unpack(&mut account_data)?;
  219. match mint.owner {
  220. COption::Some(ref owner) => {
  221. Self::validate_owner(
  222. program_id,
  223. owner,
  224. authority_info,
  225. account_info_iter.as_slice(),
  226. )?;
  227. }
  228. COption::None => return Err(TokenError::FixedSupply.into()),
  229. }
  230. mint.owner = COption::Some(*new_owner_info.key);
  231. } else {
  232. return Err(ProgramError::InvalidArgument);
  233. }
  234. Ok(())
  235. }
  236. /// Processes a [MintTo](enum.TokenInstruction.html) instruction.
  237. pub fn process_mint_to(
  238. program_id: &Pubkey,
  239. accounts: &[AccountInfo],
  240. amount: u64,
  241. ) -> ProgramResult {
  242. let account_info_iter = &mut accounts.iter();
  243. let mint_info = next_account_info(account_info_iter)?;
  244. let dest_account_info = next_account_info(account_info_iter)?;
  245. let owner_info = next_account_info(account_info_iter)?;
  246. let mut dest_account_data = dest_account_info.data.borrow_mut();
  247. let mut dest_account: &mut Account = state::unpack(&mut dest_account_data)?;
  248. if dest_account.is_native {
  249. return Err(TokenError::NativeNotSupported.into());
  250. }
  251. if mint_info.key != &dest_account.mint {
  252. return Err(TokenError::MintMismatch.into());
  253. }
  254. let mut mint_info_data = mint_info.data.borrow_mut();
  255. let mint: &mut Mint = state::unpack(&mut mint_info_data)?;
  256. match mint.owner {
  257. COption::Some(owner) => {
  258. Self::validate_owner(program_id, &owner, owner_info, account_info_iter.as_slice())?;
  259. }
  260. COption::None => {
  261. return Err(TokenError::FixedSupply.into());
  262. }
  263. }
  264. dest_account.amount = dest_account
  265. .amount
  266. .checked_add(amount)
  267. .ok_or(TokenError::Overflow)?;
  268. Ok(())
  269. }
  270. /// Processes a [Burn](enum.TokenInstruction.html) instruction.
  271. pub fn process_burn(
  272. program_id: &Pubkey,
  273. accounts: &[AccountInfo],
  274. amount: u64,
  275. ) -> ProgramResult {
  276. let account_info_iter = &mut accounts.iter();
  277. let source_account_info = next_account_info(account_info_iter)?;
  278. let authority_info = next_account_info(account_info_iter)?;
  279. let mut source_data = source_account_info.data.borrow_mut();
  280. let source_account: &mut Account = state::unpack(&mut source_data)?;
  281. if source_account.is_native {
  282. return Err(TokenError::NativeNotSupported.into());
  283. }
  284. if source_account.amount < amount {
  285. return Err(TokenError::InsufficientFunds.into());
  286. }
  287. match source_account.delegate {
  288. COption::Some(ref delegate) if authority_info.key == delegate => {
  289. Self::validate_owner(
  290. program_id,
  291. delegate,
  292. authority_info,
  293. account_info_iter.as_slice(),
  294. )?;
  295. if source_account.delegated_amount < amount {
  296. return Err(TokenError::InsufficientFunds.into());
  297. }
  298. source_account.delegated_amount -= amount;
  299. if source_account.delegated_amount == 0 {
  300. source_account.delegate = COption::None;
  301. }
  302. }
  303. _ => Self::validate_owner(
  304. program_id,
  305. &source_account.owner,
  306. authority_info,
  307. account_info_iter.as_slice(),
  308. )?,
  309. }
  310. source_account.amount -= amount;
  311. Ok(())
  312. }
  313. /// Processes a [CloseAccount](enum.TokenInstruction.html) instruction.
  314. pub fn process_close_account(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
  315. let account_info_iter = &mut accounts.iter();
  316. let source_account_info = next_account_info(account_info_iter)?;
  317. let dest_account_info = next_account_info(account_info_iter)?;
  318. let authority_info = next_account_info(account_info_iter)?;
  319. let mut source_data = source_account_info.data.borrow_mut();
  320. let source_account: &mut Account = state::unpack(&mut source_data)?;
  321. if !source_account.is_native {
  322. return Err(TokenError::NonNativeNotSupported.into());
  323. }
  324. Self::validate_owner(
  325. program_id,
  326. &source_account.owner,
  327. authority_info,
  328. account_info_iter.as_slice(),
  329. )?;
  330. **dest_account_info.lamports.borrow_mut() += source_account_info.lamports();
  331. **source_account_info.lamports.borrow_mut() = 0;
  332. source_account.amount = 0;
  333. Ok(())
  334. }
  335. /// Processes an [Instruction](enum.Instruction.html).
  336. pub fn process(program_id: &Pubkey, accounts: &[AccountInfo], input: &[u8]) -> ProgramResult {
  337. let instruction = TokenInstruction::unpack(input)?;
  338. match instruction {
  339. TokenInstruction::InitializeMint { amount, decimals } => {
  340. info!("Instruction: InitializeMint");
  341. Self::process_initialize_mint(accounts, amount, decimals)
  342. }
  343. TokenInstruction::InitializeAccount => {
  344. info!("Instruction: InitializeAccount");
  345. Self::process_initialize_account(accounts)
  346. }
  347. TokenInstruction::InitializeMultisig { m } => {
  348. info!("Instruction: InitializeMultisig");
  349. Self::process_initialize_multisig(accounts, m)
  350. }
  351. TokenInstruction::Transfer { amount } => {
  352. info!("Instruction: Transfer");
  353. Self::process_transfer(program_id, accounts, amount)
  354. }
  355. TokenInstruction::Approve { amount } => {
  356. info!("Instruction: Approve");
  357. Self::process_approve(program_id, accounts, amount)
  358. }
  359. TokenInstruction::Revoke => {
  360. info!("Instruction: Revoke");
  361. Self::process_revoke(program_id, accounts)
  362. }
  363. TokenInstruction::SetOwner => {
  364. info!("Instruction: SetOwner");
  365. Self::process_set_owner(program_id, accounts)
  366. }
  367. TokenInstruction::MintTo { amount } => {
  368. info!("Instruction: MintTo");
  369. Self::process_mint_to(program_id, accounts, amount)
  370. }
  371. TokenInstruction::Burn { amount } => {
  372. info!("Instruction: Burn");
  373. Self::process_burn(program_id, accounts, amount)
  374. }
  375. TokenInstruction::CloseAccount => {
  376. info!("Instruction: CloseAccount");
  377. Self::process_close_account(program_id, accounts)
  378. }
  379. }
  380. }
  381. /// Validates owner(s) are present
  382. pub fn validate_owner(
  383. program_id: &Pubkey,
  384. expected_owner: &Pubkey,
  385. owner_account_info: &AccountInfo,
  386. signers: &[AccountInfo],
  387. ) -> ProgramResult {
  388. if expected_owner != owner_account_info.key {
  389. return Err(TokenError::OwnerMismatch.into());
  390. }
  391. if program_id == owner_account_info.owner
  392. && owner_account_info.data_len() == std::mem::size_of::<Multisig>()
  393. {
  394. let mut owner_data = owner_account_info.data.borrow_mut();
  395. let multisig: &mut Multisig = state::unpack(&mut owner_data)?;
  396. let mut num_signers = 0;
  397. for signer in signers.iter() {
  398. if multisig.signers[0..multisig.n as usize].contains(signer.key) {
  399. if !signer.is_signer {
  400. return Err(ProgramError::MissingRequiredSignature);
  401. }
  402. num_signers += 1;
  403. }
  404. }
  405. if num_signers < multisig.m {
  406. return Err(ProgramError::MissingRequiredSignature);
  407. }
  408. } else if !owner_account_info.is_signer {
  409. return Err(ProgramError::MissingRequiredSignature);
  410. }
  411. Ok(())
  412. }
  413. }
  414. impl PrintProgramError for TokenError {
  415. fn print<E>(&self)
  416. where
  417. E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
  418. {
  419. match self {
  420. TokenError::InsufficientFunds => info!("Error: insufficient funds"),
  421. TokenError::MintMismatch => info!("Error: Account not associated with this Mint"),
  422. TokenError::OwnerMismatch => info!("Error: owner does not match"),
  423. TokenError::FixedSupply => info!("Error: the total supply of this token is fixed"),
  424. TokenError::AlreadyInUse => info!("Error: account or token already in use"),
  425. TokenError::OwnerRequiredIfNoInitialSupply => {
  426. info!("Error: An owner is required if supply is zero")
  427. }
  428. TokenError::InvalidNumberOfProvidedSigners => {
  429. info!("Error: Invalid number of provided signers")
  430. }
  431. TokenError::InvalidNumberOfRequiredSigners => {
  432. info!("Error: Invalid number of required signers")
  433. }
  434. TokenError::UninitializedState => info!("Error: State is uninitialized"),
  435. TokenError::NativeNotSupported => {
  436. info!("Error: Instruction does not support native tokens")
  437. }
  438. TokenError::NonNativeNotSupported => {
  439. info!("Error: Instruction does not support non-native tokens")
  440. }
  441. TokenError::InvalidInstruction => info!("Error: Invalid instruction"),
  442. TokenError::Overflow => info!("Error: Operation overflowed"),
  443. }
  444. }
  445. }
  446. // Pull in syscall stubs when building for non-BPF targets
  447. #[cfg(not(target_arch = "bpf"))]
  448. solana_sdk::program_stubs!();
  449. #[cfg(test)]
  450. mod tests {
  451. use super::*;
  452. use crate::instruction::{
  453. approve, burn, close_account, initialize_account, initialize_mint, initialize_multisig,
  454. mint_to, revoke, set_owner, transfer, MAX_SIGNERS,
  455. };
  456. use solana_sdk::{
  457. account::Account as SolanaAccount, account_info::create_is_signer_account_infos,
  458. clock::Epoch, instruction::Instruction,
  459. };
  460. fn pubkey_rand() -> Pubkey {
  461. Pubkey::new(&rand::random::<[u8; 32]>())
  462. }
  463. fn do_process_instruction(
  464. instruction: Instruction,
  465. accounts: Vec<&mut SolanaAccount>,
  466. ) -> ProgramResult {
  467. let mut meta = instruction
  468. .accounts
  469. .iter()
  470. .zip(accounts)
  471. .map(|(account_meta, account)| (&account_meta.pubkey, account_meta.is_signer, account))
  472. .collect::<Vec<_>>();
  473. let account_infos = create_is_signer_account_infos(&mut meta);
  474. Processor::process(&instruction.program_id, &account_infos, &instruction.data)
  475. }
  476. fn return_token_error_as_program_error() -> ProgramError {
  477. TokenError::MintMismatch.into()
  478. }
  479. #[test]
  480. fn test_print_error() {
  481. let error = return_token_error_as_program_error();
  482. error.print::<TokenError>();
  483. }
  484. #[test]
  485. #[should_panic(expected = "Custom(1)")]
  486. fn test_error_unwrap() {
  487. Err::<(), ProgramError>(return_token_error_as_program_error()).unwrap();
  488. }
  489. #[test]
  490. fn test_unique_account_sizes() {
  491. assert_ne!(size_of::<Mint>(), 0);
  492. assert_ne!(size_of::<Mint>(), size_of::<Account>());
  493. assert_ne!(size_of::<Mint>(), size_of::<Multisig>());
  494. assert_ne!(size_of::<Account>(), 0);
  495. assert_ne!(size_of::<Account>(), size_of::<Multisig>());
  496. assert_ne!(size_of::<Multisig>(), 0);
  497. }
  498. #[test]
  499. fn test_initialize_mint() {
  500. let program_id = pubkey_rand();
  501. let account_key = pubkey_rand();
  502. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  503. let account2_key = pubkey_rand();
  504. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  505. let owner_key = pubkey_rand();
  506. let mut owner_account = SolanaAccount::default();
  507. let mint_key = pubkey_rand();
  508. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  509. let mint2_key = pubkey_rand();
  510. let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  511. // account not created
  512. assert_eq!(
  513. Err(TokenError::UninitializedState.into()),
  514. do_process_instruction(
  515. initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
  516. vec![&mut mint_account, &mut account_account]
  517. )
  518. );
  519. // create account
  520. do_process_instruction(
  521. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  522. vec![&mut account_account, &mut owner_account, &mut mint_account],
  523. )
  524. .unwrap();
  525. // create new mint
  526. do_process_instruction(
  527. initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
  528. vec![&mut mint_account, &mut account_account],
  529. )
  530. .unwrap();
  531. // create another account
  532. do_process_instruction(
  533. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  534. vec![&mut account2_account, &mut owner_account, &mut mint_account],
  535. )
  536. .unwrap();
  537. // mismatch account
  538. assert_eq!(
  539. Err(TokenError::MintMismatch.into()),
  540. do_process_instruction(
  541. initialize_mint(&program_id, &mint2_key, Some(&account2_key), None, 1000, 2)
  542. .unwrap(),
  543. vec![&mut mint2_account, &mut account2_account]
  544. )
  545. );
  546. // create twice
  547. assert_eq!(
  548. Err(TokenError::AlreadyInUse.into()),
  549. do_process_instruction(
  550. initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
  551. vec![&mut mint_account, &mut account_account]
  552. )
  553. );
  554. }
  555. #[test]
  556. fn test_initialize_mint_account() {
  557. let program_id = pubkey_rand();
  558. let account_key = pubkey_rand();
  559. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  560. let owner_key = pubkey_rand();
  561. let mut owner_account = SolanaAccount::default();
  562. let mint_key = pubkey_rand();
  563. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  564. // create account
  565. do_process_instruction(
  566. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  567. vec![&mut account_account, &mut mint_account, &mut owner_account],
  568. )
  569. .unwrap();
  570. // create twice
  571. assert_eq!(
  572. Err(TokenError::AlreadyInUse.into()),
  573. do_process_instruction(
  574. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  575. vec![&mut account_account, &mut mint_account, &mut owner_account],
  576. )
  577. );
  578. }
  579. #[test]
  580. fn test_transfer() {
  581. let program_id = pubkey_rand();
  582. let account_key = pubkey_rand();
  583. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  584. let account2_key = pubkey_rand();
  585. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  586. let account3_key = pubkey_rand();
  587. let mut account3_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  588. let delegate_key = pubkey_rand();
  589. let mut delegate_account = SolanaAccount::default();
  590. let mismatch_key = pubkey_rand();
  591. let mut mismatch_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  592. let owner_key = pubkey_rand();
  593. let mut owner_account = SolanaAccount::default();
  594. let owner2_key = pubkey_rand();
  595. let mut owner2_account = SolanaAccount::default();
  596. let mint_key = pubkey_rand();
  597. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  598. let mint2_key = pubkey_rand();
  599. let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  600. // create account
  601. do_process_instruction(
  602. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  603. vec![&mut account_account, &mut mint_account, &mut owner_account],
  604. )
  605. .unwrap();
  606. // create another account
  607. do_process_instruction(
  608. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  609. vec![&mut account2_account, &mut mint_account, &mut owner_account],
  610. )
  611. .unwrap();
  612. // create another account
  613. do_process_instruction(
  614. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  615. vec![&mut account3_account, &mut mint_account, &mut owner_account],
  616. )
  617. .unwrap();
  618. // create mismatch account
  619. do_process_instruction(
  620. initialize_account(&program_id, &mismatch_key, &mint2_key, &owner_key).unwrap(),
  621. vec![
  622. &mut mismatch_account,
  623. &mut mint2_account,
  624. &mut owner_account,
  625. ],
  626. )
  627. .unwrap();
  628. // create new mint
  629. do_process_instruction(
  630. initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
  631. vec![&mut mint_account, &mut account_account],
  632. )
  633. .unwrap();
  634. // missing signer
  635. let mut instruction = transfer(
  636. &program_id,
  637. &account_key,
  638. &account2_key,
  639. &owner_key,
  640. &[],
  641. 1000,
  642. )
  643. .unwrap();
  644. instruction.accounts[2].is_signer = false;
  645. assert_eq!(
  646. Err(ProgramError::MissingRequiredSignature),
  647. do_process_instruction(
  648. instruction,
  649. vec![
  650. &mut account_account,
  651. &mut account2_account,
  652. &mut owner_account,
  653. ],
  654. )
  655. );
  656. // mismatch mint
  657. assert_eq!(
  658. Err(TokenError::MintMismatch.into()),
  659. do_process_instruction(
  660. transfer(
  661. &program_id,
  662. &account_key,
  663. &mismatch_key,
  664. &owner_key,
  665. &[],
  666. 1000
  667. )
  668. .unwrap(),
  669. vec![
  670. &mut account_account,
  671. &mut mismatch_account,
  672. &mut owner_account,
  673. ],
  674. )
  675. );
  676. // missing owner
  677. assert_eq!(
  678. Err(TokenError::OwnerMismatch.into()),
  679. do_process_instruction(
  680. transfer(
  681. &program_id,
  682. &account_key,
  683. &account2_key,
  684. &owner2_key,
  685. &[],
  686. 1000
  687. )
  688. .unwrap(),
  689. vec![
  690. &mut account_account,
  691. &mut account2_account,
  692. &mut owner2_account,
  693. ],
  694. )
  695. );
  696. // transfer
  697. do_process_instruction(
  698. transfer(
  699. &program_id,
  700. &account_key,
  701. &account2_key,
  702. &owner_key,
  703. &[],
  704. 1000,
  705. )
  706. .unwrap(),
  707. vec![
  708. &mut account_account,
  709. &mut account2_account,
  710. &mut owner_account,
  711. ],
  712. )
  713. .unwrap();
  714. // insufficient funds
  715. assert_eq!(
  716. Err(TokenError::InsufficientFunds.into()),
  717. do_process_instruction(
  718. transfer(&program_id, &account_key, &account2_key, &owner_key, &[], 1).unwrap(),
  719. vec![
  720. &mut account_account,
  721. &mut account2_account,
  722. &mut owner_account,
  723. ],
  724. )
  725. );
  726. // transfer half back
  727. do_process_instruction(
  728. transfer(
  729. &program_id,
  730. &account2_key,
  731. &account_key,
  732. &owner_key,
  733. &[],
  734. 500,
  735. )
  736. .unwrap(),
  737. vec![
  738. &mut account2_account,
  739. &mut account_account,
  740. &mut owner_account,
  741. ],
  742. )
  743. .unwrap();
  744. // transfer rest
  745. do_process_instruction(
  746. transfer(
  747. &program_id,
  748. &account2_key,
  749. &account_key,
  750. &owner_key,
  751. &[],
  752. 500,
  753. )
  754. .unwrap(),
  755. vec![
  756. &mut account2_account,
  757. &mut account_account,
  758. &mut owner_account,
  759. ],
  760. )
  761. .unwrap();
  762. // insufficient funds
  763. assert_eq!(
  764. Err(TokenError::InsufficientFunds.into()),
  765. do_process_instruction(
  766. transfer(&program_id, &account2_key, &account_key, &owner_key, &[], 1).unwrap(),
  767. vec![
  768. &mut account2_account,
  769. &mut account_account,
  770. &mut owner_account,
  771. ],
  772. )
  773. );
  774. // approve delegate
  775. do_process_instruction(
  776. approve(
  777. &program_id,
  778. &account_key,
  779. &delegate_key,
  780. &owner_key,
  781. &[],
  782. 100,
  783. )
  784. .unwrap(),
  785. vec![
  786. &mut account_account,
  787. &mut delegate_account,
  788. &mut owner_account,
  789. ],
  790. )
  791. .unwrap();
  792. // transfer via delegate
  793. do_process_instruction(
  794. transfer(
  795. &program_id,
  796. &account_key,
  797. &account2_key,
  798. &delegate_key,
  799. &[],
  800. 100,
  801. )
  802. .unwrap(),
  803. vec![
  804. &mut account_account,
  805. &mut account2_account,
  806. &mut delegate_account,
  807. ],
  808. )
  809. .unwrap();
  810. // insufficient funds approved via delegate
  811. assert_eq!(
  812. Err(TokenError::OwnerMismatch.into()),
  813. do_process_instruction(
  814. transfer(
  815. &program_id,
  816. &account_key,
  817. &account2_key,
  818. &delegate_key,
  819. &[],
  820. 100
  821. )
  822. .unwrap(),
  823. vec![
  824. &mut account_account,
  825. &mut account2_account,
  826. &mut delegate_account,
  827. ],
  828. )
  829. );
  830. // transfer rest
  831. do_process_instruction(
  832. transfer(
  833. &program_id,
  834. &account_key,
  835. &account2_key,
  836. &owner_key,
  837. &[],
  838. 900,
  839. )
  840. .unwrap(),
  841. vec![
  842. &mut account_account,
  843. &mut account2_account,
  844. &mut owner_account,
  845. ],
  846. )
  847. .unwrap();
  848. // approve delegate
  849. do_process_instruction(
  850. approve(
  851. &program_id,
  852. &account_key,
  853. &delegate_key,
  854. &owner_key,
  855. &[],
  856. 100,
  857. )
  858. .unwrap(),
  859. vec![
  860. &mut account_account,
  861. &mut delegate_account,
  862. &mut owner_account,
  863. ],
  864. )
  865. .unwrap();
  866. // insufficient funds in source account via delegate
  867. assert_eq!(
  868. Err(TokenError::InsufficientFunds.into()),
  869. do_process_instruction(
  870. transfer(
  871. &program_id,
  872. &account_key,
  873. &account2_key,
  874. &delegate_key,
  875. &[],
  876. 100
  877. )
  878. .unwrap(),
  879. vec![
  880. &mut account_account,
  881. &mut account2_account,
  882. &mut delegate_account,
  883. ],
  884. )
  885. );
  886. }
  887. #[test]
  888. fn test_mintable_token_with_zero_supply() {
  889. let program_id = pubkey_rand();
  890. let account_key = pubkey_rand();
  891. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  892. let owner_key = pubkey_rand();
  893. let mut owner_account = SolanaAccount::default();
  894. let mint_key = pubkey_rand();
  895. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  896. // create account
  897. do_process_instruction(
  898. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  899. vec![&mut account_account, &mut owner_account, &mut mint_account],
  900. )
  901. .unwrap();
  902. // create mint-able token without owner
  903. let mut instruction =
  904. initialize_mint(&program_id, &mint_key, None, Some(&owner_key), 0, 2).unwrap();
  905. instruction.accounts.pop();
  906. assert_eq!(
  907. Err(TokenError::OwnerRequiredIfNoInitialSupply.into()),
  908. do_process_instruction(instruction, vec![&mut mint_account])
  909. );
  910. // create mint-able token with zero supply
  911. let amount = 0;
  912. let decimals = 2;
  913. do_process_instruction(
  914. initialize_mint(
  915. &program_id,
  916. &mint_key,
  917. None,
  918. Some(&owner_key),
  919. amount,
  920. decimals,
  921. )
  922. .unwrap(),
  923. vec![&mut mint_account, &mut account_account],
  924. )
  925. .unwrap();
  926. let mint: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
  927. assert_eq!(
  928. *mint,
  929. Mint {
  930. owner: COption::Some(owner_key),
  931. decimals,
  932. is_initialized: true,
  933. }
  934. );
  935. // mint to
  936. do_process_instruction(
  937. mint_to(&program_id, &mint_key, &account_key, &owner_key, &[], 42).unwrap(),
  938. vec![&mut mint_account, &mut account_account, &mut owner_account],
  939. )
  940. .unwrap();
  941. let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
  942. let dest_account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  943. assert_eq!(dest_account.amount, 42);
  944. }
  945. #[test]
  946. fn test_approve() {
  947. let program_id = pubkey_rand();
  948. let account_key = pubkey_rand();
  949. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  950. let account2_key = pubkey_rand();
  951. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  952. let delegate_key = pubkey_rand();
  953. let mut delegate_account = SolanaAccount::default();
  954. let owner_key = pubkey_rand();
  955. let mut owner_account = SolanaAccount::default();
  956. let owner2_key = pubkey_rand();
  957. let mut owner2_account = SolanaAccount::default();
  958. let mint_key = pubkey_rand();
  959. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  960. // create account
  961. do_process_instruction(
  962. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  963. vec![&mut account_account, &mut owner_account, &mut mint_account],
  964. )
  965. .unwrap();
  966. // create another account
  967. do_process_instruction(
  968. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  969. vec![&mut account2_account, &mut owner_account, &mut mint_account],
  970. )
  971. .unwrap();
  972. // create new mint
  973. do_process_instruction(
  974. initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
  975. vec![&mut mint_account, &mut account_account],
  976. )
  977. .unwrap();
  978. // missing signer
  979. let mut instruction = approve(
  980. &program_id,
  981. &account_key,
  982. &delegate_key,
  983. &owner_key,
  984. &[],
  985. 100,
  986. )
  987. .unwrap();
  988. instruction.accounts[2].is_signer = false;
  989. assert_eq!(
  990. Err(ProgramError::MissingRequiredSignature),
  991. do_process_instruction(
  992. instruction,
  993. vec![
  994. &mut account_account,
  995. &mut delegate_account,
  996. &mut owner_account,
  997. ],
  998. )
  999. );
  1000. // no owner
  1001. assert_eq!(
  1002. Err(TokenError::OwnerMismatch.into()),
  1003. do_process_instruction(
  1004. approve(
  1005. &program_id,
  1006. &account_key,
  1007. &delegate_key,
  1008. &owner2_key,
  1009. &[],
  1010. 100
  1011. )
  1012. .unwrap(),
  1013. vec![
  1014. &mut account_account,
  1015. &mut delegate_account,
  1016. &mut owner2_account,
  1017. ],
  1018. )
  1019. );
  1020. // approve delegate
  1021. do_process_instruction(
  1022. approve(
  1023. &program_id,
  1024. &account_key,
  1025. &delegate_key,
  1026. &owner_key,
  1027. &[],
  1028. 100,
  1029. )
  1030. .unwrap(),
  1031. vec![
  1032. &mut account_account,
  1033. &mut delegate_account,
  1034. &mut owner_account,
  1035. ],
  1036. )
  1037. .unwrap();
  1038. // revoke delegate
  1039. do_process_instruction(
  1040. revoke(&program_id, &account_key, &owner_key, &[]).unwrap(),
  1041. vec![&mut account_account, &mut owner_account],
  1042. )
  1043. .unwrap();
  1044. }
  1045. #[test]
  1046. fn test_set_owner() {
  1047. let program_id = pubkey_rand();
  1048. let account_key = pubkey_rand();
  1049. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1050. let account2_key = pubkey_rand();
  1051. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1052. let owner_key = pubkey_rand();
  1053. let mut owner_account = SolanaAccount::default();
  1054. let owner2_key = pubkey_rand();
  1055. let mut owner2_account = SolanaAccount::default();
  1056. let owner3_key = pubkey_rand();
  1057. let mut owner3_account = SolanaAccount::default();
  1058. let mint_key = pubkey_rand();
  1059. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1060. let mint2_key = pubkey_rand();
  1061. let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1062. // invalid account
  1063. assert_eq!(
  1064. Err(TokenError::UninitializedState.into()),
  1065. do_process_instruction(
  1066. set_owner(&program_id, &account_key, &owner2_key, &owner_key, &[]).unwrap(),
  1067. vec![
  1068. &mut account_account,
  1069. &mut owner2_account,
  1070. &mut owner_account,
  1071. ],
  1072. )
  1073. );
  1074. // create account
  1075. do_process_instruction(
  1076. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1077. vec![&mut account_account, &mut mint_account, &mut owner_account],
  1078. )
  1079. .unwrap();
  1080. // create another account
  1081. do_process_instruction(
  1082. initialize_account(&program_id, &account2_key, &mint2_key, &owner_key).unwrap(),
  1083. vec![
  1084. &mut account2_account,
  1085. &mut mint2_account,
  1086. &mut owner_account,
  1087. ],
  1088. )
  1089. .unwrap();
  1090. // missing owner
  1091. assert_eq!(
  1092. Err(TokenError::OwnerMismatch.into()),
  1093. do_process_instruction(
  1094. set_owner(&program_id, &account_key, &owner_key, &owner2_key, &[]).unwrap(),
  1095. vec![
  1096. &mut account_account,
  1097. &mut owner_account,
  1098. &mut owner2_account,
  1099. ],
  1100. )
  1101. );
  1102. // owner did not sign
  1103. let mut instruction =
  1104. set_owner(&program_id, &account_key, &owner2_key, &owner_key, &[]).unwrap();
  1105. instruction.accounts[2].is_signer = false;
  1106. assert_eq!(
  1107. Err(ProgramError::MissingRequiredSignature),
  1108. do_process_instruction(
  1109. instruction,
  1110. vec![
  1111. &mut account_account,
  1112. &mut owner2_account,
  1113. &mut owner_account,
  1114. ],
  1115. )
  1116. );
  1117. // set owner
  1118. do_process_instruction(
  1119. set_owner(&program_id, &account_key, &owner2_key, &owner_key, &[]).unwrap(),
  1120. vec![
  1121. &mut account_account,
  1122. &mut owner2_account,
  1123. &mut owner_account,
  1124. ],
  1125. )
  1126. .unwrap();
  1127. // create new mint with owner
  1128. do_process_instruction(
  1129. initialize_mint(
  1130. &program_id,
  1131. &mint_key,
  1132. Some(&account_key),
  1133. Some(&owner_key),
  1134. 1000,
  1135. 2,
  1136. )
  1137. .unwrap(),
  1138. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1139. )
  1140. .unwrap();
  1141. // wrong account
  1142. assert_eq!(
  1143. Err(TokenError::OwnerMismatch.into()),
  1144. do_process_instruction(
  1145. set_owner(&program_id, &mint_key, &owner3_key, &owner2_key, &[]).unwrap(),
  1146. vec![&mut mint_account, &mut owner3_account, &mut owner2_account],
  1147. )
  1148. );
  1149. // owner did not sign
  1150. let mut instruction =
  1151. set_owner(&program_id, &mint_key, &owner2_key, &owner_key, &[]).unwrap();
  1152. instruction.accounts[2].is_signer = false;
  1153. assert_eq!(
  1154. Err(ProgramError::MissingRequiredSignature),
  1155. do_process_instruction(
  1156. instruction,
  1157. vec![&mut mint_account, &mut owner2_account, &mut owner_account],
  1158. )
  1159. );
  1160. // set owner
  1161. do_process_instruction(
  1162. set_owner(&program_id, &mint_key, &owner2_key, &owner_key, &[]).unwrap(),
  1163. vec![&mut mint_account, &mut owner2_account, &mut owner_account],
  1164. )
  1165. .unwrap();
  1166. // create new mint without owner
  1167. do_process_instruction(
  1168. initialize_mint(&program_id, &mint2_key, Some(&account2_key), None, 1000, 2).unwrap(),
  1169. vec![&mut mint2_account, &mut account2_account],
  1170. )
  1171. .unwrap();
  1172. // set owner for non-mint-able token
  1173. assert_eq!(
  1174. Err(TokenError::OwnerMismatch.into()),
  1175. do_process_instruction(
  1176. set_owner(&program_id, &mint2_key, &owner2_key, &owner_key, &[]).unwrap(),
  1177. vec![&mut mint_account, &mut owner2_account, &mut owner_account],
  1178. )
  1179. );
  1180. }
  1181. #[test]
  1182. fn test_mint_to() {
  1183. let program_id = pubkey_rand();
  1184. let account_key = pubkey_rand();
  1185. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1186. let account2_key = pubkey_rand();
  1187. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1188. let account3_key = pubkey_rand();
  1189. let mut account3_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1190. let mismatch_key = pubkey_rand();
  1191. let mut mismatch_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1192. let owner_key = pubkey_rand();
  1193. let mut owner_account = SolanaAccount::default();
  1194. let owner2_key = pubkey_rand();
  1195. let mut owner2_account = SolanaAccount::default();
  1196. let mint_key = pubkey_rand();
  1197. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1198. let mint2_key = pubkey_rand();
  1199. let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1200. let uninitialized_key = pubkey_rand();
  1201. let mut uninitialized_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1202. // create account
  1203. do_process_instruction(
  1204. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1205. vec![&mut account_account, &mut mint_account, &mut owner_account],
  1206. )
  1207. .unwrap();
  1208. // create another account
  1209. do_process_instruction(
  1210. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  1211. vec![&mut account2_account, &mut mint_account, &mut owner_account],
  1212. )
  1213. .unwrap();
  1214. // create another account
  1215. do_process_instruction(
  1216. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  1217. vec![&mut account3_account, &mut mint_account, &mut owner_account],
  1218. )
  1219. .unwrap();
  1220. // create mismatch account
  1221. do_process_instruction(
  1222. initialize_account(&program_id, &mismatch_key, &mint2_key, &owner_key).unwrap(),
  1223. vec![
  1224. &mut mismatch_account,
  1225. &mut mint2_account,
  1226. &mut owner_account,
  1227. ],
  1228. )
  1229. .unwrap();
  1230. // create new mint with owner
  1231. do_process_instruction(
  1232. initialize_mint(
  1233. &program_id,
  1234. &mint_key,
  1235. Some(&account_key),
  1236. Some(&owner_key),
  1237. 1000,
  1238. 2,
  1239. )
  1240. .unwrap(),
  1241. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1242. )
  1243. .unwrap();
  1244. // mint to
  1245. do_process_instruction(
  1246. mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap(),
  1247. vec![&mut mint_account, &mut account2_account, &mut owner_account],
  1248. )
  1249. .unwrap();
  1250. let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
  1251. let dest_account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
  1252. assert_eq!(dest_account.amount, 42);
  1253. // missing signer
  1254. let mut instruction =
  1255. mint_to(&program_id, &mint_key, &account2_key, &owner_key, &[], 42).unwrap();
  1256. instruction.accounts[2].is_signer = false;
  1257. assert_eq!(
  1258. Err(ProgramError::MissingRequiredSignature),
  1259. do_process_instruction(
  1260. instruction,
  1261. vec![&mut mint_account, &mut account2_account, &mut owner_account],
  1262. )
  1263. );
  1264. // mismatch account
  1265. assert_eq!(
  1266. Err(TokenError::MintMismatch.into()),
  1267. do_process_instruction(
  1268. mint_to(&program_id, &mint_key, &mismatch_key, &owner_key, &[], 42).unwrap(),
  1269. vec![&mut mint_account, &mut mismatch_account, &mut owner_account],
  1270. )
  1271. );
  1272. // missing owner
  1273. assert_eq!(
  1274. Err(TokenError::OwnerMismatch.into()),
  1275. do_process_instruction(
  1276. mint_to(&program_id, &mint_key, &account2_key, &owner2_key, &[], 42).unwrap(),
  1277. vec![
  1278. &mut mint_account,
  1279. &mut account2_account,
  1280. &mut owner2_account,
  1281. ],
  1282. )
  1283. );
  1284. // uninitialized destination account
  1285. assert_eq!(
  1286. Err(TokenError::UninitializedState.into()),
  1287. do_process_instruction(
  1288. mint_to(
  1289. &program_id,
  1290. &mint_key,
  1291. &uninitialized_key,
  1292. &owner_key,
  1293. &[],
  1294. 42
  1295. )
  1296. .unwrap(),
  1297. vec![
  1298. &mut mint_account,
  1299. &mut uninitialized_account,
  1300. &mut owner_account,
  1301. ],
  1302. )
  1303. );
  1304. }
  1305. #[test]
  1306. fn test_burn() {
  1307. let program_id = pubkey_rand();
  1308. let account_key = pubkey_rand();
  1309. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1310. let account2_key = pubkey_rand();
  1311. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1312. let account3_key = pubkey_rand();
  1313. let mut account3_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1314. let delegate_key = pubkey_rand();
  1315. let mut delegate_account = SolanaAccount::default();
  1316. let mismatch_key = pubkey_rand();
  1317. let mut mismatch_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1318. let owner_key = pubkey_rand();
  1319. let mut owner_account = SolanaAccount::default();
  1320. let owner2_key = pubkey_rand();
  1321. let mut owner2_account = SolanaAccount::default();
  1322. let mint_key = pubkey_rand();
  1323. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1324. let mint2_key = pubkey_rand();
  1325. let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1326. // create account
  1327. do_process_instruction(
  1328. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1329. vec![&mut account_account, &mut mint_account, &mut owner_account],
  1330. )
  1331. .unwrap();
  1332. // create another account
  1333. do_process_instruction(
  1334. initialize_account(&program_id, &account2_key, &mint_key, &owner_key).unwrap(),
  1335. vec![&mut account2_account, &mut mint_account, &mut owner_account],
  1336. )
  1337. .unwrap();
  1338. // create another account
  1339. do_process_instruction(
  1340. initialize_account(&program_id, &account3_key, &mint_key, &owner_key).unwrap(),
  1341. vec![&mut account3_account, &mut mint_account, &mut owner_account],
  1342. )
  1343. .unwrap();
  1344. // create mismatch account
  1345. do_process_instruction(
  1346. initialize_account(&program_id, &mismatch_key, &mint2_key, &owner_key).unwrap(),
  1347. vec![
  1348. &mut mismatch_account,
  1349. &mut mint2_account,
  1350. &mut owner_account,
  1351. ],
  1352. )
  1353. .unwrap();
  1354. // create new mint
  1355. do_process_instruction(
  1356. initialize_mint(&program_id, &mint_key, Some(&account_key), None, 1000, 2).unwrap(),
  1357. vec![&mut mint_account, &mut account_account],
  1358. )
  1359. .unwrap();
  1360. // missing signer
  1361. let mut instruction = burn(&program_id, &account_key, &delegate_key, &[], 42).unwrap();
  1362. instruction.accounts[1].is_signer = false;
  1363. assert_eq!(
  1364. Err(TokenError::OwnerMismatch.into()),
  1365. do_process_instruction(
  1366. instruction,
  1367. vec![&mut account_account, &mut delegate_account],
  1368. )
  1369. );
  1370. // missing owner
  1371. assert_eq!(
  1372. Err(TokenError::OwnerMismatch.into()),
  1373. do_process_instruction(
  1374. burn(&program_id, &account_key, &owner2_key, &[], 42).unwrap(),
  1375. vec![&mut account_account, &mut owner2_account],
  1376. )
  1377. );
  1378. // burn
  1379. do_process_instruction(
  1380. burn(&program_id, &account_key, &owner_key, &[], 42).unwrap(),
  1381. vec![&mut account_account, &mut owner_account],
  1382. )
  1383. .unwrap();
  1384. let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
  1385. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  1386. assert_eq!(account.amount, 1000 - 42);
  1387. // insufficient funds
  1388. assert_eq!(
  1389. Err(TokenError::InsufficientFunds.into()),
  1390. do_process_instruction(
  1391. burn(&program_id, &account_key, &owner_key, &[], 100_000_000).unwrap(),
  1392. vec![&mut account_account, &mut owner_account],
  1393. )
  1394. );
  1395. // approve delegate
  1396. do_process_instruction(
  1397. approve(
  1398. &program_id,
  1399. &account_key,
  1400. &delegate_key,
  1401. &owner_key,
  1402. &[],
  1403. 84,
  1404. )
  1405. .unwrap(),
  1406. vec![
  1407. &mut account_account,
  1408. &mut delegate_account,
  1409. &mut owner_account,
  1410. ],
  1411. )
  1412. .unwrap();
  1413. // not a delegate of source account
  1414. assert_eq!(
  1415. Err(TokenError::InsufficientFunds.into()),
  1416. do_process_instruction(
  1417. burn(&program_id, &account_key, &owner_key, &[], 100_000_000).unwrap(),
  1418. vec![&mut account_account, &mut owner_account],
  1419. )
  1420. );
  1421. // burn via delegate
  1422. do_process_instruction(
  1423. burn(&program_id, &account_key, &delegate_key, &[], 84).unwrap(),
  1424. vec![&mut account_account, &mut delegate_account],
  1425. )
  1426. .unwrap();
  1427. // match
  1428. let _: &mut Mint = state::unpack(&mut mint_account.data).unwrap();
  1429. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  1430. assert_eq!(account.amount, 1000 - 42 - 84);
  1431. // insufficient funds approved via delegate
  1432. assert_eq!(
  1433. Err(TokenError::OwnerMismatch.into()),
  1434. do_process_instruction(
  1435. burn(&program_id, &account_key, &delegate_key, &[], 100).unwrap(),
  1436. vec![&mut account_account, &mut delegate_account],
  1437. )
  1438. );
  1439. }
  1440. #[test]
  1441. fn test_multisig() {
  1442. let program_id = pubkey_rand();
  1443. let mint_key = pubkey_rand();
  1444. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1445. let account_key = pubkey_rand();
  1446. let mut account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1447. let account2_key = pubkey_rand();
  1448. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  1449. let owner_key = pubkey_rand();
  1450. let mut owner_account = SolanaAccount::default();
  1451. let multisig_key = pubkey_rand();
  1452. let mut multisig_account = SolanaAccount::new(0, size_of::<Multisig>(), &program_id);
  1453. let multisig_delegate_key = pubkey_rand();
  1454. let mut multisig_delegate_account =
  1455. SolanaAccount::new(0, size_of::<Multisig>(), &program_id);
  1456. let signer_keys = vec![pubkey_rand(); MAX_SIGNERS];
  1457. let signer_key_refs: Vec<&Pubkey> = signer_keys.iter().map(|key| key).collect();
  1458. let mut signer_accounts = vec![SolanaAccount::new(0, 0, &program_id); MAX_SIGNERS];
  1459. // single signer
  1460. let account_info_iter = &mut signer_accounts.iter_mut();
  1461. do_process_instruction(
  1462. initialize_multisig(&program_id, &multisig_key, &[&signer_keys[0]], 1).unwrap(),
  1463. vec![
  1464. &mut multisig_account,
  1465. &mut account_info_iter.next().unwrap(),
  1466. ],
  1467. )
  1468. .unwrap();
  1469. // multiple signer
  1470. let account_info_iter = &mut signer_accounts.iter_mut();
  1471. do_process_instruction(
  1472. initialize_multisig(
  1473. &program_id,
  1474. &multisig_delegate_key,
  1475. &signer_key_refs,
  1476. MAX_SIGNERS as u8,
  1477. )
  1478. .unwrap(),
  1479. vec![
  1480. &mut multisig_delegate_account,
  1481. &mut account_info_iter.next().unwrap(),
  1482. &mut account_info_iter.next().unwrap(),
  1483. &mut account_info_iter.next().unwrap(),
  1484. &mut account_info_iter.next().unwrap(),
  1485. &mut account_info_iter.next().unwrap(),
  1486. &mut account_info_iter.next().unwrap(),
  1487. &mut account_info_iter.next().unwrap(),
  1488. &mut account_info_iter.next().unwrap(),
  1489. &mut account_info_iter.next().unwrap(),
  1490. &mut account_info_iter.next().unwrap(),
  1491. &mut account_info_iter.next().unwrap(),
  1492. ],
  1493. )
  1494. .unwrap();
  1495. // create account with multisig owner
  1496. do_process_instruction(
  1497. initialize_account(&program_id, &account_key, &mint_key, &multisig_key).unwrap(),
  1498. vec![&mut account, &mut mint_account, &mut multisig_account],
  1499. )
  1500. .unwrap();
  1501. // create another account with multisig owner
  1502. do_process_instruction(
  1503. initialize_account(
  1504. &program_id,
  1505. &account2_key,
  1506. &mint_key,
  1507. &multisig_delegate_key,
  1508. )
  1509. .unwrap(),
  1510. vec![
  1511. &mut account2_account,
  1512. &mut mint_account,
  1513. &mut multisig_account,
  1514. ],
  1515. )
  1516. .unwrap();
  1517. // create new m int with multisig owner
  1518. do_process_instruction(
  1519. initialize_mint(
  1520. &program_id,
  1521. &mint_key,
  1522. Some(&account_key),
  1523. Some(&multisig_key),
  1524. 1000,
  1525. 2,
  1526. )
  1527. .unwrap(),
  1528. vec![&mut mint_account, &mut account, &mut multisig_account],
  1529. )
  1530. .unwrap();
  1531. // approve
  1532. let account_info_iter = &mut signer_accounts.iter_mut();
  1533. do_process_instruction(
  1534. approve(
  1535. &program_id,
  1536. &account_key,
  1537. &multisig_delegate_key,
  1538. &multisig_key,
  1539. &[&signer_keys[0]],
  1540. 100,
  1541. )
  1542. .unwrap(),
  1543. vec![
  1544. &mut account,
  1545. &mut multisig_delegate_account,
  1546. &mut multisig_account,
  1547. &mut account_info_iter.next().unwrap(),
  1548. ],
  1549. )
  1550. .unwrap();
  1551. // transfer
  1552. let account_info_iter = &mut signer_accounts.iter_mut();
  1553. do_process_instruction(
  1554. transfer(
  1555. &program_id,
  1556. &account_key,
  1557. &account2_key,
  1558. &multisig_key,
  1559. &[&signer_keys[0]],
  1560. 42,
  1561. )
  1562. .unwrap(),
  1563. vec![
  1564. &mut account,
  1565. &mut account2_account,
  1566. &mut multisig_account,
  1567. &mut account_info_iter.next().unwrap(),
  1568. ],
  1569. )
  1570. .unwrap();
  1571. // transfer via delegate
  1572. let account_info_iter = &mut signer_accounts.iter_mut();
  1573. do_process_instruction(
  1574. transfer(
  1575. &program_id,
  1576. &account_key,
  1577. &account2_key,
  1578. &multisig_delegate_key,
  1579. &signer_key_refs,
  1580. 42,
  1581. )
  1582. .unwrap(),
  1583. vec![
  1584. &mut account,
  1585. &mut account2_account,
  1586. &mut multisig_delegate_account,
  1587. &mut account_info_iter.next().unwrap(),
  1588. &mut account_info_iter.next().unwrap(),
  1589. &mut account_info_iter.next().unwrap(),
  1590. &mut account_info_iter.next().unwrap(),
  1591. &mut account_info_iter.next().unwrap(),
  1592. &mut account_info_iter.next().unwrap(),
  1593. &mut account_info_iter.next().unwrap(),
  1594. &mut account_info_iter.next().unwrap(),
  1595. &mut account_info_iter.next().unwrap(),
  1596. &mut account_info_iter.next().unwrap(),
  1597. &mut account_info_iter.next().unwrap(),
  1598. ],
  1599. )
  1600. .unwrap();
  1601. // mint to
  1602. let account_info_iter = &mut signer_accounts.iter_mut();
  1603. do_process_instruction(
  1604. mint_to(
  1605. &program_id,
  1606. &mint_key,
  1607. &account2_key,
  1608. &multisig_key,
  1609. &[&signer_keys[0]],
  1610. 42,
  1611. )
  1612. .unwrap(),
  1613. vec![
  1614. &mut mint_account,
  1615. &mut account2_account,
  1616. &mut multisig_account,
  1617. &mut account_info_iter.next().unwrap(),
  1618. ],
  1619. )
  1620. .unwrap();
  1621. // burn
  1622. let account_info_iter = &mut signer_accounts.iter_mut();
  1623. do_process_instruction(
  1624. burn(
  1625. &program_id,
  1626. &account_key,
  1627. &multisig_key,
  1628. &[&signer_keys[0]],
  1629. 42,
  1630. )
  1631. .unwrap(),
  1632. vec![
  1633. &mut account,
  1634. &mut multisig_account,
  1635. &mut account_info_iter.next().unwrap(),
  1636. ],
  1637. )
  1638. .unwrap();
  1639. // burn via delegate
  1640. let account_info_iter = &mut signer_accounts.iter_mut();
  1641. do_process_instruction(
  1642. burn(
  1643. &program_id,
  1644. &account_key,
  1645. &multisig_delegate_key,
  1646. &signer_key_refs,
  1647. 42,
  1648. )
  1649. .unwrap(),
  1650. vec![
  1651. &mut account,
  1652. &mut multisig_delegate_account,
  1653. &mut account_info_iter.next().unwrap(),
  1654. &mut account_info_iter.next().unwrap(),
  1655. &mut account_info_iter.next().unwrap(),
  1656. &mut account_info_iter.next().unwrap(),
  1657. &mut account_info_iter.next().unwrap(),
  1658. &mut account_info_iter.next().unwrap(),
  1659. &mut account_info_iter.next().unwrap(),
  1660. &mut account_info_iter.next().unwrap(),
  1661. &mut account_info_iter.next().unwrap(),
  1662. &mut account_info_iter.next().unwrap(),
  1663. &mut account_info_iter.next().unwrap(),
  1664. ],
  1665. )
  1666. .unwrap();
  1667. // do SetOwner on mint
  1668. let account_info_iter = &mut signer_accounts.iter_mut();
  1669. do_process_instruction(
  1670. set_owner(
  1671. &program_id,
  1672. &mint_key,
  1673. &owner_key,
  1674. &multisig_key,
  1675. &[&signer_keys[0]],
  1676. )
  1677. .unwrap(),
  1678. vec![
  1679. &mut mint_account,
  1680. &mut owner_account,
  1681. &mut multisig_account,
  1682. &mut account_info_iter.next().unwrap(),
  1683. ],
  1684. )
  1685. .unwrap();
  1686. // do SetOwner on account
  1687. let account_info_iter = &mut signer_accounts.iter_mut();
  1688. do_process_instruction(
  1689. set_owner(
  1690. &program_id,
  1691. &account_key,
  1692. &owner_key,
  1693. &multisig_key,
  1694. &[&signer_keys[0]],
  1695. )
  1696. .unwrap(),
  1697. vec![
  1698. &mut account,
  1699. &mut owner_account,
  1700. &mut multisig_account,
  1701. &mut account_info_iter.next().unwrap(),
  1702. ],
  1703. )
  1704. .unwrap();
  1705. }
  1706. #[test]
  1707. fn test_validate_owner() {
  1708. let program_id = pubkey_rand();
  1709. let owner_key = pubkey_rand();
  1710. let mut signer_keys = [Pubkey::default(); MAX_SIGNERS];
  1711. for signer_key in signer_keys.iter_mut().take(MAX_SIGNERS) {
  1712. *signer_key = pubkey_rand();
  1713. }
  1714. let mut signer_lamports = 0;
  1715. let mut signer_data = vec![];
  1716. let mut signers = vec![
  1717. AccountInfo::new(
  1718. &owner_key,
  1719. true,
  1720. false,
  1721. &mut signer_lamports,
  1722. &mut signer_data,
  1723. &program_id,
  1724. false,
  1725. Epoch::default(),
  1726. );
  1727. MAX_SIGNERS + 1
  1728. ];
  1729. for (signer, key) in signers.iter_mut().zip(&signer_keys) {
  1730. signer.key = key;
  1731. }
  1732. let mut lamports = 0;
  1733. let mut data = vec![0; size_of::<Multisig>()];
  1734. let mut multisig: &mut Multisig = state::unpack_unchecked(&mut data).unwrap();
  1735. multisig.m = MAX_SIGNERS as u8;
  1736. multisig.n = MAX_SIGNERS as u8;
  1737. multisig.signers = signer_keys;
  1738. multisig.is_initialized = true;
  1739. let owner_account_info = AccountInfo::new(
  1740. &owner_key,
  1741. false,
  1742. false,
  1743. &mut lamports,
  1744. &mut data,
  1745. &program_id,
  1746. false,
  1747. Epoch::default(),
  1748. );
  1749. // full 11 of 11
  1750. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers).unwrap();
  1751. // 1 of 11
  1752. {
  1753. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1754. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1755. multisig.m = 1;
  1756. }
  1757. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers).unwrap();
  1758. // 2:1
  1759. {
  1760. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1761. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1762. multisig.m = 2;
  1763. multisig.n = 1;
  1764. }
  1765. assert_eq!(
  1766. Err(ProgramError::MissingRequiredSignature),
  1767. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers)
  1768. );
  1769. // 0:11
  1770. {
  1771. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1772. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1773. multisig.m = 0;
  1774. multisig.n = 11;
  1775. }
  1776. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers).unwrap();
  1777. // 2:11 but 0 provided
  1778. {
  1779. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1780. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1781. multisig.m = 2;
  1782. multisig.n = 11;
  1783. }
  1784. assert_eq!(
  1785. Err(ProgramError::MissingRequiredSignature),
  1786. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &[])
  1787. );
  1788. // 2:11 but 1 provided
  1789. {
  1790. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1791. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1792. multisig.m = 2;
  1793. multisig.n = 11;
  1794. }
  1795. assert_eq!(
  1796. Err(ProgramError::MissingRequiredSignature),
  1797. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers[0..1])
  1798. );
  1799. // 2:11, 2 from middle provided
  1800. {
  1801. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1802. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1803. multisig.m = 2;
  1804. multisig.n = 11;
  1805. }
  1806. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers[5..7])
  1807. .unwrap();
  1808. // 11:11, one is not a signer
  1809. {
  1810. let mut data_ref_mut = owner_account_info.data.borrow_mut();
  1811. let mut multisig: &mut Multisig = state::unpack(&mut data_ref_mut).unwrap();
  1812. multisig.m = 2;
  1813. multisig.n = 11;
  1814. }
  1815. signers[5].is_signer = false;
  1816. assert_eq!(
  1817. Err(ProgramError::MissingRequiredSignature),
  1818. Processor::validate_owner(&program_id, &owner_key, &owner_account_info, &signers)
  1819. );
  1820. signers[5].is_signer = true;
  1821. }
  1822. #[test]
  1823. fn test_close_account() {
  1824. let program_id = pubkey_rand();
  1825. let mint_key = pubkey_rand();
  1826. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1827. let account_key = pubkey_rand();
  1828. let mut account_account = SolanaAccount::new(42, size_of::<Account>(), &program_id);
  1829. let account2_key = pubkey_rand();
  1830. let mut account2_account = SolanaAccount::new(2, size_of::<Account>(), &program_id);
  1831. let account3_key = pubkey_rand();
  1832. let mut account3_account = SolanaAccount::new(2, size_of::<Account>(), &program_id);
  1833. let owner_key = pubkey_rand();
  1834. let mut owner_account = SolanaAccount::default();
  1835. let owner2_key = pubkey_rand();
  1836. let mut owner2_account = SolanaAccount::default();
  1837. // uninitialized
  1838. assert_eq!(
  1839. Err(TokenError::UninitializedState.into()),
  1840. do_process_instruction(
  1841. close_account(&program_id, &account_key, &account3_key, &owner2_key, &[]).unwrap(),
  1842. vec![
  1843. &mut account_account,
  1844. &mut account3_account,
  1845. &mut owner2_account,
  1846. ],
  1847. )
  1848. );
  1849. // initialize non-native account
  1850. do_process_instruction(
  1851. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  1852. vec![&mut account_account, &mut mint_account, &mut owner_account],
  1853. )
  1854. .unwrap();
  1855. // initialize native account
  1856. do_process_instruction(
  1857. initialize_account(
  1858. &program_id,
  1859. &account2_key,
  1860. &crate::native_mint::id(),
  1861. &owner_key,
  1862. )
  1863. .unwrap(),
  1864. vec![&mut account2_account, &mut mint_account, &mut owner_account],
  1865. )
  1866. .unwrap();
  1867. let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
  1868. assert!(account.is_native);
  1869. assert_eq!(account.amount, 2);
  1870. // close non-native account
  1871. assert_eq!(
  1872. Err(TokenError::NonNativeNotSupported.into()),
  1873. do_process_instruction(
  1874. close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
  1875. vec![
  1876. &mut account_account,
  1877. &mut account3_account,
  1878. &mut owner_account,
  1879. ],
  1880. )
  1881. );
  1882. assert_eq!(account_account.lamports, 42);
  1883. // close native account
  1884. do_process_instruction(
  1885. close_account(&program_id, &account2_key, &account3_key, &owner_key, &[]).unwrap(),
  1886. vec![
  1887. &mut account2_account,
  1888. &mut account3_account,
  1889. &mut owner_account,
  1890. ],
  1891. )
  1892. .unwrap();
  1893. let account: &mut Account = state::unpack_unchecked(&mut account2_account.data).unwrap();
  1894. assert!(account.is_native);
  1895. assert_eq!(account.amount, 0);
  1896. assert_eq!(account3_account.lamports, 4);
  1897. }
  1898. #[test]
  1899. fn test_native_token() {
  1900. let program_id = pubkey_rand();
  1901. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  1902. let account_key = pubkey_rand();
  1903. let mut account_account = SolanaAccount::new(42, size_of::<Account>(), &program_id);
  1904. let account2_key = pubkey_rand();
  1905. let mut account2_account = SolanaAccount::new(2, size_of::<Account>(), &program_id);
  1906. let account3_key = pubkey_rand();
  1907. let mut account3_account = SolanaAccount::new(2, 0, &program_id);
  1908. let owner_key = pubkey_rand();
  1909. let mut owner_account = SolanaAccount::default();
  1910. // initialize native account
  1911. do_process_instruction(
  1912. initialize_account(
  1913. &program_id,
  1914. &account_key,
  1915. &crate::native_mint::id(),
  1916. &owner_key,
  1917. )
  1918. .unwrap(),
  1919. vec![&mut account_account, &mut mint_account, &mut owner_account],
  1920. )
  1921. .unwrap();
  1922. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  1923. assert!(account.is_native);
  1924. assert_eq!(account.amount, 42);
  1925. // initialize native account
  1926. do_process_instruction(
  1927. initialize_account(
  1928. &program_id,
  1929. &account2_key,
  1930. &crate::native_mint::id(),
  1931. &owner_key,
  1932. )
  1933. .unwrap(),
  1934. vec![&mut account2_account, &mut mint_account, &mut owner_account],
  1935. )
  1936. .unwrap();
  1937. let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
  1938. assert!(account.is_native);
  1939. assert_eq!(account.amount, 2);
  1940. // mint_to unsupported
  1941. assert_eq!(
  1942. Err(TokenError::NativeNotSupported.into()),
  1943. do_process_instruction(
  1944. mint_to(
  1945. &program_id,
  1946. &crate::native_mint::id(),
  1947. &account_key,
  1948. &owner_key,
  1949. &[],
  1950. 42
  1951. )
  1952. .unwrap(),
  1953. vec![&mut mint_account, &mut account_account, &mut owner_account],
  1954. )
  1955. );
  1956. // burn unsupported
  1957. assert_eq!(
  1958. Err(TokenError::NativeNotSupported.into()),
  1959. do_process_instruction(
  1960. burn(&program_id, &account_key, &owner_key, &[], 42).unwrap(),
  1961. vec![&mut account_account, &mut owner_account],
  1962. )
  1963. );
  1964. // initialize native account
  1965. do_process_instruction(
  1966. transfer(
  1967. &program_id,
  1968. &account_key,
  1969. &account2_key,
  1970. &owner_key,
  1971. &[],
  1972. 40,
  1973. )
  1974. .unwrap(),
  1975. vec![
  1976. &mut account_account,
  1977. &mut account2_account,
  1978. &mut owner_account,
  1979. ],
  1980. )
  1981. .unwrap();
  1982. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  1983. assert!(account.is_native);
  1984. assert_eq!(account_account.lamports, 2);
  1985. assert_eq!(account.amount, 2);
  1986. let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
  1987. assert!(account.is_native);
  1988. assert_eq!(account2_account.lamports, 42);
  1989. assert_eq!(account.amount, 42);
  1990. // close native account
  1991. do_process_instruction(
  1992. close_account(&program_id, &account_key, &account3_key, &owner_key, &[]).unwrap(),
  1993. vec![
  1994. &mut account_account,
  1995. &mut account3_account,
  1996. &mut owner_account,
  1997. ],
  1998. )
  1999. .unwrap();
  2000. let account: &mut Account = state::unpack_unchecked(&mut account_account.data).unwrap();
  2001. assert!(account.is_native);
  2002. assert_eq!(account_account.lamports, 0);
  2003. assert_eq!(account.amount, 0);
  2004. assert_eq!(account3_account.lamports, 4);
  2005. }
  2006. #[test]
  2007. fn test_overflow() {
  2008. let program_id = pubkey_rand();
  2009. let account_key = pubkey_rand();
  2010. let mut account_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  2011. let account2_key = pubkey_rand();
  2012. let mut account2_account = SolanaAccount::new(0, size_of::<Account>(), &program_id);
  2013. let owner_key = pubkey_rand();
  2014. let mut owner_account = SolanaAccount::default();
  2015. let owner2_key = pubkey_rand();
  2016. let mut owner2_account = SolanaAccount::default();
  2017. let mint_owner_key = pubkey_rand();
  2018. let mut mint_owner_account = SolanaAccount::default();
  2019. let mint_key = pubkey_rand();
  2020. let mut mint_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
  2021. // create victim account
  2022. do_process_instruction(
  2023. initialize_account(&program_id, &account_key, &mint_key, &owner_key).unwrap(),
  2024. vec![&mut account_account, &mut mint_account, &mut owner_account],
  2025. )
  2026. .unwrap();
  2027. // create another account
  2028. do_process_instruction(
  2029. initialize_account(&program_id, &account2_key, &mint_key, &owner2_key).unwrap(),
  2030. vec![
  2031. &mut account2_account,
  2032. &mut mint_account,
  2033. &mut owner2_account,
  2034. ],
  2035. )
  2036. .unwrap();
  2037. // create new mint with owner
  2038. do_process_instruction(
  2039. initialize_mint(&program_id, &mint_key, None, Some(&mint_owner_key), 0, 2).unwrap(),
  2040. vec![&mut mint_account, &mut mint_owner_account],
  2041. )
  2042. .unwrap();
  2043. // mint the max to attacker
  2044. do_process_instruction(
  2045. mint_to(
  2046. &program_id,
  2047. &mint_key,
  2048. &account2_key,
  2049. &mint_owner_key,
  2050. &[],
  2051. 42,
  2052. )
  2053. .unwrap(),
  2054. vec![
  2055. &mut mint_account,
  2056. &mut account2_account,
  2057. &mut mint_owner_account,
  2058. ],
  2059. )
  2060. .unwrap();
  2061. let account: &mut Account = state::unpack(&mut account2_account.data).unwrap();
  2062. assert_eq!(account.amount, 42);
  2063. // mint the max to victum
  2064. do_process_instruction(
  2065. mint_to(
  2066. &program_id,
  2067. &mint_key,
  2068. &account_key,
  2069. &mint_owner_key,
  2070. &[],
  2071. u64::MAX,
  2072. )
  2073. .unwrap(),
  2074. vec![
  2075. &mut mint_account,
  2076. &mut account_account,
  2077. &mut mint_owner_account,
  2078. ],
  2079. )
  2080. .unwrap();
  2081. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  2082. assert_eq!(account.amount, u64::MAX);
  2083. // mint one more
  2084. assert_eq!(
  2085. Err(TokenError::Overflow.into()),
  2086. do_process_instruction(
  2087. mint_to(
  2088. &program_id,
  2089. &mint_key,
  2090. &account_key,
  2091. &mint_owner_key,
  2092. &[],
  2093. 1,
  2094. )
  2095. .unwrap(),
  2096. vec![
  2097. &mut mint_account,
  2098. &mut account_account,
  2099. &mut mint_owner_account,
  2100. ],
  2101. )
  2102. );
  2103. // mint back to large amount
  2104. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  2105. account.amount = 0;
  2106. do_process_instruction(
  2107. mint_to(
  2108. &program_id,
  2109. &mint_key,
  2110. &account_key,
  2111. &mint_owner_key,
  2112. &[],
  2113. u64::MAX,
  2114. )
  2115. .unwrap(),
  2116. vec![
  2117. &mut mint_account,
  2118. &mut account_account,
  2119. &mut mint_owner_account,
  2120. ],
  2121. )
  2122. .unwrap();
  2123. let account: &mut Account = state::unpack(&mut account_account.data).unwrap();
  2124. assert_eq!(account.amount, u64::MAX);
  2125. // transfer to burn victim
  2126. assert_eq!(
  2127. Err(TokenError::Overflow.into()),
  2128. do_process_instruction(
  2129. transfer(
  2130. &program_id,
  2131. &account2_key,
  2132. &account_key,
  2133. &owner2_key,
  2134. &[],
  2135. 1,
  2136. )
  2137. .unwrap(),
  2138. vec![
  2139. &mut account2_account,
  2140. &mut account_account,
  2141. &mut owner2_account,
  2142. ],
  2143. )
  2144. );
  2145. }
  2146. }