Skip to content

Commit aae1212

Browse files
committed
🚧 Unifying Buffer & Component delegation instruction
1 parent 39942aa commit aae1212

File tree

7 files changed

+172
-61
lines changed

7 files changed

+172
-61
lines changed

clients/typescript/src/delegation/delegate.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function createDelegateInstruction(
8787
const keys: web3.AccountMeta[] = [
8888
{
8989
pubkey: accounts.payer,
90-
isWritable: false,
90+
isWritable: true,
9191
isSigner: true,
9292
},
9393
{
@@ -174,56 +174,63 @@ export async function DelegateComponent({
174174
delegationProgram?: web3.PublicKey;
175175
systemProgram?: web3.PublicKey;
176176
}): Promise<{
177-
instructions: TransactionInstruction[];
177+
instruction: TransactionInstruction;
178178
transaction: Transaction;
179179
componentPda: PublicKey;
180180
}> {
181181
const componentPda = FindComponentPda({ componentId, entity, seed });
182+
const componentProgram = componentId;
182183
const componentBuffer = FindBufferPda(componentPda);
183-
const delegateComponentIx = createDelegateInstruction({
184-
payer,
185-
entity,
186-
account: componentPda,
187-
ownerProgram: componentId,
188-
buffer,
189-
delegationRecord,
190-
delegationMetadata,
191-
delegationProgram,
192-
systemProgram,
193-
});
194-
195-
const componentBufferDelegationRecord =
184+
systemProgram = systemProgram ?? web3.SystemProgram.programId;
185+
buffer =
186+
buffer ??
187+
delegateBufferPdaFromDelegatedAccountAndOwnerProgram(
188+
componentPda,
189+
componentProgram,
190+
);
191+
delegationRecord =
192+
delegationRecord ?? delegationRecordPdaFromDelegatedAccount(componentPda);
193+
delegationMetadata =
194+
delegationMetadata ??
195+
delegationMetadataPdaFromDelegatedAccount(componentPda);
196+
delegationProgram = delegationProgram ?? new PublicKey(DELEGATION_PROGRAM_ID);
197+
const worldProgram = new PublicKey(worldIdl.address);
198+
199+
const bufferDelegationRecord =
196200
delegationRecordPdaFromDelegatedAccount(componentBuffer);
197201

198-
const componentBufferDelegationMetadata =
202+
const bufferDelegationMetadata =
199203
delegationMetadataPdaFromDelegatedAccount(componentBuffer);
200204

201-
const componentBufferBuffer =
202-
delegateBufferPdaFromDelegatedAccountAndOwnerProgram(
203-
componentBuffer,
204-
new PublicKey(worldIdl.address),
205-
);
205+
const bufferBuffer = delegateBufferPdaFromDelegatedAccountAndOwnerProgram(
206+
componentBuffer,
207+
worldProgram,
208+
);
206209

207210
const program = new Program(worldIdl as Idl) as unknown as Program;
208-
const delegateBufferComponentIx = await program.methods
209-
.delegateBuffer(0, null)
211+
const delegateComponentIx = await program.methods
212+
.delegateComponent(0, null)
210213
.accounts({
211214
payer,
212215
component: componentPda,
213216
componentBuffer,
214-
ownerProgram: worldIdl.address,
215-
buffer: componentBufferBuffer,
216-
delegationRecord: componentBufferDelegationRecord,
217-
delegationMetadata: componentBufferDelegationMetadata,
218-
delegationProgram: DELEGATION_PROGRAM_ID,
217+
componentProgram,
218+
buffer,
219+
delegationRecord,
220+
delegationMetadata,
221+
delegationProgram,
222+
systemProgram,
223+
entity,
224+
worldProgram,
225+
bufferBuffer,
226+
bufferDelegationRecord,
227+
bufferDelegationMetadata,
219228
})
220229
.instruction();
221230

222231
return {
223-
instructions: [delegateComponentIx, delegateBufferComponentIx], // TODO: Make it a single instruction again using the World program as a proxy.
224-
transaction: new Transaction()
225-
.add(delegateComponentIx)
226-
.add(delegateBufferComponentIx),
232+
instruction: delegateComponentIx,
233+
transaction: new Transaction().add(delegateComponentIx),
227234
componentPda,
228235
};
229236
}

clients/typescript/src/generated/idl/world.json

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,31 +225,33 @@
225225
"args": []
226226
},
227227
{
228-
"name": "delegate_buffer",
228+
"name": "delegate_component",
229229
"discriminator": [
230-
121,
231-
134,
232-
252,
233-
65,
234-
64,
235-
83,
236-
93,
237-
91
230+
191,
231+
212,
232+
179,
233+
193,
234+
178,
235+
94,
236+
119,
237+
93
238238
],
239239
"accounts": [
240240
{
241241
"name": "payer",
242+
"writable": true,
242243
"signer": true
243244
},
244245
{
245-
"name": "component"
246+
"name": "component",
247+
"writable": true
246248
},
247249
{
248250
"name": "component_buffer",
249251
"writable": true
250252
},
251253
{
252-
"name": "owner_program"
254+
"name": "component_program"
253255
},
254256
{
255257
"name": "buffer",
@@ -269,6 +271,24 @@
269271
{
270272
"name": "system_program",
271273
"address": "11111111111111111111111111111111"
274+
},
275+
{
276+
"name": "entity"
277+
},
278+
{
279+
"name": "world_program"
280+
},
281+
{
282+
"name": "buffer_buffer",
283+
"writable": true
284+
},
285+
{
286+
"name": "buffer_delegation_record",
287+
"writable": true
288+
},
289+
{
290+
"name": "buffer_delegation_metadata",
291+
"writable": true
272292
}
273293
],
274294
"args": [

clients/typescript/src/generated/types/world.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,24 @@ export type World = {
180180
args: [];
181181
},
182182
{
183-
name: "delegateBuffer";
184-
discriminator: [121, 134, 252, 65, 64, 83, 93, 91];
183+
name: "delegateComponent";
184+
discriminator: [191, 212, 179, 193, 178, 94, 119, 93];
185185
accounts: [
186186
{
187187
name: "payer";
188+
writable: true;
188189
signer: true;
189190
},
190191
{
191192
name: "component";
193+
writable: true;
192194
},
193195
{
194196
name: "componentBuffer";
195197
writable: true;
196198
},
197199
{
198-
name: "ownerProgram";
200+
name: "componentProgram";
199201
},
200202
{
201203
name: "buffer";
@@ -216,6 +218,24 @@ export type World = {
216218
name: "systemProgram";
217219
address: "11111111111111111111111111111111";
218220
},
221+
{
222+
name: "entity";
223+
},
224+
{
225+
name: "worldProgram";
226+
},
227+
{
228+
name: "bufferBuffer";
229+
writable: true;
230+
},
231+
{
232+
name: "bufferDelegationRecord";
233+
writable: true;
234+
},
235+
{
236+
name: "bufferDelegationMetadata";
237+
writable: true;
238+
},
219239
];
220240
args: [
221241
{

clients/typescript/test/intermediate-level/acceleration.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import {
77
} from "../../lib";
88
import { expect } from "chai";
99
import { Direction } from "../framework";
10-
import { convertIdlToCamelCase } from "@coral-xyz/anchor/dist/cjs/idl";
11-
import { sendAndConfirmTransaction, Keypair } from "@solana/web3.js";
12-
import fs from "fs";
1310
import { Framework } from "../framework";
1411

1512
export function acceleration(framework: Framework) {
@@ -54,7 +51,7 @@ export function acceleration(framework: Framework) {
5451
delegateComponent.transaction,
5552
[],
5653
{
57-
skipPreflight: true,
54+
skipPreflight: false,
5855
commitment: "confirmed",
5956
},
6057
);

crates/programs/bolt-component/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,38 @@ pub mod bolt_component {
2121
pub fn set_data(_ctx: Context<SetData>) -> Result<()> {
2222
Ok(())
2323
}
24+
25+
pub fn delegate(_ctx: Context<DelegateInput>, _commit_frequency_ms: u32, _validator: Option<Pubkey>) -> Result<()> {
26+
Ok(())
27+
}
28+
}
29+
30+
31+
#[derive(Accounts)]
32+
pub struct DelegateInput<'info> {
33+
#[account(mut)]
34+
pub payer: Signer<'info>,
35+
/// CHECK:
36+
#[account()]
37+
pub entity: AccountInfo<'info>,
38+
/// CHECK:
39+
#[account(mut)]
40+
pub account: AccountInfo<'info>,
41+
/// CHECK:`
42+
pub owner_program: AccountInfo<'info>,
43+
/// CHECK:
44+
#[account(mut)]
45+
pub buffer: AccountInfo<'info>,
46+
/// CHECK:`
47+
#[account(mut)]
48+
pub delegation_record: AccountInfo<'info>,
49+
/// CHECK:`
50+
#[account(mut)]
51+
pub delegation_metadata: AccountInfo<'info>,
52+
/// CHECK:`
53+
pub delegation_program: AccountInfo<'info>,
54+
/// CHECK:`
55+
pub system_program: AccountInfo<'info>,
2456
}
2557

2658
#[derive(Accounts)]

crates/programs/world/src/lib.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,16 @@ pub mod world {
288288
Ok(())
289289
}
290290

291-
pub fn delegate_buffer(ctx: Context<DelegateBuffer>, commit_frequency_ms: u32, validator: Option<Pubkey>) -> Result<()> {
291+
pub fn delegate_component(ctx: Context<DelegateComponent>, commit_frequency_ms: u32, validator: Option<Pubkey>) -> Result<()> {
292292
let pda_seeds: &[&[u8]] = &[b"buffer", &ctx.accounts.component.key().to_bytes()];
293293

294294
let del_accounts = ephemeral_rollups_sdk::cpi::DelegateAccounts {
295295
payer: &ctx.accounts.payer,
296296
pda: &ctx.accounts.component_buffer,
297-
owner_program: &ctx.accounts.owner_program,
298-
buffer: &ctx.accounts.buffer,
299-
delegation_record: &ctx.accounts.delegation_record,
300-
delegation_metadata: &ctx.accounts.delegation_metadata,
297+
owner_program: &ctx.accounts.world_program,
298+
buffer: &ctx.accounts.buffer_buffer,
299+
delegation_record: &ctx.accounts.buffer_delegation_record,
300+
delegation_metadata: &ctx.accounts.buffer_delegation_metadata,
301301
delegation_program: &ctx.accounts.delegation_program,
302302
system_program: &ctx.accounts.system_program,
303303
};
@@ -313,20 +313,40 @@ pub mod world {
313313
config,
314314
)?;
315315

316+
bolt_component::cpi::delegate(
317+
CpiContext::new(
318+
ctx.accounts.component_program.to_account_info(),
319+
bolt_component::cpi::accounts::DelegateInput {
320+
payer: ctx.accounts.payer.to_account_info(),
321+
entity: ctx.accounts.entity.to_account_info(),
322+
account: ctx.accounts.component.to_account_info(),
323+
owner_program: ctx.accounts.component_program.to_account_info(),
324+
buffer: ctx.accounts.buffer.to_account_info(),
325+
delegation_metadata: ctx.accounts.delegation_metadata.to_account_info(),
326+
delegation_record: ctx.accounts.delegation_record.to_account_info(),
327+
delegation_program: ctx.accounts.delegation_program.to_account_info(),
328+
system_program: ctx.accounts.system_program.to_account_info()
329+
}
330+
),
331+
commit_frequency_ms,
332+
validator
333+
)?;
334+
316335
Ok(())
317336
}
318337

319338
#[derive(Accounts)]
320-
pub struct DelegateBuffer<'info> {
339+
pub struct DelegateComponent<'info> {
340+
#[account(mut)]
321341
pub payer: Signer<'info>,
322342
/// CHECK:
323-
#[account()]
343+
#[account(mut)]
324344
pub component: AccountInfo<'info>,
325345
/// CHECK:
326346
#[account(mut)]
327347
pub component_buffer: AccountInfo<'info>,
328348
/// CHECK:`
329-
pub owner_program: AccountInfo<'info>,
349+
pub component_program: AccountInfo<'info>,
330350
/// CHECK:
331351
#[account(mut)]
332352
pub buffer: AccountInfo<'info>,
@@ -340,6 +360,21 @@ pub mod world {
340360
pub delegation_program: AccountInfo<'info>,
341361
/// CHECK:`
342362
pub system_program: Program<'info, System>,
363+
/// CHECK:
364+
#[account()]
365+
pub entity: AccountInfo<'info>,
366+
/// CHECK:`
367+
pub world_program: AccountInfo<'info>,
368+
/// CHECK:
369+
#[account(mut)]
370+
pub buffer_buffer: AccountInfo<'info>,
371+
/// CHECK:`
372+
#[account(mut)]
373+
pub buffer_delegation_record: AccountInfo<'info>,
374+
/// CHECK:`
375+
#[account(mut)]
376+
pub buffer_delegation_metadata: AccountInfo<'info>,
377+
343378
}
344379

345380
pub fn destroy_component(ctx: Context<DestroyComponent>) -> Result<()> {

tests/script.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ sleep 5
88
echo "Running TypeScript tests..."
99
yarn run ts-mocha -p ./tsconfig.json -t 1000000 clients/typescript/test/main.ts
1010

11-
echo "Running C# tests..."
12-
cd clients/csharp/Solana.Unity.Bolt.Test
13-
dotnet run --configuration Release
11+
# echo "Running C# tests..."
12+
# cd clients/csharp/Solana.Unity.Bolt.Test
13+
# dotnet run --configuration Release
1414

1515
echo "Tests completed."

0 commit comments

Comments
 (0)