Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions roll-dice/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 22 additions & 6 deletions roll-dice/app/app/delegated/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion roll-dice/app/lib/config.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
96 changes: 36 additions & 60 deletions roll-dice/app/lib/idl/random_dice_delegated.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"address": "7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx",
"address": "D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM",
"metadata": {
"name": "random_dice_delegated",
"version": "0.1.0",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
]
}
}
Expand Down Expand Up @@ -225,7 +228,7 @@
},
{
"name": "owner_program",
"address": "7JJKDgfhr5eKjijcWiMkKnEoiLXNRbjctq1D1o8PFuDx"
"address": "D74Ho1cWBHgZNpVG4FnBBA4JtjX4HFZ5QqqRXXVKA8gM"
},
{
"name": "delegation_program",
Expand All @@ -236,16 +239,7 @@
"address": "11111111111111111111111111111111"
}
],
"args": [
{
"name": "params",
"type": {
"defined": {
"name": "DelegateParams"
}
}
}
]
"args": []
},
{
"name": "initialize",
Expand Down Expand Up @@ -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": {
Expand Down
21 changes: 8 additions & 13 deletions roll-dice/programs/roll-dice-delegated/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<DelegateInput>, 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<DelegateInput>) -> 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(())
}
Expand Down Expand Up @@ -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<Pubkey>,
}
27 changes: 27 additions & 0 deletions roll-dice/programs/roll-dice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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<DelegateInput>) -> 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)]
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 18 additions & 4 deletions roll-dice/tests/roll-dice-delegated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
Loading