From 3e54c0b8bac5f656843be4bf677d0cbeb5ed67bf Mon Sep 17 00:00:00 2001 From: Naman Anand Date: Sun, 15 Sep 2024 08:10:14 +0530 Subject: [PATCH] feat: `DelegateComponent` wrapper function (#78) --- clients/bolt-sdk/src/delegation/delegate.ts | 64 +++++++++++++++++++++ tests/bolt.ts | 19 +++--- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/clients/bolt-sdk/src/delegation/delegate.ts b/clients/bolt-sdk/src/delegation/delegate.ts index 9fe32852..efac9dcb 100644 --- a/clients/bolt-sdk/src/delegation/delegate.ts +++ b/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, + }; +} diff --git a/tests/bolt.ts b/tests/bolt.ts index 8c9dfab2..9bc90fc0 100644 --- a/tests/bolt.ts +++ b/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); });