Skip to content

Commit

Permalink
✨ Update client SDK to use default component id
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Jan 28, 2024
1 parent 9d348f2 commit 9f84e9d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 65 deletions.
2 changes: 1 addition & 1 deletion clients/bolt-sdk/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export declare function FindEntityPda(
export declare function FindComponentPda(
componentProgramId: PublicKey,
entity: PublicKey,
componentId: string
componentId?: string
): PublicKey;
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion clients/bolt-sdk/lib/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions clients/bolt-sdk/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/bolt-sdk/lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/bolt-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bolt-sdk",
"version": "0.0.9",
"version": "0.0.10",
"description": "Bolt typescript SDK",
"author": "[email protected]",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion clients/bolt-sdk/src/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function FindEntityPda(
export function FindComponentPda(
componentProgramId: PublicKey,
entity: PublicKey,
componentId: string
componentId: string = ""
) {
return PublicKey.findProgramAddressSync(
[Buffer.from(componentId), entity.toBytes()],
Expand Down
3 changes: 1 addition & 2 deletions crates/bolt-lang/attribute/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn bolt_account(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut component_id_value = None;

if !attr.is_empty() {

let attr_meta = parse_macro_input!(attr as Meta);

component_id_value = match attr_meta {
Expand All @@ -48,7 +47,7 @@ pub fn bolt_account(attr: TokenStream, item: TokenStream) -> TokenStream {
None
}
}),
_ => None
_ => None,
};
}

Expand Down
88 changes: 30 additions & 58 deletions tests/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { SystemApplyVelocity } from "../target/types/system_apply_velocity";
import { World } from "../target/types/world";
import { expect } from "chai";
import BN from "bn.js";
import {
FindComponentPda,
FindEntityPda,
FindWorldPda,
FindWorldRegistryPda,
} from "../clients/bolt-sdk";

enum Direction {
Left = "Left",
Expand Down Expand Up @@ -57,7 +63,7 @@ describe("bolt", () => {
let componentVelocityEntity1: PublicKey;

it("InitializeWorldsRegistry", async () => {
const registryPda = FindWorldRegistryPda(worldProgram);
const registryPda = FindWorldRegistryPda(worldProgram.programId);
await worldProgram.methods
.initializeRegistry()
.accounts({
Expand All @@ -68,9 +74,9 @@ describe("bolt", () => {
});

it("InitializeNewWorld", async () => {
const registryPda = FindWorldRegistryPda(worldProgram);
const registryPda = FindWorldRegistryPda(worldProgram.programId);

const worldPda = FindWorldPda(worldProgram, new BN(0));
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
await worldProgram.methods
.initializeNewWorld()
.accounts({
Expand All @@ -82,9 +88,9 @@ describe("bolt", () => {
});

it("InitializeNewWorld 2", async () => {
const registryPda = FindWorldRegistryPda(worldProgram);
const registryPda = FindWorldRegistryPda(worldProgram.programId);

const worldPda = FindWorldPda(worldProgram, new BN(1));
const worldPda = FindWorldPda(new BN(1), worldProgram.programId);
await worldProgram.methods
.initializeNewWorld()
.accounts({
Expand All @@ -96,8 +102,8 @@ describe("bolt", () => {
});

it("Add entity 1", async () => {
const worldPda = FindWorldPda(worldProgram, new BN(0));
entity1 = FindEntityPda(worldProgram, new BN(0), new BN(0));
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
entity1 = FindEntityPda(new BN(0), new BN(0), null, worldProgram.programId);
await worldProgram.methods
.addEntity(null)
.accounts({
Expand All @@ -109,9 +115,9 @@ describe("bolt", () => {
});

it("Add entity 2", async () => {
const worldPda = FindWorldPda(worldProgram, new BN(0));
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);

entity2 = FindEntityPda(worldProgram, new BN(0), new BN(1));
entity2 = FindEntityPda(new BN(0), new BN(1), null, worldProgram.programId);
await worldProgram.methods
.addEntity(null)
.accounts({
Expand All @@ -123,9 +129,14 @@ describe("bolt", () => {
});

it("Add entity 3", async () => {
const worldPda = FindWorldPda(worldProgram, new BN(0));
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);

const entityPda = FindEntityPda(worldProgram, new BN(0), new BN(2));
const entityPda = FindEntityPda(
new BN(0),
new BN(2),
null,
worldProgram.programId
);
await worldProgram.methods
.addEntity(null)
.accounts({
Expand All @@ -137,9 +148,14 @@ describe("bolt", () => {
});

it("Add entity 4 with extra seeds", async () => {
const worldPda = FindWorldPda(worldProgram, new BN(0));
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
const seed = "extra-seed";
let entity3 = FindEntityPda(worldProgram, new BN(0), new BN(3), seed);
let entity3 = FindEntityPda(
new BN(0),
new BN(3),
seed,
worldProgram.programId
);

await worldProgram.methods
.addEntity(seed)
Expand Down Expand Up @@ -188,8 +204,7 @@ describe("bolt", () => {
it("Initialize Position Component on Entity 1", async () => {
componentPositionEntity1 = FindComponentPda(
boltComponentPositionProgram.programId,
entity1,
""
entity1
);

console.log("Component Position E1: ", componentPositionEntity1.toBase58());
Expand Down Expand Up @@ -464,47 +479,4 @@ describe("bolt", () => {
console.log("| |");
console.log("+-----------------------------+");
});

// Utils

function FindWorldRegistryPda(program: Program<World>) {
return PublicKey.findProgramAddressSync(
[Buffer.from("registry")],
program.programId
)[0];
}

function FindWorldPda(program: Program<World>, id: BN) {
return PublicKey.findProgramAddressSync(
[Buffer.from("world"), id.toBuffer("be", 8)],
program.programId
)[0];
}

function FindEntityPda(
program: Program<World>,
worldId: BN,
entityId: BN,
extraSeed?: string
) {
let seeds = [Buffer.from("entity"), worldId.toBuffer("be", 8)];
if (extraSeed) {
seeds.push(Buffer.from(new Uint8Array(8)));
seeds.push(Buffer.from(extraSeed));
} else {
seeds.push(entityId.toBuffer("be", 8));
}
return PublicKey.findProgramAddressSync(seeds, program.programId)[0];
}

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

0 comments on commit 9f84e9d

Please sign in to comment.