Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Use world.key instead of world.id to generate entity PDA #58

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions clients/bolt-sdk/idl/world.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
],
"args": [
{
"name": "extraSeed",
"name": "seed",
"type": {
"option": "string"
}
Expand Down Expand Up @@ -459,4 +459,4 @@
"binaryVersion": "0.29.0",
"libVersion": "0.29.0"
}
}
}
6 changes: 4 additions & 2 deletions clients/bolt-sdk/src/generated/instructions/addEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down
11 changes: 5 additions & 6 deletions clients/bolt-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
15 changes: 8 additions & 7 deletions clients/bolt-sdk/src/world/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now the happy path, much faster to generate transactions

} 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),
Expand Down
10 changes: 5 additions & 5 deletions programs/world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
Loading