From e6b5a6b713cadc14bbbc1e3b53e0bf7c43209ae0 Mon Sep 17 00:00:00 2001 From: Gabriele Picco Date: Sat, 6 Apr 2024 10:43:06 +0100 Subject: [PATCH] :memo: Simplify bolt ts syntax --- _posts/2024-03-01-bolt-v0.1.md | 46 +++++++++++++--------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/_posts/2024-03-01-bolt-v0.1.md b/_posts/2024-03-01-bolt-v0.1.md index f89b081..45bd108 100644 --- a/_posts/2024-03-01-bolt-v0.1.md +++ b/_posts/2024-03-01-bolt-v0.1.md @@ -117,59 +117,47 @@ npm install @magicblock-labs/bolt-sdk --save-dev ##### Create a world instance ```ts -const world = await World.fromAccountAddress(provider.connection, worldPda); -const entityId = new anchor.BN(world.entities); -entityPda = FindEntityPda(worldId, entityId); - -let createEntityIx = createAddEntityInstruction({ - world: worldPda, +const initNewWorld = await InitializeNewWorld({ payer: provider.wallet.publicKey, - entity: entityPda, + connection: provider.connection, }); const tx = new anchor.web3.Transaction().add(createEntityIx); -await provider.sendAndConfirm(tx); +await provider.sendAndConfirm(initNewWorld.transaction); ``` ###### Add a new entity ```ts -const world = await World.fromAccountAddress(provider.connection, worldPda); -const entityId = new anchor.BN(world.entities); -entityPda = FindEntityPda(worldId, entityId); - -let createEntityIx = createAddEntityInstruction({ - world: worldPda, +const addEntity = await AddEntity({ payer: provider.wallet.publicKey, - entity: entityPda, + world: initNewWorld.worldPda, + connection: provider.connection, }); -const tx = new anchor.web3.Transaction().add(createEntityIx); -await provider.sendAndConfirm(tx); +await provider.sendAndConfirm(addEntity.transaction); ``` ##### Attach the Position component to the the entity ```ts -const positionComponentPda = FindComponentPda(positionComponent.programId, entityPda, ""); -let initComponentIx = createInitializeComponentInstruction({ +const initComponent = await InitializeComponent({ payer: provider.wallet.publicKey, - entity: entityPda, - data: positionComponentPda, - componentProgram: positionComponent.programId, + entity: addEntity.entityPda, + componentId: positionComponent.programId, }); -const tx = new anchor.web3.Transaction().add(initComponentIx); -await provider.sendAndConfirm(tx); +await provider.sendAndConfirm(initComponent.transaction); ``` ##### Execute the movement system on the position Component ```ts -let applySystemIx = createApplyInstruction({ - componentProgram: positionComponent.programId, - boltSystem: systemMovement.programId, - boltComponent: positionComponentPda, +const applySystem = await ApplySystem({ + authority: provider.wallet.publicKey, + system: systemMovement.programId, + entity: addEntity.entityPda, + components: [positionComponent.programId], }); const tx = new anchor.web3.Transaction().add(applySystemIx); -await provider.sendAndConfirm(tx); +await provider.sendAndConfirm(applySystem.transaction); ``` In this simple example we have created an entity Player that holds a Position component with x,y,z coordinates. We can execute the movement system to change its state.