world.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. sessionToken: null,
  115. })
  116. .remainingAccounts([
  117. {
  118. pubkey: framework.exampleComponentPosition.programId,
  119. isSigner: false,
  120. isWritable: false,
  121. },
  122. {
  123. pubkey: framework.componentPositionEntity1Pda,
  124. isSigner: false,
  125. isWritable: true,
  126. },
  127. ])
  128. .instruction();
  129. const transaction = new anchor.web3.Transaction().add(instruction);
  130. await framework.provider.sendAndConfirm(transaction);
  131. });
  132. it("Remove Fly System", async () => {
  133. const instruction = await framework.worldProgram.methods
  134. .removeSystem()
  135. .accounts({
  136. authority: framework.provider.wallet.publicKey,
  137. system: framework.systemFly.programId,
  138. world: framework.worldPda,
  139. })
  140. .instruction();
  141. const transaction = new anchor.web3.Transaction().add(instruction);
  142. await framework.provider.sendAndConfirm(transaction, [], {
  143. skipPreflight: true,
  144. });
  145. // Get World and check permissionless and systems
  146. const worldAccount = await framework.worldProgram.account.world.fetch(
  147. framework.worldPda,
  148. );
  149. expect(worldAccount.permissionless).to.equal(false);
  150. expect(worldAccount.systems.length).to.be.greaterThan(0);
  151. });
  152. it("Apply unauthorized Fly System on Entity 1", async () => {
  153. const instruction = await framework.worldProgram.methods
  154. .apply(SerializeArgs())
  155. .accounts({
  156. authority: framework.provider.wallet.publicKey,
  157. boltSystem: framework.systemFly.programId,
  158. world: framework.worldPda,
  159. sessionToken: null,
  160. })
  161. .remainingAccounts([
  162. {
  163. pubkey: framework.exampleComponentPosition.programId,
  164. isSigner: false,
  165. isWritable: false,
  166. },
  167. {
  168. pubkey: framework.componentPositionEntity1Pda,
  169. isSigner: false,
  170. isWritable: true,
  171. },
  172. ])
  173. .instruction();
  174. const transaction = new anchor.web3.Transaction().add(instruction);
  175. let invalid = false;
  176. try {
  177. await framework.provider.sendAndConfirm(transaction);
  178. } catch (error) {
  179. expect(error.logs.join(" ")).to.contain(
  180. "Error Code: SystemNotApproved",
  181. );
  182. invalid = true;
  183. }
  184. expect(invalid).to.equal(true);
  185. });
  186. it("Check invalid component init without CPI", async () => {
  187. let invalid = false;
  188. try {
  189. await framework.exampleComponentPosition.methods
  190. .initialize()
  191. .accounts({
  192. payer: framework.provider.wallet.publicKey,
  193. data: framework.componentPositionEntity1Pda,
  194. entity: framework.entity1Pda,
  195. authority: framework.provider.wallet.publicKey,
  196. })
  197. .rpc();
  198. } catch (error) {
  199. expect(error.message).to.contain("Error Code: InvalidCaller");
  200. invalid = true;
  201. }
  202. expect(invalid).to.equal(true);
  203. });
  204. it("Check invalid component update without CPI", async () => {
  205. let invalid = false;
  206. try {
  207. await framework.exampleComponentPosition.methods
  208. .update(Buffer.from(""))
  209. .accounts({
  210. boltComponent: framework.componentPositionEntity4Pda,
  211. authority: framework.provider.wallet.publicKey,
  212. sessionToken: null,
  213. })
  214. .rpc();
  215. } catch (error) {
  216. expect(error.message).to.contain("Error Code: InvalidCaller");
  217. invalid = true;
  218. }
  219. expect(invalid).to.equal(true);
  220. });
  221. });
  222. }