diff --git a/clients/bolt-sdk/idl/world.json b/clients/bolt-sdk/idl/world.json index 7f63cef8..6722c769 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 e785b4ae..6aebd33b 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 9f2fd65b..2f4953d5 100644 --- a/clients/bolt-sdk/src/index.ts +++ b/clients/bolt-sdk/src/index.ts @@ -38,23 +38,22 @@ 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)); } 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 62e4428c..59c39eea 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, }; } @@ -70,19 +69,21 @@ 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); + const entityId = new BN(worldData.entities); + entityPda = FindEntityPda({ world, entityId }); + } 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 5d4ca647..1ead3cfc 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)] diff --git a/tests/bolt.ts b/tests/bolt.ts index 8c9dfab2..af19eb63 100644 --- a/tests/bolt.ts +++ b/tests/bolt.ts @@ -157,7 +157,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);