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

feat: Allow passing multiple entities in the ApplySystem function #51

Merged
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9d6ce88
moving non-generated files out of the generated folder
crypto-vincent Jun 13, 2024
4c156a9
move-non-generated-code
crypto-vincent Jun 13, 2024
4e4aa90
json-formatting
crypto-vincent Jun 13, 2024
4f2ff47
using-original-generated-code
crypto-vincent Jun 13, 2024
a1efd78
remove-double-declaration
crypto-vincent Jun 13, 2024
a453b2e
make-sure-instructions-parameters-are-passed-on-optionals
crypto-vincent Jun 13, 2024
80486f2
moving files out of generated folder
crypto-vincent Jun 13, 2024
51037eb
modified apply system function to take multiple entities
crypto-vincent Jun 13, 2024
0003b4a
component-id-param-name
crypto-vincent Jun 13, 2024
d18435b
nit-naming
crypto-vincent Jun 13, 2024
86a3502
removing lib from git and continue on fixing integration tests
crypto-vincent Jun 13, 2024
b990a2c
remove-lib
crypto-vincent Jun 13, 2024
9a66231
Upload progressg
crypto-vincent Jun 14, 2024
45f55a8
saving-progress-before-splitting-pr
crypto-vincent Jun 14, 2024
a0cf4e4
Merge branch 'main' into vbrunet/2024_06_12-update-transaction-apply-…
crypto-vincent Jun 14, 2024
13b9c63
yarn-build
crypto-vincent Jun 14, 2024
09d4052
Merge branch 'vbrunet/2024_06_12-update-transaction-apply-system' of …
crypto-vincent Jun 14, 2024
cbdcd22
update-before-merge
crypto-vincent Jun 14, 2024
57727de
merge-conflicts
crypto-vincent Jun 14, 2024
bba01f4
up
crypto-vincent Jun 14, 2024
c46afba
fix bolt init output for new api
crypto-vincent Jun 14, 2024
3b77834
cleanup api
crypto-vincent Jun 14, 2024
1b70434
polish new api
crypto-vincent Jun 14, 2024
695ea93
fix variable name typo
crypto-vincent Jun 17, 2024
ef603a4
fixing tests
crypto-vincent Jun 17, 2024
6743950
using seeds for velocity component
crypto-vincent Jun 17, 2024
86bda9e
polishing tests
crypto-vincent Jun 17, 2024
592982a
saving progress before meeting
crypto-vincent Jun 17, 2024
fc2ee9e
continue fixing tests
crypto-vincent Jun 17, 2024
0bd6c26
fix-component-index-in-apply
crypto-vincent Jun 17, 2024
8806f6d
get validator error message
crypto-vincent Jun 17, 2024
af22183
once again checking the error messages
crypto-vincent Jun 17, 2024
dccd1b0
update-expected-results
crypto-vincent Jun 17, 2024
d7935d8
everything should pass now
crypto-vincent Jun 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clients/bolt-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BN from "bn.js";
import { PROGRAM_ID } from "./generated";
export * from "./generated/accounts";
export * from "./generated/instructions";
export * from "./transactions/transactions";
export * from "./world/transactions";
export * from "./delegation/accounts";
export * from "./delegation/delegate";

