Skip to content

Commit

Permalink
✨ Simplify the Bolt typescript Sdk (#36)
Browse files Browse the repository at this point in the history
* ✨ Simplify the Bolt typescript Sdk

* ♻️ Generate ecs template using the simpler SDK syntax

* ⚡ Increase max components to 5
  • Loading branch information
GabrielePicco authored Apr 2, 2024
1 parent 71c10f5 commit 12eb5b0
Show file tree
Hide file tree
Showing 30 changed files with 1,611 additions and 101 deletions.
142 changes: 49 additions & 93 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,7 @@ pub fn mocha(name: &str) -> String {
r#"const anchor = require("@magicblock-labs/anchor");
const boltSdk = require("@magicblock-labs/bolt-sdk");
const {{
createInitializeNewWorldInstruction,
FindWorldPda,
FindWorldRegistryPda,
Registry,
World
InitializeNewWorld,
}} = boltSdk;
describe("{}", () => {{
Expand All @@ -361,19 +357,12 @@ describe("{}", () => {{
anchor.setProvider(provider);
it("InitializeNewWorld", async () => {{
const registry = await Registry.fromAccountAddress(provider.connection, registryPda);
worldId = new anchor.BN(registry.worlds);
worldPda = FindWorldPda(new anchor.BN(worldId))
const initializeWorldIx = createInitializeNewWorldInstruction(
{{
world: worldPda,
registry: registryPda,
payer: provider.wallet.publicKey,
}});
const tx = new anchor.web3.Transaction().add(initializeWorldIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new world (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
const initNewWorld = await InitializeNewWorld({{
payer: provider.wallet.publicKey,
connection: provider.connection,
}});
const txSign = await provider.sendAndConfirm(initNewWorld.transaction);
console.log(`Initialized a new world (ID=${{initNewWorld.worldPda}}). Initialization signature: ${{txSign}}`);
}});
}});
}});
Expand All @@ -387,11 +376,7 @@ pub fn jest(name: &str) -> String {
r#"const anchor = require("@magicblock-labs/anchor");
const boltSdk = require("@magicblock-labs/bolt-sdk");
const {{
createInitializeNewWorldInstruction,
FindWorldPda,
FindWorldRegistryPda,
Registry,
World
InitializeNewWorld,
}} = boltSdk;
describe("{}", () => {{
Expand All @@ -400,24 +385,16 @@ describe("{}", () => {{
anchor.setProvider(provider);
// Constants used to test the program.
const registryPda = FindWorldRegistryPda();
let worldId: anchor.BN;
let worldPda: PublicKey;
it("InitializeNewWorld", async () => {{
const registry = await Registry.fromAccountAddress(provider.connection, registryPda);
worldId = new anchor.BN(registry.worlds);
worldPda = FindWorldPda(new anchor.BN(worldId))
const initializeWorldIx = createInitializeNewWorldInstruction(
{{
world: worldPda,
registry: registryPda,
payer: provider.wallet.publicKey,
}});
const tx = new anchor.web3.Transaction().add(initializeWorldIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new world (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
const initNewWorld = await InitializeNewWorld({{
payer: provider.wallet.publicKey,
connection: provider.connection,
}});
const txSign = await provider.sendAndConfirm(initNewWorld.transaction);
worldPda = initNewWorld.worldPda;
console.log(`Initialized a new world (ID=${{worldPda}}). Initialization signature: ${{txSign}}`);
}});
}});
"#,
Expand All @@ -433,15 +410,11 @@ import {{ PublicKey }} from "@solana/web3.js";
import {{ Position }} from "../target/types/position";
import {{ Movement }} from "../target/types/movement";
import {{
createInitializeNewWorldInstruction,
FindWorldPda,
FindWorldRegistryPda,
FindEntityPda,
Registry,
World,
createAddEntityInstruction,
createInitializeComponentInstruction,
FindComponentPda, createApplyInstruction
InitializeNewWorld,
AddEntity,
InitializeComponent,
ApplySystem,
FindComponentPda,
}} from "@magicblock-labs/bolt-sdk"
import {{expect}} from "chai";
Expand All @@ -451,75 +424,58 @@ describe("{}", () => {{
anchor.setProvider(provider);
// Constants used to test the program.
const registryPda = FindWorldRegistryPda();
let worldId: anchor.BN;
let worldPda: PublicKey;
let entityPda: PublicKey;
const positionComponent = anchor.workspace.Position as Program<Position>;
const systemMovement = anchor.workspace.Movement as Program<Movement>;
it("InitializeNewWorld", async () => {{
const registry = await Registry.fromAccountAddress(provider.connection, registryPda);
worldId = new anchor.BN(registry.worlds);
worldPda = FindWorldPda(new anchor.BN(worldId))
const initializeWorldIx = createInitializeNewWorldInstruction(
{{
world: worldPda,
registry: registryPda,
payer: provider.wallet.publicKey,
}});
const tx = new anchor.web3.Transaction().add(initializeWorldIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new world (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
const initNewWorld = await InitializeNewWorld({{
payer: provider.wallet.publicKey,
connection: provider.connection,
}});
const txSign = await provider.sendAndConfirm(initNewWorld.transaction);
worldPda = initNewWorld.worldPda;
console.log(`Initialized a new world (ID=${{worldPda}}). Initialization signature: ${{txSign}}`);
}});
it("Add an entity", async () => {{
const world = await World.fromAccountAddress(provider.connection, worldPda);
const entityId = new anchor.BN(world.entities);
entityPda = FindEntityPda(worldId, entityId);
let createEntityIx = createAddEntityInstruction({{
world: worldPda,
payer: provider.wallet.publicKey,
entity: entityPda,
const addEntity = await AddEntity({{
payer: provider.wallet.publicKey,
worldPda: worldPda,
connection: provider.connection,
}});
const tx = new anchor.web3.Transaction().add(createEntityIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new Entity (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
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 positionComponentPda = FindComponentPda(positionComponent.programId, entityPda, "");
let initComponentIx = createInitializeComponentInstruction({{
const initComponent = await InitializeComponent({{
payer: provider.wallet.publicKey,
entity: entityPda,
data: positionComponentPda,
componentProgram: positionComponent.programId,
entityPda,
componentId: positionComponent.programId,
}});
const tx = new anchor.web3.Transaction().add(initComponentIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new component. Initialization signature: ${{txSign}}`);
const txSign = await provider.sendAndConfirm(initComponent.transaction);
console.log(`Initialized the grid component. Initialization signature: ${{txSign}}`);
}});
it("Apply a system", async () => {{
const positionComponentPda = FindComponentPda(positionComponent.programId, entityPda, "");
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
);
expect(positionData.x.toNumber()).to.eq(0);
let applySystemIx = createApplyInstruction({{
componentProgram: positionComponent.programId,
boltSystem: systemMovement.programId,
boltComponent: positionComponentPda,
authority: provider.wallet.publicKey,
}}, {{args: new Uint8Array()}});
const tx = new anchor.web3.Transaction().add(applySystemIx);
await provider.sendAndConfirm(tx);
const applySystem = await ApplySystem({{
authority: provider.wallet.publicKey,
boltSystem: systemMovement.programId,
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(
Expand Down
1 change: 1 addition & 0 deletions clients/bolt-sdk/.solitarc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
idlGenerator: "anchor",
programName: "world",
programId: "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n",
removeExistingIdl: false,
idlDir,
sdkDir,
binaryInstallDir,
Expand Down
144 changes: 143 additions & 1 deletion clients/bolt-sdk/idl/world.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.1",
"version": "0.1.1",
"name": "world",
"instructions": [
{
Expand Down Expand Up @@ -260,6 +260,148 @@
"type": "bytes"
}
]
},
{
"name": "apply4",
"accounts": [
{
"name": "boltSystem",
"isMut": false,
"isSigner": false
},
{
"name": "componentProgram1",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent1",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram2",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent2",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram3",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent3",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram4",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent4",
"isMut": true,
"isSigner": false
},
{
"name": "authority",
"isMut": false,
"isSigner": false
},
{
"name": "instructionSysvarAccount",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "args",
"type": "bytes"
}
]
},
{
"name": "apply5",
"accounts": [
{
"name": "boltSystem",
"isMut": false,
"isSigner": false
},
{
"name": "componentProgram1",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent1",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram2",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent2",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram3",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent3",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram4",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent4",
"isMut": true,
"isSigner": false
},
{
"name": "componentProgram5",
"isMut": false,
"isSigner": false
},
{
"name": "boltComponent5",
"isMut": true,
"isSigner": false
},
{
"name": "authority",
"isMut": false,
"isSigner": false
},
{
"name": "instructionSysvarAccount",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "args",
"type": "bytes"
}
]
}
],
"accounts": [
Expand Down
3 changes: 3 additions & 0 deletions clients/bolt-sdk/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/// <reference types="node" />
import { PublicKey } from "@solana/web3.js";
import type BN from "bn.js";
export * from "./accounts";
export * from "./instructions";
export * from "./transactions/transactions";
export declare const PROGRAM_ADDRESS =
"WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n";
export declare const SYSVAR_INSTRUCTIONS_PUBKEY: PublicKey;
Expand All @@ -19,4 +21,5 @@ export declare function FindComponentPda(
entity: PublicKey,
componentId?: string
): PublicKey;
export declare function SerializeArgs(args?: any): Buffer;
//# 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.

Loading

0 comments on commit 12eb5b0

Please sign in to comment.