world.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { expect } from "chai";
  2. import { anchor, SerializeArgs } from "../../../clients/bolt-sdk/lib";
  3. export function world(framework) {
  4. describe("World authority", () => {
  5. it("Add authority", async () => {
  6. const instruction = await framework.worldProgram.methods
  7. .addAuthority(framework.worldId)
  8. .accounts({
  9. authority: framework.provider.wallet.publicKey,
  10. newAuthority: framework.provider.wallet.publicKey,
  11. world: framework.worldPda,
  12. })
  13. .instruction();
  14. const transaction = new anchor.web3.Transaction().add(instruction);
  15. await framework.provider.sendAndConfirm(transaction, [], {
  16. skipPreflight: true,
  17. });
  18. const worldAccount = await framework.worldProgram.account.world.fetch(
  19. framework.worldPda,
  20. );
  21. expect(
  22. worldAccount.authorities.some((auth) =>
  23. auth.equals(framework.provider.wallet.publicKey),
  24. ),
  25. );
  26. });
  27. it("Add a second authority", async () => {
  28. const instruction = await framework.worldProgram.methods
  29. .addAuthority(framework.worldId)
  30. .accounts({
  31. authority: framework.provider.wallet.publicKey,
  32. newAuthority: framework.secondAuthority,
  33. world: framework.worldPda,
  34. })
  35. .instruction();
  36. const transaction = new anchor.web3.Transaction().add(instruction);
  37. await framework.provider.sendAndConfirm(transaction);
  38. const worldAccount = await framework.worldProgram.account.world.fetch(
  39. framework.worldPda,
  40. );
  41. expect(
  42. worldAccount.authorities.some((auth) =>
  43. auth.equals(framework.secondAuthority),
  44. ),
  45. );
  46. });
  47. it("Remove an authority", async () => {
  48. const instruction = await framework.worldProgram.methods
  49. .removeAuthority(framework.worldId)
  50. .accounts({
  51. authority: framework.provider.wallet.publicKey,
  52. authorityToDelete: framework.secondAuthority,
  53. world: framework.worldPda,
  54. })
  55. .instruction();
  56. const transaction = new anchor.web3.Transaction().add(instruction);
  57. await framework.provider.sendAndConfirm(transaction);
  58. const worldAccount = await framework.worldProgram.account.world.fetch(
  59. framework.worldPda,
  60. );
  61. expect(
  62. !worldAccount.authorities.some((auth) =>
  63. auth.equals(framework.secondAuthority),
  64. ),
  65. );
  66. });
  67. it("Whitelist Fly System", async () => {
  68. const instruction = await framework.worldProgram.methods
  69. .approveSystem()
  70. .accounts({
  71. authority: framework.provider.wallet.publicKey,
  72. system: framework.systemFly.programId,
  73. world: framework.worldPda,
  74. })
  75. .instruction();
  76. const transaction = new anchor.web3.Transaction().add(instruction);
  77. await framework.provider.sendAndConfirm(transaction, [], {
  78. skipPreflight: true,
  79. });
  80. // Get World and check permissionless and systems
  81. const worldAccount = await framework.worldProgram.account.world.fetch(
  82. framework.worldPda,
  83. );
  84. expect(worldAccount.permissionless).to.equal(false);
  85. expect(worldAccount.systems.length).to.be.greaterThan(0);
  86. });
  87. it("Whitelist Apply Velocity System system", async () => {
  88. const instruction = await framework.worldProgram.methods
  89. .approveSystem()
  90. .accounts({
  91. authority: framework.provider.wallet.publicKey,
  92. system: framework.systemApplyVelocity.programId,
  93. world: framework.worldPda,
  94. })
  95. .instruction();
  96. const transaction = new anchor.web3.Transaction().add(instruction);
  97. await framework.provider.sendAndConfirm(transaction, [], {
  98. skipPreflight: true,
  99. });
  100. // Get World and check permissionless and systems
  101. const worldAccount = await framework.worldProgram.account.world.fetch(
  102. framework.worldPda,
  103. );
  104. expect(worldAccount.permissionless).to.equal(false);
  105. expect(worldAccount.systems.length).to.be.greaterThan(0);
  106. });
  107. it("Apply Fly System on Entity 1", async () => {
  108. const instruction = await framework.worldProgram.methods
  109. .apply(SerializeArgs())
  110. .accounts({
  111. authority: framework.provider.wallet.publicKey,
  112. boltSystem: framework.systemFly.programId,
  113. world: framework.worldPda,
  114. })
  115. .remainingAccounts([
  116. {
  117. pubkey: framework.exampleComponentPosition.programId,
  118. isSigner: false,
  119. isWritable: false,
  120. },
  121. {
  122. pubkey: framework.componentPositionEntity1Pda,
  123. isSigner: false,
  124. isWritable: true,
  125. },
  126. ])
  127. .instruction();
  128. const transaction = new anchor.web3.Transaction().add(instruction);
  129. await framework.provider.sendAndConfirm(transaction);
  130. });
  131. it("Remove Fly System", async () => {
  132. const instruction = await framework.worldProgram.methods
  133. .removeSystem()
  134. .accounts({
  135. authority: framework.provider.wallet.publicKey,
  136. system: framework.systemFly.programId,
  137. world: framework.worldPda,
  138. })
  139. .instruction();
  140. const transaction = new anchor.web3.Transaction().add(instruction);
  141. await framework.provider.sendAndConfirm(transaction, [], {
  142. skipPreflight: true,
  143. });
  144. // Get World and check permissionless and systems
  145. const worldAccount = await framework.worldProgram.account.world.fetch(
  146. framework.worldPda,
  147. );
  148. expect(worldAccount.permissionless).to.equal(false);
  149. expect(worldAccount.systems.length).to.be.greaterThan(0);
  150. });
  151. it("Apply unauthorized Fly System on Entity 1", async () => {
  152. const instruction = await framework.worldProgram.methods
  153. .apply(SerializeArgs())
  154. .accounts({
  155. authority: framework.provider.wallet.publicKey,
  156. boltSystem: framework.systemFly.programId,
  157. world: framework.worldPda,
  158. })
  159. .remainingAccounts([
  160. {
  161. pubkey: framework.exampleComponentPosition.programId,
  162. isSigner: false,
  163. isWritable: false,
  164. },
  165. {
  166. pubkey: framework.componentPositionEntity1Pda,
  167. isSigner: false,
  168. isWritable: true,
  169. },
  170. ])
  171. .instruction();
  172. const transaction = new anchor.web3.Transaction().add(instruction);
  173. let invalid = false;
  174. try {
  175. await framework.provider.sendAndConfirm(transaction);
  176. } catch (error) {
  177. expect(error.logs.join(" ")).to.contain(
  178. "Error Code: SystemNotApproved",
  179. );
  180. invalid = true;
  181. }
  182. expect(invalid).to.equal(true);
  183. });
  184. it("Check invalid component init without CPI", async () => {
  185. let invalid = false;
  186. try {
  187. await framework.exampleComponentPosition.methods
  188. .initialize()
  189. .accounts({
  190. payer: framework.provider.wallet.publicKey,
  191. data: framework.componentPositionEntity1Pda,
  192. entity: framework.entity1Pda,
  193. authority: framework.provider.wallet.publicKey,
  194. })
  195. .rpc();
  196. } catch (error) {
  197. expect(error.message).to.contain("Error Code: InvalidCaller");
  198. invalid = true;
  199. }
  200. expect(invalid).to.equal(true);
  201. });
  202. it("Check invalid component update without CPI", async () => {
  203. let invalid = false;
  204. try {
  205. await framework.exampleComponentPosition.methods
  206. .update(Buffer.from(""))
  207. .accounts({
  208. boltComponent: framework.componentPositionEntity4Pda,
  209. authority: framework.provider.wallet.publicKey,
  210. })
  211. .rpc();
  212. } catch (error) {
  213. expect(error.message).to.contain("Error Code: InvalidCaller");
  214. invalid = true;
  215. }
  216. expect(invalid).to.equal(true);
  217. });
  218. });
  219. }