favorites.test.ts 3.3 KB

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