session.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { expect } from "chai";
  2. import {
  3. anchor,
  4. FindComponentPda,
  5. FindEntityPda,
  6. SerializeArgs,
  7. SessionProgram,
  8. FindSessionTokenPda,
  9. BN,
  10. } from "../../clients/bolt-sdk/lib";
  11. import { Keypair } from "@solana/web3.js";
  12. export function session(framework) {
  13. describe("Session", () => {
  14. const sessionSigner: Keypair = Keypair.generate();
  15. let sessionToken: anchor.web3.PublicKey;
  16. let entity: anchor.web3.PublicKey;
  17. let component: anchor.web3.PublicKey;
  18. let entityWithAuthority: anchor.web3.PublicKey;
  19. let componentWithAuthority: anchor.web3.PublicKey;
  20. it("Create Session", async () => {
  21. sessionToken = FindSessionTokenPda({
  22. sessionSigner: sessionSigner.publicKey,
  23. authority: framework.provider.wallet.publicKey,
  24. });
  25. let instruction = await SessionProgram.methods
  26. .createSession(true, null, new BN(1000000000))
  27. .accounts({
  28. sessionSigner: sessionSigner.publicKey,
  29. authority: framework.provider.wallet.publicKey,
  30. targetProgram: framework.worldProgram.programId,
  31. sessionToken,
  32. })
  33. .instruction();
  34. const transaction = new anchor.web3.Transaction().add(instruction);
  35. await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
  36. });
  37. it("Add entity", async () => {
  38. const world = await framework.worldProgram.account.world.fetch(
  39. framework.worldPda,
  40. );
  41. entity = FindEntityPda({
  42. worldId: world.id,
  43. entityId: world.entities,
  44. });
  45. const instruction = await framework.worldProgram.methods
  46. .addEntity(null)
  47. .accounts({
  48. payer: sessionSigner.publicKey,
  49. entity: entity,
  50. world: framework.worldPda,
  51. })
  52. .instruction();
  53. const transaction = new anchor.web3.Transaction().add(instruction);
  54. await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
  55. });
  56. it("Initialize position component", async () => {
  57. const componentId = framework.exampleComponentPosition.programId;
  58. component = FindComponentPda({
  59. componentId,
  60. entity,
  61. });
  62. const instruction = await framework.worldProgram.methods
  63. .initializeComponent()
  64. .accounts({
  65. payer: sessionSigner.publicKey,
  66. entity: entity,
  67. data: component,
  68. componentProgram: componentId,
  69. authority: framework.worldProgram.programId,
  70. })
  71. .instruction();
  72. const transaction = new anchor.web3.Transaction().add(instruction);
  73. await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
  74. });
  75. it("Apply Fly System on component using session token", async () => {
  76. const positionBefore =
  77. await framework.exampleComponentPosition.account.position.fetch(
  78. component,
  79. );
  80. const instruction = await framework.worldProgram.methods
  81. .applyWithSession(SerializeArgs())
  82. .accounts({
  83. authority: sessionSigner.publicKey,
  84. boltSystem: framework.systemFly.programId,
  85. world: framework.worldPda,
  86. sessionToken,
  87. })
  88. .remainingAccounts([
  89. {
  90. pubkey: framework.exampleComponentPosition.programId,
  91. isSigner: false,
  92. isWritable: false,
  93. },
  94. {
  95. pubkey: component,
  96. isSigner: false,
  97. isWritable: true,
  98. },
  99. ])
  100. .instruction();
  101. const transaction = new anchor.web3.Transaction().add(instruction);
  102. let signature = await framework.provider.connection.sendTransaction(
  103. transaction,
  104. [sessionSigner],
  105. );
  106. await framework.provider.connection.confirmTransaction(signature);
  107. const positionAfter =
  108. await framework.exampleComponentPosition.account.position.fetch(
  109. component,
  110. );
  111. expect(positionAfter.x.toNumber()).to.equal(positionBefore.x.toNumber());
  112. expect(positionAfter.y.toNumber()).to.equal(positionBefore.y.toNumber());
  113. expect(positionAfter.z.toNumber()).to.equal(
  114. positionBefore.z.toNumber() + 1,
  115. );
  116. });
  117. it("Add entity for authority test", async () => {
  118. const world = await framework.worldProgram.account.world.fetch(
  119. framework.worldPda,
  120. );
  121. entityWithAuthority = FindEntityPda({
  122. worldId: world.id,
  123. entityId: world.entities,
  124. });
  125. const instruction = await framework.worldProgram.methods
  126. .addEntity(null)
  127. .accounts({
  128. payer: sessionSigner.publicKey,
  129. world: framework.worldPda,
  130. entity: entityWithAuthority,
  131. })
  132. .instruction();
  133. const transaction = new anchor.web3.Transaction().add(instruction);
  134. await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
  135. });
  136. it("Initialize position component with authority", async () => {
  137. const componentId = framework.exampleComponentPosition.programId;
  138. componentWithAuthority = FindComponentPda({
  139. componentId,
  140. entity: entityWithAuthority,
  141. });
  142. const instruction = await framework.worldProgram.methods
  143. .initializeComponent()
  144. .accounts({
  145. payer: sessionSigner.publicKey,
  146. entity: entityWithAuthority,
  147. data: componentWithAuthority,
  148. componentProgram: componentId,
  149. authority: framework.provider.wallet.publicKey,
  150. })
  151. .instruction();
  152. const transaction = new anchor.web3.Transaction().add(instruction);
  153. await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
  154. });
  155. it("Apply Fly System on component with authority using session token", async () => {
  156. const positionBefore =
  157. await framework.exampleComponentPosition.account.position.fetch(
  158. componentWithAuthority,
  159. );
  160. const instruction = await framework.worldProgram.methods
  161. .applyWithSession(SerializeArgs())
  162. .accounts({
  163. authority: sessionSigner.publicKey,
  164. boltSystem: framework.systemFly.programId,
  165. world: framework.worldPda,
  166. sessionToken,
  167. })
  168. .remainingAccounts([
  169. {
  170. pubkey: framework.exampleComponentPosition.programId,
  171. isSigner: false,
  172. isWritable: false,
  173. },
  174. {
  175. pubkey: componentWithAuthority,
  176. isSigner: false,
  177. isWritable: true,
  178. },
  179. ])
  180. .instruction();
  181. const transaction = new anchor.web3.Transaction().add(instruction);
  182. let signature = await framework.provider.connection.sendTransaction(
  183. transaction,
  184. [sessionSigner],
  185. );
  186. await framework.provider.connection.confirmTransaction(signature);
  187. const positionAfter =
  188. await framework.exampleComponentPosition.account.position.fetch(
  189. componentWithAuthority,
  190. );
  191. expect(positionAfter.x.toNumber()).to.equal(positionBefore.x.toNumber());
  192. expect(positionAfter.y.toNumber()).to.equal(positionBefore.y.toNumber());
  193. expect(positionAfter.z.toNumber()).to.equal(
  194. positionBefore.z.toNumber() + 1,
  195. );
  196. });
  197. });
  198. }