-
Notifications
You must be signed in to change notification settings - Fork 10
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
crypto-vincent
merged 34 commits into
main
from
vbrunet/2024_06_12-update-transaction-apply-system
Jun 17, 2024
+542
−1,191
Merged
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 4c156a9
move-non-generated-code
crypto-vincent 4e4aa90
json-formatting
crypto-vincent 4f2ff47
using-original-generated-code
crypto-vincent a1efd78
remove-double-declaration
crypto-vincent a453b2e
make-sure-instructions-parameters-are-passed-on-optionals
crypto-vincent 80486f2
moving files out of generated folder
crypto-vincent 51037eb
modified apply system function to take multiple entities
crypto-vincent 0003b4a
component-id-param-name
crypto-vincent d18435b
nit-naming
crypto-vincent 86a3502
removing lib from git and continue on fixing integration tests
crypto-vincent b990a2c
remove-lib
crypto-vincent 9a66231
Upload progressg
crypto-vincent 45f55a8
saving-progress-before-splitting-pr
crypto-vincent a0cf4e4
Merge branch 'main' into vbrunet/2024_06_12-update-transaction-apply-…
crypto-vincent 13b9c63
yarn-build
crypto-vincent 09d4052
Merge branch 'vbrunet/2024_06_12-update-transaction-apply-system' of …
crypto-vincent cbdcd22
update-before-merge
crypto-vincent 57727de
merge-conflicts
crypto-vincent bba01f4
up
crypto-vincent c46afba
fix bolt init output for new api
crypto-vincent 3b77834
cleanup api
crypto-vincent 1b70434
polish new api
crypto-vincent 695ea93
fix variable name typo
crypto-vincent ef603a4
fixing tests
crypto-vincent 6743950
using seeds for velocity component
crypto-vincent 86bda9e
polishing tests
crypto-vincent 592982a
saving progress before meeting
crypto-vincent fc2ee9e
continue fixing tests
crypto-vincent 0bd6c26
fix-component-index-in-apply
crypto-vincent 8806f6d
get validator error message
crypto-vincent af22183
once again checking the error messages
crypto-vincent dccd1b0
update-expected-results
crypto-vincent d7935d8
everything should pass now
crypto-vincent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -48,7 +47,6 @@ export async function InitializeNewWorld({ | |
return { | ||
transaction: new Transaction().add(initializeWorldIx), | ||
worldPda, | ||
worldId, | ||
}; | ||
} | ||
|
||
|
@@ -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, | ||
}; | ||
} | ||
|
||
|
@@ -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, | ||
}); | ||
|
@@ -130,120 +132,119 @@ export async function InitializeComponent({ | |
} | ||
|
||
interface ApplySystemInstruction { | ||
entity: PublicKey; | ||
components: PublicKey[]; | ||
system: PublicKey; | ||
authority: PublicKey; | ||
seeds?: string[]; | ||
systemId: PublicKey; | ||
entities: ApplySystemEntity[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ | |
}, | ||
"license": "MIT", | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.