瀏覽代碼

feat: `DelegateComponent` wrapper function (#78)

Naman Anand 1 年之前
父節點
當前提交
3e54c0b8ba
共有 2 個文件被更改,包括 76 次插入7 次删除
  1. 64 0
      clients/bolt-sdk/src/delegation/delegate.ts
  2. 12 7
      tests/bolt.ts

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

@@ -4,6 +4,12 @@ import {
   DelegateAccounts,
   DELEGATION_PROGRAM_ID,
 } from "@magicblock-labs/delegation-program";
+import { FindComponentPda } from "../index";
+import {
+  type PublicKey,
+  Transaction,
+  type TransactionInstruction,
+} from "@solana/web3.js";
 
 export interface DelegateInstructionArgs {
   validUntil: beet.bignum;
@@ -120,3 +126,61 @@ export function createDelegateInstruction(
     data,
   });
 }
+
+/**
+ * Create the transaction to Delegate a component
+ * @param payer
+ * @param entityPda
+ * @param componentId
+ * @param seeds
+ * @param buffer
+ * @param delegationRecord
+ * @param delegationMetadata
+ * @param delegationProgram
+ * @param systemProgram
+ * @constructor
+ */
+export async function DelegateComponent({
+  payer,
+  entity,
+  componentId,
+  seed = "",
+  buffer,
+  delegationRecord,
+  delegationMetadata,
+  delegationProgram,
+  systemProgram,
+}: {
+  payer: PublicKey;
+  entity: PublicKey;
+  componentId: PublicKey;
+  seed?: string;
+  buffer?: web3.PublicKey;
+  delegationRecord?: web3.PublicKey;
+  delegationMetadata?: web3.PublicKey;
+  delegationProgram?: web3.PublicKey;
+  systemProgram?: web3.PublicKey;
+}): Promise<{
+  instruction: TransactionInstruction;
+  transaction: Transaction;
+  componentPda: PublicKey;
+}> {
+  const componentPda = FindComponentPda({ componentId, entity, seed });
+  const delegateComponentIx = createDelegateInstruction({
+    payer,
+    entity,
+    account: componentPda,
+    ownerProgram: componentId,
+    buffer,
+    delegationRecord,
+    delegationMetadata,
+    delegationProgram,
+    systemProgram,
+  });
+
+  return {
+    instruction: delegateComponentIx,
+    transaction: new Transaction().add(delegateComponentIx),
+    componentPda,
+  };
+}

+ 12 - 7
tests/bolt.ts

@@ -21,6 +21,7 @@ import {
   ApplySystem,
   createAllowUndelegationInstruction,
 } from "../clients/bolt-sdk";
+import { DelegateComponent } from "../clients/bolt-sdk/src";
 
 enum Direction {
   Left = "Left",
@@ -500,16 +501,20 @@ describe("bolt", () => {
   });
 
   it("Check component delegation", async () => {
-    const delegateIx = createDelegateInstruction({
-      entity: entity1Pda,
-      account: componentPositionEntity1Pda,
-      ownerProgram: exampleComponentPosition.programId,
+    const delegateComponent = await DelegateComponent({
       payer: provider.wallet.publicKey,
+      entity: entity1Pda,
+      componentId: exampleComponentPosition.programId,
     });
-    const tx = new anchor.web3.Transaction().add(delegateIx);
-    await provider.sendAndConfirm(tx);
+
+    const txSign = await provider.sendAndConfirm(
+      delegateComponent.transaction,
+      [],
+      { skipPreflight: true, commitment: "finalized" }
+    );
+    console.log(`Delegation signature: ${txSign}`);
     const acc = await provider.connection.getAccountInfo(
-      componentPositionEntity1Pda
+      delegateComponent.componentPda
     );
     expect(acc.owner.toString()).to.equal(DELEGATION_PROGRAM_ID);
   });