Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow passing multiple entities in the ApplySystem function #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9d6ce88
moving non-generated files out of the generated folder
crypto-vincent Jun 13, 2024
4c156a9
move-non-generated-code
crypto-vincent Jun 13, 2024
4e4aa90
json-formatting
crypto-vincent Jun 13, 2024
4f2ff47
using-original-generated-code
crypto-vincent Jun 13, 2024
a1efd78
remove-double-declaration
crypto-vincent Jun 13, 2024
a453b2e
make-sure-instructions-parameters-are-passed-on-optionals
crypto-vincent Jun 13, 2024
80486f2
moving files out of generated folder
crypto-vincent Jun 13, 2024
51037eb
modified apply system function to take multiple entities
crypto-vincent Jun 13, 2024
0003b4a
component-id-param-name
crypto-vincent Jun 13, 2024
d18435b
nit-naming
crypto-vincent Jun 13, 2024
86a3502
removing lib from git and continue on fixing integration tests
crypto-vincent Jun 13, 2024
b990a2c
remove-lib
crypto-vincent Jun 13, 2024
9a66231
Upload progressg
crypto-vincent Jun 14, 2024
45f55a8
saving-progress-before-splitting-pr
crypto-vincent Jun 14, 2024
a0cf4e4
Merge branch 'main' into vbrunet/2024_06_12-update-transaction-apply-…
crypto-vincent Jun 14, 2024
13b9c63
yarn-build
crypto-vincent Jun 14, 2024
09d4052
Merge branch 'vbrunet/2024_06_12-update-transaction-apply-system' of …
crypto-vincent Jun 14, 2024
cbdcd22
update-before-merge
crypto-vincent Jun 14, 2024
57727de
merge-conflicts
crypto-vincent Jun 14, 2024
bba01f4
up
crypto-vincent Jun 14, 2024
c46afba
fix bolt init output for new api
crypto-vincent Jun 14, 2024
3b77834
cleanup api
crypto-vincent Jun 14, 2024
1b70434
polish new api
crypto-vincent Jun 14, 2024
695ea93
fix variable name typo
crypto-vincent Jun 17, 2024
ef603a4
fixing tests
crypto-vincent Jun 17, 2024
6743950
using seeds for velocity component
crypto-vincent Jun 17, 2024
86bda9e
polishing tests
crypto-vincent Jun 17, 2024
592982a
saving progress before meeting
crypto-vincent Jun 17, 2024
fc2ee9e
continue fixing tests
crypto-vincent Jun 17, 2024
0bd6c26
fix-component-index-in-apply
crypto-vincent Jun 17, 2024
8806f6d
get validator error message
crypto-vincent Jun 17, 2024
af22183
once again checking the error messages
crypto-vincent Jun 17, 2024
dccd1b0
update-expected-results
crypto-vincent Jun 17, 2024
d7935d8
everything should pass now
crypto-vincent Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 39 additions & 35 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ import {{
AddEntity,
InitializeComponent,
ApplySystem,
FindComponentPda,
}} from "@magicblock-labs/bolt-sdk"
import {{expect}} from "chai";

Expand All @@ -422,6 +421,7 @@ describe("{}", () => {{
// Constants used to test the program.
let worldPda: PublicKey;
let entityPda: PublicKey;
let componentPda: PublicKey;

const positionComponent = anchor.workspace.Position as Program<Position>;
const systemMovement = anchor.workspace.Movement as Program<Movement>;
Expand All @@ -437,47 +437,51 @@ describe("{}", () => {{
}});

it("Add an entity", async () => {{
const addEntity = await AddEntity({{
payer: provider.wallet.publicKey,
world: worldPda,
connection: provider.connection,
}});
const txSign = await provider.sendAndConfirm(addEntity.transaction);
entityPda = addEntity.entityPda;
console.log(`Initialized a new Entity (ID=${{addEntity.entityId}}). Initialization signature: ${{txSign}}`);
const addEntity = await AddEntity({{
payer: provider.wallet.publicKey,
world: worldPda,
connection: provider.connection,
}});
const txSign = await provider.sendAndConfirm(addEntity.transaction);
entityPda = addEntity.entityPda;
console.log(`Initialized a new Entity (ID=${{addEntity.entityId}}). Initialization signature: ${{txSign}}`);
}});

it("Add a component", async () => {{
const initComponent = await InitializeComponent({{
payer: provider.wallet.publicKey,
entity: entityPda,
componentId: positionComponent.programId,
}});
const txSign = await provider.sendAndConfirm(initComponent.transaction);
console.log(`Initialized the grid component. Initialization signature: ${{txSign}}`);
const initializeComponent = await InitializeComponent({{
payer: provider.wallet.publicKey,
entity: entityPda,
componentId: positionComponent.programId,
}});
const txSign = await provider.sendAndConfirm(initializeComponent.transaction);
componentPda = initializeComponent.componentPda;
console.log(`Initialized the grid component. Initialization signature: ${{txSign}}`);
}});

it("Apply a system", async () => {{
const positionComponentPda = FindComponentPda(positionComponent.programId, entityPda);
// Check that the component has been initialized and x is 0
let positionData = await positionComponent.account.position.fetch(
positionComponentPda
);

const applySystem = await ApplySystem({{
authority: provider.wallet.publicKey,
system: systemMovement.programId,
// Check that the component has been initialized and x is 0
const positionBefore = await positionComponent.account.position.fetch(
componentPda
);
expect(positionBefore.x.toNumber()).to.equal(0);

// Run the movement system
const applySystem = await ApplySystem({{
authority: provider.wallet.publicKey,
systemId: systemMovement.programId,
entities: [{{
entity: entityPda,
components: [positionComponent.programId],
}});
const txSign = await provider.sendAndConfirm(applySystem.transaction);
console.log(`Applied a system. Signature: ${{txSign}}`);

// Check that the system has been applied and x is > 0
positionData = await positionComponent.account.position.fetch(
positionComponentPda
);
expect(positionData.x.toNumber()).to.gt(0);
components: [{{ componentId: positionComponent.programId }}],
}}]
}});
const txSign = await provider.sendAndConfirm(applySystem.transaction);
console.log(`Applied a system. Signature: ${{txSign}}`);

// Check that the system has been applied and x is > 0
const positionAfter = await positionComponent.account.position.fetch(
componentPda
);
expect(positionAfter.x.toNumber()).to.gt(0);
}});

}});
Expand Down
83 changes: 43 additions & 40 deletions clients/bolt-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,78 @@
import { PublicKey } from "@solana/web3.js";
import BN from "bn.js";
import type BN from "bn.js";
import { PROGRAM_ID } from "./generated";
export * from "./generated/accounts";
export * from "./generated/instructions";
export * from "./transactions/transactions";
export * from "./world/transactions";
export * from "./delegation/accounts";
export * from "./delegation/delegate";
export * from "./delegation/undelegate";

