123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870 |
- import { Keypair, type PublicKey } from "@solana/web3.js";
- import { type Position } from "../target/types/position";
- import { type Velocity } from "../target/types/velocity";
- import { type BoltComponent } from "../target/types/bolt_component";
- import { type SystemSimpleMovement } from "../target/types/system_simple_movement";
- import { type SystemFly } from "../target/types/system_fly";
- import { type SystemApplyVelocity } from "../target/types/system_apply_velocity";
- import { expect } from "chai";
- import BN from "bn.js";
- import {
- DELEGATION_PROGRAM_ID,
- DelegateComponent,
- type Program,
- anchor,
- web3,
- FindRegistryPda,
- FindWorldPda,
- FindEntityPda,
- FindComponentPda,
- SerializeArgs,
- WORLD_PROGRAM_IDL as World,
- } from "../clients/bolt-sdk";
- enum Direction {
- Left = "Left",
- Right = "Right",
- Up = "Up",
- Down = "Down",
- }
- function padCenter(value: string, width: number) {
- const length = value.length;
- if (width <= length) {
- return value;
- }
- const padding = (width - length) / 2;
- const align = width - padding;
- return value.padStart(align, " ").padEnd(width, " ");
- }
- function logPosition(title: string, { x, y, z }: { x: BN; y: BN; z: BN }) {
- console.log(" +----------------------------------+");
- console.log(` | ${padCenter(title, 32)} |`);
- console.log(" +-----------------+----------------+");
- console.log(` | X Position | ${String(x).padEnd(14, " ")} |`);
- console.log(` | Y Position | ${String(y).padEnd(14, " ")} |`);
- console.log(` | Z Position | ${String(z).padEnd(14, " ")} |`);
- console.log(" +-----------------+----------------+");
- }
- function logVelocity(
- title: string,
- { x, y, z, lastApplied }: { x: BN; y: BN; z: BN; lastApplied: BN },
- ) {
- console.log(" +----------------------------------+");
- console.log(` | ${padCenter(title, 32)} |`);
- console.log(" +-----------------+----------------+");
- console.log(` | X Velocity | ${String(x).padEnd(14, " ")} |`);
- console.log(` | Y Velocity | ${String(y).padEnd(14, " ")} |`);
- console.log(` | Z Velocity | ${String(z).padEnd(14, " ")} |`);
- console.log(` | Last Applied | ${String(lastApplied).padEnd(14, " ")} |`);
- console.log(" +-----------------+----------------+");
- }
- describe("bolt", () => {
- const provider = anchor.AnchorProvider.env();
- anchor.setProvider(provider);
- const worldProgram = anchor.workspace.World as Program<World>;
- const boltComponentProgram = anchor.workspace
- .BoltComponent as Program<BoltComponent>;
- const exampleComponentPosition = anchor.workspace
- .Position as Program<Position>;
- const exampleComponentVelocity = anchor.workspace
- .Velocity as Program<Velocity>;
- const exampleSystemSimpleMovement = (
- anchor.workspace.SystemSimpleMovement as Program<SystemSimpleMovement>
- ).programId;
- const exampleSystemFly = (anchor.workspace.SystemFly as Program<SystemFly>)
- .programId;
- const exampleSystemApplyVelocity = (
- anchor.workspace.SystemApplyVelocity as Program<SystemApplyVelocity>
- ).programId;
- let worldPda: PublicKey;
- let worldId: BN;
- let entity1Pda: PublicKey;
- let entity2Pda: PublicKey;
- let entity4Pda: PublicKey;
- let entity5Pda: PublicKey;
- let componentPositionEntity1Pda: PublicKey;
- let componentVelocityEntity1Pda: PublicKey;
- let componentPositionEntity4Pda: PublicKey;
- let componentPositionEntity5Pda: PublicKey;
- const secondAuthority = Keypair.generate().publicKey;
- it("InitializeRegistry", async () => {
- const registryPda = FindRegistryPda({});
- const instruction = await worldProgram.methods
- .initializeRegistry()
- .accounts({
- registry: registryPda,
- payer: provider.wallet.publicKey,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await provider.sendAndConfirm(transaction);
- });
- it("InitializeNewWorld", async () => {
- const registryPda = FindRegistryPda({});
- const registry = await worldProgram.account.registry.fetch(registryPda);
- worldId = new BN(registry.worlds);
- worldPda = FindWorldPda({ worldId });
- const instruction = await worldProgram.methods
- .initializeNewWorld()
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- registry: registryPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("InitializeNewWorld signature: ", signature);
- });
- it("Add authority", async () => {
- const instruction = await worldProgram.methods
- .addAuthority(worldId)
- .accounts({
- authority: provider.wallet.publicKey,
- newAuthority: provider.wallet.publicKey,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await provider.sendAndConfirm(transaction, [], { skipPreflight: true });
- const worldAccount = await worldProgram.account.world.fetch(worldPda);
- expect(
- worldAccount.authorities.some((auth) =>
- auth.equals(provider.wallet.publicKey),
- ),
- );
- });
- it("Add a second authority", async () => {
- const instruction = await worldProgram.methods
- .addAuthority(worldId)
- .accounts({
- authority: provider.wallet.publicKey,
- newAuthority: secondAuthority,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(`Add Authority signature: ${signature}`);
- const worldAccount = await worldProgram.account.world.fetch(worldPda);
- expect(
- worldAccount.authorities.some((auth) => auth.equals(secondAuthority)),
- );
- });
- it("Remove an authority", async () => {
- const instruction = await worldProgram.methods
- .removeAuthority(worldId)
- .accounts({
- authority: provider.wallet.publicKey,
- authorityToDelete: secondAuthority,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(`Remove Authority signature: ${signature}`);
- const worldAccount = await worldProgram.account.world.fetch(worldPda);
- expect(
- !worldAccount.authorities.some((auth) => auth.equals(secondAuthority)),
- );
- });
- it("InitializeNewWorld 2", async () => {
- const registryPda = FindRegistryPda({});
- const registry = await worldProgram.account.registry.fetch(registryPda);
- const worldId = new BN(registry.worlds);
- const worldPda = FindWorldPda({ worldId });
- const instruction = await worldProgram.methods
- .initializeNewWorld()
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- registry: registryPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("InitializeNewWorld 2 signature: ", signature);
- });
- it("Add entity 1", async () => {
- const world = await worldProgram.account.world.fetch(worldPda);
- entity1Pda = FindEntityPda({ worldId: world.id, entityId: world.entities });
- const instruction = await worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- entity: entity1Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Add Entity 1 signature: ", signature);
- });
- it("Add entity 2", async () => {
- const world = await worldProgram.account.world.fetch(worldPda);
- entity2Pda = FindEntityPda({ worldId: world.id, entityId: world.entities });
- const instruction = await worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- entity: entity2Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Add Entity 2 signature: ", signature);
- });
- it("Add entity 3", async () => {
- const world = await worldProgram.account.world.fetch(worldPda);
- const entity3Pda = FindEntityPda({
- worldId: world.id,
- entityId: world.entities,
- });
- const instruction = await worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- entity: entity3Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Add Entity 3 signature: ", signature);
- });
- it("Add entity 4 (with seed)", async () => {
- const world = await worldProgram.account.world.fetch(worldPda);
- const seed = Buffer.from("custom-seed");
- entity4Pda = FindEntityPda({ worldId: world.id, seed });
- const instruction = await worldProgram.methods
- .addEntity(seed)
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- entity: entity4Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Add Entity 4 signature: ", signature);
- });
- it("Add entity 5", async () => {
- const world = await worldProgram.account.world.fetch(worldPda);
- entity5Pda = FindEntityPda({ worldId: world.id, entityId: world.entities });
- const instruction = await worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: provider.wallet.publicKey,
- world: worldPda,
- entity: entity5Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Add Entity 5 signature: ", signature);
- });
- it("Initialize Original Component on Entity 1, through the world instance", async () => {
- const componentId = boltComponentProgram.programId;
- const componentPda = FindComponentPda({
- componentId,
- entity: entity1Pda,
- seed: "origin-component",
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- data: componentPda,
- componentProgram: componentId,
- authority: provider.wallet.publicKey,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Original Component on Entity 1 signature: ",
- signature,
- );
- });
- it("Initialize Original Component on Entity 2, trough the world instance", async () => {
- const componentId = boltComponentProgram.programId;
- const componentPda = FindComponentPda({
- componentId,
- entity: entity2Pda,
- seed: "origin-component",
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity2Pda,
- data: componentPda,
- componentProgram: componentId,
- authority: provider.wallet.publicKey,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Original Component on Entity 2 signature: ",
- signature,
- );
- });
- it("Initialize Position Component on Entity 1", async () => {
- const componentId = exampleComponentPosition.programId;
- componentPositionEntity1Pda = FindComponentPda({
- componentId,
- entity: entity1Pda,
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- data: componentPositionEntity1Pda,
- componentProgram: componentId,
- authority: worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Position Component on Entity 1 signature: ",
- signature,
- );
- });
- it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
- const componentId = exampleComponentVelocity.programId;
- componentVelocityEntity1Pda = FindComponentPda({
- componentId,
- entity: entity1Pda,
- seed: "component-velocity",
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- data: componentVelocityEntity1Pda,
- componentProgram: componentId,
- authority: worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Velocity Component on Entity 1 signature: ",
- signature,
- );
- });
- it("Initialize Position Component on Entity 2", async () => {
- const componentId = exampleComponentPosition.programId;
- const componentPositionEntity2Pda = FindComponentPda({
- componentId,
- entity: entity2Pda,
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity2Pda,
- data: componentPositionEntity2Pda,
- componentProgram: componentId,
- authority: worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Position Component on Entity 2 signature: ",
- signature,
- );
- });
- it("Initialize Position Component on Entity 4", async () => {
- const componentId = exampleComponentPosition.programId;
- componentPositionEntity4Pda = FindComponentPda({
- componentId,
- entity: entity4Pda,
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity4Pda,
- data: componentPositionEntity4Pda,
- componentProgram: componentId,
- authority: worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Position Component on Entity 4 signature: ",
- signature,
- );
- });
- it("Initialize Position Component on Entity 5 (with authority)", async () => {
- const componentId = exampleComponentPosition.programId;
- componentPositionEntity5Pda = FindComponentPda({
- componentId,
- entity: entity5Pda,
- });
- const instruction = await worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: provider.wallet.publicKey,
- entity: entity5Pda,
- data: componentPositionEntity5Pda,
- componentProgram: componentId,
- authority: provider.wallet.publicKey,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Initialize Position Component on Entity 5 signature: ",
- signature,
- );
- });
- it("Check Position on Entity 1 is default", async () => {
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Default State: Entity 1", position);
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(0);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Simple Movement System (Up) on Entity 1 using Apply", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs({ direction: Direction.Up }))
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemSimpleMovement,
- boltComponent: componentPositionEntity1Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Apply Simple Movement System (Up) on Entity 1 signature: ",
- signature,
- );
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Movement System: Entity 1", position);
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(1);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Simple Movement System (Up) on Entity 1", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs({ direction: Direction.Up }))
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemSimpleMovement,
- boltComponent: componentPositionEntity1Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Apply Simple Movement System (Up) on Entity 1 signature: ",
- signature,
- );
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Movement System: Entity 1", position);
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(2);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Simple Movement System (Right) on Entity 1", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs({ direction: Direction.Right }))
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemSimpleMovement,
- boltComponent: componentPositionEntity1Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log(
- "Apply Simple Movement System (Right) on Entity 1 signature: ",
- signature,
- );
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Movement System: Entity 1", position);
- expect(position.x.toNumber()).to.equal(1);
- expect(position.y.toNumber()).to.equal(2);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Fly System on Entity 1", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemFly,
- boltComponent: componentPositionEntity1Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Apply Fly System on Entity 1 signature: ", signature);
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Fly System: Entity 1", position);
- expect(position.x.toNumber()).to.equal(1);
- expect(position.y.toNumber()).to.equal(2);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply System Velocity on Entity 1", async () => {
- const instruction = await worldProgram.methods
- .apply2(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemApplyVelocity,
- boltComponent1: componentVelocityEntity1Pda,
- componentProgram1: exampleComponentVelocity.programId,
- boltComponent2: componentPositionEntity1Pda,
- componentProgram2: exampleComponentPosition.programId,
- world: worldPda,
- })
- .remainingAccounts([])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction);
- console.log("Apply System Velocity on Entity 1 signature: ", signature);
- const velocity = await exampleComponentVelocity.account.velocity.fetch(
- componentVelocityEntity1Pda,
- );
- logVelocity("Apply System Velocity: Entity 1", velocity);
- expect(velocity.x.toNumber()).to.equal(10);
- expect(velocity.y.toNumber()).to.equal(0);
- expect(velocity.z.toNumber()).to.equal(0);
- expect(velocity.lastApplied.toNumber()).to.not.equal(0);
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Apply System Velocity: Entity 1", position);
- expect(position.x.toNumber()).to.greaterThan(1);
- expect(position.y.toNumber()).to.equal(2);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply System Velocity on Entity 1, with Clock external account", async () => {
- const instruction = await worldProgram.methods
- .apply2(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemApplyVelocity,
- boltComponent1: componentVelocityEntity1Pda,
- componentProgram1: exampleComponentVelocity.programId,
- boltComponent2: componentPositionEntity1Pda,
- componentProgram2: exampleComponentPosition.programId,
- world: worldPda,
- })
- .remainingAccounts([
- {
- pubkey: new web3.PublicKey(
- "SysvarC1ock11111111111111111111111111111111",
- ),
- isWritable: false,
- isSigner: false,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await provider.sendAndConfirm(transaction);
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity1Pda,
- );
- logPosition("Apply System Velocity: Entity 1", position);
- expect(position.x.toNumber()).to.greaterThan(1);
- expect(position.y.toNumber()).to.equal(2);
- expect(position.z.toNumber()).to.equal(300);
- });
- it("Apply Fly System on Entity 4", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemFly,
- boltComponent: componentPositionEntity4Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await provider.sendAndConfirm(transaction);
- const position = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity4Pda,
- );
- logPosition("Fly System: Entity 4", position);
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(0);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply Fly System on Entity 5 (should fail with wrong authority)", async () => {
- const positionBefore =
- await exampleComponentPosition.account.position.fetch(
- componentPositionEntity5Pda,
- );
- const instruction = await worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemFly,
- boltComponent: componentPositionEntity5Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- let failed = false;
- try {
- await provider.sendAndConfirm(transaction);
- } catch (error) {
- failed = true;
- // console.log("error", error);
- expect(error.logs.join("\n")).to.contain("Error Code: InvalidAuthority");
- }
- expect(failed).to.equal(true);
- const positionAfter = await exampleComponentPosition.account.position.fetch(
- componentPositionEntity5Pda,
- );
- expect(positionBefore.x.toNumber()).to.equal(positionAfter.x.toNumber());
- expect(positionBefore.y.toNumber()).to.equal(positionAfter.y.toNumber());
- expect(positionBefore.z.toNumber()).to.equal(positionAfter.z.toNumber());
- });
- it("Whitelist System", async () => {
- const instruction = await worldProgram.methods
- .approveSystem()
- .accounts({
- authority: provider.wallet.publicKey,
- system: exampleSystemFly,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction, [], {
- skipPreflight: true,
- });
- console.log(`Whitelist 2 system approval signature: ${signature}`);
- // Get World and check permissionless and systems
- const worldAccount = await worldProgram.account.world.fetch(worldPda);
- expect(worldAccount.permissionless).to.equal(false);
- expect(worldAccount.systems.length).to.be.greaterThan(0);
- });
- it("Whitelist System 2", async () => {
- const instruction = await worldProgram.methods
- .approveSystem()
- .accounts({
- authority: provider.wallet.publicKey,
- system: exampleSystemApplyVelocity,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction, [], {
- skipPreflight: true,
- });
- console.log(`Whitelist 2 system approval signature: ${signature}`);
- // Get World and check permissionless and systems
- const worldAccount = await worldProgram.account.world.fetch(worldPda);
- expect(worldAccount.permissionless).to.equal(false);
- expect(worldAccount.systems.length).to.be.greaterThan(0);
- });
- it("Apply Fly System on Entity 1", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemFly,
- boltComponent: componentPositionEntity1Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await provider.sendAndConfirm(transaction);
- });
- it("Remove System 1", async () => {
- const instruction = await worldProgram.methods
- .removeSystem()
- .accounts({
- authority: provider.wallet.publicKey,
- system: exampleSystemFly,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- const signature = await provider.sendAndConfirm(transaction, [], {
- skipPreflight: true,
- });
- console.log(`Remove System 1 signature: ${signature}`);
- // Get World and check permissionless and systems
- const worldAccount = await worldProgram.account.world.fetch(worldPda);
- expect(worldAccount.permissionless).to.equal(false);
- expect(worldAccount.systems.length).to.be.greaterThan(0);
- });
- it("Apply Invalid Fly System on Entity 1", async () => {
- const instruction = await worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: provider.wallet.publicKey,
- boltSystem: exampleSystemFly,
- boltComponent: componentPositionEntity1Pda,
- componentProgram: exampleComponentPosition.programId,
- world: worldPda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- let invalid = false;
- try {
- await provider.sendAndConfirm(transaction);
- } catch (error) {
- expect(error.logs.join(" ")).to.contain("Error Code: SystemNotApproved");
- invalid = true;
- }
- expect(invalid).to.equal(true);
- });
- it("Check invalid component init without CPI", async () => {
- let invalid = false;
- try {
- await exampleComponentPosition.methods
- .initialize()
- .accounts({
- payer: provider.wallet.publicKey,
- data: componentPositionEntity5Pda,
- entity: entity5Pda,
- authority: provider.wallet.publicKey,
- })
- .rpc();
- } catch (error) {
- // console.log("error", error);
- expect(error.message).to.contain("Error Code: InvalidCaller");
- invalid = true;
- }
- expect(invalid).to.equal(true);
- });
- it("Check invalid component update without CPI", async () => {
- let invalid = false;
- try {
- await boltComponentProgram.methods
- .update(Buffer.from(""))
- .accounts({
- boltComponent: componentPositionEntity4Pda,
- authority: provider.wallet.publicKey,
- })
- .rpc();
- } catch (error) {
- // console.log("error", error);
- expect(error.message).to.contain(
- "bolt_component. Error Code: AccountOwnedByWrongProgram",
- );
- invalid = true;
- }
- expect(invalid).to.equal(true);
- });
- it("Check component delegation", async () => {
- const delegateComponent = await DelegateComponent({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- componentId: exampleComponentPosition.programId,
- });
- const instruction = delegateComponent.transaction;
- const transaction = new anchor.web3.Transaction().add(instruction);
- const txSign = await provider.sendAndConfirm(transaction, [], {
- skipPreflight: true,
- commitment: "confirmed",
- });
- console.log(`Delegation signature: ${txSign}`);
- const acc = await provider.connection.getAccountInfo(
- delegateComponent.componentPda,
- );
- expect(acc?.owner.toBase58()).to.equal(DELEGATION_PROGRAM_ID.toBase58());
- });
- });
|