|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use tuktuk_sdk::tuktuk::{tuktuk::program::Tuktuk, TaskQueueV0}; |
| 3 | + |
| 4 | +declare_id!("cpic9j9sjqvhn2ZX3mqcCgzHKCwiiBTyEszyCwN7MBC"); |
| 5 | + |
| 6 | +#[program] |
| 7 | +pub mod cpi_example { |
| 8 | + use anchor_lang::{solana_program::instruction::Instruction, InstructionData}; |
| 9 | + use tuktuk_sdk::{ |
| 10 | + compiled_transaction::*, |
| 11 | + tuktuk::{ |
| 12 | + tuktuk::cpi::{accounts::QueueTaskV0, queue_task_v0}, |
| 13 | + types::{QueueTaskArgsV0, TriggerV0}, |
| 14 | + }, |
| 15 | + }; |
| 16 | + |
| 17 | + use super::*; |
| 18 | + |
| 19 | + pub fn schedule_next(ctx: Context<ScheduleNext>) -> Result<()> { |
| 20 | + let my_tx = CpiContext::new( |
| 21 | + ctx.accounts.system_program.to_account_info(), |
| 22 | + crate::__cpi_client_accounts_schedule_next::ScheduleNext { |
| 23 | + queue_authority: ctx.accounts.queue_authority.to_account_info(), |
| 24 | + system_program: ctx.accounts.system_program.to_account_info(), |
| 25 | + tuktuk_program: ctx.accounts.tuktuk_program.to_account_info(), |
| 26 | + task_queue: ctx.accounts.task_queue.to_account_info(), |
| 27 | + free_task_1: ctx.accounts.free_task_1.to_account_info(), |
| 28 | + }, |
| 29 | + ); |
| 30 | + // Only take the first 3 accounts. Tuktuk will pass the task queue and a free task account automatically. |
| 31 | + // We do this because we don't actually know what the task ID will be since its PDA depends on the available task ID. |
| 32 | + let account_metas = my_tx.accounts.to_account_metas(None)[..3].to_vec(); |
| 33 | + let data = crate::instruction::ScheduleNext.data(); |
| 34 | + let (compiled_tx, _) = compile_transaction( |
| 35 | + vec![Instruction { |
| 36 | + program_id: crate::ID, |
| 37 | + accounts: account_metas, |
| 38 | + data, |
| 39 | + }], |
| 40 | + vec![], |
| 41 | + ) |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + queue_task_v0( |
| 45 | + CpiContext::new_with_signer( |
| 46 | + ctx.accounts.tuktuk_program.to_account_info(), |
| 47 | + QueueTaskV0 { |
| 48 | + payer: ctx.accounts.queue_authority.to_account_info(), |
| 49 | + queue_authority: ctx.accounts.queue_authority.to_account_info(), |
| 50 | + task_queue: ctx.accounts.task_queue.to_account_info(), |
| 51 | + task: ctx.accounts.free_task_1.to_account_info(), |
| 52 | + system_program: ctx.accounts.system_program.to_account_info(), |
| 53 | + }, |
| 54 | + &[&[b"queue_authority".as_ref(), &[ctx.bumps.queue_authority]]], |
| 55 | + ) |
| 56 | + .with_remaining_accounts(vec![ |
| 57 | + ctx.accounts.queue_authority.to_account_info(), |
| 58 | + ctx.accounts.system_program.to_account_info(), |
| 59 | + ctx.accounts.tuktuk_program.to_account_info(), |
| 60 | + ]), |
| 61 | + QueueTaskArgsV0 { |
| 62 | + id: ctx.accounts.task_queue.next_available_task_id().unwrap() as u16, |
| 63 | + // 5 seconds from now |
| 64 | + trigger: TriggerV0::Timestamp(Clock::get()?.unix_timestamp + 5), |
| 65 | + transaction: compiled_tx.into(), |
| 66 | + crank_reward: None, |
| 67 | + free_tasks: 1, |
| 68 | + }, |
| 69 | + )?; |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Accounts)] |
| 75 | +pub struct ScheduleNext<'info> { |
| 76 | + #[account( |
| 77 | + mut, |
| 78 | + seeds = [b"queue_authority".as_ref()], |
| 79 | + bump, |
| 80 | + )] |
| 81 | + pub queue_authority: AccountInfo<'info>, |
| 82 | + pub system_program: Program<'info, System>, |
| 83 | + pub tuktuk_program: Program<'info, Tuktuk>, |
| 84 | + pub task_queue: Account<'info, TaskQueueV0>, |
| 85 | + /// CHECK: free task account |
| 86 | + pub free_task_1: UncheckedAccount<'info>, |
| 87 | +} |
0 commit comments