Skip to content

Commit a5670ff

Browse files
✨ Add allow undelegation ix (#68)
1 parent 28aaf73 commit a5670ff

File tree

12 files changed

+162
-71
lines changed

12 files changed

+162
-71
lines changed

Cargo.lock

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ anyhow = "1.0.32"
5151
heck = "0.5.0"
5252
clap = { version = "4.2.4", features = ["derive"] }
5353
ahash = "=0.8.11"
54-
delegation-program-sdk = "=0.0.2"
54+
delegation-program-sdk = "=0.1.3"
5555

5656
[profile.release]
5757
overflow-checks = true

clients/bolt-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dependencies": {
99
"@metaplex-foundation/beet": "^0.7.1",
1010
"@metaplex-foundation/beet-solana": "^0.4.0",
11-
"@magicblock-labs/delegation-program": "0.1.1"
11+
"@magicblock-labs/delegation-program": "0.1.3"
1212
},
1313
"devDependencies": {
1414
"@metaplex-foundation/solita": "^0.20.1",

clients/bolt-sdk/src/delegation/accounts.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as beet from "@metaplex-foundation/beet";
2+
import * as web3 from "@solana/web3.js";
3+
import {
4+
DelegateAccounts,
5+
DELEGATION_PROGRAM_ID,
6+
} from "@magicblock-labs/delegation-program";
7+
8+
export const allowUndelegationStruct = new beet.BeetArgsStruct<{
9+
instructionDiscriminator: number[] /* size: 8 */;
10+
}>(
11+
[["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)]],
12+
"allowUndelegationInstructionArgs"
13+
);
14+
15+
export interface AllowUndelegationInstructionAccounts {
16+
delegatedAccount: web3.PublicKey;
17+
ownerProgram: web3.PublicKey;
18+
buffer?: web3.PublicKey;
19+
}
20+
21+
export const allowUndelegateInstructionDiscriminator = [
22+
255, 66, 82, 208, 247, 5, 210, 126,
23+
];
24+
25+
/**
26+
* Creates a Delegate instruction.
27+
*/
28+
29+
export function createAllowUndelegationInstruction(
30+
accounts: AllowUndelegationInstructionAccounts
31+
) {
32+
const [data] = allowUndelegationStruct.serialize({
33+
instructionDiscriminator: allowUndelegateInstructionDiscriminator,
34+
});
35+
36+
const { delegationPda, delegationMetadata, bufferPda } = DelegateAccounts(
37+
accounts.delegatedAccount,
38+
accounts.ownerProgram
39+
);
40+
41+
const keys: web3.AccountMeta[] = [
42+
{
43+
pubkey: accounts.delegatedAccount,
44+
isWritable: false,
45+
isSigner: false,
46+
},
47+
{
48+
pubkey: delegationPda,
49+
isWritable: false,
50+
isSigner: false,
51+
},
52+
{
53+
pubkey: delegationMetadata,
54+
isWritable: true,
55+
isSigner: false,
56+
},
57+
{
58+
pubkey: bufferPda,
59+
isWritable: false,
60+
isSigner: false,
61+
},
62+
{
63+
pubkey: new web3.PublicKey(DELEGATION_PROGRAM_ID),
64+
isWritable: true,
65+
isSigner: false,
66+
},
67+
];
68+
69+
const programId = accounts.ownerProgram;
70+
return new web3.TransactionInstruction({
71+
programId,
72+
keys,
73+
data,
74+
});
75+
}

clients/bolt-sdk/src/delegation/delegate.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as beet from "@metaplex-foundation/beet";
22
import * as web3 from "@solana/web3.js";
3-
import { getDelegationAccounts } from "./accounts";
4-
import { DELEGATION_PROGRAM_ID } from "@magicblock-labs/delegation-program";
3+
import {
4+
DelegateAccounts,
5+
DELEGATION_PROGRAM_ID,
6+
} from "@magicblock-labs/delegation-program";
57

68
export interface DelegateInstructionArgs {
79
validUntil: beet.bignum;
@@ -33,7 +35,7 @@ export interface DelegateInstructionAccounts {
3335
ownerProgram: web3.PublicKey;
3436
buffer?: web3.PublicKey;
3537
delegationRecord?: web3.PublicKey;
36-
delegateAccountSeeds?: web3.PublicKey;
38+
delegationMetadata?: web3.PublicKey;
3739
delegationProgram?: web3.PublicKey;
3840
systemProgram?: web3.PublicKey;
3941
}
@@ -58,8 +60,10 @@ export function createDelegateInstruction(
5860
commitFrequencyMs,
5961
});
6062

61-
const { delegationPda, delegatedAccountSeedsPda, bufferPda } =
62-
getDelegationAccounts(accounts.account, accounts.ownerProgram);
63+
const { delegationPda, delegationMetadata, bufferPda } = DelegateAccounts(
64+
accounts.account,
65+
accounts.ownerProgram
66+
);
6367

6468
const keys: web3.AccountMeta[] = [
6569
{
@@ -93,7 +97,7 @@ export function createDelegateInstruction(
9397
isSigner: false,
9498
},
9599
{
96-
pubkey: accounts.delegateAccountSeeds ?? delegatedAccountSeedsPda,
100+
pubkey: accounts.delegationMetadata ?? delegationMetadata,
97101
isWritable: true,
98102
isSigner: false,
99103
},

clients/bolt-sdk/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { PROGRAM_ID } from "./generated";
44
export * from "./generated/accounts";
55
export * from "./generated/instructions";
66
export * from "./world/transactions";
7-
export * from "./delegation/accounts";
87
export * from "./delegation/delegate";
8+
export * from "./delegation/allow_undelegation";
99
export {
1010
createCommitInstruction,
1111
createUndelegateInstruction,

clients/bolt-sdk/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@
8484
wrap-ansi "^8.1.0"
8585
wrap-ansi-cjs "npm:wrap-ansi@^7.0.0"
8686

87-
"@magicblock-labs/[email protected].1":
88-
version "0.1.1"
89-
resolved "https://registry.yarnpkg.com/@magicblock-labs/delegation-program/-/delegation-program-0.1.1.tgz#71a5fb25ccf88ea7746ea70473fb109ff6b67433"
90-
integrity sha512-4He8V7jkrGy8MTp6qAz2h5y70+6y4cmvQc+7Km6B7WDLODlymPu9zZEl1/bmY24bOeKVKfrEEE7pDVa8qr3BwQ==
87+
"@magicblock-labs/[email protected].2":
88+
version "0.1.2"
89+
resolved "https://registry.yarnpkg.com/@magicblock-labs/delegation-program/-/delegation-program-0.1.2.tgz#7b298398259d80a368eb9e91cb911e5a6e542391"
90+
integrity sha512-9EGtcBWGAXGCOjTJVyzxydMpM8GGk8QjzQlgpV0q96BAbmpEO9C6b/N3lvMRTR1FfDuBn43a3Pa5YEiHyUu8Eg==
9191
dependencies:
9292
"@metaplex-foundation/beet" "^0.7.2"
9393
"@solana/web3.js" "^1.92.3"

crates/bolt-lang/attribute/delegate/src/lib.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ pub fn delegate(args: TokenStream, input: TokenStream) -> TokenStream {
3434
fn modify_component_module(mut module: ItemMod, component_type: &Type) -> ItemMod {
3535
let (delegate_fn, delegate_struct) = generate_delegate(component_type);
3636
let (undelegate_fn, undelegate_struct) = generate_undelegate();
37+
let (allow_undelegate_fn, allow_undelegate_struct) = generate_allow_undelegate();
3738
module.content = module.content.map(|(brace, mut items)| {
3839
items.extend(
3940
vec![
4041
delegate_fn,
4142
delegate_struct,
4243
undelegate_fn,
4344
undelegate_struct,
45+
allow_undelegate_fn,
46+
allow_undelegate_struct,
4447
]
4548
.into_iter()
4649
.map(|item| syn::parse2(item).unwrap())
@@ -51,6 +54,47 @@ fn modify_component_module(mut module: ItemMod, component_type: &Type) -> ItemMo
5154
module
5255
}
5356

57+
/// Generates the allow_undelegate function and struct.
58+
fn generate_allow_undelegate() -> (TokenStream2, TokenStream2) {
59+
(
60+
quote! {
61+
#[automatically_derived]
62+
pub fn allow_undelegation(ctx: Context<AllowUndelegation>) -> Result<()> {
63+
::bolt_lang::allow_undelegation(
64+
&ctx.accounts.base_account,
65+
&ctx.accounts.delegation_record,
66+
&ctx.accounts.delegation_metadata,
67+
&ctx.accounts.buffer,
68+
&ctx.accounts.delegation_program,
69+
&id(),
70+
)?;
71+
Ok(())
72+
}
73+
},
74+
quote! {
75+
#[automatically_derived]
76+
#[derive(Accounts)]
77+
pub struct AllowUndelegation<'info> {
78+
#[account()]
79+
/// CHECK: The delegated component
80+
pub base_account: AccountInfo<'info>,
81+
#[account()]
82+
/// CHECK: delegation record
83+
pub delegation_record: AccountInfo<'info>,
84+
#[account(mut)]
85+
/// CHECK: delegation metadata
86+
pub delegation_metadata: AccountInfo<'info>,
87+
#[account()]
88+
/// CHECK: singer buffer to enforce CPI
89+
pub buffer: AccountInfo<'info>,
90+
#[account()]
91+
/// CHECK:`
92+
pub delegation_program: AccountInfo<'info>,
93+
}
94+
},
95+
)
96+
}
97+
5498
/// Generates the undelegate function and struct.
5599
fn generate_undelegate() -> (TokenStream2, TokenStream2) {
56100
(
@@ -63,7 +107,7 @@ fn generate_undelegate() -> (TokenStream2, TokenStream2) {
63107
&ctx.accounts.payer,
64108
&ctx.accounts.system_program,
65109
];
66-
undelegate_account(
110+
::bolt_lang::undelegate_account(
67111
delegated_account,
68112
&id(),
69113
buffer,
@@ -77,7 +121,7 @@ fn generate_undelegate() -> (TokenStream2, TokenStream2) {
77121
quote! {
78122
#[automatically_derived]
79123
#[derive(Accounts)]
80-
pub struct InitializeAfterUndelegation<'info> {
124+
pub struct InitializeAfterUndelegation<'info> {
81125
/// CHECK:`
82126
#[account(mut)]
83127
pub base_account: AccountInfo<'info>,
@@ -115,7 +159,7 @@ fn generate_delegate(component_type: &Type) -> (TokenStream2, TokenStream2) {
115159

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

118-
delegate_account(
162+
::bolt_lang::delegate_account(
119163
payer,
120164
account,
121165
owner_program,

crates/bolt-lang/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use world;
2020
pub use world::program::World;
2121
pub use world::Entity;
2222

23-
pub use delegation_program_sdk::{delegate_account, undelegate_account};
23+
pub use delegation_program_sdk::{allow_undelegation, delegate_account, undelegate_account};
2424

2525
pub use serde;
2626

tests/bolt.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
InitializeComponent,
2020
InitializeNewWorld,
2121
ApplySystem,
22+
createAllowUndelegationInstruction,
2223
} from "../clients/bolt-sdk";
2324

2425
enum Direction {
@@ -514,13 +515,19 @@ describe("bolt", () => {
514515
});
515516

516517
it("Check component undelegation", async () => {
518+
const allowUndelegateIx = createAllowUndelegationInstruction({
519+
delegatedAccount: componentPositionEntity1Pda,
520+
ownerProgram: exampleComponentPosition.programId,
521+
});
517522
const delegateIx = createUndelegateInstruction({
518523
payer: provider.wallet.publicKey,
519524
delegatedAccount: componentPositionEntity1Pda,
520525
ownerProgram: exampleComponentPosition.programId,
521526
reimbursement: provider.wallet.publicKey,
522527
});
523-
const tx = new anchor.web3.Transaction().add(delegateIx);
528+
const tx = new anchor.web3.Transaction()
529+
.add(allowUndelegateIx)
530+
.add(delegateIx);
524531
await provider.sendAndConfirm(tx);
525532
const acc = await provider.connection.getAccountInfo(
526533
componentPositionEntity1Pda

tests/fixtures/delegation.so

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)