Skip to content

Commit

Permalink
fix: fixing apply and adding a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 29, 2024
1 parent 8c3ae4a commit 1fb2e7c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 42 deletions.
6 changes: 6 additions & 0 deletions clients/bolt-sdk/src/generated/instructions/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface ApplyInstructionAccounts {
boltComponent: web3.PublicKey;
authority: web3.PublicKey;
instructionSysvarAccount: web3.PublicKey;
world: web3.PublicKey;
anchorRemainingAccounts?: web3.AccountMeta[];
}

Expand Down Expand Up @@ -102,6 +103,11 @@ export function createApplyInstruction(
isWritable: false,
isSigner: false,
},
{
pubkey: accounts.world,
isWritable: false,
isSigner: false,
},
];

if (accounts.anchorRemainingAccounts != null) {
Expand Down
89 changes: 52 additions & 37 deletions clients/bolt-sdk/src/world/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ export async function InitializeNewWorld({
const registry = await Registry.fromAccountAddress(connection, registryPda);
const worldId = new BN(registry.worlds);
const worldPda = FindWorldPda({ worldId });
const initializeWorldIx = createInitializeNewWorldInstruction({
const instruction = createInitializeNewWorldInstruction({
world: worldPda,
registry: registryPda,
payer,
});
const transaction = new Transaction().add(instruction);
return {
instruction: initializeWorldIx,
transaction: new Transaction().add(initializeWorldIx),
instruction,
transaction,
worldPda,
worldId,
};
Expand Down Expand Up @@ -88,17 +89,18 @@ export async function AddAuthority({
) as unknown as Program<WorldProgram>;
const worldInstance = await World.fromAccountAddress(connection, world);
const worldId = new BN(worldInstance.id);
const addAuthorityIx = await program.methods
const instruction = await program.methods
.addAuthority(worldId)
.accounts({
authority,
newAuthority,
world,
})
.instruction();
const transaction = new Transaction().add(instruction);
return {
instruction: addAuthorityIx,
transaction: new Transaction().add(addAuthorityIx),
instruction,
transaction,
};
}

Expand Down Expand Up @@ -129,17 +131,18 @@ export async function RemoveAuthority({
) as unknown as Program<WorldProgram>;
const worldInstance = await World.fromAccountAddress(connection, world);
const worldId = new BN(worldInstance.id);
const removeAuthorityIx = await program.methods
const instruction = await program.methods
.removeAuthority(worldId)
.accounts({
authority,
authorityToDelete,
world,
})
.instruction();
const transaction = new Transaction().add(instruction);
return {
instruction: removeAuthorityIx,
transaction: new Transaction().add(removeAuthorityIx),
instruction,
transaction,
};
}

Expand All @@ -165,17 +168,18 @@ export async function ApproveSystem({
const program = new Program(
worldIdl as Idl,
) as unknown as Program<WorldProgram>;
const approveSystemIx = await program.methods
const instruction = await program.methods
.approveSystem()
.accounts({
authority,
system: systemToApprove,
world,
})
.instruction();
const transaction = new Transaction().add(instruction);
return {
instruction: approveSystemIx,
transaction: new Transaction().add(approveSystemIx),
instruction,
transaction,
};
}

Expand All @@ -201,17 +205,18 @@ export async function RemoveSystem({
const program = new Program(
worldIdl as Idl,
) as unknown as Program<WorldProgram>;
const removeSystemIx = await program.methods
const instruction = await program.methods
.removeSystem()
.accounts({
authority,
system: systemToRemove,
world,
})
.instruction();
const transaction = new Transaction().add(instruction);
return {
instruction: removeSystemIx,
transaction: new Transaction().add(removeSystemIx),
instruction,
transaction,
};
}

Expand Down Expand Up @@ -243,17 +248,18 @@ export async function AddEntity({
seed !== undefined
? FindEntityPda({ worldId, seed })
: FindEntityPda({ worldId, entityId: new BN(worldInstance.entities) });
const addEntityIx = createAddEntityInstruction(
const instruction = createAddEntityInstruction(
{
world,
payer,
entity: entityPda,
},
{ extraSeed: seed ?? null },
);
const transaction = new Transaction().add(instruction);
return {
instruction: addEntityIx,
transaction: new Transaction().add(addEntityIx),
instruction,
transaction,
entityPda,
};
}
Expand Down Expand Up @@ -288,7 +294,7 @@ export async function InitializeComponent({
componentPda: PublicKey;
}> {
const componentPda = FindComponentPda({ componentId, entity, seed });
const initializeComponentIx = createInitializeComponentInstruction({
const instruction = createInitializeComponentInstruction({
payer,
entity,
data: componentPda,
Expand All @@ -297,9 +303,10 @@ export async function InitializeComponent({
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts,
});
const transaction = new Transaction().add(instruction);
return {
instruction: initializeComponentIx,
transaction: new Transaction().add(initializeComponentIx),
instruction,
transaction,
componentPda,
};
}
Expand All @@ -310,31 +317,38 @@ export async function Apply({
boltComponent,
componentProgram,
anchorRemainingAccounts,
args
world,
args,
}: {
authority: PublicKey;
boltSystem: PublicKey;
boltComponent: PublicKey;
componentProgram: PublicKey;
world: PublicKey;
anchorRemainingAccounts?: web3.AccountMeta[];
args: Uint8Array;
}): Promise<{
instruction: TransactionInstruction;
transaction: Transaction;
}> {
const initializeComponentIx = createApplyInstruction({
authority,
boltSystem,
boltComponent,
componentProgram,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts
}, {
args
});
const instruction = createApplyInstruction(
{
authority,
boltSystem,
boltComponent,
componentProgram,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts,
world,
},
{
args,
},
);
const transaction = new Transaction().add(instruction);
return {
instruction: initializeComponentIx,
transaction: new Transaction().add(initializeComponentIx)
instruction,
transaction,
};
}

Expand Down Expand Up @@ -445,16 +459,17 @@ export async function ApplySystem({
extraAccounts?: web3.AccountMeta[];
args?: object;
}): Promise<{ instruction: TransactionInstruction; transaction: Transaction }> {
const applySystemIx = await createApplySystemInstruction({
const instruction = await createApplySystemInstruction({
authority,
systemId,
entities,
world,
extraAccounts,
args,
});
const transaction = new Transaction().add(instruction);
return {
instruction: applySystemIx,
transaction: new Transaction().add(applySystemIx),
instruction,
transaction,
};
}
36 changes: 31 additions & 5 deletions tests/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
InitializeComponent,
InitializeNewWorld,
ApplySystem,
Apply,
DelegateComponent,
AddAuthority,
RemoveAuthority,
Expand All @@ -24,6 +25,7 @@ import {
type Program,
anchor,
web3,
SerializeArgs,
} from "../clients/bolt-sdk";

enum Direction {
Expand Down Expand Up @@ -314,6 +316,30 @@ describe("bolt", () => {
expect(position.z.toNumber()).to.equal(0);
});

it("Apply Simple Movement System (Up) on Entity 1 using Apply", async () => {
console.log("Apply", Apply);
const apply = await Apply({
authority: provider.wallet.publicKey,
boltSystem: exampleSystemSimpleMovement,
boltComponent: componentPositionEntity1Pda,
componentProgram: exampleComponentPosition.programId,
world: worldPda,
args: SerializeArgs({
direction: Direction.Up,
}),
});

await provider.sendAndConfirm(apply.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 (Up) on Entity 1", async () => {
const applySystem = await ApplySystem({
authority: provider.wallet.publicKey,
Expand Down Expand Up @@ -341,7 +367,7 @@ describe("bolt", () => {
);
logPosition("Movement System: Entity 1", position);
expect(position.x.toNumber()).to.equal(0);
expect(position.y.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(2);
expect(position.z.toNumber()).to.equal(0);
});

Expand All @@ -367,7 +393,7 @@ describe("bolt", () => {
);
logPosition("Movement System: Entity 1", position);
expect(position.x.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(2);
expect(position.z.toNumber()).to.equal(0);
});

Expand All @@ -390,7 +416,7 @@ describe("bolt", () => {
);
logPosition("Fly System: Entity 1", position);
expect(position.x.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(2);
expect(position.z.toNumber()).to.equal(1);
});

Expand Down Expand Up @@ -428,7 +454,7 @@ describe("bolt", () => {
);
logPosition("Apply System Velocity: Entity 1", position);
expect(position.x.toNumber()).to.greaterThan(1);
expect(position.y.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(2);
expect(position.z.toNumber()).to.equal(1);
});

Expand Down Expand Up @@ -466,7 +492,7 @@ describe("bolt", () => {
);
logPosition("Apply System Velocity: Entity 1", position);
expect(position.x.toNumber()).to.greaterThan(1);
expect(position.y.toNumber()).to.equal(1);
expect(position.y.toNumber()).to.equal(2);
expect(position.z.toNumber()).to.equal(300);
});

Expand Down

0 comments on commit 1fb2e7c

Please sign in to comment.