|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use ntt_messages::chain_id::ChainId; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + config::Config, |
| 6 | + peer::NttManagerPeer, |
| 7 | + queue::{inbox::InboxRateLimit, outbox::OutboxRateLimit, rate_limit::RateLimitState}, |
| 8 | + registered_transceiver::RegisteredTransceiver, |
| 9 | +}; |
| 10 | + |
| 11 | +pub mod transfer_ownership; |
| 12 | +pub mod transfer_token_authority; |
| 13 | + |
| 14 | +pub use transfer_ownership::*; |
| 15 | +pub use transfer_token_authority::*; |
| 16 | + |
| 17 | +// * Set peers |
| 18 | + |
| 19 | +#[derive(Accounts)] |
| 20 | +#[instruction(args: SetPeerArgs)] |
| 21 | +pub struct SetPeer<'info> { |
| 22 | + #[account(mut)] |
| 23 | + pub payer: Signer<'info>, |
| 24 | + |
| 25 | + pub owner: Signer<'info>, |
| 26 | + |
| 27 | + #[account( |
| 28 | + has_one = owner, |
| 29 | + )] |
| 30 | + pub config: Account<'info, Config>, |
| 31 | + |
| 32 | + #[account( |
| 33 | + init_if_needed, |
| 34 | + space = 8 + NttManagerPeer::INIT_SPACE, |
| 35 | + payer = payer, |
| 36 | + seeds = [NttManagerPeer::SEED_PREFIX, args.chain_id.id.to_be_bytes().as_ref()], |
| 37 | + bump |
| 38 | + )] |
| 39 | + pub peer: Account<'info, NttManagerPeer>, |
| 40 | + |
| 41 | + #[account( |
| 42 | + init_if_needed, |
| 43 | + space = 8 + InboxRateLimit::INIT_SPACE, |
| 44 | + payer = payer, |
| 45 | + seeds = [ |
| 46 | + InboxRateLimit::SEED_PREFIX, |
| 47 | + args.chain_id.id.to_be_bytes().as_ref() |
| 48 | + ], |
| 49 | + bump, |
| 50 | + )] |
| 51 | + pub inbox_rate_limit: Account<'info, InboxRateLimit>, |
| 52 | + |
| 53 | + pub system_program: Program<'info, System>, |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(AnchorDeserialize, AnchorSerialize)] |
| 57 | +pub struct SetPeerArgs { |
| 58 | + pub chain_id: ChainId, |
| 59 | + pub address: [u8; 32], |
| 60 | + pub limit: u64, |
| 61 | + /// The token decimals on the peer chain. |
| 62 | + pub token_decimals: u8, |
| 63 | +} |
| 64 | + |
| 65 | +pub fn set_peer(ctx: Context<SetPeer>, args: SetPeerArgs) -> Result<()> { |
| 66 | + ctx.accounts.peer.set_inner(NttManagerPeer { |
| 67 | + bump: ctx.bumps.peer, |
| 68 | + address: args.address, |
| 69 | + token_decimals: args.token_decimals, |
| 70 | + }); |
| 71 | + |
| 72 | + ctx.accounts.inbox_rate_limit.set_inner(InboxRateLimit { |
| 73 | + bump: ctx.bumps.inbox_rate_limit, |
| 74 | + rate_limit: RateLimitState::new(args.limit), |
| 75 | + }); |
| 76 | + Ok(()) |
| 77 | +} |
| 78 | + |
| 79 | +// * Register transceivers |
| 80 | + |
| 81 | +#[derive(Accounts)] |
| 82 | +pub struct RegisterTransceiver<'info> { |
| 83 | + #[account( |
| 84 | + mut, |
| 85 | + has_one = owner, |
| 86 | + )] |
| 87 | + pub config: Account<'info, Config>, |
| 88 | + |
| 89 | + pub owner: Signer<'info>, |
| 90 | + |
| 91 | + #[account(mut)] |
| 92 | + pub payer: Signer<'info>, |
| 93 | + |
| 94 | + #[account(executable)] |
| 95 | + /// CHECK: transceiver is meant to be a transceiver program. Arguably a `Program` constraint could be |
| 96 | + /// used here that wraps the Transceiver account type. |
| 97 | + pub transceiver: UncheckedAccount<'info>, |
| 98 | + |
| 99 | + #[account( |
| 100 | + init, |
| 101 | + space = 8 + RegisteredTransceiver::INIT_SPACE, |
| 102 | + payer = payer, |
| 103 | + seeds = [RegisteredTransceiver::SEED_PREFIX, transceiver.key().as_ref()], |
| 104 | + bump |
| 105 | + )] |
| 106 | + pub registered_transceiver: Account<'info, RegisteredTransceiver>, |
| 107 | + |
| 108 | + pub system_program: Program<'info, System>, |
| 109 | +} |
| 110 | + |
| 111 | +pub fn register_transceiver(ctx: Context<RegisterTransceiver>) -> Result<()> { |
| 112 | + let id = ctx.accounts.config.next_transceiver_id; |
| 113 | + ctx.accounts.config.next_transceiver_id += 1; |
| 114 | + ctx.accounts |
| 115 | + .registered_transceiver |
| 116 | + .set_inner(RegisteredTransceiver { |
| 117 | + bump: ctx.bumps.registered_transceiver, |
| 118 | + id, |
| 119 | + transceiver_address: ctx.accounts.transceiver.key(), |
| 120 | + }); |
| 121 | + |
| 122 | + ctx.accounts.config.enabled_transceivers.set(id, true)?; |
| 123 | + Ok(()) |
| 124 | +} |
| 125 | + |
| 126 | +// * Limit rate adjustment |
| 127 | + |
| 128 | +#[derive(Accounts)] |
| 129 | +pub struct SetOutboundLimit<'info> { |
| 130 | + #[account( |
| 131 | + has_one = owner, |
| 132 | + )] |
| 133 | + pub config: Account<'info, Config>, |
| 134 | + |
| 135 | + pub owner: Signer<'info>, |
| 136 | + |
| 137 | + #[account(mut)] |
| 138 | + pub rate_limit: Account<'info, OutboxRateLimit>, |
| 139 | +} |
| 140 | + |
| 141 | +#[derive(AnchorDeserialize, AnchorSerialize)] |
| 142 | +pub struct SetOutboundLimitArgs { |
| 143 | + pub limit: u64, |
| 144 | +} |
| 145 | + |
| 146 | +pub fn set_outbound_limit( |
| 147 | + ctx: Context<SetOutboundLimit>, |
| 148 | + args: SetOutboundLimitArgs, |
| 149 | +) -> Result<()> { |
| 150 | + ctx.accounts.rate_limit.set_limit(args.limit); |
| 151 | + Ok(()) |
| 152 | +} |
| 153 | + |
| 154 | +#[derive(Accounts)] |
| 155 | +#[instruction(args: SetInboundLimitArgs)] |
| 156 | +pub struct SetInboundLimit<'info> { |
| 157 | + #[account( |
| 158 | + has_one = owner, |
| 159 | + )] |
| 160 | + pub config: Account<'info, Config>, |
| 161 | + |
| 162 | + pub owner: Signer<'info>, |
| 163 | + |
| 164 | + #[account( |
| 165 | + mut, |
| 166 | + seeds = [ |
| 167 | + InboxRateLimit::SEED_PREFIX, |
| 168 | + args.chain_id.id.to_be_bytes().as_ref() |
| 169 | + ], |
| 170 | + bump = rate_limit.bump |
| 171 | + )] |
| 172 | + pub rate_limit: Account<'info, InboxRateLimit>, |
| 173 | +} |
| 174 | + |
| 175 | +#[derive(AnchorDeserialize, AnchorSerialize)] |
| 176 | +pub struct SetInboundLimitArgs { |
| 177 | + pub limit: u64, |
| 178 | + pub chain_id: ChainId, |
| 179 | +} |
| 180 | + |
| 181 | +pub fn set_inbound_limit(ctx: Context<SetInboundLimit>, args: SetInboundLimitArgs) -> Result<()> { |
| 182 | + ctx.accounts.rate_limit.set_limit(args.limit); |
| 183 | + Ok(()) |
| 184 | +} |
| 185 | + |
| 186 | +// * Pausing |
| 187 | + |
| 188 | +#[derive(Accounts)] |
| 189 | +pub struct SetPaused<'info> { |
| 190 | + pub owner: Signer<'info>, |
| 191 | + |
| 192 | + #[account( |
| 193 | + mut, |
| 194 | + has_one = owner, |
| 195 | + )] |
| 196 | + pub config: Account<'info, Config>, |
| 197 | +} |
| 198 | + |
| 199 | +pub fn set_paused(ctx: Context<SetPaused>, paused: bool) -> Result<()> { |
| 200 | + ctx.accounts.config.paused = paused; |
| 201 | + Ok(()) |
| 202 | +} |
0 commit comments