favorites.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { assert } from 'chai';
  4. import type { FavoritesProgram } from '../target/types/favorites_program';
  5. const web3 = anchor.web3;
  6. describe('Favorites', () => {
  7. // Use the cluster and the keypair from Anchor.toml
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. // See https://github.com/coral-xyz/anchor/issues/3122
  11. const user = (provider.wallet as anchor.Wallet).payer;
  12. const someRandomGuy = anchor.web3.Keypair.generate();
  13. const program = anchor.workspace.FavoritesProgram as Program<FavoritesProgram>;
  14. // Here"s what we want to write to the blockchain
  15. const favoriteNumber = new anchor.BN(23);
  16. const favoriteColor = 'purple';
  17. const favoriteHobbies = ['skiing', 'skydiving', 'biking'];
  18. // We don"t need to airdrop if we"re using the local cluster
  19. // because the local cluster gives us 85 billion dollars worth of SOL
  20. before(async () => {
  21. const balance = await provider.connection.getBalance(user.publicKey);
  22. const balanceInSOL = balance / web3.LAMPORTS_PER_SOL;
  23. const formattedBalance = new Intl.NumberFormat().format(balanceInSOL);
  24. console.log(`Balance: ${formattedBalance} SOL`);
  25. });
  26. it('Writes our favorites to the blockchain', async () => {
  27. await program.methods
  28. // set_favourites in Rust becomes setFavorites in TypeScript
  29. .setFavorites(favoriteNumber, favoriteColor, favoriteHobbies)
  30. // Sign the transaction
  31. .signers([user])
  32. // Send the transaction to the cluster or RPC
  33. .rpc();
  34. // Find the PDA for the user"s favorites
  35. const favoritesPdaAndBump = web3.PublicKey.findProgramAddressSync([Buffer.from('favorites'), user.publicKey.toBuffer()], program.programId);
  36. const favoritesPda = favoritesPdaAndBump[0];
  37. const dataFromPda = await program.account.favorites.fetch(favoritesPda);
  38. // And make sure it matches!
  39. assert.equal(dataFromPda.color, favoriteColor);
  40. // A little extra work to make sure the BNs are equal
  41. assert.equal(dataFromPda.number.toString(), favoriteNumber.toString());
  42. // And check the hobbies too
  43. assert.deepEqual(dataFromPda.hobbies, favoriteHobbies);
  44. });
  45. it('Updates the favorites', async () => {
  46. const newFavoriteHobbies = ['skiing', 'skydiving', 'biking', 'swimming'];
  47. try {
  48. const signature = await program.methods.setFavorites(favoriteNumber, favoriteColor, newFavoriteHobbies).signers([user]).rpc();
  49. console.log(`Transaction signature: ${signature}`);
  50. } catch (error) {
  51. const errorMessage = (error as Error).message;
  52. assert.isTrue(errorMessage.includes('SendTransactionError'));
  53. }
  54. });
  55. it('Rejects transactions from unauthorized signers', async () => {
  56. try {
  57. await program.methods
  58. // set_favourites in Rust becomes setFavorites in TypeScript
  59. .setFavorites(favoriteNumber, favoriteColor, favoriteHobbies)
  60. // Sign the transaction
  61. .signers([someRandomGuy])
  62. // Send the transaction to the cluster or RPC
  63. .rpc();
  64. } catch (error) {
  65. const errorMessage = (error as Error).message;
  66. assert.isTrue(errorMessage.includes('unknown signer'));
  67. }
  68. });
  69. });