Browse Source

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
crypto-vincent 1 year ago
parent
commit
03a585c36a

+ 0 - 0
clients/bolt-sdk/src/generated/delegation/accounts.ts → clients/bolt-sdk/src/delegation/accounts.ts


+ 0 - 0
clients/bolt-sdk/src/generated/delegation/delegate.ts → clients/bolt-sdk/src/delegation/delegate.ts


+ 0 - 0
clients/bolt-sdk/src/generated/delegation/undelegate.ts → clients/bolt-sdk/src/delegation/undelegate.ts


+ 7 - 0
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";

+ 8 - 83
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
-  );
-}

+ 1 - 1
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({

+ 4 - 5
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,
     },

+ 4 - 5
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,
     },

+ 4 - 5
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,
     },

+ 4 - 5
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,
     },

+ 4 - 5
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,
     },

+ 7 - 0
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";

+ 6 - 9
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,
     },

+ 87 - 0
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
+  );
+}

+ 14 - 7
clients/bolt-sdk/src/generated/transactions/transactions.ts → 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,
   };
 

+ 16 - 10
tsconfig.json

@@ -1,11 +1,17 @@
 {
-            "compilerOptions": {
-              "types": ["mocha", "chai"],
-              "typeRoots": ["./node_modules/@types"],
-              "lib": ["es2015"],
-              "module": "commonjs",
-              "target": "es6",
-              "esModuleInterop": true
-            }
-          }
-          
+    "compilerOptions": {
+        "types": [
+            "mocha",
+            "chai"
+        ],
+        "typeRoots": [
+            "./node_modules/@types"
+        ],
+        "lib": [
+            "es2015"
+        ],
+        "module": "commonjs",
+        "target": "es6",
+        "esModuleInterop": true
+    }
+}