123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- import { expect } from "chai";
- import {
- anchor,
- FindComponentPda,
- FindEntityPda,
- SerializeArgs,
- SessionProgram,
- FindSessionTokenPda,
- BN,
- } from "../../clients/bolt-sdk/lib";
- import { Keypair } from "@solana/web3.js";
- export function session(framework) {
- describe("Session", () => {
- const sessionSigner: Keypair = Keypair.generate();
- let sessionToken: anchor.web3.PublicKey;
- let entity: anchor.web3.PublicKey;
- let component: anchor.web3.PublicKey;
- let entityWithAuthority: anchor.web3.PublicKey;
- let componentWithAuthority: anchor.web3.PublicKey;
- it("Create Session", async () => {
- sessionToken = FindSessionTokenPda({
- sessionSigner: sessionSigner.publicKey,
- authority: framework.provider.wallet.publicKey,
- });
- let instruction = await SessionProgram.methods
- .createSession(true, null, new BN(1000000000))
- .accounts({
- sessionSigner: sessionSigner.publicKey,
- authority: framework.provider.wallet.publicKey,
- targetProgram: framework.worldProgram.programId,
- sessionToken,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
- });
- it("Add entity", async () => {
- const world = await framework.worldProgram.account.world.fetch(
- framework.worldPda,
- );
- entity = FindEntityPda({
- worldId: world.id,
- entityId: world.entities,
- });
- const instruction = await framework.worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: sessionSigner.publicKey,
- entity: entity,
- world: framework.worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
- });
- it("Initialize position component", async () => {
- const componentId = framework.exampleComponentPosition.programId;
- component = FindComponentPda({
- componentId,
- entity,
- });
- const instruction = await framework.worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: sessionSigner.publicKey,
- entity: entity,
- data: component,
- componentProgram: componentId,
- authority: framework.worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
- });
- it("Apply Fly System on component using session token", async () => {
- const positionBefore =
- await framework.exampleComponentPosition.account.position.fetch(
- component,
- );
- const instruction = await framework.worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: sessionSigner.publicKey,
- boltSystem: framework.systemFly.programId,
- world: framework.worldPda,
- sessionToken,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: component,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- let signature = await framework.provider.connection.sendTransaction(
- transaction,
- [sessionSigner],
- );
- await framework.provider.connection.confirmTransaction(signature);
- const positionAfter =
- await framework.exampleComponentPosition.account.position.fetch(
- component,
- );
- expect(positionAfter.x.toNumber()).to.equal(positionBefore.x.toNumber());
- expect(positionAfter.y.toNumber()).to.equal(positionBefore.y.toNumber());
- expect(positionAfter.z.toNumber()).to.equal(
- positionBefore.z.toNumber() + 1,
- );
- });
- it("Add entity for authority test", async () => {
- const world = await framework.worldProgram.account.world.fetch(
- framework.worldPda,
- );
- entityWithAuthority = FindEntityPda({
- worldId: world.id,
- entityId: world.entities,
- });
- const instruction = await framework.worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: sessionSigner.publicKey,
- world: framework.worldPda,
- entity: entityWithAuthority,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
- });
- it("Initialize position component with authority", async () => {
- const componentId = framework.exampleComponentPosition.programId;
- componentWithAuthority = FindComponentPda({
- componentId,
- entity: entityWithAuthority,
- });
- const instruction = await framework.worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: sessionSigner.publicKey,
- entity: entityWithAuthority,
- data: componentWithAuthority,
- componentProgram: componentId,
- authority: framework.provider.wallet.publicKey,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction, [sessionSigner]);
- });
- it("Apply Fly System on component with authority using session token", async () => {
- const positionBefore =
- await framework.exampleComponentPosition.account.position.fetch(
- componentWithAuthority,
- );
- const instruction = await framework.worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: sessionSigner.publicKey,
- boltSystem: framework.systemFly.programId,
- world: framework.worldPda,
- sessionToken,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: componentWithAuthority,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- let signature = await framework.provider.connection.sendTransaction(
- transaction,
- [sessionSigner],
- );
- await framework.provider.connection.confirmTransaction(signature);
- const positionAfter =
- await framework.exampleComponentPosition.account.position.fetch(
- componentWithAuthority,
- );
- expect(positionAfter.x.toNumber()).to.equal(positionBefore.x.toNumber());
- expect(positionAfter.y.toNumber()).to.equal(positionBefore.y.toNumber());
- expect(positionAfter.z.toNumber()).to.equal(
- positionBefore.z.toNumber() + 1,
- );
- });
- });
- }
|