Skip to content

Commit

Permalink
feat: Support delegation of on-curve accounts (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco authored Oct 11, 2024
1 parent ba3aab8 commit 672c444
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.0.6"
version = "0.0.7"
authors = ["Magicblock Labs <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -17,8 +17,8 @@ readme = "./README.md"
keywords = ["solana", "crypto", "delegation", "ephemeral-rollups", "magicblock"]

[workspace.dependencies]
ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.6" }
ephemeral-rollups-sdk-attribute-commit = { path = "sdk/commit_attribute", version = "=0.0.6" }
ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.7" }
ephemeral-rollups-sdk-attribute-commit = { path = "sdk/commit_attribute", version = "=0.0.7" }

## External crates
anchor-lang = "0.30.1"
Expand Down
2 changes: 1 addition & 1 deletion sdk/ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magicblock-labs/ephemeral-rollups-sdk",
"version": "0.0.6",
"version": "0.0.7",
"author": "MagicBlock Labs",
"license": "MIT",
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions sdk/ts/src/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getAccounts(
) {
const pdaBytes = accountToDelegate.toBytes();

const [delegationPda] = PublicKey.findProgramAddressSync(
const [delegationRecord] = PublicKey.findProgramAddressSync(
[Buffer.from(SEED_DELEGATION), pdaBytes],
new PublicKey(DELEGATION_PROGRAM_ID),
);
Expand All @@ -69,7 +69,7 @@ function getAccounts(
new PublicKey(DELEGATION_PROGRAM_ID),
);
return {
delegationPda,
delegationRecord,
delegationMetadata,
bufferPda,
commitStateRecordPda,
Expand Down
1 change: 1 addition & 0 deletions sdk/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./seeds";
export * from "./constants";
export * from "./instructions/undelegate";
export * from "./instructions/delegate";
export * from "./accounts";
export * from "./utils";
94 changes: 94 additions & 0 deletions sdk/ts/src/instructions/delegate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as beet from "@metaplex-foundation/beet";
import * as web3 from "@solana/web3.js";
import { PublicKey } from "@solana/web3.js";
import { DELEGATION_PROGRAM_ID } from "../constants";
import { DelegateAccounts } from "../accounts";

export const delegateStruct = new beet.FixableBeetArgsStruct<{
instructionDiscriminator: number[];
valid_until: beet.bignum;
commit_frequency_ms: beet.bignum;
seeds: number[][];
}>(
[
["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)],
["valid_until", beet.i64],
["commit_frequency_ms", beet.u32],
["seeds", beet.array(beet.array(beet.u8))],
],
"UndelegateInstructionArgs",
);

export const delegateInstructionDiscriminator = [0, 0, 0, 0, 0, 0, 0, 0];

// Define the DelegateAccountArgs structure
interface DelegateAccountArgs {
valid_until: number;
commit_frequency_ms: number;
seeds: Uint8Array[][];
}

// Function to create a delegate instruction
export function createDelegateInstruction(
accounts: {
payer: web3.PublicKey;
delegateAccount: web3.PublicKey;
ownerProgram: web3.PublicKey;
buffer?: web3.PublicKey;
delegationRecord?: web3.PublicKey;
delegationMetadata?: web3.PublicKey;
systemProgram?: web3.PublicKey;
},
args?: DelegateAccountArgs,
programId = new PublicKey(DELEGATION_PROGRAM_ID),
) {
const { delegationRecord, delegationMetadata, bufferPda } = DelegateAccounts(
accounts.delegateAccount,
accounts.ownerProgram,
);

args = args ?? {
valid_until: 0,
commit_frequency_ms: 4294967295, // 2 ** 4 - 1,
seeds: [],
};

const keys: web3.AccountMeta[] = [
{ pubkey: accounts.payer, isWritable: false, isSigner: true },
{ pubkey: accounts.delegateAccount, isWritable: true, isSigner: true },
{ pubkey: accounts.ownerProgram, isWritable: false, isSigner: false },
{
pubkey: accounts.buffer ?? bufferPda,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.delegationRecord ?? delegationRecord,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.delegationMetadata ?? delegationMetadata,
isWritable: true,
isSigner: false,
},
{
pubkey: accounts.systemProgram ?? web3.SystemProgram.programId,
isWritable: false,
isSigner: false,
},
];

const [data] = delegateStruct.serialize({
instructionDiscriminator: delegateInstructionDiscriminator,
valid_until: args.valid_until,
commit_frequency_ms: args.commit_frequency_ms,
seeds: args.seeds.map((seed) => seed.map(Number)),
});

return new web3.TransactionInstruction({
programId,
keys,
data,
});
}
4 changes: 2 additions & 2 deletions sdk/ts/src/instructions/undelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createUndelegateInstruction(
});

const {
delegationPda,
delegationRecord,
delegationMetadata,
bufferPda,
commitStateRecordPda,
Expand Down Expand Up @@ -83,7 +83,7 @@ export function createUndelegateInstruction(
isSigner: false,
},
{
pubkey: accounts.delegationRecord ?? delegationPda,
pubkey: accounts.delegationRecord ?? delegationRecord,
isWritable: true,
isSigner: false,
},
Expand Down

0 comments on commit 672c444

Please sign in to comment.