Browse Source

using world-pda instead of world-id to generate entity pda

Vincent Brunet 1 year ago
parent
commit
c8c68ac6f4

+ 7 - 0
.vscode/settings.json

@@ -0,0 +1,7 @@
+{
+    "workbench.colorCustomizations": {
+        "activityBar.background": "#13341D",
+        "titleBar.activeBackground": "#1B4829",
+        "titleBar.activeForeground": "#F7FCF9"
+    }
+}

+ 2 - 2
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"
   }
-}
+}

+ 4 - 2
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<string>;
+  seed: beet.COption<string>;
 }
+
 /**
  * @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
  *

+ 5 - 6
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));

+ 8 - 7
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),

+ 5 - 5
programs/world/src/lib.rs

@@ -36,7 +36,7 @@ pub mod world {
     }
 
     #[allow(unused_variables)]
-    pub fn add_entity(ctx: Context<AddEntity>, extra_seed: Option<String>) -> Result<()> {
+    pub fn add_entity(ctx: Context<AddEntity>, seed: Option<String>) -> 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<String>)]
+#[instruction(seed: Option<String>)]
 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)]