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

✨ Adding low and intermediate api-levels #120

Merged
merged 11 commits into from
Jan 10, 2025
9 changes: 8 additions & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ members = [
]

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/bolt.ts"
test = """
echo "Waiting for 2 seconds..."
sleep 2
echo "Running low level API tests..."
yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/bolt.low-level.api.ts
echo "Running intermediate level API tests..."
yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/bolt.intermediate-level.api.ts
"""

[test]
startup_wait = 5000
Expand Down
2 changes: 1 addition & 1 deletion clients/bolt-sdk/src/generated/idl/world.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"address": "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n",
"metadata": {
"name": "world",
"version": "0.1.12",
"version": "0.2.0",
"spec": "0.1.0",
"description": "Bolt World program",
"repository": "https://github.com/magicblock-labs/bolt"
Expand Down
1 change: 1 addition & 0 deletions clients/bolt-sdk/src/generated/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./world";
2 changes: 1 addition & 1 deletion clients/bolt-sdk/src/generated/types/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type World = {
address: "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n";
metadata: {
name: "world";
version: "0.1.12";
version: "0.2.0";
spec: "0.1.0";
description: "Bolt World program";
repository: "https://github.com/magicblock-labs/bolt";
Expand Down
14 changes: 10 additions & 4 deletions clients/bolt-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PublicKey } from "@solana/web3.js";
import type BN from "bn.js";
import { PROGRAM_ID } from "./generated";
import { PROGRAM_ID as WORLD_PROGRAM_ID } from "./generated";
import { World as WORLD_PROGRAM_IDL } from "./generated/types";
export * from "./generated/accounts";
export * from "./generated/instructions";
export * from "./world/transactions";
Expand All @@ -12,6 +13,7 @@ export { DELEGATION_PROGRAM_ID } from "@magicblock-labs/ephemeral-rollups-sdk";
import * as anchor from "@coral-xyz/anchor";
export { anchor };
export { Provider, Program, Wallet, web3, workspace } from "@coral-xyz/anchor";
export { WORLD_PROGRAM_ID, WORLD_PROGRAM_IDL };

export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey(
"Sysvar1nstructions1111111111111111111111111",
Expand All @@ -20,7 +22,7 @@ export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey(
export function FindRegistryPda({ programId }: { programId?: PublicKey }) {
return PublicKey.findProgramAddressSync(
[Buffer.from("registry")],
programId ?? PROGRAM_ID,
programId ?? WORLD_PROGRAM_ID,
)[0];
}

Expand All @@ -34,7 +36,7 @@ export function FindWorldPda({
const idBuffer = Buffer.from(worldId.toArrayLike(Buffer, "be", 8));
return PublicKey.findProgramAddressSync(
[Buffer.from("world"), idBuffer],
programId ?? PROGRAM_ID,
programId ?? WORLD_PROGRAM_ID,
)[0];
}

Expand All @@ -60,9 +62,13 @@ export function FindEntityPda({
} else {
throw new Error("An entity must have either an Id or a Seed");
}
return PublicKey.findProgramAddressSync(seeds, programId ?? PROGRAM_ID)[0];
return PublicKey.findProgramAddressSync(
seeds,
programId ?? WORLD_PROGRAM_ID,
)[0];
}

// TODO: seed must be Uint8Array like the other FindPda functions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: this TODO should be addressed before merging - inconsistent seed types between functions could lead to runtime errors

export function FindComponentPda({
componentId,
entity,
Expand Down
67 changes: 24 additions & 43 deletions clients/bolt-sdk/src/world/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,34 @@ import {
type TransactionInstruction,
} from "@solana/web3.js";
import type WorldProgram from "../generated";
import { PROGRAM_ID, worldIdl } from "../generated";
import {
createInitializeRegistryInstruction,
PROGRAM_ID,
worldIdl,
} from "../generated";
import { type Idl, Program } from "@coral-xyz/anchor";

const MAX_COMPONENTS = 5;

export async function InitializeRegistry({
payer,
connection,
}: {
payer: PublicKey;
connection: Connection;
}): Promise<{
instruction: TransactionInstruction;
transaction: Transaction;
}> {
const registry = FindRegistryPda({});
const instruction = createInitializeRegistryInstruction({ registry, payer });
const transaction = new Transaction().add(instruction);
return {
instruction,
transaction,
};
}

/**
* Create the transaction to Initialize a new world
* @param payer
Expand Down Expand Up @@ -311,47 +334,6 @@ export async function InitializeComponent({
};
}

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 @@ -398,7 +380,6 @@ async function createApplySystemInstruction({
const applyAccounts = {
authority: authority ?? PROGRAM_ID,
boltSystem: systemId,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
world,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/programs/bolt-component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ custom-heap = []
custom-panic = []

[dependencies]
anchor-lang = { workspace = true }
bolt-system = { workspace = true, cpi = true }
anchor-lang.workspace = true
bolt-system.workspace = true
4 changes: 2 additions & 2 deletions crates/programs/bolt-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ custom-heap = []
custom-panic = []

[dependencies]
anchor-lang = { workspace = true }
bolt-helpers-system-template = { workspace = true, cpi = true }
anchor-lang.workspace = true
bolt-helpers-system-template.workspace = true
12 changes: 6 additions & 6 deletions crates/programs/world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ custom-heap = []
custom-panic = []

[dependencies]
anchor-lang = { workspace = true }
bolt-component = { workspace = true, cpi = true }
bolt-helpers-world-apply = { workspace = true }
bolt-system = { workspace = true, cpi = true }
solana-security-txt = { workspace = true }
tuple-conv = { workspace = true }
anchor-lang.workspace = true
bolt-component.workspace = true
bolt-helpers-world-apply.workspace = true
bolt-system.workspace = true
solana-security-txt.workspace = true
tuple-conv.workspace = true

23 changes: 11 additions & 12 deletions tests/bolt.ts → tests/bolt.intermediate-level.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import { type Position } from "../target/types/position";
import { type Velocity } from "../target/types/velocity";
import { type BoltComponent } from "../target/types/bolt_component";
import { type SystemSimpleMovement } from "../target/types/system_simple_movement";
import { type World } from "../target/types/world";
import { type SystemFly } from "../target/types/system_fly";
import { type SystemApplyVelocity } from "../target/types/system_apply_velocity";
import { expect } from "chai";
import type BN from "bn.js";
import {
AddEntity,
createInitializeRegistryInstruction,
DELEGATION_PROGRAM_ID,
FindRegistryPda,
InitializeRegistry,
InitializeComponent,
InitializeNewWorld,
ApplySystem,
Expand All @@ -24,6 +22,7 @@ import {
type Program,
anchor,
web3,
WORLD_PROGRAM_IDL as World,
} from "../clients/bolt-sdk";

enum Direction {
Expand Down Expand Up @@ -106,13 +105,15 @@ describe("bolt", () => {
const secondAuthority = Keypair.generate().publicKey;

it("InitializeRegistry", async () => {
const registryPda = FindRegistryPda({});
const initializeRegistryIx = createInitializeRegistryInstruction({
registry: registryPda,
const initializeRegistry = await InitializeRegistry({
payer: provider.wallet.publicKey,
connection: provider.connection,
});
const tx = new anchor.web3.Transaction().add(initializeRegistryIx);
await provider.sendAndConfirm(tx);
try {
await provider.sendAndConfirm(initializeRegistry.transaction);
} catch (error) {
// This is expected to fail because the registry already exists if another api level test ran before
}
});

it("InitializeNewWorld", async () => {
Expand Down Expand Up @@ -216,7 +217,7 @@ describe("bolt", () => {
const addEntity = await AddEntity({
payer: provider.wallet.publicKey,
world: worldPda,
seed: Buffer.from("extra-seed"),
seed: Buffer.from("custom-seed"),
connection: provider.connection,
});
await provider.sendAndConfirm(addEntity.transaction);
Expand Down Expand Up @@ -325,9 +326,7 @@ describe("bolt", () => {
},
],
world: worldPda,
args: {
direction: Direction.Up,
},
args: { direction: Direction.Up },
});

await provider.sendAndConfirm(apply.transaction);
Expand Down
Loading
Loading