component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Keypair } from "@solana/web3.js";
  2. import {
  3. anchor,
  4. FindEntityPda,
  5. FindComponentPda,
  6. SerializeArgs,
  7. } from "../../../clients/bolt-sdk/lib";
  8. import { assert, expect } from "chai";
  9. export function component(framework) {
  10. let entity: anchor.web3.PublicKey;
  11. let component: anchor.web3.PublicKey;
  12. describe("Component authority", () => {
  13. it("Add entity 5", async () => {
  14. const world = await framework.worldProgram.account.world.fetch(
  15. framework.worldPda,
  16. );
  17. entity = FindEntityPda({
  18. worldId: world.id,
  19. entityId: world.entities,
  20. });
  21. const instruction = await framework.worldProgram.methods
  22. .addEntity(null)
  23. .accounts({
  24. payer: framework.provider.wallet.publicKey,
  25. world: framework.worldPda,
  26. entity: entity,
  27. })
  28. .instruction();
  29. const transaction = new anchor.web3.Transaction().add(instruction);
  30. await framework.provider.sendAndConfirm(transaction);
  31. });
  32. it("Initialize position component with authority on authority test entity", async () => {
  33. const componentId = framework.exampleComponentPosition.programId;
  34. component = FindComponentPda({
  35. componentId,
  36. entity: entity,
  37. });
  38. const instruction = await framework.worldProgram.methods
  39. .initializeComponent()
  40. .accounts({
  41. payer: framework.provider.wallet.publicKey,
  42. entity: entity,
  43. data: component,
  44. componentProgram: componentId,
  45. authority: framework.provider.wallet.publicKey,
  46. })
  47. .instruction();
  48. const transaction = new anchor.web3.Transaction().add(instruction);
  49. await framework.provider.sendAndConfirm(transaction);
  50. });
  51. it("Shouldn't apply fly system on authority test entity with wrong authority", async () => {
  52. const positionBefore =
  53. await framework.exampleComponentPosition.account.position.fetch(
  54. component,
  55. );
  56. const keypair = Keypair.generate();
  57. const instruction = await framework.worldProgram.methods
  58. .apply(SerializeArgs())
  59. .accounts({
  60. authority: keypair.publicKey,
  61. boltSystem: framework.systemFly.programId,
  62. world: framework.worldPda,
  63. })
  64. .remainingAccounts([
  65. {
  66. pubkey: framework.exampleComponentPosition.programId,
  67. isSigner: false,
  68. isWritable: false,
  69. },
  70. {
  71. pubkey: component,
  72. isSigner: false,
  73. isWritable: true,
  74. },
  75. ])
  76. .instruction();
  77. const transaction = new anchor.web3.Transaction().add(instruction);
  78. try {
  79. await framework.provider.sendAndConfirm(transaction, [keypair]);
  80. assert.fail(
  81. "Shouldn't apply fly system on authority test entity with wrong authority",
  82. );
  83. } catch (error) {
  84. expect(error.logs.join("\n")).to.contain(
  85. "Error Code: InvalidAuthority",
  86. );
  87. }
  88. const positionAfter =
  89. await framework.exampleComponentPosition.account.position.fetch(
  90. component,
  91. );
  92. expect(positionBefore.x.toNumber()).to.equal(positionAfter.x.toNumber());
  93. expect(positionBefore.y.toNumber()).to.equal(positionAfter.y.toNumber());
  94. expect(positionBefore.z.toNumber()).to.equal(positionAfter.z.toNumber());
  95. });
  96. it("Apply Fly System on authority test entity should succeed with correct authority", async () => {
  97. const positionBefore =
  98. await framework.exampleComponentPosition.account.position.fetch(
  99. component,
  100. );
  101. const instruction = await framework.worldProgram.methods
  102. .apply(SerializeArgs())
  103. .accounts({
  104. authority: framework.provider.wallet.publicKey,
  105. boltSystem: framework.systemFly.programId,
  106. world: framework.worldPda,
  107. })
  108. .remainingAccounts([
  109. {
  110. pubkey: framework.exampleComponentPosition.programId,
  111. isSigner: false,
  112. isWritable: false,
  113. },
  114. {
  115. pubkey: component,
  116. isSigner: false,
  117. isWritable: true,
  118. },
  119. ])
  120. .instruction();
  121. const transaction = new anchor.web3.Transaction().add(instruction);
  122. await framework.provider.sendAndConfirm(transaction);
  123. const positionAfter =
  124. await framework.exampleComponentPosition.account.position.fetch(
  125. component,
  126. );
  127. expect(positionAfter.x.toNumber()).to.equal(positionBefore.x.toNumber());
  128. expect(positionAfter.y.toNumber()).to.equal(positionBefore.y.toNumber());
  129. expect(positionAfter.z.toNumber()).to.equal(
  130. positionBefore.z.toNumber() + 1,
  131. );
  132. });
  133. });
  134. }