From c8c68ac6f42b00e85f8ce5ac04005c58fcf91eb8 Mon Sep 17 00:00:00 2001 From: Vincent Brunet Date: Tue, 18 Jun 2024 14:28:22 +0200 Subject: [PATCH 1/5] using world-pda instead of world-id to generate entity pda --- .vscode/settings.json | 7 +++++++ clients/bolt-sdk/idl/world.json | 4 ++-- .../src/generated/instructions/addEntity.ts | 6 ++++-- clients/bolt-sdk/src/index.ts | 11 +++++------ clients/bolt-sdk/src/world/transactions.ts | 15 ++++++++------- programs/world/src/lib.rs | 10 +++++----- 6 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2175ca5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "workbench.colorCustomizations": { + "activityBar.background": "#13341D", + "titleBar.activeBackground": "#1B4829", + "titleBar.activeForeground": "#F7FCF9" + } +} \ No newline at end of file diff --git a/clients/bolt-sdk/idl/world.json b/clients/bolt-sdk/idl/world.json index 7f63cef..6722c76 100644 --- a/clients/bolt-sdk/idl/world.json +++ b/clients/bolt-sdk/idl/world.json @@ -75,7 +75,7 @@ ], "args": [ { - "name": "extraSeed", + "name": "seed", "type": { "option": "string" } @@ -459,4 +459,4 @@ "binaryVersion": "0.29.0", "libVersion": "0.29.0" } -} \ No newline at end of file +} diff --git a/clients/bolt-sdk/src/generated/instructions/addEntity.ts b/clients/bolt-sdk/src/generated/instructions/addEntity.ts index e785b4a..6aebd33 100644 --- a/clients/bolt-sdk/src/generated/instructions/addEntity.ts +++ b/clients/bolt-sdk/src/generated/instructions/addEntity.ts @@ -14,8 +14,9 @@ import * as web3 from "@solana/web3.js"; * @category generated */ export interface AddEntityInstructionArgs { - extraSeed: beet.COption; + seed: beet.COption; } + /** * @category Instructions * @category AddEntity @@ -28,10 +29,11 @@ export const addEntityStruct = new beet.FixableBeetArgsStruct< >( [ ["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)], - ["extraSeed", beet.coption(beet.utf8String)], + ["seed", beet.coption(beet.utf8String)], ], "AddEntityInstructionArgs" ); + /** * Accounts required by the _addEntity_ instruction * diff --git a/clients/bolt-sdk/src/index.ts b/clients/bolt-sdk/src/index.ts index 6d62a13..57699ef 100644 --- a/clients/bolt-sdk/src/index.ts +++ b/clients/bolt-sdk/src/index.ts @@ -34,18 +34,17 @@ export function FindWorldPda({ } export function FindEntityPda({ - worldId, - entityId, + world, seed, + entityId, programId, }: { - worldId: BN; - entityId?: BN; + world: PublicKey; seed?: string; + entityId?: BN; programId?: PublicKey; }) { - const worldIdBuffer = Buffer.from(worldId.toArrayLike(Buffer, "be", 8)); - const seeds = [Buffer.from("entity"), worldIdBuffer]; + const seeds = [Buffer.from("entity"), world.toBytes()]; if (seed !== undefined) { seeds.push(Buffer.from(new Uint8Array(8))); seeds.push(Buffer.from(seed)); diff --git a/clients/bolt-sdk/src/world/transactions.ts b/clients/bolt-sdk/src/world/transactions.ts index 62e4428..b11bd43 100644 --- a/clients/bolt-sdk/src/world/transactions.ts +++ b/clients/bolt-sdk/src/world/transactions.ts @@ -70,19 +70,20 @@ export async function AddEntity({ seed?: string; connection: Connection; }): Promise<{ transaction: Transaction; entityPda: PublicKey }> { - const worldInstance = await World.fromAccountAddress(connection, world); - const worldId = new BN(worldInstance.id); - const entityPda = - seed !== undefined - ? FindEntityPda({ worldId, seed }) - : FindEntityPda({ worldId, entityId: new BN(worldInstance.entities) }); + let entityPda: PublicKey; + if (seed !== undefined) { + entityPda = FindEntityPda({ world, seed }); + } else { + const worldData = await World.fromAccountAddress(connection, world); + entityPda = FindEntityPda({ world, entityId: new BN(worldData.entities) }); + } const addEntityIx = createAddEntityInstruction( { world, payer, entity: entityPda, }, - { extraSeed: seed ?? null } + { seed: seed ?? null } ); return { transaction: new Transaction().add(addEntityIx), diff --git a/programs/world/src/lib.rs b/programs/world/src/lib.rs index 5d4ca64..1ead3cf 100644 --- a/programs/world/src/lib.rs +++ b/programs/world/src/lib.rs @@ -36,7 +36,7 @@ pub mod world { } #[allow(unused_variables)] - pub fn add_entity(ctx: Context, extra_seed: Option) -> Result<()> { + pub fn add_entity(ctx: Context, seed: Option) -> Result<()> { require!( ctx.accounts.world.key() == ctx.accounts.world.pda().0, WorldError::WorldAccountMismatch @@ -132,16 +132,16 @@ pub struct InitializeNewWorld<'info> { } #[derive(Accounts)] -#[instruction(extra_seed: Option)] +#[instruction(seed: Option)] pub struct AddEntity<'info> { #[account(mut)] pub payer: Signer<'info>, - #[account(init, payer = payer, space = World::size(), seeds = [Entity::seed(), &world.id.to_be_bytes(), - &match extra_seed { + #[account(init, payer = payer, space = World::size(), seeds = [Entity::seed(), &world.key().to_bytes(), + &match seed { Some(ref seed) => [0; 8], None => world.entities.to_be_bytes() }, - match extra_seed { + match seed { Some(ref seed) => seed.as_bytes(), None => &[], }], bump)] From 21007d6fb0d39fb4368b81be0851d391af03a317 Mon Sep 17 00:00:00 2001 From: crypto-vincent Date: Tue, 18 Jun 2024 14:31:17 +0200 Subject: [PATCH 2/5] Delete .vscode/settings.json --- .vscode/settings.json | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 2175ca5..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "workbench.colorCustomizations": { - "activityBar.background": "#13341D", - "titleBar.activeBackground": "#1B4829", - "titleBar.activeForeground": "#F7FCF9" - } -} \ No newline at end of file From 651beb58ff15a30b615149ab3a2a40a1b384d6c5 Mon Sep 17 00:00:00 2001 From: Vincent Brunet Date: Tue, 18 Jun 2024 17:09:01 +0200 Subject: [PATCH 3/5] remove world id from public api --- clients/bolt-sdk/src/index.ts | 2 +- clients/bolt-sdk/src/world/transactions.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clients/bolt-sdk/src/index.ts b/clients/bolt-sdk/src/index.ts index 57699ef..f76f2b1 100644 --- a/clients/bolt-sdk/src/index.ts +++ b/clients/bolt-sdk/src/index.ts @@ -49,7 +49,7 @@ export function FindEntityPda({ seeds.push(Buffer.from(new Uint8Array(8))); seeds.push(Buffer.from(seed)); } else if (entityId !== undefined) { - const entityIdBuffer = Buffer.from(entityId.toArrayLike(Buffer, "be", 8)); + const entityIdBuffer = entityId.toArrayLike(Buffer, "be", 8); seeds.push(entityIdBuffer); } else { throw new Error("An entity must have either an Id or a Seed"); diff --git a/clients/bolt-sdk/src/world/transactions.ts b/clients/bolt-sdk/src/world/transactions.ts index b11bd43..59c39ee 100644 --- a/clients/bolt-sdk/src/world/transactions.ts +++ b/clients/bolt-sdk/src/world/transactions.ts @@ -35,7 +35,7 @@ export async function InitializeNewWorld({ }: { payer: PublicKey; connection: Connection; -}): Promise<{ transaction: Transaction; worldPda: PublicKey; worldId: BN }> { +}): Promise<{ transaction: Transaction; worldPda: PublicKey }> { const registryPda = FindRegistryPda({}); const registry = await Registry.fromAccountAddress(connection, registryPda); const worldId = new BN(registry.worlds); @@ -48,7 +48,6 @@ export async function InitializeNewWorld({ return { transaction: new Transaction().add(initializeWorldIx), worldPda, - worldId, }; } @@ -75,7 +74,8 @@ export async function AddEntity({ entityPda = FindEntityPda({ world, seed }); } else { const worldData = await World.fromAccountAddress(connection, world); - entityPda = FindEntityPda({ world, entityId: new BN(worldData.entities) }); + const entityId = new BN(worldData.entities); + entityPda = FindEntityPda({ world, entityId }); } const addEntityIx = createAddEntityInstruction( { From e8a89928025d14e69517b8c07e8a7a24e857b70c Mon Sep 17 00:00:00 2001 From: Vincent Brunet Date: Tue, 18 Jun 2024 17:17:10 +0200 Subject: [PATCH 4/5] nit-seed-name --- tests/bolt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bolt.ts b/tests/bolt.ts index 497cd74..436770d 100644 --- a/tests/bolt.ts +++ b/tests/bolt.ts @@ -158,7 +158,7 @@ describe("bolt", () => { const addEntity = await AddEntity({ payer: provider.wallet.publicKey, world: worldPda, - seed: "extra-seed", + seed: "custom-seed", connection: provider.connection, }); await provider.sendAndConfirm(addEntity.transaction); From 7b4f178ad8eb96e48b0e772e78c7286b1c2aff3d Mon Sep 17 00:00:00 2001 From: Vincent Brunet Date: Tue, 18 Jun 2024 18:28:38 +0200 Subject: [PATCH 5/5] logging-errors-for-testing --- tests/bolt.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/bolt.ts b/tests/bolt.ts index 436770d..ffb7694 100644 --- a/tests/bolt.ts +++ b/tests/bolt.ts @@ -523,7 +523,11 @@ describe("bolt", () => { reimbursement: provider.wallet.publicKey, }); const tx = new anchor.web3.Transaction().add(delegateIx); - await provider.sendAndConfirm(tx); + try { + await provider.sendAndConfirm(tx); + } catch (error) { + console.log("error", error); + } const acc = await provider.connection.getAccountInfo( componentPositionEntity1Pda );