bolt.intermediate-level.api.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. import { Keypair, type PublicKey } from "@solana/web3.js";
  2. import { type Position } from "../target/types/position";
  3. import { type Velocity } from "../target/types/velocity";
  4. import { type BoltComponent } from "../target/types/bolt_component";
  5. import { type SystemSimpleMovement } from "../target/types/system_simple_movement";
  6. import { type SystemFly } from "../target/types/system_fly";
  7. import { type SystemApplyVelocity } from "../target/types/system_apply_velocity";
  8. import { expect } from "chai";
  9. import type BN from "bn.js";
  10. import {
  11. AddEntity,
  12. DELEGATION_PROGRAM_ID,
  13. InitializeRegistry,
  14. InitializeComponent,
  15. InitializeNewWorld,
  16. ApplySystem,
  17. DelegateComponent,
  18. AddAuthority,
  19. RemoveAuthority,
  20. ApproveSystem,
  21. RemoveSystem,
  22. type Program,
  23. anchor,
  24. web3,
  25. WORLD_PROGRAM_IDL as World,
  26. } from "../clients/bolt-sdk";
  27. enum Direction {
  28. Left = "Left",
  29. Right = "Right",
  30. Up = "Up",
  31. Down = "Down",
  32. }
  33. function padCenter(value: string, width: number) {
  34. const length = value.length;
  35. if (width <= length) {
  36. return value;
  37. }
  38. const padding = (width - length) / 2;
  39. const align = width - padding;
  40. return value.padStart(align, " ").padEnd(width, " ");
  41. }
  42. function logPosition(title: string, { x, y, z }: { x: BN; y: BN; z: BN }) {
  43. console.log(" +----------------------------------+");
  44. console.log(` | ${padCenter(title, 32)} |`);
  45. console.log(" +-----------------+----------------+");
  46. console.log(` | X Position | ${String(x).padEnd(14, " ")} |`);
  47. console.log(` | Y Position | ${String(y).padEnd(14, " ")} |`);
  48. console.log(` | Z Position | ${String(z).padEnd(14, " ")} |`);
  49. console.log(" +-----------------+----------------+");
  50. }
  51. function logVelocity(
  52. title: string,
  53. { x, y, z, lastApplied }: { x: BN; y: BN; z: BN; lastApplied: BN },
  54. ) {
  55. console.log(" +----------------------------------+");
  56. console.log(` | ${padCenter(title, 32)} |`);
  57. console.log(" +-----------------+----------------+");
  58. console.log(` | X Velocity | ${String(x).padEnd(14, " ")} |`);
  59. console.log(` | Y Velocity | ${String(y).padEnd(14, " ")} |`);
  60. console.log(` | Z Velocity | ${String(z).padEnd(14, " ")} |`);
  61. console.log(` | Last Applied | ${String(lastApplied).padEnd(14, " ")} |`);
  62. console.log(" +-----------------+----------------+");
  63. }
  64. describe("bolt", () => {
  65. const provider = anchor.AnchorProvider.env();
  66. anchor.setProvider(provider);
  67. const worldProgram = anchor.workspace.World as Program<World>;
  68. const boltComponentProgram = anchor.workspace
  69. .BoltComponent as Program<BoltComponent>;
  70. const exampleComponentPosition = anchor.workspace
  71. .Position as Program<Position>;
  72. const exampleComponentVelocity = anchor.workspace
  73. .Velocity as Program<Velocity>;
  74. const exampleSystemSimpleMovement = (
  75. anchor.workspace.SystemSimpleMovement as Program<SystemSimpleMovement>
  76. ).programId;
  77. const exampleSystemFly = (anchor.workspace.SystemFly as Program<SystemFly>)
  78. .programId;
  79. const exampleSystemApplyVelocity = (
  80. anchor.workspace.SystemApplyVelocity as Program<SystemApplyVelocity>
  81. ).programId;
  82. let worldPda: PublicKey;
  83. let entity1Pda: PublicKey;
  84. let entity2Pda: PublicKey;
  85. let entity4Pda: PublicKey;
  86. let entity5Pda: PublicKey;
  87. let componentPositionEntity1Pda: PublicKey;
  88. let componentVelocityEntity1Pda: PublicKey;
  89. let componentPositionEntity4Pda: PublicKey;
  90. let componentPositionEntity5Pda: PublicKey;
  91. const secondAuthority = Keypair.generate().publicKey;
  92. it("InitializeRegistry", async () => {
  93. const initializeRegistry = await InitializeRegistry({
  94. payer: provider.wallet.publicKey,
  95. connection: provider.connection,
  96. });
  97. try {
  98. await provider.sendAndConfirm(initializeRegistry.transaction);
  99. } catch (error) {
  100. // This is expected to fail because the registry already exists if another api level test ran before
  101. }
  102. });
  103. it("InitializeNewWorld", async () => {
  104. const initializeNewWorld = await InitializeNewWorld({
  105. payer: provider.wallet.publicKey,
  106. connection: provider.connection,
  107. });
  108. const signature = await provider.sendAndConfirm(
  109. initializeNewWorld.transaction,
  110. );
  111. console.log("InitializeNewWorld signature: ", signature);
  112. worldPda = initializeNewWorld.worldPda; // Saved for later
  113. });
  114. it("Add authority", async () => {
  115. const addAuthority = await AddAuthority({
  116. authority: provider.wallet.publicKey,
  117. newAuthority: provider.wallet.publicKey,
  118. world: worldPda,
  119. connection: provider.connection,
  120. });
  121. await provider.sendAndConfirm(addAuthority.transaction, [], {
  122. skipPreflight: true,
  123. });
  124. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  125. expect(
  126. worldAccount.authorities.some((auth) =>
  127. auth.equals(provider.wallet.publicKey),
  128. ),
  129. );
  130. });
  131. it("Add a second authority", async () => {
  132. const addAuthority = await AddAuthority({
  133. authority: provider.wallet.publicKey,
  134. newAuthority: secondAuthority,
  135. world: worldPda,
  136. connection: provider.connection,
  137. });
  138. const signature = await provider.sendAndConfirm(addAuthority.transaction);
  139. console.log(`Add Authority signature: ${signature}`);
  140. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  141. expect(
  142. worldAccount.authorities.some((auth) => auth.equals(secondAuthority)),
  143. );
  144. });
  145. it("Remove an authority", async () => {
  146. const addAuthority = await RemoveAuthority({
  147. authority: provider.wallet.publicKey,
  148. authorityToDelete: secondAuthority,
  149. world: worldPda,
  150. connection: provider.connection,
  151. });
  152. const signature = await provider.sendAndConfirm(addAuthority.transaction);
  153. console.log(`Add Authority signature: ${signature}`);
  154. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  155. expect(
  156. !worldAccount.authorities.some((auth) => auth.equals(secondAuthority)),
  157. );
  158. });
  159. it("InitializeNewWorld 2", async () => {
  160. const initializeNewWorld = await InitializeNewWorld({
  161. payer: provider.wallet.publicKey,
  162. connection: provider.connection,
  163. });
  164. await provider.sendAndConfirm(initializeNewWorld.transaction);
  165. });
  166. it("Add entity 1", async () => {
  167. const addEntity = await AddEntity({
  168. payer: provider.wallet.publicKey,
  169. world: worldPda,
  170. connection: provider.connection,
  171. });
  172. await provider.sendAndConfirm(addEntity.transaction);
  173. entity1Pda = addEntity.entityPda; // Saved for later
  174. });
  175. it("Add entity 2", async () => {
  176. const addEntity = await AddEntity({
  177. payer: provider.wallet.publicKey,
  178. world: worldPda,
  179. connection: provider.connection,
  180. });
  181. await provider.sendAndConfirm(addEntity.transaction);
  182. entity2Pda = addEntity.entityPda; // Saved for later
  183. });
  184. it("Add entity 3", async () => {
  185. const addEntity = await AddEntity({
  186. payer: provider.wallet.publicKey,
  187. world: worldPda,
  188. connection: provider.connection,
  189. });
  190. await provider.sendAndConfirm(addEntity.transaction);
  191. });
  192. it("Add entity 4 (with seed)", async () => {
  193. const addEntity = await AddEntity({
  194. payer: provider.wallet.publicKey,
  195. world: worldPda,
  196. seed: Buffer.from("custom-seed"),
  197. connection: provider.connection,
  198. });
  199. await provider.sendAndConfirm(addEntity.transaction);
  200. entity4Pda = addEntity.entityPda;
  201. });
  202. it("Add entity 5", async () => {
  203. const addEntity = await AddEntity({
  204. payer: provider.wallet.publicKey,
  205. world: worldPda,
  206. connection: provider.connection,
  207. });
  208. await provider.sendAndConfirm(addEntity.transaction);
  209. entity5Pda = addEntity.entityPda; // Saved for later
  210. });
  211. it("Initialize Original Component on Entity 1, trough the world instance", async () => {
  212. const initializeComponent = await InitializeComponent({
  213. payer: provider.wallet.publicKey,
  214. entity: entity1Pda,
  215. seed: "origin-component",
  216. componentId: boltComponentProgram.programId,
  217. });
  218. await provider.sendAndConfirm(initializeComponent.transaction);
  219. });
  220. it("Initialize Original Component on Entity 2, trough the world instance", async () => {
  221. const initializeComponent = await InitializeComponent({
  222. payer: provider.wallet.publicKey,
  223. entity: entity2Pda,
  224. seed: "origin-component",
  225. componentId: boltComponentProgram.programId,
  226. });
  227. await provider.sendAndConfirm(initializeComponent.transaction);
  228. });
  229. it("Initialize Position Component on Entity 1", async () => {
  230. const initializeComponent = await InitializeComponent({
  231. payer: provider.wallet.publicKey,
  232. entity: entity1Pda,
  233. componentId: exampleComponentPosition.programId,
  234. });
  235. await provider.sendAndConfirm(initializeComponent.transaction);
  236. componentPositionEntity1Pda = initializeComponent.componentPda; // Saved for later
  237. });
  238. it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
  239. const initializeComponent = await InitializeComponent({
  240. payer: provider.wallet.publicKey,
  241. entity: entity1Pda,
  242. componentId: exampleComponentVelocity.programId,
  243. seed: "component-velocity",
  244. });
  245. await provider.sendAndConfirm(initializeComponent.transaction);
  246. componentVelocityEntity1Pda = initializeComponent.componentPda; // Saved for later
  247. });
  248. it("Initialize Position Component on Entity 2", async () => {
  249. const initializeComponent = await InitializeComponent({
  250. payer: provider.wallet.publicKey,
  251. entity: entity2Pda,
  252. componentId: exampleComponentPosition.programId,
  253. });
  254. await provider.sendAndConfirm(initializeComponent.transaction);
  255. });
  256. it("Initialize Position Component on Entity 4", async () => {
  257. const initializeComponent = await InitializeComponent({
  258. payer: provider.wallet.publicKey,
  259. entity: entity4Pda,
  260. componentId: exampleComponentPosition.programId,
  261. });
  262. await provider.sendAndConfirm(initializeComponent.transaction);
  263. componentPositionEntity4Pda = initializeComponent.componentPda; // Saved for later
  264. });
  265. it("Initialize Position Component on Entity 5 (with authority)", async () => {
  266. const initializeComponent = await InitializeComponent({
  267. payer: provider.wallet.publicKey,
  268. entity: entity5Pda,
  269. componentId: exampleComponentPosition.programId,
  270. authority: provider.wallet.publicKey,
  271. });
  272. await provider.sendAndConfirm(initializeComponent.transaction);
  273. componentPositionEntity5Pda = initializeComponent.componentPda; // Saved for later
  274. });
  275. it("Check Position on Entity 1 is default", async () => {
  276. const position = await exampleComponentPosition.account.position.fetch(
  277. componentPositionEntity1Pda,
  278. );
  279. logPosition("Default State: Entity 1", position);
  280. expect(position.x.toNumber()).to.equal(0);
  281. expect(position.y.toNumber()).to.equal(0);
  282. expect(position.z.toNumber()).to.equal(0);
  283. });
  284. it("Apply Simple Movement System (Up) on Entity 1 using Apply", async () => {
  285. const apply = await ApplySystem({
  286. authority: provider.wallet.publicKey,
  287. systemId: exampleSystemSimpleMovement,
  288. entities: [
  289. {
  290. entity: entity1Pda,
  291. components: [{ componentId: exampleComponentPosition.programId }],
  292. },
  293. ],
  294. world: worldPda,
  295. args: { direction: Direction.Up },
  296. });
  297. await provider.sendAndConfirm(apply.transaction);
  298. const position = await exampleComponentPosition.account.position.fetch(
  299. componentPositionEntity1Pda,
  300. );
  301. logPosition("Movement System: Entity 1", position);
  302. expect(position.x.toNumber()).to.equal(0);
  303. expect(position.y.toNumber()).to.equal(1);
  304. expect(position.z.toNumber()).to.equal(0);
  305. });
  306. it("Apply Simple Movement System (Up) on Entity 1", async () => {
  307. const applySystem = await ApplySystem({
  308. authority: provider.wallet.publicKey,
  309. systemId: exampleSystemSimpleMovement,
  310. world: worldPda,
  311. entities: [
  312. {
  313. entity: entity1Pda,
  314. components: [{ componentId: exampleComponentPosition.programId }],
  315. },
  316. ],
  317. args: {
  318. direction: Direction.Up,
  319. },
  320. });
  321. const signature = await provider.sendAndConfirm(
  322. applySystem.transaction,
  323. [],
  324. { skipPreflight: true },
  325. );
  326. console.log(`Signature: ${signature}`);
  327. const position = await exampleComponentPosition.account.position.fetch(
  328. componentPositionEntity1Pda,
  329. );
  330. logPosition("Movement System: Entity 1", position);
  331. expect(position.x.toNumber()).to.equal(0);
  332. expect(position.y.toNumber()).to.equal(2);
  333. expect(position.z.toNumber()).to.equal(0);
  334. });
  335. it("Apply Simple Movement System (Right) on Entity 1", async () => {
  336. const applySystem = await ApplySystem({
  337. authority: provider.wallet.publicKey,
  338. systemId: exampleSystemSimpleMovement,
  339. world: worldPda,
  340. entities: [
  341. {
  342. entity: entity1Pda,
  343. components: [{ componentId: exampleComponentPosition.programId }],
  344. },
  345. ],
  346. args: {
  347. direction: Direction.Right,
  348. },
  349. });
  350. await provider.sendAndConfirm(applySystem.transaction);
  351. const position = await exampleComponentPosition.account.position.fetch(
  352. componentPositionEntity1Pda,
  353. );
  354. logPosition("Movement System: Entity 1", position);
  355. expect(position.x.toNumber()).to.equal(1);
  356. expect(position.y.toNumber()).to.equal(2);
  357. expect(position.z.toNumber()).to.equal(0);
  358. });
  359. it("Apply Fly System on Entity 1", async () => {
  360. const applySystem = await ApplySystem({
  361. authority: provider.wallet.publicKey,
  362. systemId: exampleSystemFly,
  363. world: worldPda,
  364. entities: [
  365. {
  366. entity: entity1Pda,
  367. components: [{ componentId: exampleComponentPosition.programId }],
  368. },
  369. ],
  370. });
  371. await provider.sendAndConfirm(applySystem.transaction);
  372. const position = await exampleComponentPosition.account.position.fetch(
  373. componentPositionEntity1Pda,
  374. );
  375. logPosition("Fly System: Entity 1", position);
  376. expect(position.x.toNumber()).to.equal(1);
  377. expect(position.y.toNumber()).to.equal(2);
  378. expect(position.z.toNumber()).to.equal(1);
  379. });
  380. it("Apply System Velocity on Entity 1", async () => {
  381. const applySystem = await ApplySystem({
  382. authority: provider.wallet.publicKey,
  383. systemId: exampleSystemApplyVelocity,
  384. world: worldPda,
  385. entities: [
  386. {
  387. entity: entity1Pda,
  388. components: [
  389. {
  390. componentId: exampleComponentVelocity.programId,
  391. seed: "component-velocity",
  392. },
  393. { componentId: exampleComponentPosition.programId },
  394. ],
  395. },
  396. ],
  397. });
  398. await provider.sendAndConfirm(applySystem.transaction);
  399. const velocity = await exampleComponentVelocity.account.velocity.fetch(
  400. componentVelocityEntity1Pda,
  401. );
  402. logVelocity("Apply System Velocity: Entity 1", velocity);
  403. expect(velocity.x.toNumber()).to.equal(10);
  404. expect(velocity.y.toNumber()).to.equal(0);
  405. expect(velocity.z.toNumber()).to.equal(0);
  406. expect(velocity.lastApplied.toNumber()).to.not.equal(0);
  407. const position = await exampleComponentPosition.account.position.fetch(
  408. componentPositionEntity1Pda,
  409. );
  410. logPosition("Apply System Velocity: Entity 1", position);
  411. expect(position.x.toNumber()).to.greaterThan(1);
  412. expect(position.y.toNumber()).to.equal(2);
  413. expect(position.z.toNumber()).to.equal(1);
  414. });
  415. it("Apply System Velocity on Entity 1, with Clock external account", async () => {
  416. const applySystem = await ApplySystem({
  417. authority: provider.wallet.publicKey,
  418. systemId: exampleSystemApplyVelocity,
  419. world: worldPda,
  420. entities: [
  421. {
  422. entity: entity1Pda,
  423. components: [
  424. {
  425. componentId: exampleComponentVelocity.programId,
  426. seed: "component-velocity",
  427. },
  428. { componentId: exampleComponentPosition.programId },
  429. ],
  430. },
  431. ],
  432. extraAccounts: [
  433. {
  434. pubkey: new web3.PublicKey(
  435. "SysvarC1ock11111111111111111111111111111111",
  436. ),
  437. isWritable: false,
  438. isSigner: false,
  439. },
  440. ],
  441. });
  442. await provider.sendAndConfirm(applySystem.transaction);
  443. const position = await exampleComponentPosition.account.position.fetch(
  444. componentPositionEntity1Pda,
  445. );
  446. logPosition("Apply System Velocity: Entity 1", position);
  447. expect(position.x.toNumber()).to.greaterThan(1);
  448. expect(position.y.toNumber()).to.equal(2);
  449. expect(position.z.toNumber()).to.equal(300);
  450. });
  451. it("Apply Fly System on Entity 4", async () => {
  452. const applySystem = await ApplySystem({
  453. authority: provider.wallet.publicKey,
  454. systemId: exampleSystemFly,
  455. world: worldPda,
  456. entities: [
  457. {
  458. entity: entity4Pda,
  459. components: [{ componentId: exampleComponentPosition.programId }],
  460. },
  461. ],
  462. });
  463. await provider.sendAndConfirm(applySystem.transaction);
  464. const position = await exampleComponentPosition.account.position.fetch(
  465. componentPositionEntity4Pda,
  466. );
  467. logPosition("Fly System: Entity 4", position);
  468. expect(position.x.toNumber()).to.equal(0);
  469. expect(position.y.toNumber()).to.equal(0);
  470. expect(position.z.toNumber()).to.equal(1);
  471. });
  472. it("Apply Fly System on Entity 5 (should fail with wrong authority)", async () => {
  473. const positionBefore =
  474. await exampleComponentPosition.account.position.fetch(
  475. componentPositionEntity5Pda,
  476. );
  477. const applySystem = await ApplySystem({
  478. authority: provider.wallet.publicKey,
  479. systemId: exampleSystemFly,
  480. world: worldPda,
  481. entities: [
  482. {
  483. entity: entity5Pda,
  484. components: [{ componentId: exampleComponentPosition.programId }],
  485. },
  486. ],
  487. });
  488. let failed = false;
  489. try {
  490. await provider.sendAndConfirm(applySystem.transaction);
  491. } catch (error) {
  492. failed = true;
  493. // console.log("error", error);
  494. expect(error.logs.join("\n")).to.contain("Error Code: InvalidAuthority");
  495. }
  496. expect(failed).to.equal(true);
  497. const positionAfter = await exampleComponentPosition.account.position.fetch(
  498. componentPositionEntity5Pda,
  499. );
  500. expect(positionBefore.x.toNumber()).to.equal(positionAfter.x.toNumber());
  501. expect(positionBefore.y.toNumber()).to.equal(positionAfter.y.toNumber());
  502. expect(positionBefore.z.toNumber()).to.equal(positionAfter.z.toNumber());
  503. });
  504. it("Whitelist System", async () => {
  505. const approveSystem = await ApproveSystem({
  506. authority: provider.wallet.publicKey,
  507. systemToApprove: exampleSystemFly,
  508. world: worldPda,
  509. });
  510. const signature = await provider.sendAndConfirm(
  511. approveSystem.transaction,
  512. [],
  513. { skipPreflight: true },
  514. );
  515. console.log(`Whitelist 2 system approval signature: ${signature}`);
  516. // Get World and check permissionless and systems
  517. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  518. expect(worldAccount.permissionless).to.equal(false);
  519. expect(worldAccount.systems.length).to.be.greaterThan(0);
  520. });
  521. it("Whitelist System 2", async () => {
  522. const approveSystem = await ApproveSystem({
  523. authority: provider.wallet.publicKey,
  524. systemToApprove: exampleSystemApplyVelocity,
  525. world: worldPda,
  526. });
  527. const signature = await provider.sendAndConfirm(
  528. approveSystem.transaction,
  529. [],
  530. { skipPreflight: true },
  531. );
  532. console.log(`Whitelist 2 system approval signature: ${signature}`);
  533. // Get World and check permissionless and systems
  534. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  535. expect(worldAccount.permissionless).to.equal(false);
  536. expect(worldAccount.systems.length).to.be.greaterThan(0);
  537. });
  538. it("Apply Fly System on Entity 1", async () => {
  539. const applySystem = await ApplySystem({
  540. authority: provider.wallet.publicKey,
  541. systemId: exampleSystemFly,
  542. world: worldPda,
  543. entities: [
  544. {
  545. entity: entity1Pda,
  546. components: [{ componentId: exampleComponentPosition.programId }],
  547. },
  548. ],
  549. });
  550. await provider.sendAndConfirm(applySystem.transaction);
  551. });
  552. it("Remove System 1", async () => {
  553. const approveSystem = await RemoveSystem({
  554. authority: provider.wallet.publicKey,
  555. systemToRemove: exampleSystemFly,
  556. world: worldPda,
  557. });
  558. const signature = await provider.sendAndConfirm(
  559. approveSystem.transaction,
  560. [],
  561. { skipPreflight: true },
  562. );
  563. console.log(`Whitelist 2 system approval signature: ${signature}`);
  564. // Get World and check permissionless and systems
  565. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  566. expect(worldAccount.permissionless).to.equal(false);
  567. expect(worldAccount.systems.length).to.be.greaterThan(0);
  568. });
  569. it("Apply Invalid Fly System on Entity 1", async () => {
  570. const applySystem = await ApplySystem({
  571. authority: provider.wallet.publicKey,
  572. systemId: exampleSystemFly,
  573. world: worldPda,
  574. entities: [
  575. {
  576. entity: entity1Pda,
  577. components: [{ componentId: exampleComponentPosition.programId }],
  578. },
  579. ],
  580. });
  581. let invalid = false;
  582. try {
  583. await provider.sendAndConfirm(applySystem.transaction);
  584. } catch (error) {
  585. expect(error.logs.join(" ")).to.contain("Error Code: SystemNotApproved");
  586. invalid = true;
  587. }
  588. expect(invalid).to.equal(true);
  589. });
  590. it("Check invalid component init without CPI", async () => {
  591. let invalid = false;
  592. try {
  593. await exampleComponentPosition.methods
  594. .initialize()
  595. .accounts({
  596. payer: provider.wallet.publicKey,
  597. data: componentPositionEntity5Pda,
  598. entity: entity5Pda,
  599. authority: provider.wallet.publicKey,
  600. })
  601. .rpc();
  602. } catch (error) {
  603. // console.log("error", error);
  604. expect(error.message).to.contain("Error Code: InvalidCaller");
  605. invalid = true;
  606. }
  607. expect(invalid).to.equal(true);
  608. });
  609. it("Check invalid component update without CPI", async () => {
  610. let invalid = false;
  611. try {
  612. await boltComponentProgram.methods
  613. .update(Buffer.from(""))
  614. .accounts({
  615. boltComponent: componentPositionEntity4Pda,
  616. authority: provider.wallet.publicKey,
  617. })
  618. .rpc();
  619. } catch (error) {
  620. // console.log("error", error);
  621. expect(error.message).to.contain(
  622. "bolt_component. Error Code: AccountOwnedByWrongProgram",
  623. );
  624. invalid = true;
  625. }
  626. expect(invalid).to.equal(true);
  627. });
  628. it("Check component delegation", async () => {
  629. const delegateComponent = await DelegateComponent({
  630. payer: provider.wallet.publicKey,
  631. entity: entity1Pda,
  632. componentId: exampleComponentPosition.programId,
  633. });
  634. const txSign = await provider.sendAndConfirm(
  635. delegateComponent.transaction,
  636. [],
  637. { skipPreflight: true, commitment: "confirmed" },
  638. );
  639. const acc = await provider.connection.getAccountInfo(
  640. delegateComponent.componentPda,
  641. );
  642. expect(acc?.owner.toBase58()).to.equal(DELEGATION_PROGRAM_ID.toBase58());
  643. });
  644. });