bolt.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. "use strict";
  2. var __createBinding =
  3. (this && this.__createBinding) ||
  4. (Object.create
  5. ? function (o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. var desc = Object.getOwnPropertyDescriptor(m, k);
  8. if (
  9. !desc ||
  10. ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
  11. ) {
  12. desc = {
  13. enumerable: true,
  14. get: function () {
  15. return m[k];
  16. },
  17. };
  18. }
  19. Object.defineProperty(o, k2, desc);
  20. }
  21. : function (o, m, k, k2) {
  22. if (k2 === undefined) k2 = k;
  23. o[k2] = m[k];
  24. });
  25. var __setModuleDefault =
  26. (this && this.__setModuleDefault) ||
  27. (Object.create
  28. ? function (o, v) {
  29. Object.defineProperty(o, "default", { enumerable: true, value: v });
  30. }
  31. : function (o, v) {
  32. o["default"] = v;
  33. });
  34. var __importStar =
  35. (this && this.__importStar) ||
  36. function (mod) {
  37. if (mod && mod.__esModule) return mod;
  38. var result = {};
  39. if (mod != null)
  40. for (var k in mod)
  41. if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
  42. __createBinding(result, mod, k);
  43. __setModuleDefault(result, mod);
  44. return result;
  45. };
  46. var __awaiter =
  47. (this && this.__awaiter) ||
  48. function (thisArg, _arguments, P, generator) {
  49. function adopt(value) {
  50. return value instanceof P
  51. ? value
  52. : new P(function (resolve) {
  53. resolve(value);
  54. });
  55. }
  56. return new (P || (P = Promise))(function (resolve, reject) {
  57. function fulfilled(value) {
  58. try {
  59. step(generator.next(value));
  60. } catch (e) {
  61. reject(e);
  62. }
  63. }
  64. function rejected(value) {
  65. try {
  66. step(generator["throw"](value));
  67. } catch (e) {
  68. reject(e);
  69. }
  70. }
  71. function step(result) {
  72. result.done
  73. ? resolve(result.value)
  74. : adopt(result.value).then(fulfilled, rejected);
  75. }
  76. step((generator = generator.apply(thisArg, _arguments || [])).next());
  77. });
  78. };
  79. var __importDefault =
  80. (this && this.__importDefault) ||
  81. function (mod) {
  82. return mod && mod.__esModule ? mod : { default: mod };
  83. };
  84. Object.defineProperty(exports, "__esModule", { value: true });
  85. const anchor = __importStar(require("@coral-xyz/anchor"));
  86. const web3_js_1 = require("@solana/web3.js");
  87. const chai_1 = require("chai");
  88. const bn_js_1 = __importDefault(require("bn.js"));
  89. var Direction;
  90. (function (Direction) {
  91. Direction["Left"] = "Left";
  92. Direction["Right"] = "Right";
  93. Direction["Up"] = "Up";
  94. Direction["Down"] = "Down";
  95. })(Direction || (Direction = {}));
  96. function serializeArgs(args = {}) {
  97. const jsonString = JSON.stringify(args);
  98. const encoder = new TextEncoder();
  99. const binaryData = encoder.encode(jsonString);
  100. return Buffer.from(
  101. binaryData.buffer,
  102. binaryData.byteOffset,
  103. binaryData.byteLength
  104. );
  105. }
  106. describe("bolt", () => {
  107. const provider = anchor.AnchorProvider.env();
  108. anchor.setProvider(provider);
  109. const worldProgram = anchor.workspace.World;
  110. const boltComponentPositionProgram = anchor.workspace.ComponentPosition;
  111. const boltComponentVelocityProgram = anchor.workspace.ComponentVelocity;
  112. const boltComponentProgramOrigin = anchor.workspace.BoltComponent;
  113. const systemSimpleMovement = anchor.workspace.SystemSimpleMovement.programId;
  114. const systemFly = anchor.workspace.SystemFly.programId;
  115. const applyVelocity = anchor.workspace.SystemApplyVelocity.programId;
  116. let entity1;
  117. let entity2;
  118. let componentPositionEntity1;
  119. let componentPositionEntity2;
  120. let componentVelocityEntity1;
  121. it("InitializeWorldsRegistry", () =>
  122. __awaiter(void 0, void 0, void 0, function* () {
  123. const registryPda = FindWorldRegistryPda(worldProgram);
  124. yield worldProgram.methods
  125. .initializeRegistry()
  126. .accounts({
  127. registry: registryPda,
  128. payer: provider.wallet.publicKey,
  129. })
  130. .rpc();
  131. }));
  132. it("InitializeNewWorld", () =>
  133. __awaiter(void 0, void 0, void 0, function* () {
  134. const registryPda = FindWorldRegistryPda(worldProgram);
  135. const worldPda = FindWorldPda(worldProgram, new bn_js_1.default(0));
  136. yield worldProgram.methods
  137. .initializeNewWorld()
  138. .accounts({
  139. world: worldPda,
  140. registry: registryPda,
  141. payer: provider.wallet.publicKey,
  142. })
  143. .rpc();
  144. }));
  145. it("Add entity 1", () =>
  146. __awaiter(void 0, void 0, void 0, function* () {
  147. const worldPda = FindWorldPda(worldProgram, new bn_js_1.default(0));
  148. entity1 = FindEntityPda(
  149. worldProgram,
  150. new bn_js_1.default(0),
  151. new bn_js_1.default(0)
  152. );
  153. yield worldProgram.methods
  154. .addEntity()
  155. .accounts({
  156. world: worldPda,
  157. entity: entity1,
  158. payer: provider.wallet.publicKey,
  159. })
  160. .rpc();
  161. }));
  162. it("Add entity 2", () =>
  163. __awaiter(void 0, void 0, void 0, function* () {
  164. const worldPda = FindWorldPda(worldProgram, new bn_js_1.default(0));
  165. entity2 = FindEntityPda(
  166. worldProgram,
  167. new bn_js_1.default(0),
  168. new bn_js_1.default(1)
  169. );
  170. yield worldProgram.methods
  171. .addEntity()
  172. .accounts({
  173. world: worldPda,
  174. entity: entity2,
  175. payer: provider.wallet.publicKey,
  176. })
  177. .rpc();
  178. }));
  179. it("Add entity 3", () =>
  180. __awaiter(void 0, void 0, void 0, function* () {
  181. const worldPda = FindWorldPda(worldProgram, new bn_js_1.default(0));
  182. const entityPda = FindEntityPda(
  183. worldProgram,
  184. new bn_js_1.default(0),
  185. new bn_js_1.default(2)
  186. );
  187. yield worldProgram.methods
  188. .addEntity()
  189. .accounts({
  190. world: worldPda,
  191. entity: entityPda,
  192. payer: provider.wallet.publicKey,
  193. })
  194. .rpc();
  195. }));
  196. it("Initialize Original Component on Entity 1, trough the world instance", () =>
  197. __awaiter(void 0, void 0, void 0, function* () {
  198. let componentEntity1 = FindComponentPda(
  199. boltComponentProgramOrigin.programId,
  200. entity1,
  201. "origin-component"
  202. );
  203. yield worldProgram.methods
  204. .initializeComponent()
  205. .accounts({
  206. payer: provider.wallet.publicKey,
  207. data: componentEntity1,
  208. componentProgram: boltComponentProgramOrigin.programId,
  209. entity: entity1,
  210. })
  211. .rpc();
  212. }));
  213. it("Initialize Original Component on Entity 2, trough the world instance", () =>
  214. __awaiter(void 0, void 0, void 0, function* () {
  215. let componentEntity2 = FindComponentPda(
  216. boltComponentProgramOrigin.programId,
  217. entity2,
  218. "origin-component"
  219. );
  220. yield worldProgram.methods
  221. .initializeComponent()
  222. .accounts({
  223. payer: provider.wallet.publicKey,
  224. data: componentEntity2,
  225. componentProgram: boltComponentProgramOrigin.programId,
  226. entity: entity2,
  227. })
  228. .rpc();
  229. }));
  230. it("Initialize Position Component on Entity 1", () =>
  231. __awaiter(void 0, void 0, void 0, function* () {
  232. componentPositionEntity1 = FindComponentPda(
  233. boltComponentPositionProgram.programId,
  234. entity1,
  235. "component-position"
  236. );
  237. console.log(
  238. "Component Position E1: ",
  239. componentPositionEntity1.toBase58()
  240. );
  241. yield worldProgram.methods
  242. .initializeComponent()
  243. .accounts({
  244. payer: provider.wallet.publicKey,
  245. data: componentPositionEntity1,
  246. componentProgram: boltComponentPositionProgram.programId,
  247. entity: entity1,
  248. })
  249. .rpc();
  250. }));
  251. it("Initialize Velocity Component on Entity 1", () =>
  252. __awaiter(void 0, void 0, void 0, function* () {
  253. componentVelocityEntity1 = FindComponentPda(
  254. boltComponentVelocityProgram.programId,
  255. entity1,
  256. "component-velocity"
  257. );
  258. yield worldProgram.methods
  259. .initializeComponent()
  260. .accounts({
  261. payer: provider.wallet.publicKey,
  262. data: componentVelocityEntity1,
  263. componentProgram: boltComponentVelocityProgram.programId,
  264. entity: entity1,
  265. })
  266. .rpc();
  267. }));
  268. it("Initialize Position Component on Entity 2", () =>
  269. __awaiter(void 0, void 0, void 0, function* () {
  270. componentPositionEntity2 = FindComponentPda(
  271. boltComponentPositionProgram.programId,
  272. entity2,
  273. "component-position"
  274. );
  275. yield worldProgram.methods
  276. .initializeComponent()
  277. .accounts({
  278. payer: provider.wallet.publicKey,
  279. data: componentPositionEntity2,
  280. componentProgram: boltComponentPositionProgram.programId,
  281. entity: entity2,
  282. })
  283. .rpc();
  284. }));
  285. it("Check Position on Entity 1 is default", () =>
  286. __awaiter(void 0, void 0, void 0, function* () {
  287. (0, chai_1.expect)(
  288. (yield boltComponentPositionProgram.account.position.fetch(
  289. componentPositionEntity1
  290. )).x.toNumber()
  291. ).to.equal(0);
  292. (0, chai_1.expect)(
  293. (yield boltComponentPositionProgram.account.position.fetch(
  294. componentPositionEntity1
  295. )).y.toNumber()
  296. ).to.equal(0);
  297. (0, chai_1.expect)(
  298. (yield boltComponentPositionProgram.account.position.fetch(
  299. componentPositionEntity1
  300. )).z.toNumber()
  301. ).to.equal(0);
  302. }));
  303. it("Simple Movement System and Up direction on Entity 1", () =>
  304. __awaiter(void 0, void 0, void 0, function* () {
  305. const args = {
  306. direction: Direction.Up,
  307. };
  308. yield worldProgram.methods
  309. .apply(serializeArgs(args)) // Move Up
  310. .accounts({
  311. componentProgram: boltComponentPositionProgram.programId,
  312. boltSystem: systemSimpleMovement,
  313. boltComponent: componentPositionEntity1,
  314. })
  315. .rpc({ skipPreflight: true });
  316. (0, chai_1.expect)(
  317. (yield boltComponentPositionProgram.account.position.fetch(
  318. componentPositionEntity1
  319. )).y.toNumber()
  320. ).to.equal(1);
  321. const componentData =
  322. yield boltComponentPositionProgram.account.position.fetch(
  323. componentPositionEntity1
  324. );
  325. const x = componentData.x.toNumber();
  326. const y = componentData.y.toNumber();
  327. const z = componentData.z.toNumber();
  328. console.log("+-----------------------------+");
  329. console.log("| Movement System: Entity 1 |");
  330. console.log("+----------------+------------+");
  331. console.log("| Coordinate | Value |");
  332. console.log("+----------------+------------+");
  333. console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
  334. console.log("| | |");
  335. console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
  336. console.log("| | |");
  337. console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
  338. console.log("+----------------+------------+");
  339. console.log("| |");
  340. console.log("+-----------------------------+");
  341. }));
  342. it("Simple Movement System and Right direction on Entity 1", () =>
  343. __awaiter(void 0, void 0, void 0, function* () {
  344. const args = {
  345. direction: Direction.Right,
  346. };
  347. yield worldProgram.methods
  348. .apply(serializeArgs(args)) // Move Right
  349. .accounts({
  350. componentProgram: boltComponentPositionProgram.programId,
  351. boltSystem: systemSimpleMovement,
  352. boltComponent: componentPositionEntity1,
  353. })
  354. .rpc({ skipPreflight: true });
  355. (0, chai_1.expect)(
  356. (yield boltComponentPositionProgram.account.position.fetch(
  357. componentPositionEntity1
  358. )).y.toNumber()
  359. ).to.equal(1);
  360. (0, chai_1.expect)(
  361. (yield boltComponentPositionProgram.account.position.fetch(
  362. componentPositionEntity1
  363. )).y.toNumber()
  364. ).to.equal(1);
  365. const componentData =
  366. yield boltComponentPositionProgram.account.position.fetch(
  367. componentPositionEntity1
  368. );
  369. const x = componentData.x.toNumber();
  370. const y = componentData.y.toNumber();
  371. const z = componentData.z.toNumber();
  372. console.log("+-----------------------------+");
  373. console.log("| Movement System: Entity 1 |");
  374. console.log("+----------------+------------+");
  375. console.log("| Coordinate | Value |");
  376. console.log("+----------------+------------+");
  377. console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
  378. console.log("| | |");
  379. console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
  380. console.log("| | |");
  381. console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
  382. console.log("+----------------+------------+");
  383. console.log("| |");
  384. console.log("+-----------------------------+");
  385. }));
  386. it("Fly System on Entity 1", () =>
  387. __awaiter(void 0, void 0, void 0, function* () {
  388. yield worldProgram.methods
  389. .apply(Buffer.alloc(0)) // Move Up
  390. .accounts({
  391. componentProgram: boltComponentPositionProgram.programId,
  392. boltSystem: systemFly,
  393. boltComponent: componentPositionEntity1,
  394. })
  395. .rpc();
  396. (0, chai_1.expect)(
  397. (yield boltComponentPositionProgram.account.position.fetch(
  398. componentPositionEntity1
  399. )).z.toNumber()
  400. ).to.equal(1);
  401. const componentData =
  402. yield boltComponentPositionProgram.account.position.fetch(
  403. componentPositionEntity1
  404. );
  405. const x = componentData.x.toNumber();
  406. const y = componentData.y.toNumber();
  407. const z = componentData.z.toNumber();
  408. console.log("+-----------------------------+");
  409. console.log("| Fly: Position Entity 1 |");
  410. console.log("+----------------+------------+");
  411. console.log("| Coordinate | Value |");
  412. console.log("+----------------+------------+");
  413. console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
  414. console.log("| | |");
  415. console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
  416. console.log("| | |");
  417. console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
  418. console.log("+----------------+------------+");
  419. console.log("| |");
  420. console.log("+-----------------------------+");
  421. }));
  422. it("Apply Velocity on Entity 1", () =>
  423. __awaiter(void 0, void 0, void 0, function* () {
  424. yield worldProgram.methods
  425. .apply2(Buffer.alloc(0))
  426. .accounts({
  427. componentProgram1: boltComponentVelocityProgram.programId,
  428. componentProgram2: boltComponentPositionProgram.programId,
  429. boltSystem: applyVelocity,
  430. boltComponent1: componentVelocityEntity1,
  431. boltComponent2: componentPositionEntity1,
  432. })
  433. .remainingAccounts([
  434. {
  435. pubkey: componentPositionEntity1,
  436. isWritable: false,
  437. isSigner: false,
  438. },
  439. ])
  440. .rpc();
  441. console.log("Component Velocity: ", componentVelocityEntity1.toBase58());
  442. let componentData =
  443. yield boltComponentVelocityProgram.account.velocity.fetch(
  444. componentVelocityEntity1
  445. );
  446. let x = componentData.x.toNumber();
  447. let y = componentData.y.toNumber();
  448. let z = componentData.z.toNumber();
  449. const tmp = componentData.lastApplied.toNumber();
  450. console.log("+-----------------------------+");
  451. console.log("| Apply Velocity: Velocity Entity 1 |");
  452. console.log("+----------------+------------+");
  453. console.log("| Coordinate | Value |");
  454. console.log("+----------------+------------+");
  455. console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
  456. console.log("| | |");
  457. console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
  458. console.log("| | |");
  459. console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
  460. console.log("| | |");
  461. console.log(`| Timestamp | ${String(tmp).padEnd(10, " ")} |`);
  462. console.log("+----------------+------------+");
  463. console.log("| |");
  464. console.log("+-----------------------------+");
  465. let positionData =
  466. yield boltComponentPositionProgram.account.position.fetch(
  467. componentPositionEntity1
  468. );
  469. x = positionData.x.toNumber();
  470. y = positionData.y.toNumber();
  471. z = positionData.z.toNumber();
  472. console.log("+-----------------------------+");
  473. console.log("| Apply Velocity: Position Entity 1 |");
  474. console.log("+----------------+------------+");
  475. console.log("| Coordinate | Value |");
  476. console.log("+----------------+------------+");
  477. console.log(`| X Position | ${String(x).padEnd(10, " ")} |`);
  478. console.log("| | |");
  479. console.log(`| Y Position | ${String(y).padEnd(10, " ")} |`);
  480. console.log("| | |");
  481. console.log(`| Z Position | ${String(z).padEnd(10, " ")} |`);
  482. console.log("+----------------+------------+");
  483. console.log("| |");
  484. console.log("+-----------------------------+");
  485. }));
  486. // Utils
  487. function FindWorldRegistryPda(program) {
  488. return web3_js_1.PublicKey.findProgramAddressSync(
  489. [Buffer.from("registry")],
  490. program.programId
  491. )[0];
  492. }
  493. function FindWorldPda(program, id) {
  494. return web3_js_1.PublicKey.findProgramAddressSync(
  495. [Buffer.from("world"), id.toBuffer("le", 8)],
  496. program.programId
  497. )[0];
  498. }
  499. function FindEntityPda(program, worldId, entityId) {
  500. return web3_js_1.PublicKey.findProgramAddressSync(
  501. [
  502. Buffer.from("entity"),
  503. worldId.toBuffer("be", 8),
  504. entityId.toBuffer("be", 8),
  505. ],
  506. program.programId
  507. )[0];
  508. }
  509. function FindComponentPda(program, entity, seed = "component") {
  510. return web3_js_1.PublicKey.findProgramAddressSync(
  511. [Buffer.from(seed), entity.toBytes()],
  512. program
  513. )[0];
  514. }
  515. });