Skip to content

Commit

Permalink
polish new api
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-vincent committed Jun 14, 2024
1 parent 3b77834 commit 1b70434
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
5 changes: 3 additions & 2 deletions clients/bolt-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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";
Expand Down Expand Up @@ -49,7 +49,8 @@ export function FindEntityPda({
seeds.push(Buffer.from(new Uint8Array(8)));
seeds.push(Buffer.from(seed));
} else if (entityId !== undefined) {
seeds.push(Buffer.from(entityId.toArrayLike(Buffer, "be", 8)));
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");
}
Expand Down
18 changes: 9 additions & 9 deletions clients/bolt-sdk/src/world/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ export async function AddEntity({
}): Promise<{ transaction: Transaction; entityPda: PublicKey }> {
const worldInstance = await World.fromAccountAddress(connection, world);
const worldId = new BN(worldInstance.id);
const entityPda = seed
? FindEntityPda({ worldId, seed })
: FindEntityPda({ worldId, entityId: new BN(worldInstance.entities) });
const entityPda =
seed !== undefined
? FindEntityPda({ worldId, seed })
: FindEntityPda({ worldId, entityId: new BN(worldInstance.entities) });
const createEntityIx = createAddEntityInstruction(
{
world,
Expand Down Expand Up @@ -126,7 +127,6 @@ export async function InitializeComponent({
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts,
});

return {
transaction: new Transaction().add(initComponentIx),
componentPda,
Expand Down Expand Up @@ -182,11 +182,11 @@ function createApplySystemInstruction({

entities.forEach(function (entity) {
entity.components.forEach(function (component) {
const componentPda = FindComponentPda(
component.id,
entity.entity,
component.seed ?? ""
);
const componentPda = FindComponentPda({
componentId: component.id,
entity: entity.entity,
seed: component.seed,
});
instructionArgs[
getBoltComponentProgramName(componentCount, componentCount)
] = component.id;
Expand Down
54 changes: 23 additions & 31 deletions tests/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import BN from "bn.js";
import {
AddEntity,
createDelegateInstruction,
createUndelegateInstruction,
createInitializeRegistryInstruction,
DELEGATION_PROGRAM_ID,
FindComponentPda,
Expand All @@ -21,7 +22,6 @@ import {
InitializeNewWorld,
SYSVAR_INSTRUCTIONS_PUBKEY,
} from "../clients/bolt-sdk";
import { createUndelegateInstruction } from "../clients/bolt-sdk/lib/delegation/undelegate";
import { ApplySystem } from "../clients/bolt-sdk/src";

enum Direction {
Expand All @@ -31,46 +31,38 @@ enum Direction {
Down = "Down",
}

function serializeArgs(args: any = {}) {
const jsonString = JSON.stringify(args);
const encoder = new TextEncoder();
const binaryData = encoder.encode(jsonString);
return Buffer.from(
binaryData.buffer,
binaryData.byteOffset,
binaryData.byteLength
);
}

function padCenter(value: string, width: number) {
const length = value.length;
if (width <= length) {
return value;
}
const padding = (width - length) / 2;
const align = length - padding;
return value.padEnd(align, " ").padStart(width, " ");
}

function logPosition(title: string, { x, y, z }: { x: BN; y: BN; z: BN }) {
console.log(" +----------------------------+");
console.log(` | ${padCenter(title, 26)} |`);
console.log(" +---------------+------------+");
console.log(` | X Position | ${String(x).padEnd(10, " ")} |`);
console.log(` | Y Position | ${String(y).padEnd(10, " ")} |`);
console.log(` | Z Position | ${String(z).padEnd(10, " ")} |`);
console.log(" +---------------+------------+");
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, 26)} |`);
console.log(" +---------------+------------+");
console.log(` | X Velocity | ${String(x).padEnd(10, " ")} |`);
console.log(` | Y Velocity | ${String(y).padEnd(10, " ")} |`);
console.log(` | Z Velocity | ${String(z).padEnd(10, " ")} |`);
console.log(` | Last Applied | ${String(lastApplied).padEnd(10, " ")} |`);
console.log(" +---------------+------------+");
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", () => {
Expand Down Expand Up @@ -105,7 +97,7 @@ describe("bolt", () => {
let componentVelocityEntity1Pda: PublicKey;

it("InitializeWorldsRegistry", async () => {
const registryPda = FindWorldRegistryPda(boltWorld.programId);
const registryPda = FindWorldRegistryPda({});
const initializeRegistryIx = createInitializeRegistryInstruction({
registry: registryPda,
payer: provider.wallet.publicKey,
Expand Down Expand Up @@ -164,7 +156,7 @@ describe("bolt", () => {
const addEntity = await AddEntity({
payer: provider.wallet.publicKey,
world: worldPda,
seed: "extra-seed", // TODO(vbrunet) - extra seed doesn't work for some reason?
seed: "extra-seed",
connection: provider.connection,
});
await provider.sendAndConfirm(addEntity.transaction);
Expand Down Expand Up @@ -439,7 +431,7 @@ describe("bolt", () => {
})
.rpc();
} catch (e) {
console.log("ERROR:", e);
console.log("Check invalid init without CPI: error:", e);
invalid = true;
}
expect(invalid).to.equal(true);
Expand All @@ -462,7 +454,7 @@ describe("bolt", () => {
})
.rpc();
} catch (e) {
console.log("ERROR2:", e);
console.log("Check invalid update without CPI: error:", e);
invalid = true;
}
expect(invalid).to.equal(true);
Expand Down

0 comments on commit 1b70434

Please sign in to comment.