Skip to content

Commit

Permalink
feat: DelegateComponent wrapper function (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnamananand996 authored Sep 15, 2024
1 parent 094515d commit 3e54c0b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
64 changes: 64 additions & 0 deletions clients/bolt-sdk/src/delegation/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
}
19 changes: 12 additions & 7 deletions tests/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ApplySystem,
createAllowUndelegationInstruction,
} from "../clients/bolt-sdk";
import { DelegateComponent } from "../clients/bolt-sdk/src";

enum Direction {
Left = "Left",
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 3e54c0b

Please sign in to comment.