counter.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import * as anchor from "@coral-xyz/anchor"
  2. import { Program } from "@coral-xyz/anchor"
  3. import { Counter } from "../target/types/counter"
  4. import { assert } from "chai"
  5. describe("counter", () => {
  6. // Configure the client to use the local cluster.
  7. const provider = anchor.AnchorProvider.env()
  8. anchor.setProvider(provider)
  9. // Generate a new random keypair for the data account.
  10. const dataAccount = anchor.web3.Keypair.generate()
  11. const wallet = provider.wallet
  12. const program = anchor.workspace.Counter as Program<Counter>
  13. it("Is initialized!", async () => {
  14. // Initialize new Counter account
  15. const tx = await program.methods
  16. .new() // wallet.publicKey is the payer for the new account
  17. .accounts({ dataAccount: dataAccount.publicKey })
  18. .signers([dataAccount]) // dataAccount keypair is a required signer because we're using it to create a new account
  19. .rpc()
  20. console.log("Your transaction signature", tx)
  21. // Fetch the counter value
  22. const val = await program.methods
  23. .get()
  24. .accounts({ dataAccount: dataAccount.publicKey })
  25. .view()
  26. assert(Number(val) === 0)
  27. console.log("Count:", Number(val))
  28. })
  29. it("Increment", async () => {
  30. // Increment the counter
  31. const tx = await program.methods
  32. .increment()
  33. .accounts({ dataAccount: dataAccount.publicKey })
  34. .rpc()
  35. console.log("Your transaction signature", tx)
  36. // Fetch the counter value
  37. const val = await program.methods
  38. .get()
  39. .accounts({ dataAccount: dataAccount.publicKey })
  40. .view()
  41. assert(Number(val) === 1)
  42. console.log("Count:", Number(val))
  43. })
  44. })