floats.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import * as anchor from "@project-serum/anchor";
  2. import { Program, getProvider } from "@project-serum/anchor";
  3. import { Keypair, SystemProgram } from "@solana/web3.js";
  4. import { Floats } from "../target/types/floats";
  5. import { assert } from "chai";
  6. describe("floats", () => {
  7. // Configure the client to use the local cluster.
  8. anchor.setProvider(anchor.Provider.env());
  9. const program = anchor.workspace.Floats as Program<Floats>;
  10. it("Creates an account with float data", async () => {
  11. const accountKeypair = Keypair.generate();
  12. await program.methods
  13. .create(1.0, 2.0)
  14. .accounts({
  15. account: accountKeypair.publicKey,
  16. authority: getProvider().wallet.publicKey,
  17. systemProgram: SystemProgram.programId,
  18. })
  19. .signers([accountKeypair])
  20. .rpc();
  21. const account = await program.account.floatDataAccount.fetch(
  22. accountKeypair.publicKey
  23. );
  24. assert.strictEqual(account.dataF32, 1.0);
  25. assert.strictEqual(account.dataF64, 2.0);
  26. });
  27. it("Updates an account with float data", async () => {
  28. const accountKeypair = Keypair.generate();
  29. const authorityPublicKey = getProvider().wallet.publicKey;
  30. await program.methods
  31. .create(1.0, 2.0)
  32. .accounts({
  33. account: accountKeypair.publicKey,
  34. authority: authorityPublicKey,
  35. systemProgram: SystemProgram.programId,
  36. })
  37. .signers([accountKeypair])
  38. .rpc();
  39. let account = await program.account.floatDataAccount.fetch(
  40. accountKeypair.publicKey
  41. );
  42. await program.methods
  43. .update(3.0, 4.0)
  44. .accounts({
  45. account: accountKeypair.publicKey,
  46. authority: authorityPublicKey,
  47. })
  48. .rpc();
  49. account = await program.account.floatDataAccount.fetch(
  50. accountKeypair.publicKey
  51. );
  52. assert.strictEqual(account.dataF32, 3.0);
  53. assert.strictEqual(account.dataF64, 4.0);
  54. });
  55. });