From 51037eb4255ce9152f106c44e8f7a2decfc5d526 Mon Sep 17 00:00:00 2001 From: Vincent Brunet Date: Thu, 13 Jun 2024 16:33:15 +0100 Subject: [PATCH] modified apply system function to take multiple entities --- clients/bolt-sdk/lib/index.d.ts | 4 +- clients/bolt-sdk/src/world/transactions.ts | 136 +++++++++++---------- 2 files changed, 72 insertions(+), 68 deletions(-) diff --git a/clients/bolt-sdk/lib/index.d.ts b/clients/bolt-sdk/lib/index.d.ts index 85d29f6..67c2c73 100644 --- a/clients/bolt-sdk/lib/index.d.ts +++ b/clients/bolt-sdk/lib/index.d.ts @@ -4,8 +4,8 @@ import BN from "bn.js"; export * from "./accounts"; export * from "./instructions"; export * from "./transactions/transactions"; -export * from "../delegation/accounts"; -export * from "../delegation/delegate"; +export * from "./delegation/accounts"; +export * from "./delegation/delegate"; export declare const PROGRAM_ADDRESS = "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n"; export declare const SYSVAR_INSTRUCTIONS_PUBKEY: PublicKey; diff --git a/clients/bolt-sdk/src/world/transactions.ts b/clients/bolt-sdk/src/world/transactions.ts index 1b40717..856f88e 100644 --- a/clients/bolt-sdk/src/world/transactions.ts +++ b/clients/bolt-sdk/src/world/transactions.ts @@ -130,100 +130,119 @@ export async function InitializeComponent({ } interface ApplySystemInstruction { - entity: PublicKey; - components: PublicKey[]; - seeds?: string[]; - system: PublicKey; authority: PublicKey; + system: PublicKey; + entities: ApplySystemEntity[]; extraAccounts?: web3.AccountMeta[]; args?: object; } -export function createApplySystemInstruction({ - entity, - components, - system, - seeds, +function getApplyInstructionFunctionName(componentsCount: number) { + if (componentsCount === 1) return "createApplyInstruction"; + return `createApply${componentsCount}Instruction`; +} + +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, + system, + 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; + for (const entityIndex in entities) { + const entity = entities[entityIndex]; + 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, + authority, boltSystem: system, instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY, anchorRemainingAccounts: extraAccounts, }; - components.forEach((component, index) => { - instructionArgs[getBoltComponentProgramName(index, components.length)] = - component; - instructionArgs[getBoltComponentName(index, components.length)] = - componentPdas[index]; - }); + for (const entityIndex in entities) { + const entity = entities[entityIndex]; + for (const componentIndex in entity.components) { + const component = entity.components[componentIndex]; + const componentPda = FindComponentPda( + component.programId, + entity.entity, + component.seed ?? "" + ); + instructionArgs[ + getBoltComponentProgramName(componentCount, componentCount) + ] = component.programId; + instructionArgs[getBoltComponentName(componentCount, componentCount)] = + componentPda; + componentCount++; + } + } - 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 { + programId: 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 entities * @param extraAccounts - * @param seeds + * @param args * @constructor */ export async function ApplySystem({ authority, system, - entity, - components, - args = {}, + entities, extraAccounts, - seeds, + args = {}, }: { authority: PublicKey; system: PublicKey; - entity: PublicKey; - components: PublicKey[]; - args?: object; + entities: ApplySystemEntity[]; extraAccounts?: web3.AccountMeta[]; - seeds?: string[]; + args?: object; }): Promise<{ transaction: Transaction }> { const applySystemIx = createApplySystemInstruction({ - entity, - components, - system, authority, - seeds, + system, + entities, extraAccounts, args, }); @@ -231,18 +250,3 @@ export async function ApplySystem({ 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}`; -}