123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- import * as anchor from "@coral-xyz/anchor";
- import { type Program, web3 } from "@coral-xyz/anchor";
- import { 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 type BN from "bn.js";
- import {
- AddEntity,
- createDelegateInstruction,
- createUndelegateInstruction,
- createInitializeRegistryInstruction,
- DELEGATION_PROGRAM_ID,
- FindRegistryPda,
- InitializeComponent,
- InitializeNewWorld,
- ApplySystem,
- } 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 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 entity1Pda: PublicKey;
- let entity2Pda: PublicKey;
- let entity4Pda: PublicKey;
- let entity5Pda: PublicKey;
- let componentPositionEntity1Pda: PublicKey;
- let componentVelocityEntity1Pda: PublicKey;
- let componentPositionEntity4Pda: PublicKey;
- let componentPositionEntity5Pda: PublicKey;
- it("InitializeRegistry", async () => {
- const registryPda = FindRegistryPda({});
- const initializeRegistryIx = createInitializeRegistryInstruction({
- registry: registryPda,
- payer: provider.wallet.publicKey,
- });
- const tx = new anchor.web3.Transaction().add(initializeRegistryIx);
- await provider.sendAndConfirm(tx);
- });
- it("InitializeNewWorld", async () => {
- const initializeNewWorld = await InitializeNewWorld({
- payer: provider.wallet.publicKey,
- connection: provider.connection,
- });
- await provider.sendAndConfirm(initializeNewWorld.transaction);
- worldPda = initializeNewWorld.worldPda; // Saved for later
- });
- it("InitializeNewWorld 2", async () => {
- const initializeNewWorld = await InitializeNewWorld({
- payer: provider.wallet.publicKey,
- connection: provider.connection,
- });
- await provider.sendAndConfirm(initializeNewWorld.transaction);
- });
- it("Add entity 1", async () => {
- const addEntity = await AddEntity({
- payer: provider.wallet.publicKey,
- world: worldPda,
- connection: provider.connection,
- });
- await provider.sendAndConfirm(addEntity.transaction);
- entity1Pda = addEntity.entityPda; // Saved for later
- });
- it("Add entity 2", async () => {
- const addEntity = await AddEntity({
- payer: provider.wallet.publicKey,
- world: worldPda,
- connection: provider.connection,
- });
- await provider.sendAndConfirm(addEntity.transaction);
- entity2Pda = addEntity.entityPda; // Saved for later
- });
- it("Add entity 3", async () => {
- const addEntity = await AddEntity({
- payer: provider.wallet.publicKey,
- world: worldPda,
- connection: provider.connection,
- });
- await provider.sendAndConfirm(addEntity.transaction);
- });
- it("Add entity 4 (with seed)", async () => {
- const addEntity = await AddEntity({
- payer: provider.wallet.publicKey,
- world: worldPda,
- seed: "extra-seed",
- connection: provider.connection,
- });
- await provider.sendAndConfirm(addEntity.transaction);
- entity4Pda = addEntity.entityPda;
- });
- it("Add entity 5", async () => {
- const addEntity = await AddEntity({
- payer: provider.wallet.publicKey,
- world: worldPda,
- connection: provider.connection,
- });
- await provider.sendAndConfirm(addEntity.transaction);
- entity5Pda = addEntity.entityPda; // Saved for later
- });
- it("Initialize Original Component on Entity 1, trough the world instance", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- seed: "origin-component",
- componentId: boltComponentProgram.programId,
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- });
- it("Initialize Original Component on Entity 2, trough the world instance", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity2Pda,
- seed: "origin-component",
- componentId: boltComponentProgram.programId,
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- });
- it("Initialize Position Component on Entity 1", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- componentId: exampleComponentPosition.programId,
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- componentPositionEntity1Pda = initializeComponent.componentPda; // Saved for later
- });
- it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity1Pda,
- componentId: exampleComponentVelocity.programId,
- seed: "component-velocity",
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- componentVelocityEntity1Pda = initializeComponent.componentPda; // Saved for later
- });
- it("Initialize Position Component on Entity 2", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity2Pda,
- componentId: exampleComponentPosition.programId,
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- });
- it("Initialize Position Component on Entity 4", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity4Pda,
- componentId: exampleComponentPosition.programId,
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- componentPositionEntity4Pda = initializeComponent.componentPda; // Saved for later
- });
- it("Initialize Position Component on Entity 5 (with authority)", async () => {
- const initializeComponent = await InitializeComponent({
- payer: provider.wallet.publicKey,
- entity: entity5Pda,
- componentId: exampleComponentPosition.programId,
- authority: provider.wallet.publicKey,
- });
- await provider.sendAndConfirm(initializeComponent.transaction);
- componentPositionEntity5Pda = initializeComponent.componentPda; // Saved for later
- });
- 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", async () => {
- const applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemSimpleMovement,
- entities: [
- {
- entity: entity1Pda,
- components: [{ componentId: exampleComponentPosition.programId }],
- },
- ],
- args: {
- direction: Direction.Up,
- },
- });
- await provider.sendAndConfirm(applySystem.transaction);
- 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 (Right) on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemSimpleMovement,
- entities: [
- {
- entity: entity1Pda,
- components: [{ componentId: exampleComponentPosition.programId }],
- },
- ],
- args: {
- direction: Direction.Right,
- },
- });
- await provider.sendAndConfirm(applySystem.transaction);
- 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(1);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Fly System on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemFly,
- entities: [
- {
- entity: entity1Pda,
- components: [{ componentId: exampleComponentPosition.programId }],
- },
- ],
- });
- await provider.sendAndConfirm(applySystem.transaction);
- 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(1);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply System Velocity on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemApplyVelocity,
- entities: [
- {
- entity: entity1Pda,
- components: [
- {
- componentId: exampleComponentVelocity.programId,
- seed: "component-velocity",
- },
- { componentId: exampleComponentPosition.programId },
- ],
- },
- ],
- });
- await provider.sendAndConfirm(applySystem.transaction);
- 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(1);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply System Velocity on Entity 1, with Clock external account", async () => {
- const applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemApplyVelocity,
- entities: [
- {
- entity: entity1Pda,
- components: [
- {
- componentId: exampleComponentVelocity.programId,
- seed: "component-velocity",
- },
- { componentId: exampleComponentPosition.programId },
- ],
- },
- ],
- extraAccounts: [
- {
- pubkey: new web3.PublicKey(
- "SysvarC1ock11111111111111111111111111111111"
- ),
- isWritable: false,
- isSigner: false,
- },
- ],
- });
- await provider.sendAndConfirm(applySystem.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(1);
- expect(position.z.toNumber()).to.equal(300);
- });
- it("Apply Fly System on Entity 4", async () => {
- const applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemFly,
- entities: [
- {
- entity: entity4Pda,
- components: [{ componentId: exampleComponentPosition.programId }],
- },
- ],
- });
- await provider.sendAndConfirm(applySystem.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 applySystem = await ApplySystem({
- authority: provider.wallet.publicKey,
- systemId: exampleSystemFly,
- entities: [
- {
- entity: entity5Pda,
- components: [{ componentId: exampleComponentPosition.programId }],
- },
- ],
- });
- let failed = false;
- try {
- await provider.sendAndConfirm(applySystem.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("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 delegateIx = createDelegateInstruction({
- entity: entity1Pda,
- account: componentPositionEntity1Pda,
- ownerProgram: exampleComponentPosition.programId,
- payer: provider.wallet.publicKey,
- });
- const tx = new anchor.web3.Transaction().add(delegateIx);
- await provider.sendAndConfirm(tx);
- const acc = await provider.connection.getAccountInfo(
- componentPositionEntity1Pda
- );
- expect(acc.owner.toString()).to.equal(DELEGATION_PROGRAM_ID);
- });
- it("Check component undelegation", async () => {
- const delegateIx = createUndelegateInstruction({
- payer: provider.wallet.publicKey,
- delegatedAccount: componentPositionEntity1Pda,
- ownerProgram: exampleComponentPosition.programId,
- reimbursement: provider.wallet.publicKey,
- });
- const tx = new anchor.web3.Transaction().add(delegateIx);
- await provider.sendAndConfirm(tx);
- const acc = await provider.connection.getAccountInfo(
- componentPositionEntity1Pda
- );
- expect(acc.owner).to.deep.equal(exampleComponentPosition.programId);
- });
- });
|