export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey(
"Sysvar1nstructions1111111111111111111111111"
);

export function FindWorldRegistryPda(
programId: PublicKey = new PublicKey(PROGRAM_ID)
) {
export function FindRegistryPda({ programId }: { programId?: PublicKey }) {
return PublicKey.findProgramAddressSync(
[Buffer.from("registry")],
programId
programId ?? PROGRAM_ID
)[0];
}

export function FindWorldPda(
id: BN | string | number | Uint8Array,
programId: PublicKey = new PublicKey(PROGRAM_ID)
) {
id = CastToBN(id);
const idBuffer = Buffer.from(id.toArrayLike(Buffer, "be", 8));
export function FindWorldPda({
worldId,
programId,
}: {
worldId: BN;
programId?: PublicKey;
}) {
const idBuffer = Buffer.from(worldId.toArrayLike(Buffer, "be", 8));
return PublicKey.findProgramAddressSync(
[Buffer.from("world"), idBuffer],
programId
programId ?? PROGRAM_ID
)[0];
}

export function FindEntityPda(
worldId: BN | string | number | Uint8Array,
entityId: BN | string | number | Uint8Array,
extraSeed?: string,
programId: PublicKey = new PublicKey(PROGRAM_ID)
) {
worldId = CastToBN(worldId);
entityId = CastToBN(entityId);
export function FindEntityPda({
worldId,
entityId,
seed,
programId,
}: {
worldId: BN;
entityId?: BN;
seed?: string;
programId?: PublicKey;
}) {
const worldIdBuffer = Buffer.from(worldId.toArrayLike(Buffer, "be", 8));
const entityIdBuffer = Buffer.from(entityId.toArrayLike(Buffer, "be", 8));
const seeds = [Buffer.from("entity"), worldIdBuffer];
if (extraSeed != null) {
if (seed !== undefined) {
seeds.push(Buffer.from(new Uint8Array(8)));
seeds.push(Buffer.from(extraSeed));
} else {
seeds.push(Buffer.from(seed));
} else if (entityId !== undefined) {
const entityIdBuffer = Buffer.from(entityId.toArrayLike(Buffer, "be", 8));
seeds.push(entityIdBuffer);
} else {
throw new Error("An entity must have either an Id or a Seed");
}
return PublicKey.findProgramAddressSync(seeds, programId)[0];
return PublicKey.findProgramAddressSync(seeds, programId ?? PROGRAM_ID)[0];
}

export function FindComponentPda(
componentProgramId: PublicKey,
entity: PublicKey,
componentId: string = ""
) {
export function FindComponentPda({
componentId,
entity,
seed,
}: {
componentId: PublicKey;
entity: PublicKey;
seed?: string;
}) {
return PublicKey.findProgramAddressSync(
[Buffer.from(componentId), entity.toBytes()],
componentProgramId
[Buffer.from(seed ?? ""), entity.toBytes()],
componentId
)[0];
}

function CastToBN(id: BN | string | number | Uint8Array) {
if (!(id instanceof BN)) {
id = new BN(id);
}
return id;
}

/**
* Serialize arguments to a buffer
* @param args
Expand Down
Loading
Loading