floats.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program, getProvider } from "@coral-xyz/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. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const program = anchor.workspace.Floats as Program<Floats>;
  11. it("Creates an account with float data", async () => {
  12. const accountKeypair = Keypair.generate();
  13. await program.methods
  14. .create(1.0, 2.0)
  15. .accounts({
  16. account: accountKeypair.publicKey,
  17. authority: provider.wallet.publicKey,
  18. systemProgram: SystemProgram.programId,
  19. })
  20. .signers([accountKeypair])
  21. .rpc();
  22. const account = await program.account.floatDataAccount.fetch(
  23. accountKeypair.publicKey
  24. );
  25. assert.strictEqual(account.dataF32, 1.0);
  26. assert.strictEqual(account.dataF64, 2.0);
  27. });
  28. it("Updates an account with float data", async () => {
  29. const accountKeypair = Keypair.generate();
  30. const authorityPublicKey = provider.wallet.publicKey;
  31. await program.methods
  32. .create(1.0, 2.0)
  33. .accounts({
  34. account: accountKeypair.publicKey,
  35. authority: authorityPublicKey,
  36. systemProgram: SystemProgram.programId,
  37. })
  38. .signers([accountKeypair])
  39. .rpc();
  40. let account = await program.account.floatDataAccount.fetch(
  41. accountKeypair.publicKey
  42. );
  43. await program.methods
  44. .update(3.0, 4.0)
  45. .accounts({
  46. account: accountKeypair.publicKey,
  47. authority: authorityPublicKey,
  48. })
  49. .rpc();
  50. account = await program.account.floatDataAccount.fetch(
  51. accountKeypair.publicKey
  52. );
  53. assert.strictEqual(account.dataF32, 3.0);
  54. assert.strictEqual(account.dataF64, 4.0);
  55. });
  56. });