Expand Down Expand Up @@ -41,13 +41,12 @@ export function FindEntityPda(
worldId = CastToBN(worldId);
entityId = CastToBN(entityId);
const worldIdBuffer = Buffer.from(worldId.toArrayLike(Buffer, "be", 8));
const entityIdBuffer = Buffer.from(entityId.toArrayLike(Buffer, "be", 8));
const seeds = [Buffer.from("entity"), worldIdBuffer];
if (extraSeed != null) {
seeds.push(Buffer.from(new Uint8Array(8)));
seeds.push(Buffer.from(extraSeed));
} else {
seeds.push(entityIdBuffer);
seeds.push(Buffer.from(entityId.toArrayLike(Buffer, "be", 8)));
}
return PublicKey.findProgramAddressSync(seeds, programId)[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ export async function InitializeNewWorld({
}: {
payer: PublicKey;
connection: Connection;
}): Promise<{ transaction: Transaction; worldPda: PublicKey; worldId: BN }> {
}): Promise<{ transaction: Transaction; worldPda: PublicKey }> {
const registryPda = FindWorldRegistryPda();
const registry = await Registry.fromAccountAddress(connection, registryPda);
const worldId = new BN(registry.worlds);
const worldPda = FindWorldPda(new BN(worldId));
const worldPda = FindWorldPda(new BN(registry.worlds));
const initializeWorldIx = createInitializeNewWorldInstruction({
world: worldPda,
registry: registryPda,
Expand All @@ -48,7 +47,6 @@ export async function InitializeNewWorld({
return {
transaction: new Transaction().add(initializeWorldIx),
worldPda,
worldId,
};
}

Expand All @@ -62,28 +60,31 @@ export async function InitializeNewWorld({
export async function AddEntity({
payer,
world,
seed,
connection,
}: {
payer: PublicKey;
world: PublicKey;
seed?: string;
connection: Connection;
}): Promise<{ transaction: Transaction; entityPda: PublicKey; entityId: BN }> {
}): Promise<{ transaction: Transaction; entityPda: PublicKey }> {
const worldInstance = await World.fromAccountAddress(connection, world);
const entityId = new BN(worldInstance.entities);
const entityPda = FindEntityPda(new BN(worldInstance.id), entityId);

const entityPda = FindEntityPda(
new BN(worldInstance.id),
new BN(worldInstance.entities),
seed
);
const createEntityIx = createAddEntityInstruction(
{
world,
payer,
entity: entityPda,
},
{ extraSeed: null }
{ extraSeed: seed ?? null }
);
return {
transaction: new Transaction().add(createEntityIx),
entityPda,
entityId,
Copy link
Contributor Author

@crypto-vincent crypto-vincent Jun 14, 2024

Choose a reason for hiding this comment

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

removing this entityId because this might not be correct if the user passed a seed as parameter. This should not be part of the public API imo.

};
}

Expand Down Expand Up @@ -119,6 +120,7 @@ export async function InitializeComponent({
data: componentPda,
componentProgram: componentId,
authority: authority ?? PROGRAM_ID,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
});
Expand All @@ -130,120 +132,119 @@ export async function InitializeComponent({
}

interface ApplySystemInstruction {
entity: PublicKey;
components: PublicKey[];
system: PublicKey;
authority: PublicKey;
seeds?: string[];
systemId: PublicKey;
entities: ApplySystemEntity[];
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 the main change to the API

extraAccounts?: web3.AccountMeta[];
args?: object;
}
function getApplyInstructionFunctionName(componentsCount: number) {
if (componentsCount === 1) return "createApplyInstruction";
return `createApply${componentsCount}Instruction`;
}

export function createApplySystemInstruction({
entity,
components,
system,
seeds,
function getBoltComponentName(index: number, componentsCount: number) {
if (componentsCount === 1) return "boltComponent";
return `boltComponent${index + 1}`;
}
function getBoltComponentProgramName(index: number, componentsCount: number) {
if (componentsCount === 1) return "componentProgram";
return `componentProgram${index + 1}`;
}
function createApplySystemInstruction({
authority,
systemId,
entities,
extraAccounts,
args,
}: ApplySystemInstruction): web3.TransactionInstruction {
const instructionFunctions = {
createApplyInstruction,
createApply2Instruction,
createApply3Instruction,
createApply4Instruction,
createApply5Instruction,
};
if (components.length === 0) throw new Error("No components provided");
if (seeds == null) seeds = new Array(components.length).fill("");
if (seeds.length !== components.length)
throw new Error("Seed length does not match components length");
const componentPdas: PublicKey[] = [];

for (let i = 0; i < components.length; i++) {
const componentPda = FindComponentPda(components[i], entity, seeds[i]);
componentPdas.push(componentPda);
let componentCount = 0;
entities.forEach(function (entity) {
componentCount += entity.components.length;
});
if (componentCount <= 0) {
throw new Error("No components provided");
}
if (components.length < 1 || components.length > MAX_COMPONENTS) {
if (componentCount > MAX_COMPONENTS) {
throw new Error(
`Not implemented for component counts outside 1-${MAX_COMPONENTS}`
);
}

const instructionArgs = {
authority: authority ?? PROGRAM_ID,
boltSystem: system,
boltSystem: systemId,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
anchorRemainingAccounts: extraAccounts,
};

components.forEach((component, index) => {
instructionArgs[getBoltComponentProgramName(index, components.length)] =
component;
instructionArgs[getBoltComponentName(index, components.length)] =
componentPdas[index];
entities.forEach(function (entity) {
entity.components.forEach(function (component) {
const componentPda = FindComponentPda(
component.id,
entity.entity,
component.seed ?? ""
);
instructionArgs[
getBoltComponentProgramName(componentCount, componentCount)
] = component.id;
instructionArgs[getBoltComponentName(componentCount, componentCount)] =
componentPda;
});
});

const functionName = getApplyInstructionFunctionName(components.length);
const instructionFunctions = {
createApplyInstruction,
createApply2Instruction,
createApply3Instruction,
createApply4Instruction,
createApply5Instruction,
};
const functionName = getApplyInstructionFunctionName(componentCount);
return instructionFunctions[functionName](instructionArgs, {
args: SerializeArgs(args),
});
}

interface ApplySystemEntity {
entity: PublicKey;
components: ApplySystemComponent[];
}
interface ApplySystemComponent {
id: PublicKey;
seed?: string;
}

/**
* Apply a system to an entity and its components
* Apply a system to a set of components
* @param authority
* @param system
* @param entity
* @param components
* @param args
* @param systemId
* @param entities
* @param extraAccounts
* @param seeds
* @param args
* @constructor
*/
export async function ApplySystem({
authority,
system,
entity,
components,
args = {},
systemId,
entities,
extraAccounts,
seeds,
args,
}: {
authority: PublicKey;
system: PublicKey;
entity: PublicKey;
components: PublicKey[];
args?: object;
systemId: PublicKey;
entities: ApplySystemEntity[];
extraAccounts?: web3.AccountMeta[];
seeds?: string[];
args?: object;
}): Promise<{ transaction: Transaction }> {
const applySystemIx = createApplySystemInstruction({
entity,
components,
system,
authority,
seeds,
systemId,
entities,
extraAccounts,
args,
});
return {
transaction: new Transaction().add(applySystemIx),
};
}

function getApplyInstructionFunctionName(componentsLength: number) {
if (componentsLength === 1) return "createApplyInstruction";
return `createApply${componentsLength}Instruction`;
}

function getBoltComponentName(index: number, componentsLength: number) {
if (componentsLength === 1) return "boltComponent";
return `boltComponent${index + 1}`;
}

function getBoltComponentProgramName(index: number, componentsLength: number) {
if (componentsLength === 1) return "componentProgram";
return `componentProgram${index + 1}`;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
},
"license": "MIT",
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
}
Loading
Loading