Skip to content

Commit

Permalink
feat: add missing apply function in sdk and add test for it (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: obstoody <[email protected]>
  • Loading branch information
notdanilo and obstoody authored Dec 3, 2024
1 parent f0a98a0 commit acb50b4
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 29 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
98 changes: 74 additions & 24 deletions clients/bolt-sdk/src/world/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
createApplyInstruction,
createAddEntityInstruction,
createInitializeComponentInstruction,
createInitializeNewWorldInstruction,
Expand Down Expand Up @@ -47,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 @@ -87,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 @@ -128,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 @@ -164,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 @@ -200,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 @@ -242,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 @@ -287,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 @@ -296,13 +303,55 @@ 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,
};
}

export async function Apply({
authority,
boltSystem,
boltComponent,
componentProgram,
anchorRemainingAccounts,
world,
args,
}: {
authority: PublicKey;
boltSystem: PublicKey;
boltComponent: PublicKey;
componentProgram: PublicKey;
world: PublicKey;
anchorRemainingAccounts?: web3.AccountMeta[];
args: Uint8Array;
}): Promise<{
instruction: TransactionInstruction;
transaction: Transaction;
}> {
const instruction = createApplyInstruction(
{
authority,
boltSystem,
boltComponent,
componentProgram,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts,
world,
},
{
args,
},
);
const transaction = new Transaction().add(instruction);
return {
instruction,
transaction,
};
}

interface ApplySystemInstruction {
authority: PublicKey;
systemId: PublicKey;
Expand Down Expand Up @@ -410,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 acb50b4

Please sign in to comment.