From 03a585c36ad3386bd119b7ed6b400dfffaa97c7d Mon Sep 17 00:00:00 2001 From: crypto-vincent Date: Fri, 14 Jun 2024 17:47:04 +0100 Subject: [PATCH] feat: separate generated code from human code in bolt-sdk (#55) ## Problem Some files in `bolt-sdk/src/generated` have been manually edited, which may cause confusion in the future. ## Solution We re-generate the whole folder and re-insert all the manual modifications in a separate code folder --- .../{generated => }/delegation/accounts.ts | 0 .../{generated => }/delegation/delegate.ts | 0 .../{generated => }/delegation/undelegate.ts | 0 .../bolt-sdk/src/generated/accounts/index.ts | 7 ++ clients/bolt-sdk/src/generated/index.ts | 91 ++----------------- .../src/generated/instructions/addEntity.ts | 2 +- .../src/generated/instructions/apply.ts | 9 +- .../src/generated/instructions/apply2.ts | 9 +- .../src/generated/instructions/apply3.ts | 9 +- .../src/generated/instructions/apply4.ts | 9 +- .../src/generated/instructions/apply5.ts | 9 +- .../src/generated/instructions/index.ts | 7 ++ .../instructions/initializeComponent.ts | 15 ++- clients/bolt-sdk/src/index.ts | 87 ++++++++++++++++++ .../transactions/transactions.ts | 21 +++-- tsconfig.json | 26 ++++-- 16 files changed, 166 insertions(+), 135 deletions(-) rename clients/bolt-sdk/src/{generated => }/delegation/accounts.ts (100%) rename clients/bolt-sdk/src/{generated => }/delegation/delegate.ts (100%) rename clients/bolt-sdk/src/{generated => }/delegation/undelegate.ts (100%) create mode 100644 clients/bolt-sdk/src/index.ts rename clients/bolt-sdk/src/{generated => }/transactions/transactions.ts (93%) diff --git a/clients/bolt-sdk/src/generated/delegation/accounts.ts b/clients/bolt-sdk/src/delegation/accounts.ts similarity index 100% rename from clients/bolt-sdk/src/generated/delegation/accounts.ts rename to clients/bolt-sdk/src/delegation/accounts.ts diff --git a/clients/bolt-sdk/src/generated/delegation/delegate.ts b/clients/bolt-sdk/src/delegation/delegate.ts similarity index 100% rename from clients/bolt-sdk/src/generated/delegation/delegate.ts rename to clients/bolt-sdk/src/delegation/delegate.ts diff --git a/clients/bolt-sdk/src/generated/delegation/undelegate.ts b/clients/bolt-sdk/src/delegation/undelegate.ts similarity index 100% rename from clients/bolt-sdk/src/generated/delegation/undelegate.ts rename to clients/bolt-sdk/src/delegation/undelegate.ts diff --git a/clients/bolt-sdk/src/generated/accounts/index.ts b/clients/bolt-sdk/src/generated/accounts/index.ts index 67dd624..a616381 100644 --- a/clients/bolt-sdk/src/generated/accounts/index.ts +++ b/clients/bolt-sdk/src/generated/accounts/index.ts @@ -1,3 +1,10 @@ +/** + * This code was GENERATED using the solita package. + * Please DO NOT EDIT THIS FILE, instead rerun solita to update it or write a wrapper to add functionality. + * + * See: https://github.com/metaplex-foundation/solita + */ + import { Entity } from "./Entity"; import { Registry } from "./Registry"; import { World } from "./World"; diff --git a/clients/bolt-sdk/src/generated/index.ts b/clients/bolt-sdk/src/generated/index.ts index 0cc8b0e..7dbd9d2 100644 --- a/clients/bolt-sdk/src/generated/index.ts +++ b/clients/bolt-sdk/src/generated/index.ts @@ -1,10 +1,14 @@ +/** + * This code was GENERATED using the solita package. + * Please DO NOT EDIT THIS FILE, instead rerun solita to update it or write a wrapper to add functionality. + * + * See: https://github.com/metaplex-foundation/solita + */ + import { PublicKey } from "@solana/web3.js"; -import BN from "bn.js"; export * from "./accounts"; +export * from "./errors"; export * from "./instructions"; -export * from "./transactions/transactions"; -export * from "./delegation/accounts"; -export * from "./delegation/delegate"; /** * Program address @@ -14,10 +18,6 @@ export * from "./delegation/delegate"; */ export const PROGRAM_ADDRESS = "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n"; -export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey( - "Sysvar1nstructions1111111111111111111111111" -); - /** * Program public key * @@ -25,78 +25,3 @@ export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey( * @category generated */ export const PROGRAM_ID = new PublicKey(PROGRAM_ADDRESS); - -export function FindWorldRegistryPda( - programId: PublicKey = new PublicKey(PROGRAM_ID) -) { - return PublicKey.findProgramAddressSync( - [Buffer.from("registry")], - programId - )[0]; -} - -export function FindWorldPda( - id: BN | string | number | Uint8Array, - programId: PublicKey = new PublicKey(PROGRAM_ID) -) { - id = CastToBN(id); - const idBuffer = Buffer.from(id.toArrayLike(Buffer, "be", 8)); - return PublicKey.findProgramAddressSync( - [Buffer.from("world"), idBuffer], - programId - )[0]; -} - -export function FindEntityPda( - worldId: BN | string | number | Uint8Array, - entityId: BN | string | number | Uint8Array, - extraSeed?: string, - programId: PublicKey = new PublicKey(PROGRAM_ID) -) { - 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); - } - return PublicKey.findProgramAddressSync(seeds, programId)[0]; -} - -export function FindComponentPda( - componentProgramId: PublicKey, - entity: PublicKey, - componentId: string = "" -) { - return PublicKey.findProgramAddressSync( - [Buffer.from(componentId), entity.toBytes()], - componentProgramId - )[0]; -} - -function CastToBN(id: BN | string | number | Uint8Array) { - if (!(id instanceof BN)) { - id = new BN(id); - } - return id; -} - -/** - * Serialize arguments to a buffer - * @param args - * @constructor - */ -export function SerializeArgs(args: any = {}) { - const jsonString = JSON.stringify(args); - const encoder = new TextEncoder(); - const binaryData = encoder.encode(jsonString); - return Buffer.from( - binaryData.buffer, - binaryData.byteOffset, - binaryData.byteLength - ); -} diff --git a/clients/bolt-sdk/src/generated/instructions/addEntity.ts b/clients/bolt-sdk/src/generated/instructions/addEntity.ts index a2a8a2c..e785b4a 100644 --- a/clients/bolt-sdk/src/generated/instructions/addEntity.ts +++ b/clients/bolt-sdk/src/generated/instructions/addEntity.ts @@ -66,7 +66,7 @@ export const addEntityInstructionDiscriminator = [ */ export function createAddEntityInstruction( accounts: AddEntityInstructionAccounts, - args: AddEntityInstructionArgs = { extraSeed: null }, + args: AddEntityInstructionArgs, programId = new web3.PublicKey("WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n") ) { const [data] = addEntityStruct.serialize({ diff --git a/clients/bolt-sdk/src/generated/instructions/apply.ts b/clients/bolt-sdk/src/generated/instructions/apply.ts index b1528fe..1cfa7d8 100644 --- a/clients/bolt-sdk/src/generated/instructions/apply.ts +++ b/clients/bolt-sdk/src/generated/instructions/apply.ts @@ -7,7 +7,6 @@ import * as beet from "@metaplex-foundation/beet"; import * as web3 from "@solana/web3.js"; -import { SYSVAR_INSTRUCTIONS_PUBKEY } from "../index"; /** * @category Instructions @@ -49,8 +48,8 @@ export interface ApplyInstructionAccounts { componentProgram: web3.PublicKey; boltSystem: web3.PublicKey; boltComponent: web3.PublicKey; - authority?: web3.PublicKey; - instructionSysvarAccount?: web3.PublicKey; + authority: web3.PublicKey; + instructionSysvarAccount: web3.PublicKey; anchorRemainingAccounts?: web3.AccountMeta[]; } @@ -94,12 +93,12 @@ export function createApplyInstruction( isSigner: false, }, { - pubkey: accounts.authority ?? programId, + pubkey: accounts.authority, isWritable: false, isSigner: false, }, { - pubkey: accounts.instructionSysvarAccount ?? SYSVAR_INSTRUCTIONS_PUBKEY, + pubkey: accounts.instructionSysvarAccount, isWritable: false, isSigner: false, }, diff --git a/clients/bolt-sdk/src/generated/instructions/apply2.ts b/clients/bolt-sdk/src/generated/instructions/apply2.ts index ab94334..dc999dc 100644 --- a/clients/bolt-sdk/src/generated/instructions/apply2.ts +++ b/clients/bolt-sdk/src/generated/instructions/apply2.ts @@ -7,7 +7,6 @@ import * as beet from "@metaplex-foundation/beet"; import * as web3 from "@solana/web3.js"; -import { SYSVAR_INSTRUCTIONS_PUBKEY } from "../index"; /** * @category Instructions @@ -53,8 +52,8 @@ export interface Apply2InstructionAccounts { boltComponent1: web3.PublicKey; componentProgram2: web3.PublicKey; boltComponent2: web3.PublicKey; - authority?: web3.PublicKey; - instructionSysvarAccount?: web3.PublicKey; + authority: web3.PublicKey; + instructionSysvarAccount: web3.PublicKey; anchorRemainingAccounts?: web3.AccountMeta[]; } @@ -108,12 +107,12 @@ export function createApply2Instruction( isSigner: false, }, { - pubkey: accounts.authority ?? programId, + pubkey: accounts.authority, isWritable: false, isSigner: false, }, { - pubkey: accounts.instructionSysvarAccount ?? SYSVAR_INSTRUCTIONS_PUBKEY, + pubkey: accounts.instructionSysvarAccount, isWritable: false, isSigner: false, }, diff --git a/clients/bolt-sdk/src/generated/instructions/apply3.ts b/clients/bolt-sdk/src/generated/instructions/apply3.ts index 75ec6de..fc2334c 100644 --- a/clients/bolt-sdk/src/generated/instructions/apply3.ts +++ b/clients/bolt-sdk/src/generated/instructions/apply3.ts @@ -7,7 +7,6 @@ import * as beet from "@metaplex-foundation/beet"; import * as web3 from "@solana/web3.js"; -import { SYSVAR_INSTRUCTIONS_PUBKEY } from "../index"; /** * @category Instructions @@ -57,8 +56,8 @@ export interface Apply3InstructionAccounts { boltComponent2: web3.PublicKey; componentProgram3: web3.PublicKey; boltComponent3: web3.PublicKey; - authority?: web3.PublicKey; - instructionSysvarAccount?: web3.PublicKey; + authority: web3.PublicKey; + instructionSysvarAccount: web3.PublicKey; anchorRemainingAccounts?: web3.AccountMeta[]; } @@ -122,12 +121,12 @@ export function createApply3Instruction( isSigner: false, }, { - pubkey: accounts.authority ?? programId, + pubkey: accounts.authority, isWritable: false, isSigner: false, }, { - pubkey: accounts.instructionSysvarAccount ?? SYSVAR_INSTRUCTIONS_PUBKEY, + pubkey: accounts.instructionSysvarAccount, isWritable: false, isSigner: false, }, diff --git a/clients/bolt-sdk/src/generated/instructions/apply4.ts b/clients/bolt-sdk/src/generated/instructions/apply4.ts index a086ad1..8c03c3c 100644 --- a/clients/bolt-sdk/src/generated/instructions/apply4.ts +++ b/clients/bolt-sdk/src/generated/instructions/apply4.ts @@ -7,7 +7,6 @@ import * as beet from "@metaplex-foundation/beet"; import * as web3 from "@solana/web3.js"; -import { SYSVAR_INSTRUCTIONS_PUBKEY } from "../index"; /** * @category Instructions @@ -61,8 +60,8 @@ export interface Apply4InstructionAccounts { boltComponent3: web3.PublicKey; componentProgram4: web3.PublicKey; boltComponent4: web3.PublicKey; - authority?: web3.PublicKey; - instructionSysvarAccount?: web3.PublicKey; + authority: web3.PublicKey; + instructionSysvarAccount: web3.PublicKey; anchorRemainingAccounts?: web3.AccountMeta[]; } @@ -136,12 +135,12 @@ export function createApply4Instruction( isSigner: false, }, { - pubkey: accounts.authority ?? programId, + pubkey: accounts.authority, isWritable: false, isSigner: false, }, { - pubkey: accounts.instructionSysvarAccount ?? SYSVAR_INSTRUCTIONS_PUBKEY, + pubkey: accounts.instructionSysvarAccount, isWritable: false, isSigner: false, }, diff --git a/clients/bolt-sdk/src/generated/instructions/apply5.ts b/clients/bolt-sdk/src/generated/instructions/apply5.ts index 5bedf64..53b0ed3 100644 --- a/clients/bolt-sdk/src/generated/instructions/apply5.ts +++ b/clients/bolt-sdk/src/generated/instructions/apply5.ts @@ -7,7 +7,6 @@ import * as beet from "@metaplex-foundation/beet"; import * as web3 from "@solana/web3.js"; -import { SYSVAR_INSTRUCTIONS_PUBKEY } from "../index"; /** * @category Instructions @@ -65,8 +64,8 @@ export interface Apply5InstructionAccounts { boltComponent4: web3.PublicKey; componentProgram5: web3.PublicKey; boltComponent5: web3.PublicKey; - authority?: web3.PublicKey; - instructionSysvarAccount?: web3.PublicKey; + authority: web3.PublicKey; + instructionSysvarAccount: web3.PublicKey; anchorRemainingAccounts?: web3.AccountMeta[]; } @@ -150,12 +149,12 @@ export function createApply5Instruction( isSigner: false, }, { - pubkey: accounts.authority ?? programId, + pubkey: accounts.authority, isWritable: false, isSigner: false, }, { - pubkey: accounts.instructionSysvarAccount ?? SYSVAR_INSTRUCTIONS_PUBKEY, + pubkey: accounts.instructionSysvarAccount, isWritable: false, isSigner: false, }, diff --git a/clients/bolt-sdk/src/generated/instructions/index.ts b/clients/bolt-sdk/src/generated/instructions/index.ts index 9c68d81..abde1fd 100644 --- a/clients/bolt-sdk/src/generated/instructions/index.ts +++ b/clients/bolt-sdk/src/generated/instructions/index.ts @@ -1,3 +1,10 @@ +/** + * This code was GENERATED using the solita package. + * Please DO NOT EDIT THIS FILE, instead rerun solita to update it or write a wrapper to add functionality. + * + * See: https://github.com/metaplex-foundation/solita + */ + export * from "./addEntity"; export * from "./apply"; export * from "./apply2"; diff --git a/clients/bolt-sdk/src/generated/instructions/initializeComponent.ts b/clients/bolt-sdk/src/generated/instructions/initializeComponent.ts index 47dcb1c..7b1099a 100644 --- a/clients/bolt-sdk/src/generated/instructions/initializeComponent.ts +++ b/clients/bolt-sdk/src/generated/instructions/initializeComponent.ts @@ -7,7 +7,6 @@ import * as beet from "@metaplex-foundation/beet"; import * as web3 from "@solana/web3.js"; -import { FindComponentPda, SYSVAR_INSTRUCTIONS_PUBKEY } from "../index"; /** * @category Instructions @@ -35,11 +34,11 @@ export const initializeComponentStruct = new beet.BeetArgsStruct<{ */ export interface InitializeComponentInstructionAccounts { payer: web3.PublicKey; - data?: web3.PublicKey; + data: web3.PublicKey; entity: web3.PublicKey; componentProgram: web3.PublicKey; - authority?: web3.PublicKey; - instructionSysvarAccount?: web3.PublicKey; + authority: web3.PublicKey; + instructionSysvarAccount: web3.PublicKey; systemProgram?: web3.PublicKey; anchorRemainingAccounts?: web3.AccountMeta[]; } @@ -70,9 +69,7 @@ export function createInitializeComponentInstruction( isSigner: true, }, { - pubkey: - accounts.data ?? - FindComponentPda(accounts.componentProgram, accounts.entity), + pubkey: accounts.data, isWritable: true, isSigner: false, }, @@ -87,12 +84,12 @@ export function createInitializeComponentInstruction( isSigner: false, }, { - pubkey: accounts.authority ?? programId, + pubkey: accounts.authority, isWritable: false, isSigner: false, }, { - pubkey: accounts.instructionSysvarAccount ?? SYSVAR_INSTRUCTIONS_PUBKEY, + pubkey: accounts.instructionSysvarAccount, isWritable: false, isSigner: false, }, diff --git a/clients/bolt-sdk/src/index.ts b/clients/bolt-sdk/src/index.ts new file mode 100644 index 0000000..5386c2a --- /dev/null +++ b/clients/bolt-sdk/src/index.ts @@ -0,0 +1,87 @@ +import { PublicKey } from "@solana/web3.js"; +import BN from "bn.js"; +import { PROGRAM_ID } from "./generated"; +export * from "./generated/accounts"; +export * from "./generated/instructions"; +export * from "./transactions/transactions"; +export * from "./delegation/accounts"; +export * from "./delegation/delegate"; + +export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey( + "Sysvar1nstructions1111111111111111111111111" +); + +export function FindWorldRegistryPda( + programId: PublicKey = new PublicKey(PROGRAM_ID) +) { + return PublicKey.findProgramAddressSync( + [Buffer.from("registry")], + programId + )[0]; +} + +export function FindWorldPda( + id: BN | string | number | Uint8Array, + programId: PublicKey = new PublicKey(PROGRAM_ID) +) { + id = CastToBN(id); + const idBuffer = Buffer.from(id.toArrayLike(Buffer, "be", 8)); + return PublicKey.findProgramAddressSync( + [Buffer.from("world"), idBuffer], + programId + )[0]; +} + +export function FindEntityPda( + worldId: BN | string | number | Uint8Array, + entityId: BN | string | number | Uint8Array, + extraSeed?: string, + programId: PublicKey = new PublicKey(PROGRAM_ID) +) { + 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); + } + return PublicKey.findProgramAddressSync(seeds, programId)[0]; +} + +export function FindComponentPda( + componentProgramId: PublicKey, + entity: PublicKey, + componentId: string = "" +) { + return PublicKey.findProgramAddressSync( + [Buffer.from(componentId), entity.toBytes()], + componentProgramId + )[0]; +} + +function CastToBN(id: BN | string | number | Uint8Array) { + if (!(id instanceof BN)) { + id = new BN(id); + } + return id; +} + +/** + * Serialize arguments to a buffer + * @param args + * @constructor + */ +export function SerializeArgs(args: any = {}) { + const jsonString = JSON.stringify(args); + const encoder = new TextEncoder(); + const binaryData = encoder.encode(jsonString); + return Buffer.from( + binaryData.buffer, + binaryData.byteOffset, + binaryData.byteLength + ); +} diff --git a/clients/bolt-sdk/src/generated/transactions/transactions.ts b/clients/bolt-sdk/src/transactions/transactions.ts similarity index 93% rename from clients/bolt-sdk/src/generated/transactions/transactions.ts rename to clients/bolt-sdk/src/transactions/transactions.ts index 9b7bf35..dc0ae9e 100644 --- a/clients/bolt-sdk/src/generated/transactions/transactions.ts +++ b/clients/bolt-sdk/src/transactions/transactions.ts @@ -13,11 +13,13 @@ import { FindWorldRegistryPda, Registry, SerializeArgs, + SYSVAR_INSTRUCTIONS_PUBKEY, World, } from "../index"; import BN from "bn.js"; import type web3 from "@solana/web3.js"; import { type Connection, type PublicKey, Transaction } from "@solana/web3.js"; +import { PROGRAM_ID } from "../generated"; const MAX_COMPONENTS = 5; @@ -70,11 +72,14 @@ export async function AddEntity({ const entityId = new BN(worldInstance.entities); const entityPda = FindEntityPda(new BN(worldInstance.id), entityId); - const createEntityIx = createAddEntityInstruction({ - world, - payer, - entity: entityPda, - }); + const createEntityIx = createAddEntityInstruction( + { + world, + payer, + entity: entityPda, + }, + { extraSeed: null } + ); return { transaction: new Transaction().add(createEntityIx), entityPda, @@ -113,8 +118,9 @@ export async function InitializeComponent({ entity, data: componentPda, componentProgram: componentId, - authority, + authority: authority ?? PROGRAM_ID, anchorRemainingAccounts, + instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY, }); return { @@ -166,8 +172,9 @@ export function createApplySystemInstruction({ } const instructionArgs = { - authority, + authority: authority ?? PROGRAM_ID, boltSystem: system, + instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY, anchorRemainingAccounts: extraAccounts, }; diff --git a/tsconfig.json b/tsconfig.json index 558b83e..5faac89 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,17 @@ { - "compilerOptions": { - "types": ["mocha", "chai"], - "typeRoots": ["./node_modules/@types"], - "lib": ["es2015"], - "module": "commonjs", - "target": "es6", - "esModuleInterop": true - } - } - \ No newline at end of file + "compilerOptions": { + "types": [ + "mocha", + "chai" + ], + "typeRoots": [ + "./node_modules/@types" + ], + "lib": [ + "es2015" + ], + "module": "commonjs", + "target": "es6", + "esModuleInterop": true + } +} \ No newline at end of file