Skip to content

Commit

Permalink
✨ Add allow undelegation ix
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Jun 27, 2024
1 parent 28aaf73 commit 2796f80
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 71 deletions.
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ anyhow = "1.0.32"
heck = "0.5.0"
clap = { version = "4.2.4", features = ["derive"] }
ahash = "=0.8.11"
delegation-program-sdk = "=0.0.2"
delegation-program-sdk = "=0.1.3"

[profile.release]
overflow-checks = true
Expand Down
2 changes: 1 addition & 1 deletion clients/bolt-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"@metaplex-foundation/beet": "^0.7.1",
"@metaplex-foundation/beet-solana": "^0.4.0",
"@magicblock-labs/delegation-program": "0.1.1"
"@magicblock-labs/delegation-program": "0.1.3"
},
"devDependencies": {
"@metaplex-foundation/solita": "^0.20.1",
Expand Down
51 changes: 0 additions & 51 deletions clients/bolt-sdk/src/delegation/accounts.ts

This file was deleted.

75 changes: 75 additions & 0 deletions clients/bolt-sdk/src/delegation/allow_undelegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as beet from "@metaplex-foundation/beet";
import * as web3 from "@solana/web3.js";
import {
DelegateAccounts,
DELEGATION_PROGRAM_ID,
} from "@magicblock-labs/delegation-program";

export const allowUndelegationStruct = new beet.BeetArgsStruct<{
instructionDiscriminator: number[] /* size: 8 */;
}>(
[["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)]],
"allowUndelegationInstructionArgs"
);

export interface AllowUndelegationInstructionAccounts {
delegatedAccount: web3.PublicKey;
ownerProgram: web3.PublicKey;
buffer?: web3.PublicKey;
}

export const allowUndelegateInstructionDiscriminator = [
255, 66, 82, 208, 247, 5, 210, 126,
];

/**
* Creates a Delegate instruction.
*/

export function createAllowUndelegationInstruction(
accounts: AllowUndelegationInstructionAccounts
) {
const [data] = allowUndelegationStruct.serialize({
instructionDiscriminator: allowUndelegateInstructionDiscriminator,
});

const { delegationPda, delegationMetadata, bufferPda } = DelegateAccounts(
accounts.delegatedAccount,
accounts.ownerProgram
);

const keys: web3.AccountMeta[] = [
{
pubkey: accounts.delegatedAccount,
isWritable: false,
isSigner: false,
},
{
pubkey: delegationPda,
isWritable: false,
isSigner: false,
},
{
pubkey: delegationMetadata,
isWritable: true,
isSigner: false,
},
{
pubkey: bufferPda,
isWritable: false,
isSigner: false,
},
{
pubkey: new web3.PublicKey(DELEGATION_PROGRAM_ID),
isWritable: true,
isSigner: false,
},
];

const programId = accounts.ownerProgram;
return new web3.TransactionInstruction({
programId,
keys,
data,
});
}
16 changes: 10 additions & 6 deletions clients/bolt-sdk/src/delegation/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as beet from "@metaplex-foundation/beet";
import * as web3 from "@solana/web3.js";
import { getDelegationAccounts } from "./accounts";
import { DELEGATION_PROGRAM_ID } from "@magicblock-labs/delegation-program";
import {
DelegateAccounts,
DELEGATION_PROGRAM_ID,
} from "@magicblock-labs/delegation-program";

export interface DelegateInstructionArgs {
validUntil: beet.bignum;
Expand Down Expand Up @@ -33,7 +35,7 @@ export interface DelegateInstructionAccounts {
ownerProgram: web3.PublicKey;
buffer?: web3.PublicKey;
delegationRecord?: web3.PublicKey;
delegateAccountSeeds?: web3.PublicKey;
delegationMetadata?: web3.PublicKey;
delegationProgram?: web3.PublicKey;
systemProgram?: web3.PublicKey;
}
Expand All @@ -58,8 +60,10 @@ export function createDelegateInstruction(
commitFrequencyMs,
});

const { delegationPda, delegatedAccountSeedsPda, bufferPda } =
getDelegationAccounts(accounts.account, accounts.ownerProgram);
const { delegationPda, delegationMetadata, bufferPda } = DelegateAccounts(
accounts.account,
accounts.ownerProgram
);

const keys: web3.AccountMeta[] = [
{
Expand Down Expand Up @@ -93,7 +97,7 @@ export function createDelegateInstruction(
isSigner: false,
},
{
pubkey: accounts.delegateAccountSeeds ?? delegatedAccountSeedsPda,
pubkey: accounts.delegationMetadata ?? delegationMetadata,
isWritable: true,
isSigner: false,
},
Expand Down
2 changes: 1 addition & 1 deletion clients/bolt-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { PROGRAM_ID } from "./generated";
export * from "./generated/accounts";
export * from "./generated/instructions";
export * from "./world/transactions";
export * from "./delegation/accounts";
export * from "./delegation/delegate";
export * from "./delegation/allow_undelegation";
export {
createCommitInstruction,
createUndelegateInstruction,
Expand Down
8 changes: 4 additions & 4 deletions clients/bolt-sdk/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
wrap-ansi "^8.1.0"
wrap-ansi-cjs "npm:wrap-ansi@^7.0.0"

"@magicblock-labs/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@magicblock-labs/delegation-program/-/delegation-program-0.1.1.tgz#71a5fb25ccf88ea7746ea70473fb109ff6b67433"
integrity sha512-4He8V7jkrGy8MTp6qAz2h5y70+6y4cmvQc+7Km6B7WDLODlymPu9zZEl1/bmY24bOeKVKfrEEE7pDVa8qr3BwQ==
"@magicblock-labs/[email protected].2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@magicblock-labs/delegation-program/-/delegation-program-0.1.2.tgz#7b298398259d80a368eb9e91cb911e5a6e542391"
integrity sha512-9EGtcBWGAXGCOjTJVyzxydMpM8GGk8QjzQlgpV0q96BAbmpEO9C6b/N3lvMRTR1FfDuBn43a3Pa5YEiHyUu8Eg==
dependencies:
"@metaplex-foundation/beet" "^0.7.2"
"@solana/web3.js" "^1.92.3"
Expand Down
50 changes: 47 additions & 3 deletions crates/bolt-lang/attribute/delegate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ pub fn delegate(args: TokenStream, input: TokenStream) -> TokenStream {
fn modify_component_module(mut module: ItemMod, component_type: &Type) -> ItemMod {
let (delegate_fn, delegate_struct) = generate_delegate(component_type);
let (undelegate_fn, undelegate_struct) = generate_undelegate();
let (allow_undelegate_fn, allow_undelegate_struct) = generate_allow_undelegate();
module.content = module.content.map(|(brace, mut items)| {
items.extend(
vec![
delegate_fn,
delegate_struct,
undelegate_fn,
undelegate_struct,
allow_undelegate_fn,
allow_undelegate_struct,
]
.into_iter()
.map(|item| syn::parse2(item).unwrap())
Expand All @@ -51,6 +54,47 @@ fn modify_component_module(mut module: ItemMod, component_type: &Type) -> ItemMo
module
}

/// Generates the allow_undelegate function and struct.
fn generate_allow_undelegate() -> (TokenStream2, TokenStream2) {
(
quote! {
#[automatically_derived]
pub fn allow_undelegation(ctx: Context<AllowUndelegation>) -> Result<()> {
::bolt_lang::allow_undelegation(
&ctx.accounts.base_account,
&ctx.accounts.delegation_record,
&ctx.accounts.delegation_metadata,
&ctx.accounts.buffer,
&ctx.accounts.delegation_program,
&id(),
)?;
Ok(())
}
},
quote! {
#[automatically_derived]
#[derive(Accounts)]
pub struct AllowUndelegation<'info> {
#[account()]
/// CHECK: The delegated component
pub base_account: AccountInfo<'info>,
#[account()]
/// CHECK: delegation record
pub delegation_record: AccountInfo<'info>,
#[account(mut)]
/// CHECK: delegation metadata
pub delegation_metadata: AccountInfo<'info>,
#[account()]
/// CHECK: singer buffer to enforce CPI
pub buffer: AccountInfo<'info>,
#[account()]
/// CHECK:`
pub delegation_program: AccountInfo<'info>,
}
},
)
}

/// Generates the undelegate function and struct.
fn generate_undelegate() -> (TokenStream2, TokenStream2) {
(
Expand All @@ -63,7 +107,7 @@ fn generate_undelegate() -> (TokenStream2, TokenStream2) {
&ctx.accounts.payer,
&ctx.accounts.system_program,
];
undelegate_account(
::bolt_lang::undelegate_account(
delegated_account,
&id(),
buffer,
Expand All @@ -77,7 +121,7 @@ fn generate_undelegate() -> (TokenStream2, TokenStream2) {
quote! {
#[automatically_derived]
#[derive(Accounts)]
pub struct InitializeAfterUndelegation<'info> {
pub struct InitializeAfterUndelegation<'info> {
/// CHECK:`
#[account(mut)]
pub base_account: AccountInfo<'info>,
Expand Down Expand Up @@ -115,7 +159,7 @@ fn generate_delegate(component_type: &Type) -> (TokenStream2, TokenStream2) {

let pda_seeds: &[&[u8]] = &[<#component_type>::seed(), &entity.key.to_bytes()];

delegate_account(
::bolt_lang::delegate_account(
payer,
account,
owner_program,
Expand Down
2 changes: 1 addition & 1 deletion crates/bolt-lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use world;
pub use world::program::World;
pub use world::Entity;

pub use delegation_program_sdk::{delegate_account, undelegate_account};
pub use delegation_program_sdk::{allow_undelegation, delegate_account, undelegate_account};

pub use serde;

Expand Down
9 changes: 8 additions & 1 deletion tests/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
InitializeComponent,
InitializeNewWorld,
ApplySystem,
createAllowUndelegationInstruction,
} from "../clients/bolt-sdk";

enum Direction {
Expand Down Expand Up @@ -514,13 +515,19 @@ describe("bolt", () => {
});

it("Check component undelegation", async () => {
const allowUndelegateIx = createAllowUndelegationInstruction({
delegatedAccount: componentPositionEntity1Pda,
ownerProgram: exampleComponentPosition.programId,
});
const delegateIx = createUndelegateInstruction({
payer: provider.wallet.publicKey,
delegatedAccount: componentPositionEntity1Pda,
ownerProgram: exampleComponentPosition.programId,
reimbursement: provider.wallet.publicKey,
});
const tx = new anchor.web3.Transaction().add(delegateIx);
const tx = new anchor.web3.Transaction()
.add(allowUndelegateIx)
.add(delegateIx);
await provider.sendAndConfirm(tx);
const acc = await provider.connection.getAccountInfo(
componentPositionEntity1Pda
Expand Down
Binary file modified tests/fixtures/delegation.so
Binary file not shown.

0 comments on commit 2796f80

Please sign in to comment.