From b7cdee233847340177cdfb4a745e23b0c2829624 Mon Sep 17 00:00:00 2001 From: fauxfire Date: Tue, 2 Dec 2025 14:11:26 +0400 Subject: [PATCH] fix: update to new pattern --- roll-dice/Anchor.toml | 4 +- roll-dice/app/app/delegated/page.tsx | 28 ++++-- roll-dice/app/lib/config.ts | 2 +- .../app/lib/idl/random_dice_delegated.json | 96 +++++++------------ .../programs/roll-dice-delegated/src/lib.rs | 21 ++-- roll-dice/programs/roll-dice/src/lib.rs | 27 ++++++ roll-dice/tests/roll-dice-delegated.ts | 22 ++++- 7 files changed, 114 insertions(+), 86 deletions(-) diff --git a/roll-dice/Anchor.toml b/roll-dice/Anchor.toml index c325ce8..5dbe313 100644 --- a/roll-dice/Anchor.toml +++ b/roll-dice/Anchor.toml @@ -7,11 +7,11 @@ skip-lint = false [programs.devnet] rool_dice = "8xgZ1hY7TnVZ4Bbh7v552Rs3BZMSq3LisyWckkBsNLP" -roll-dice-delegated = "7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx" +roll-dice-delegated = "D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM" [programs.localnet] rool_dice = "8xgZ1hY7TnVZ4Bbh7v552Rs3BZMSq3LisyWckkBsNLP" -roll-dice-delegated = "7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx" +roll-dice-delegated = "D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM" [registry] url = "https://api.apr.dev" diff --git a/roll-dice/app/app/delegated/page.tsx b/roll-dice/app/app/delegated/page.tsx index f33737d..710ba30 100644 --- a/roll-dice/app/app/delegated/page.tsx +++ b/roll-dice/app/app/delegated/page.tsx @@ -439,11 +439,19 @@ export default function DiceRollerDelegated() { await ensureFunds(connection, playerKeypairRef.current) const validatorPubkey = new PublicKey(validatorIdentity) + const remainingAccounts = [ + { + pubkey: validatorPubkey, + isSigner: false, + isWritable: false, + }, + ] await program.methods - .delegate({ - commitFrequencyMs: 30000, - validator: validatorPubkey, + .delegate() + .accounts({ + user: playerKeypairRef.current.publicKey, }) + .remainingAccounts(remainingAccounts) .rpc() // Poll every second until delegation succeeds @@ -545,11 +553,19 @@ export default function DiceRollerDelegated() { await updateEphemeralConnectionToValidator(validatorFqdn) await ensureFunds(connection, playerKeypair) + const remainingAccounts = [ + { + pubkey: validatorPubkey, + isSigner: false, + isWritable: false, + }, + ] await programRef.current.methods - .delegate({ - commitFrequencyMs: 30000, - validator: validatorPubkey, + .delegate() + .accounts({ + user: playerKeypair.publicKey, }) + .remainingAccounts(remainingAccounts) .rpc() // Poll every second until delegation succeeds diff --git a/roll-dice/app/lib/config.ts b/roll-dice/app/lib/config.ts index 7d0e712..aeedd6e 100644 --- a/roll-dice/app/lib/config.ts +++ b/roll-dice/app/lib/config.ts @@ -1,6 +1,6 @@ import { PublicKey } from "@solana/web3.js" -export const PROGRAM_ID = new PublicKey("7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx") +export const PROGRAM_ID = new PublicKey("D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM") export const PROGRAM_ID_STANDARD = new PublicKey("8xgZ1hY7TnVZ4Bbh7v552Rs3BZMSq3LisyWckkBsNLP") export const PLAYER_SEED = "playerd2" export const ORACLE_QUEUE = new PublicKey("5hBR571xnXppuCPveTrctfTU7tJLSN94nq7kv7FRK5Tc") diff --git a/roll-dice/app/lib/idl/random_dice_delegated.json b/roll-dice/app/lib/idl/random_dice_delegated.json index 0f5ecd0..c165925 100644 --- a/roll-dice/app/lib/idl/random_dice_delegated.json +++ b/roll-dice/app/lib/idl/random_dice_delegated.json @@ -1,5 +1,5 @@ { - "address": "7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx", + "address": "D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM", "metadata": { "name": "random_dice_delegated", "version": "0.1.0", @@ -48,6 +48,9 @@ }, { "name": "delegate", + "docs": [ + "Set specific validator based on ER, see https://docs.magicblock.gg/pages/get-started/how-integrate-your-program/local-setup" + ], "discriminator": [ 90, 147, @@ -88,38 +91,38 @@ "program": { "kind": "const", "value": [ - 93, - 148, - 146, - 139, - 193, + 179, + 217, + 114, + 141, + 12, + 154, + 18, + 123, + 68, + 119, + 180, + 125, + 173, + 111, 60, - 52, - 112, - 108, - 219, - 27, - 42, - 121, - 156, + 210, + 43, + 63, + 230, + 240, + 225, + 132, + 125, + 237, + 175, + 38, + 240, + 134, + 203, 177, - 71, - 107, - 9, - 33, - 199, - 196, - 69, - 79, - 93, - 188, - 139, - 129, - 194, - 82, - 176, - 198, - 175 + 191, + 110 ] } } @@ -225,7 +228,7 @@ }, { "name": "owner_program", - "address": "7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx" + "address": "D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM" }, { "name": "delegation_program", @@ -236,16 +239,7 @@ "address": "11111111111111111111111111111111" } ], - "args": [ - { - "name": "params", - "type": { - "defined": { - "name": "DelegateParams" - } - } - } - ] + "args": [] }, { "name": "initialize", @@ -493,24 +487,6 @@ } ], "types": [ - { - "name": "DelegateParams", - "type": { - "kind": "struct", - "fields": [ - { - "name": "commit_frequency_ms", - "type": "u32" - }, - { - "name": "validator", - "type": { - "option": "pubkey" - } - } - ] - } - }, { "name": "Player", "type": { diff --git a/roll-dice/programs/roll-dice-delegated/src/lib.rs b/roll-dice/programs/roll-dice-delegated/src/lib.rs index 81f4a7d..03defd3 100644 --- a/roll-dice/programs/roll-dice-delegated/src/lib.rs +++ b/roll-dice/programs/roll-dice-delegated/src/lib.rs @@ -7,7 +7,7 @@ use ephemeral_rollups_sdk::cpi::DelegateConfig; use ephemeral_rollups_sdk::ephem::{commit_and_undelegate_accounts}; -declare_id!("7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx"); +declare_id!("D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM"); pub const PLAYER_SEED: &[u8] = b"playerd2"; @@ -63,15 +63,16 @@ pub mod random_dice_delegated { } // Delegate the player account to use the VRF in the ephemeral rollups - pub fn delegate(ctx: Context, params: DelegateParams) -> Result<()> { - let config = DelegateConfig { - commit_frequency_ms: params.commit_frequency_ms, - validator: params.validator, - }; + /// Set specific validator based on ER, see https://docs.magicblock.gg/pages/get-started/how-integrate-your-program/local-setup + pub fn delegate(ctx: Context) -> Result<()> { ctx.accounts.delegate_player( &ctx.accounts.user, &[PLAYER_SEED, &ctx.accounts.user.key().to_bytes().as_slice()], - config, + DelegateConfig { + // Optionally set a specific validator from the first remaining account + validator: ctx.remaining_accounts.first().map(|acc| acc.key()), + ..Default::default() + }, )?; Ok(()) } @@ -164,10 +165,4 @@ pub struct Undelegate<'info> { pub struct Player { pub last_result: u8, pub rollnum: u8, -} - -#[derive(AnchorSerialize, AnchorDeserialize, Clone)] -pub struct DelegateParams { - pub commit_frequency_ms: u32, - pub validator: Option, } \ No newline at end of file diff --git a/roll-dice/programs/roll-dice/src/lib.rs b/roll-dice/programs/roll-dice/src/lib.rs index 390a79a..52aea13 100644 --- a/roll-dice/programs/roll-dice/src/lib.rs +++ b/roll-dice/programs/roll-dice/src/lib.rs @@ -2,6 +2,8 @@ use anchor_lang::prelude::*; use ephemeral_vrf_sdk::anchor::vrf; use ephemeral_vrf_sdk::instructions::{create_request_randomness_ix, RequestRandomnessParams}; use ephemeral_vrf_sdk::types::SerializableAccountMeta; +use ephemeral_rollups_sdk::anchor::{delegate}; +use ephemeral_rollups_sdk::cpi::DelegateConfig; declare_id!("8xgZ1hY7TnVZ4Bbh7v552Rs3BZMSq3LisyWckkBsNLP"); @@ -50,6 +52,21 @@ pub mod random_dice { player.last_result = rnd_u8; // Update the player's last result Ok(()) } + + /// Delegate the account to the delegation program + /// Set specific validator based on ER, see https://docs.magicblock.gg/pages/get-started/how-integrate-your-program/local-setup + pub fn delegate(ctx: Context) -> Result<()> { + ctx.accounts.delegate_pda( + &ctx.accounts.payer, + &[PLAYER, &ctx.accounts.payer.key().to_bytes().as_slice()], + DelegateConfig { + // Optionally set a specific validator from the first remaining account + validator: ctx.remaining_accounts.first().map(|acc| acc.key()), + ..Default::default() + }, + )?; + Ok(()) + } } #[derive(Accounts)] @@ -83,6 +100,16 @@ pub struct CallbackRollDiceCtx<'info> { pub player: Account<'info, Player>, } +/// Add delegate function to the context +#[delegate] +#[derive(Accounts)] +pub struct DelegateInput<'info> { + pub payer: Signer<'info>, + /// CHECK The pda to delegate + #[account(mut, del, seeds = [PLAYER, payer.key().to_bytes().as_slice()], bump)] + pub pda: AccountInfo<'info>, +} + #[account] pub struct Player { pub last_result: u8, diff --git a/roll-dice/tests/roll-dice-delegated.ts b/roll-dice/tests/roll-dice-delegated.ts index 450aca0..e8a2ecd 100644 --- a/roll-dice/tests/roll-dice-delegated.ts +++ b/roll-dice/tests/roll-dice-delegated.ts @@ -42,10 +42,24 @@ describe("roll-dice-delegated", () => { }); it("Delegate Roll Dice!", async () => { - const tx = await program.methods.delegate({ - commitFrequencyMs: 30000, - validator: new PublicKey("MAS1Dt9qreoRMQ14YQuhg8UTZMMzDdKhmkZMECCzk57"), - }).rpc(); + const remainingAccounts = + providerEphemeralRollup.connection.rpcEndpoint.includes("localhost") || + providerEphemeralRollup.connection.rpcEndpoint.includes("127.0.0.1") + ? [ + { + pubkey: new PublicKey("mAGicPQYBMvcYveUZA5F5UNNwyHvfYh5xkLS2Fr1mev"), + isSigner: false, + isWritable: false, + }, + ] + : []; + const tx = await program.methods + .delegate() + .accounts({ + user: anchor.Wallet.local().publicKey, + }) + .remainingAccounts(remainingAccounts) + .rpc(); console.log("Your transaction signature", tx); });