diff --git a/Cargo.lock b/Cargo.lock index 43ce065..c529949 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -747,7 +747,7 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "ephemeral-rollups-sdk" -version = "0.0.6" +version = "0.0.7" dependencies = [ "anchor-lang", "borsh 0.10.3", @@ -759,7 +759,7 @@ dependencies = [ [[package]] name = "ephemeral-rollups-sdk-attribute-commit" -version = "0.0.6" +version = "0.0.7" dependencies = [ "proc-macro2", "quote", @@ -768,7 +768,7 @@ dependencies = [ [[package]] name = "ephemeral-rollups-sdk-attribute-delegate" -version = "0.0.6" +version = "0.0.7" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index ea333e5..d45c1e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ resolver = "2" [workspace.package] -version = "0.0.6" +version = "0.0.7" authors = ["Magicblock Labs "] edition = "2021" license = "MIT" @@ -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" diff --git a/sdk/ts/package.json b/sdk/ts/package.json index 759778f..a0eaa74 100644 --- a/sdk/ts/package.json +++ b/sdk/ts/package.json @@ -1,6 +1,6 @@ { "name": "@magicblock-labs/ephemeral-rollups-sdk", - "version": "0.0.6", + "version": "0.0.7", "author": "MagicBlock Labs", "license": "MIT", "publishConfig": { diff --git a/sdk/ts/src/accounts.ts b/sdk/ts/src/accounts.ts index 0174fdf..e6bcba5 100644 --- a/sdk/ts/src/accounts.ts +++ b/sdk/ts/src/accounts.ts @@ -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), ); @@ -69,7 +69,7 @@ function getAccounts( new PublicKey(DELEGATION_PROGRAM_ID), ); return { - delegationPda, + delegationRecord, delegationMetadata, bufferPda, commitStateRecordPda, diff --git a/sdk/ts/src/index.ts b/sdk/ts/src/index.ts index a79ad24..40bbc75 100644 --- a/sdk/ts/src/index.ts +++ b/sdk/ts/src/index.ts @@ -1,5 +1,6 @@ export * from "./seeds"; export * from "./constants"; export * from "./instructions/undelegate"; +export * from "./instructions/delegate"; export * from "./accounts"; export * from "./utils"; diff --git a/sdk/ts/src/instructions/delegate.ts b/sdk/ts/src/instructions/delegate.ts new file mode 100644 index 0000000..dd0d7e8 --- /dev/null +++ b/sdk/ts/src/instructions/delegate.ts @@ -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, + }); +} diff --git a/sdk/ts/src/instructions/undelegate.ts b/sdk/ts/src/instructions/undelegate.ts index c83e83e..e4b15a9 100644 --- a/sdk/ts/src/instructions/undelegate.ts +++ b/sdk/ts/src/instructions/undelegate.ts @@ -44,7 +44,7 @@ export function createUndelegateInstruction( }); const { - delegationPda, + delegationRecord, delegationMetadata, bufferPda, commitStateRecordPda, @@ -83,7 +83,7 @@ export function createUndelegateInstruction( isSigner: false, }, { - pubkey: accounts.delegationRecord ?? delegationPda, + pubkey: accounts.delegationRecord ?? delegationRecord, isWritable: true, isSigner: false, },